Skip to content

Commit

Permalink
fix(whale-api-client): new default url
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-lim committed Feb 2, 2024
1 parent e04c2b0 commit dc4296d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 36 deletions.
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 dc4296d

Please sign in to comment.