From 3760431bb2d84b4c07bbf50ed2188c4c2791a104 Mon Sep 17 00:00:00 2001 From: Nichelle <8347352+nichellekoh@users.noreply.github.com> Date: Mon, 23 May 2022 21:55:08 +0800 Subject: [PATCH] chore(whale-api-client): Default whale api client to v0 and let url be optional (#1466) * Default whale api client to v0 and let url be optional * Revert trimming of url * Default stub test whale api version to v0 * Default stub test whale api version to v0.0 * Remove constructor url as it is optional * Update test case whale version due to changed default * Fix mock version for test cases --- .../whale-api-client/__tests__/WhaleApiClient.test.ts | 3 ++- packages/whale-api-client/__tests__/stub.client.ts | 8 +++----- packages/whale-api-client/src/Version.ts | 1 - packages/whale-api-client/src/WhaleApiClient.ts | 9 ++++----- 4 files changed, 9 insertions(+), 12 deletions(-) delete mode 100644 packages/whale-api-client/src/Version.ts diff --git a/packages/whale-api-client/__tests__/WhaleApiClient.test.ts b/packages/whale-api-client/__tests__/WhaleApiClient.test.ts index 5a8c1b0168..cfec2ee916 100644 --- a/packages/whale-api-client/__tests__/WhaleApiClient.test.ts +++ b/packages/whale-api-client/__tests__/WhaleApiClient.test.ts @@ -3,7 +3,8 @@ import { WhaleApiClient } from '../src/WhaleApiClient' const client = new WhaleApiClient({ url: 'http://whale-api-test.internal', - network: 'whale' + network: 'whale', + version: 'v0.0' }) it('should requestData via GET', async () => { diff --git a/packages/whale-api-client/__tests__/stub.client.ts b/packages/whale-api-client/__tests__/stub.client.ts index d3c565e6ed..605c2b3339 100644 --- a/packages/whale-api-client/__tests__/stub.client.ts +++ b/packages/whale-api-client/__tests__/stub.client.ts @@ -1,6 +1,5 @@ import { Method, ResponseAsString, WhaleApiClient, WhaleRpcClient } from '../src' import { StubService } from './stub.service' -import { version } from '../src/Version' import AbortController from 'abort-controller' /** @@ -9,7 +8,7 @@ import AbortController from 'abort-controller' */ export class StubWhaleApiClient extends WhaleApiClient { constructor (readonly service: StubService) { - super({ url: 'not required for stub service' }) + super({}) } async requestAsString (method: Method, path: string, body?: string): Promise { @@ -17,10 +16,9 @@ export class StubWhaleApiClient extends WhaleApiClient { throw new Error('StubService is not yet started.') } - const version = this.options.version as string const res = await this.service.app.inject({ method: method, - url: `/${version}/regtest/${path}`, + url: `/v0.0/regtest/${path}`, payload: body, headers: method !== 'GET' ? { 'Content-Type': 'application/json' } : {} }) @@ -44,7 +42,7 @@ export class StubWhaleRpcClient extends WhaleRpcClient { const res = await this.service.app.inject({ method: 'POST', - url: `/${version as string}/regtest/rpc`, + url: '/v0.0/regtest/rpc', payload: body, headers: { 'Content-Type': 'application/json' } }) diff --git a/packages/whale-api-client/src/Version.ts b/packages/whale-api-client/src/Version.ts deleted file mode 100644 index a886d4f0f0..0000000000 --- a/packages/whale-api-client/src/Version.ts +++ /dev/null @@ -1 +0,0 @@ -export const version = 'v0.0' diff --git a/packages/whale-api-client/src/WhaleApiClient.ts b/packages/whale-api-client/src/WhaleApiClient.ts index d81683861f..2f6d793603 100644 --- a/packages/whale-api-client/src/WhaleApiClient.ts +++ b/packages/whale-api-client/src/WhaleApiClient.ts @@ -1,7 +1,6 @@ import 'url-search-params-polyfill' import AbortController from 'abort-controller' import fetch from 'cross-fetch' -import { version } from './Version' import { Address } from './api/Address' import { PoolPairs } from './api/PoolPairs' import { Rpc } from './api/Rpc' @@ -22,7 +21,7 @@ import { raiseIfError, WhaleApiException, WhaleClientException, WhaleClientTimeo * WhaleApiClient Options */ export interface WhaleApiClientOptions { - url: string + url?: string /** * Millis before request is aborted. @@ -48,7 +47,7 @@ export interface WhaleApiClientOptions { const DEFAULT_OPTIONS: WhaleApiClientOptions = { url: 'https://ocean.defichain.com', timeout: 60000, - version: version, + version: 'v0', network: 'mainnet' } @@ -81,7 +80,7 @@ export class WhaleApiClient { protected readonly options: WhaleApiClientOptions ) { this.options = { ...DEFAULT_OPTIONS, ...options } - this.options.url = this.options.url.replace(/\/$/, '') + this.options.url = this.options.url?.replace(/\/$/, '') } /** @@ -160,7 +159,7 @@ export class WhaleApiClient { */ async requestAsString (method: Method, path: string, body?: string): Promise { const { url: urlString, version, network, timeout } = this.options - const url = `${urlString}/${version as string}/${network as string}/${path}` + const url = `${urlString as string}/${version as string}/${network as string}/${path}` const controller = new AbortController() const id = setTimeout(() => controller.abort(), timeout)