Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(whale-api-client): new default url #2178

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading