Skip to content

Commit 26b8eb1

Browse files
fix(whale-api-client): new default url (#2178)
#### 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]>
1 parent e04c2b0 commit 26b8eb1

File tree

3 files changed

+72
-41
lines changed

3 files changed

+72
-41
lines changed

packages/ocean-api-client/src/OceanApiClient.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'url-search-params-polyfill'
22
import AbortController from 'abort-controller'
33
import fetch from 'cross-fetch'
44
import { ApiException, ApiMethod, ApiPagedResponse, ApiResponse, ClientException, TimeoutException } from './'
5+
import { NetworkName } from '@defichain/jellyfish-network'
56

67
/**
78
* OceanApiClient configurable options
@@ -24,7 +25,19 @@ export interface OceanApiClientOptions {
2425
/**
2526
* Network that ocean client is configured to
2627
*/
27-
network?: 'mainnet' | 'testnet' | 'devnet' | 'regtest' | 'changi' | string
28+
network?: NetworkName | 'playground'
29+
}
30+
31+
/**
32+
* OceanApiClient default options
33+
*/
34+
function getDefaultOptions (network: NetworkName | 'playground'): OceanApiClientOptions {
35+
return {
36+
url: `https://${network}.ocean.jellyfishsdk.com`,
37+
timeout: 60000,
38+
version: 'v0',
39+
network
40+
}
2841
}
2942

3043
/**
@@ -35,10 +48,7 @@ export class OceanApiClient {
3548
protected readonly options: OceanApiClientOptions
3649
) {
3750
this.options = {
38-
url: 'https://ocean.defichain.com',
39-
timeout: 60000,
40-
version: 'v1',
41-
network: 'mainnet',
51+
...getDefaultOptions(options?.network ?? 'mainnet'),
4252
...options
4353
}
4454
this.options.url = this.options.url?.replace(/\/$/, '')
Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,58 @@
11
import nock from 'nock'
22
import { WhaleApiClient } from '@defichain/whale-api-client/dist/whale.api.client'
3+
import { NetworkName } from '@defichain/jellyfish-network'
34

4-
const client = new WhaleApiClient({
5-
url: 'http://whale-api-test.internal',
6-
network: 'regtest',
7-
version: 'v0.0'
5+
describe('WhaleApiClient default url', () => {
6+
it.each<NetworkName>([
7+
'mainnet',
8+
'testnet'
9+
])('should query default url successfully: %s', async (network: NetworkName) => {
10+
const client = new WhaleApiClient({ network })
11+
const response = await client.stats.get()
12+
expect(response).toMatchObject({
13+
blockchain: expect.any(Object)
14+
})
15+
})
816
})
917

10-
it('should requestData via GET', async () => {
11-
nock('http://whale-api-test.internal')
12-
.get('/v0.0/regtest/foo')
13-
.reply(200, function () {
14-
return {
15-
data: {
16-
bar: ['1', '2']
18+
describe('WhaleApiClient HTTP', () => {
19+
const client = new WhaleApiClient({
20+
url: 'http://whale-api-test.internal',
21+
network: 'regtest',
22+
version: 'v0.0'
23+
})
24+
25+
it('should requestData via GET', async () => {
26+
nock('http://whale-api-test.internal')
27+
.get('/v0.0/regtest/foo')
28+
.reply(200, function () {
29+
return {
30+
data: {
31+
bar: ['1', '2']
32+
}
1733
}
18-
}
19-
})
34+
})
2035

21-
const result = await client.requestData('GET', 'foo')
22-
await expect(result).toStrictEqual({
23-
bar: ['1', '2']
36+
const result = await client.requestData('GET', 'foo')
37+
await expect(result).toStrictEqual({
38+
bar: ['1', '2']
39+
})
2440
})
25-
})
2641

27-
it('should requestData via POST', async () => {
28-
nock('http://whale-api-test.internal')
29-
.post('/v0.0/regtest/bar')
30-
.reply(200, function (_, body: object) {
31-
return {
32-
data: body
33-
}
34-
})
42+
it('should requestData via POST', async () => {
43+
nock('http://whale-api-test.internal')
44+
.post('/v0.0/regtest/bar')
45+
.reply(200, function (_, body: object) {
46+
return {
47+
data: body
48+
}
49+
})
3550

36-
const result = await client.requestData('POST', 'bar', {
37-
abc: ['a', 'b', 'c']
38-
})
39-
await expect(result).toStrictEqual({
40-
abc: ['a', 'b', 'c']
51+
const result = await client.requestData('POST', 'bar', {
52+
abc: ['a', 'b', 'c']
53+
})
54+
await expect(result).toStrictEqual({
55+
abc: ['a', 'b', 'c']
56+
})
4157
})
4258
})

packages/whale-api-client/src/whale.api.client.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ export interface WhaleApiClientOptions {
4747
/**
4848
* WhaleApiClient default options
4949
*/
50-
const DEFAULT_OPTIONS: WhaleApiClientOptions = {
51-
url: 'https://ocean.defichain.com',
52-
timeout: 60000,
53-
version: 'v0',
54-
network: 'mainnet'
50+
function getDefaultOptions (network: NetworkName): WhaleApiClientOptions {
51+
return {
52+
url: `https://${network}.ocean.jellyfishsdk.com`,
53+
timeout: 60000,
54+
version: 'v0',
55+
network
56+
}
5557
}
5658

5759
/**
@@ -88,7 +90,10 @@ export class WhaleApiClient {
8890
constructor (
8991
protected readonly options: WhaleApiClientOptions
9092
) {
91-
this.options = { ...DEFAULT_OPTIONS, ...options }
93+
this.options = {
94+
...getDefaultOptions(options?.network ?? 'mainnet'),
95+
...options
96+
}
9297
this.options.url = this.options.url?.replace(/\/$/, '')
9398
}
9499

0 commit comments

Comments
 (0)