diff --git a/package-lock.json b/package-lock.json index 1b93cbf..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", @@ -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", 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, }); } 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 { 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;