Skip to content

Commit

Permalink
feat(ocean-api-client): add ocean-api client core (#909)
Browse files Browse the repository at this point in the history
* 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
fuxingloh and chee-chyuan authored Dec 23, 2021
1 parent bc6e321 commit 6a0ffc4
Show file tree
Hide file tree
Showing 15 changed files with 847 additions and 0 deletions.
48 changes: 48 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions packages/ocean-api-client/__tests__/ApiResponse.test.ts
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 packages/ocean-api-client/__tests__/OceanApiClient.test.ts
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']
})
})
Loading

0 comments on commit 6a0ffc4

Please sign in to comment.