From d9a16b2f46239875986869ac264d256dc0168c3c Mon Sep 17 00:00:00 2001 From: Tiago Siebler Date: Mon, 18 Nov 2024 14:39:41 +0000 Subject: [PATCH 1/4] feat(): allow pass-through headers for endpoints that support it --- src/lib/BaseRestClient.ts | 42 ++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/lib/BaseRestClient.ts b/src/lib/BaseRestClient.ts index 867b801..4271902 100644 --- a/src/lib/BaseRestClient.ts +++ b/src/lib/BaseRestClient.ts @@ -45,12 +45,16 @@ interface UnsignedRequest { 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: @@ -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); } @@ -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); } @@ -221,7 +228,7 @@ export abstract class BaseRestClient { private async _call( method: Method, endpoint: string, - params?: ParamsInQueryAndOrBody, + params?: ParamsInQueryBodyOrHeader, isPublicApi?: boolean, ): Promise { // Sanity check to make sure it's only ever prefixed by one forward slash @@ -307,7 +314,9 @@ export abstract class BaseRestClient { /** * @private sign request and set recv window */ - private async signRequest( + private async signRequest< + T extends ParamsInQueryBodyOrHeader | undefined = {}, + >( data: T, endpoint: string, method: Method, @@ -405,7 +414,7 @@ export abstract class BaseRestClient { } private async prepareSignParams< - TParams extends ParamsInQueryAndOrBody | undefined, + TParams extends ParamsInQueryBodyOrHeader | undefined, >( method: Method, endpoint: string, @@ -414,7 +423,7 @@ export abstract class BaseRestClient { isPublicApi?: true, ): Promise>; private async prepareSignParams< - TParams extends ParamsInQueryAndOrBody | undefined, + TParams extends ParamsInQueryBodyOrHeader | undefined, >( method: Method, endpoint: string, @@ -423,7 +432,7 @@ export abstract class BaseRestClient { isPublicApi?: false | undefined, ): Promise>; private async prepareSignParams< - TParams extends ParamsInQueryAndOrBody | undefined, + TParams extends ParamsInQueryBodyOrHeader | undefined, >( method: Method, endpoint: string, @@ -450,18 +459,23 @@ export abstract class BaseRestClient { method: Method, endpoint: string, url: string, - params?: ParamsInQueryAndOrBody, + params?: ParamsInQueryBodyOrHeader, isPublicApi?: boolean, ): Promise { 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 { From febc39d9a422ce1be0ced1f23477666f5758566d Mon Sep 17 00:00:00 2001 From: Tiago Siebler Date: Mon, 18 Nov 2024 14:40:44 +0000 Subject: [PATCH 2/4] chore(): address audit fix --- package-lock.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1b93cbf..866315d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2172,9 +2172,9 @@ "dev": true }, "node_modules/cross-spawn": { - "version": "7.0.3", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", - "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==", + "version": "7.0.6", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", + "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==", "dev": true, "dependencies": { "path-key": "^3.1.0", From 078f2314ba7cb366bd8a9a259594bfae5c5245ea Mon Sep 17 00:00:00 2001 From: Tiago Siebler Date: Mon, 18 Nov 2024 14:54:46 +0000 Subject: [PATCH 3/4] feat(v1.1.0): allow endpoints to define custom headers, if supported. feat(): add exptime parameter to futures order req interface. --- package-lock.json | 4 ++-- package.json | 2 +- src/RestClient.ts | 11 +++++++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 866315d..afeae0d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "gateio-api", - "version": "1.0.22", + "version": "1.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "gateio-api", - "version": "1.0.22", + "version": "1.1.0", "license": "MIT", "dependencies": { "axios": "^1.6.6", diff --git a/package.json b/package.json index 948ac26..8d3673b 100644 --- a/package.json +++ b/package.json @@ -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/*", diff --git a/src/RestClient.ts b/src/RestClient.ts index 1e3d402..08c209d 100644 --- a/src/RestClient.ts +++ b/src/RestClient.ts @@ -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. `, @@ -2473,9 +2473,16 @@ export class RestClient extends BaseRestClient { * @returns Promise */ updateFuturesOrder(params: UpdateFuturesOrderReq): Promise { - 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, }); } From 4da1487da1bfc85928438db052bde72a35c9fd96 Mon Sep 17 00:00:00 2001 From: Tiago Siebler Date: Mon, 18 Nov 2024 14:57:33 +0000 Subject: [PATCH 4/4] feat(v1.1.0): add header param for amend futures order --- src/types/request/futures.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/types/request/futures.ts b/src/types/request/futures.ts index 9e762ac..4bee667 100644 --- a/src/types/request/futures.ts +++ b/src/types/request/futures.ts @@ -140,6 +140,7 @@ export interface GetFuturesOrdersByTimeRangeReq { } export interface UpdateFuturesOrderReq { + 'x-gate-exptime'?: number; settle: 'btc' | 'usdt' | 'usd'; order_id: string; size?: number;