Skip to content

Commit

Permalink
feat(ghn): ✨ add sendRequest method for quick call GHN
Browse files Browse the repository at this point in the history
  • Loading branch information
lehuygiang28 committed Apr 21, 2024
1 parent 8dcdf5d commit 1b438f4
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/giaohangnhanh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { GhnConfig } from './types';
import { GhnAddress } from './address';
import { CalculateFee } from './calculate-fee';
import { Order } from './order';
import { resolveUrl } from './utils';

/**
* Đối tượng Ghn chứa các đối tượng con như address, calculateFee, ...
Expand Down Expand Up @@ -70,4 +71,21 @@ export class Ghn extends GhnAbstract {
* Order instance to interact with order API
*/
public order: Order;

/**
*
* @experimental Used for sending a request to GHN that is currently not implemented yet
* @param path Path of the API
* @param data Object data to send
* @returns Response
*/
public async sendRequest(path: string, data: unknown): Promise<unknown> {
const response = await this.fetch(resolveUrl(this.globalConfig.host, path), data);
const result = (await response.json()) as { data: unknown; message: string };

if (!response.ok) {
throw new Error(`Failed to request '${path}': ${result.message}`);
}
return result.data;
}
}

0 comments on commit 1b438f4

Please sign in to comment.