Skip to content

Commit

Permalink
chore(whale-api-client): Default whale api client to v0 and let url b…
Browse files Browse the repository at this point in the history
…e 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
  • Loading branch information
nichellekoh authored May 23, 2022
1 parent d937bb6 commit 3760431
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/whale-api-client/__tests__/WhaleApiClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
8 changes: 3 additions & 5 deletions packages/whale-api-client/__tests__/stub.client.ts
Original file line number Diff line number Diff line change
@@ -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'

/**
Expand All @@ -9,18 +8,17 @@ 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<ResponseAsString> {
if (this.service.app === undefined) {
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' } : {}
})
Expand All @@ -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' }
})
Expand Down
1 change: 0 additions & 1 deletion packages/whale-api-client/src/Version.ts

This file was deleted.

9 changes: 4 additions & 5 deletions packages/whale-api-client/src/WhaleApiClient.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -22,7 +21,7 @@ import { raiseIfError, WhaleApiException, WhaleClientException, WhaleClientTimeo
* WhaleApiClient Options
*/
export interface WhaleApiClientOptions {
url: string
url?: string

/**
* Millis before request is aborted.
Expand All @@ -48,7 +47,7 @@ export interface WhaleApiClientOptions {
const DEFAULT_OPTIONS: WhaleApiClientOptions = {
url: 'https://ocean.defichain.com',
timeout: 60000,
version: version,
version: 'v0',
network: 'mainnet'
}

Expand Down Expand Up @@ -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(/\/$/, '')
}

/**
Expand Down Expand Up @@ -160,7 +159,7 @@ export class WhaleApiClient {
*/
async requestAsString (method: Method, path: string, body?: string): Promise<ResponseAsString> {
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)
Expand Down

0 comments on commit 3760431

Please sign in to comment.