Skip to content

Commit

Permalink
Merge pull request #38 from tiagosiebler/reqheadersparam
Browse files Browse the repository at this point in the history
feat(v1.1.0): allow pass-through headers for endpoints that support it
  • Loading branch information
tiagosiebler authored Nov 18, 2024
2 parents ac1f3f9 + 4da1487 commit 8a07564
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gateio-api",
"version": "1.0.22",
"version": "1.1.0",
"description": "Complete & robust Node.js SDK for Gate.io's REST APIs, WebSockets & WebSocket APIs, with TypeScript declarations.",
"scripts": {
"clean": "rm -rf dist/*",
Expand Down
11 changes: 9 additions & 2 deletions src/RestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ export class RestClient extends BaseRestClient {
console.log(result);

console.log(
`Your approximate latency to exchange server:
`Your approximate latency to exchange server:
One way: ${estimatedOneWayLatency}ms.
Round trip: ${roundTripTime}ms.
`,
Expand Down Expand Up @@ -2473,9 +2473,16 @@ export class RestClient extends BaseRestClient {
* @returns Promise<FuturesOrder>
*/
updateFuturesOrder(params: UpdateFuturesOrderReq): Promise<FuturesOrder> {
const { settle, order_id, ...body } = params;
const { settle, order_id, ...rest } = params;
const { ['x-gate-exptime']: xGateExptime, ...body } = rest;

const headers = xGateExptime
? { 'x-gate-exptime': xGateExptime }
: undefined;

return this.putPrivate(`/futures/${settle}/orders/${order_id}`, {
body: body,
headers: headers,
});
}

Expand Down
42 changes: 28 additions & 14 deletions src/lib/BaseRestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ interface UnsignedRequest<T extends object | undefined = {}> {
type SignMethod = 'gateV4';

/**
* Some requests require some params to be in the query string and some in the body.
* This type anticipates both are possible in any combination.
* Some requests require some params to be in the query string and some in the body. Some even support passing params via headers.
* This type anticipates these are possible in any combination.
*
* The request builder will automatically handle where parameters should go.
*/
type ParamsInQueryAndOrBody = { query?: object; body?: object };
type ParamsInQueryBodyOrHeader = {
query?: object;
body?: object;
headers?: object;
};

/**
* Enables:
Expand Down Expand Up @@ -183,7 +187,7 @@ export abstract class BaseRestClient {
return this._call('GET', endpoint, { query: params }, isPublicAPI);
}

protected post(endpoint: string, params?: ParamsInQueryAndOrBody) {
protected post(endpoint: string, params?: ParamsInQueryBodyOrHeader) {
const isPublicAPI = true;
return this._call('POST', endpoint, params, isPublicAPI);
}
Expand All @@ -194,23 +198,26 @@ export abstract class BaseRestClient {
return this._call('GET', endpoint, { query: params }, isPublicAPI);
}

protected postPrivate(endpoint: string, params?: ParamsInQueryAndOrBody) {
protected postPrivate(endpoint: string, params?: ParamsInQueryBodyOrHeader) {
const isPublicAPI = false;
return this._call('POST', endpoint, params, isPublicAPI);
}

protected deletePrivate(endpoint: string, params?: ParamsInQueryAndOrBody) {
protected deletePrivate(
endpoint: string,
params?: ParamsInQueryBodyOrHeader,
) {
const isPublicAPI = false;
return this._call('DELETE', endpoint, params, isPublicAPI);
}

protected putPrivate(endpoint: string, params?: ParamsInQueryAndOrBody) {
protected putPrivate(endpoint: string, params?: ParamsInQueryBodyOrHeader) {
const isPublicAPI = false;
return this._call('PUT', endpoint, params, isPublicAPI);
}

// protected patchPrivate(endpoint: string, params?: any) {
protected patchPrivate(endpoint: string, params?: ParamsInQueryAndOrBody) {
protected patchPrivate(endpoint: string, params?: ParamsInQueryBodyOrHeader) {
const isPublicAPI = false;
return this._call('PATCH', endpoint, params, isPublicAPI);
}
Expand All @@ -221,7 +228,7 @@ export abstract class BaseRestClient {
private async _call(
method: Method,
endpoint: string,
params?: ParamsInQueryAndOrBody,
params?: ParamsInQueryBodyOrHeader,
isPublicApi?: boolean,
): Promise<any> {
// Sanity check to make sure it's only ever prefixed by one forward slash
Expand Down Expand Up @@ -307,7 +314,9 @@ export abstract class BaseRestClient {
/**
* @private sign request and set recv window
*/
private async signRequest<T extends ParamsInQueryAndOrBody | undefined = {}>(
private async signRequest<
T extends ParamsInQueryBodyOrHeader | undefined = {},
>(
data: T,
endpoint: string,
method: Method,
Expand Down Expand Up @@ -405,7 +414,7 @@ export abstract class BaseRestClient {
}

private async prepareSignParams<
TParams extends ParamsInQueryAndOrBody | undefined,
TParams extends ParamsInQueryBodyOrHeader | undefined,
>(
method: Method,
endpoint: string,
Expand All @@ -414,7 +423,7 @@ export abstract class BaseRestClient {
isPublicApi?: true,
): Promise<UnsignedRequest<TParams>>;
private async prepareSignParams<
TParams extends ParamsInQueryAndOrBody | undefined,
TParams extends ParamsInQueryBodyOrHeader | undefined,
>(
method: Method,
endpoint: string,
Expand All @@ -423,7 +432,7 @@ export abstract class BaseRestClient {
isPublicApi?: false | undefined,
): Promise<SignedRequest<TParams>>;
private async prepareSignParams<
TParams extends ParamsInQueryAndOrBody | undefined,
TParams extends ParamsInQueryBodyOrHeader | undefined,
>(
method: Method,
endpoint: string,
Expand All @@ -450,18 +459,23 @@ export abstract class BaseRestClient {
method: Method,
endpoint: string,
url: string,
params?: ParamsInQueryAndOrBody,
params?: ParamsInQueryBodyOrHeader,
isPublicApi?: boolean,
): Promise<AxiosRequestConfig> {
const options: AxiosRequestConfig = {
...this.globalRequestOptions,
url: url,
method: method,
headers: {
...params?.headers,
...this.globalRequestOptions.headers,
},
};

deleteUndefinedValues(params);
deleteUndefinedValues(params?.body);
deleteUndefinedValues(params?.query);
deleteUndefinedValues(params?.headers);

if (isPublicApi || !this.apiKey || !this.apiSecret) {
return {
Expand Down
1 change: 1 addition & 0 deletions src/types/request/futures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export interface GetFuturesOrdersByTimeRangeReq {
}

export interface UpdateFuturesOrderReq {
'x-gate-exptime'?: number;
settle: 'btc' | 'usdt' | 'usd';
order_id: string;
size?: number;
Expand Down

0 comments on commit 8a07564

Please sign in to comment.