Skip to content

Commit

Permalink
fix(whale-api-client): new default url (#2178)
Browse files Browse the repository at this point in the history
#### What this PR does / why we need it:
- Updates the default URLs to point to
`<network>.ocean.jellyfishsdk.com`
- Adds the necessary test

---------

Co-authored-by: canonbrother <[email protected]>
  • Loading branch information
eli-lim and canonbrother committed Feb 5, 2024
1 parent e04c2b0 commit 26b8eb1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 41 deletions.
20 changes: 15 additions & 5 deletions packages/ocean-api-client/src/OceanApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'url-search-params-polyfill'
import AbortController from 'abort-controller'
import fetch from 'cross-fetch'
import { ApiException, ApiMethod, ApiPagedResponse, ApiResponse, ClientException, TimeoutException } from './'
import { NetworkName } from '@defichain/jellyfish-network'

/**
* OceanApiClient configurable options
Expand All @@ -24,7 +25,19 @@ export interface OceanApiClientOptions {
/**
* Network that ocean client is configured to
*/
network?: 'mainnet' | 'testnet' | 'devnet' | 'regtest' | 'changi' | string
network?: NetworkName | 'playground'
}

/**
* OceanApiClient default options
*/
function getDefaultOptions (network: NetworkName | 'playground'): OceanApiClientOptions {
return {
url: `https://${network}.ocean.jellyfishsdk.com`,
timeout: 60000,
version: 'v0',
network
}
}

/**
Expand All @@ -35,10 +48,7 @@ export class OceanApiClient {
protected readonly options: OceanApiClientOptions
) {
this.options = {
url: 'https://ocean.defichain.com',
timeout: 60000,
version: 'v1',
network: 'mainnet',
...getDefaultOptions(options?.network ?? 'mainnet'),
...options
}
this.options.url = this.options.url?.replace(/\/$/, '')
Expand Down
76 changes: 46 additions & 30 deletions packages/whale-api-client/__tests__/WhaleApiClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,58 @@
import nock from 'nock'
import { WhaleApiClient } from '@defichain/whale-api-client/dist/whale.api.client'
import { NetworkName } from '@defichain/jellyfish-network'

const client = new WhaleApiClient({
url: 'http://whale-api-test.internal',
network: 'regtest',
version: 'v0.0'
describe('WhaleApiClient default url', () => {
it.each<NetworkName>([
'mainnet',
'testnet'
])('should query default url successfully: %s', async (network: NetworkName) => {
const client = new WhaleApiClient({ network })
const response = await client.stats.get()
expect(response).toMatchObject({
blockchain: expect.any(Object)
})
})
})

it('should requestData via GET', async () => {
nock('http://whale-api-test.internal')
.get('/v0.0/regtest/foo')
.reply(200, function () {
return {
data: {
bar: ['1', '2']
describe('WhaleApiClient HTTP', () => {
const client = new WhaleApiClient({
url: 'http://whale-api-test.internal',
network: 'regtest',
version: 'v0.0'
})

it('should requestData via GET', async () => {
nock('http://whale-api-test.internal')
.get('/v0.0/regtest/foo')
.reply(200, function () {
return {
data: {
bar: ['1', '2']
}
}
}
})
})

const result = await client.requestData('GET', 'foo')
await expect(result).toStrictEqual({
bar: ['1', '2']
const result = await client.requestData('GET', 'foo')
await expect(result).toStrictEqual({
bar: ['1', '2']
})
})
})

it('should requestData via POST', async () => {
nock('http://whale-api-test.internal')
.post('/v0.0/regtest/bar')
.reply(200, function (_, body: object) {
return {
data: body
}
})
it('should requestData via POST', async () => {
nock('http://whale-api-test.internal')
.post('/v0.0/regtest/bar')
.reply(200, function (_, body: object) {
return {
data: body
}
})

const result = await client.requestData('POST', 'bar', {
abc: ['a', 'b', 'c']
})
await expect(result).toStrictEqual({
abc: ['a', 'b', 'c']
const result = await client.requestData('POST', 'bar', {
abc: ['a', 'b', 'c']
})
await expect(result).toStrictEqual({
abc: ['a', 'b', 'c']
})
})
})
17 changes: 11 additions & 6 deletions packages/whale-api-client/src/whale.api.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ export interface WhaleApiClientOptions {
/**
* WhaleApiClient default options
*/
const DEFAULT_OPTIONS: WhaleApiClientOptions = {
url: 'https://ocean.defichain.com',
timeout: 60000,
version: 'v0',
network: 'mainnet'
function getDefaultOptions (network: NetworkName): WhaleApiClientOptions {
return {
url: `https://${network}.ocean.jellyfishsdk.com`,
timeout: 60000,
version: 'v0',
network
}
}

/**
Expand Down Expand Up @@ -88,7 +90,10 @@ export class WhaleApiClient {
constructor (
protected readonly options: WhaleApiClientOptions
) {
this.options = { ...DEFAULT_OPTIONS, ...options }
this.options = {
...getDefaultOptions(options?.network ?? 'mainnet'),
...options
}
this.options.url = this.options.url?.replace(/\/$/, '')
}

Expand Down

0 comments on commit 26b8eb1

Please sign in to comment.