-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ocean-api-client): add
ocean-api
client core (#909)
* feat(ocean-api): add ocean-api-client core * add missing package * Update packages/ocean-api-client/__tests__/ApiResponse.test.ts Co-authored-by: chee-chyuan <[email protected]> Co-authored-by: chee-chyuan <[email protected]>
- Loading branch information
1 parent
bc6e321
commit 6a0ffc4
Showing
15 changed files
with
847 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { ApiPagedResponse, ApiResponse } from '@defichain/ocean-api-client' | ||
|
||
describe('ApiPagedResponse', () => { | ||
it('should have getter methods', () => { | ||
const response: ApiResponse<number[]> = { | ||
data: [0, 1, 2] | ||
} | ||
const pagination = new ApiPagedResponse(response, 'GET', '/') | ||
|
||
expect(pagination[0]).toStrictEqual(0) | ||
expect(pagination.endpoint).toStrictEqual('/') | ||
expect(pagination.method).toStrictEqual('GET') | ||
expect(pagination.page).toStrictEqual(undefined) | ||
expect(pagination.hasNext).toStrictEqual(false) | ||
expect(pagination.nextToken).toStrictEqual(undefined) | ||
}) | ||
|
||
it('should behave as an array', () => { | ||
const response: ApiResponse<number[]> = { | ||
data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
} | ||
|
||
const pagination = new ApiPagedResponse(response, 'GET', '/') | ||
|
||
expect(pagination.length).toStrictEqual(10) | ||
expect(pagination[0]).toStrictEqual(0) | ||
expect(pagination[4]).toStrictEqual(4) | ||
expect(pagination[9]).toStrictEqual(9) | ||
expect(pagination[10]).toBeUndefined() | ||
}) | ||
|
||
it('should have next token', () => { | ||
const response: ApiResponse<number[]> = { | ||
data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | ||
page: { | ||
next: '9' | ||
} | ||
} | ||
|
||
const pagination = new ApiPagedResponse(response, 'GET', '/items') | ||
|
||
expect(pagination.hasNext).toStrictEqual(true) | ||
expect(pagination.nextToken).toStrictEqual('9') | ||
}) | ||
|
||
it('should not have next', () => { | ||
const response: ApiResponse<number[]> = { | ||
data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
} | ||
|
||
const pagination = new ApiPagedResponse(response, 'GET', '/items') | ||
|
||
expect(pagination.hasNext).toStrictEqual(false) | ||
expect(pagination.nextToken).toBeUndefined() | ||
}) | ||
|
||
it('should be able to filter', () => { | ||
const response: ApiResponse<number[]> = { | ||
data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
} | ||
|
||
const pagination = new ApiPagedResponse(response, 'GET', '/items') | ||
|
||
expect(pagination.filter(value => value % 2 === 0)).toStrictEqual([ | ||
0, 2, 4, 6, 8 | ||
]) | ||
}) | ||
|
||
it('should be able to map', () => { | ||
const response: ApiResponse<number[]> = { | ||
data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
} | ||
|
||
const pagination = new ApiPagedResponse(response, 'GET', '/items') | ||
|
||
expect(pagination.map(value => value * 11)).toStrictEqual([ | ||
0, 11, 22, 33, 44, 55, 66, 77, 88, 99 | ||
]) | ||
}) | ||
}) |
42 changes: 42 additions & 0 deletions
42
packages/ocean-api-client/__tests__/OceanApiClient.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import nock from 'nock' | ||
import { OceanApiClient } from '@defichain/ocean-api-client' | ||
|
||
const client = new OceanApiClient({ | ||
url: 'http://ocean-api-test.internal', | ||
version: 'v0', | ||
network: 'mainnet' | ||
}) | ||
|
||
it('should requestData via GET', async () => { | ||
nock('http://ocean-api-test.internal') | ||
.get('/v0/mainnet/foo') | ||
.reply(200, function () { | ||
return { | ||
data: { | ||
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://ocean-api-test.internal') | ||
.post('/v0/mainnet/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'] | ||
}) | ||
}) |
Oops, something went wrong.