Skip to content

Commit

Permalink
✨ Change public api interface
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Nov 2, 2021
1 parent 867d930 commit 8818753
Show file tree
Hide file tree
Showing 28 changed files with 274 additions and 1,382 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@
}
},
"import/extensions": [".ts", ".tsx"]
}
},
"ignorePatterns": ["dist"]
}
3 changes: 1 addition & 2 deletions jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"^.+\\.(t|j)sx?$": "@swc/jest"
},
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/lib/$1",
"^@mock/(.*)$": "<rootDir>/tests/mock/$1"
"^@/(.*)$": "<rootDir>/lib/$1"
},
"clearMocks": true,
"collectCoverage": true,
Expand Down
3 changes: 3 additions & 0 deletions lib/api/public/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * from '@/api/public/ticker'
export * from '@/api/public/exchange_rate'
export * from '@/api/public/trades'
export * from '@/api/public/order_books'
export * from '@/api/public/rate'
40 changes: 40 additions & 0 deletions lib/api/public/order_books.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import fetch from 'node-fetch'

import { BASE_URL, API_ORDER_BOOKS } from '@/constants/api'

import type { RequestInit } from 'node-fetch'

const reciever = (key: string, value: [string, string][]) => {
if (
key === 'asks' ||
(key === 'bids' && Array.isArray(value) && !!value.length)
) {
return value.map((v) => {
const [vv, vvv] = v
return [parseFloat(vv), parseFloat(vvv)]
})
}
return value
}

type OrderBooksResponse = {
asks: [number, number][]
bids: [number, number][]
}

const orderBooks = async (init?: RequestInit): Promise<OrderBooksResponse> => {
const url = new URL(API_ORDER_BOOKS, BASE_URL)

const res = await fetch(url.toString(), init)

if (!res.ok) {
throw Error(res.statusText)
}

const text = await res.text()

return JSON.parse(text, reciever)
}

export { orderBooks }
export type { OrderBooksResponse }
55 changes: 55 additions & 0 deletions lib/api/public/rate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import fetch from 'node-fetch'

import { BASE_URL, API_RATE } from '../../constants/api'

import { join } from 'path'

import type { PublicAPI } from '@/shared/types'
import type { Join } from '@/utils/types'
import type { btc, jpy, etc, fct, mona, iost } from 'cryptocurrency-types'

const ALL_RATE_PAIRS: RatePair[] = [
'btc_jpy',
'etc_jpy',
'fct_jpy',
'iost_jpy',
'mona_jpy'
]

type RatePair =
| Join<btc, jpy>
| Join<etc, jpy>
| Join<fct, jpy>
| Join<mona, jpy>
| Join<iost, jpy>

type RateOptions = {
pair: RatePair
}

type RateResponse = {
rate: number
}

const reciever = (key: string, value: unknown) => {
if (key === 'rate' && typeof value === 'string') {
return parseFloat(value)
}
return value
}

const rate: PublicAPI<RateOptions, RateResponse> = async ({ pair }, init) => {
const url = new URL(join(API_RATE, pair), BASE_URL)
const res = await fetch(url.toString(), init)

if (!res.ok) {
throw Error(res.statusText)
}

const text = await res.text()

return JSON.parse(text, reciever) as RateResponse
}

export { rate, ALL_RATE_PAIRS }
export type { RateOptions, RateResponse }
73 changes: 73 additions & 0 deletions lib/api/public/trades.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import fetch from 'node-fetch'

import { ALL_EXCHANGE_RATE_PAIRS } from '@/api/public/exchange_rate'
import type { ExchangeRatePair } from '@/api/public/exchange_rate'
import { BASE_URL, API_TRADES } from '@/constants/api'
import { DateRegExp } from '@/utils/regex'

import type { PublicAPI, OrderType, Order } from '@/shared/types'

type TradesPair = ExchangeRatePair

const ALL_TRADES_PAIRS = ALL_EXCHANGE_RATE_PAIRS

const reciever = (key: string, value: unknown) => {
if (
key === 'created_at' &&
typeof value === 'string' &&
value.match(DateRegExp)
) {
return new Date(Date.parse(value))
} else if (
(key === 'amount' && typeof value === 'string') ||
(key === 'rate' && typeof value === 'string')
) {
return parseFloat(value)
}

return value
}

type TradesOptions = {
pair: TradesPair
}

type TradesResponse = {
success: boolean
pagination: {
limit: number
order: Order
starting_after: null
ending_before: null
}
data: {
id: number
amount: number
rate: number
pair: TradesPair
order_type: OrderType
created_at: Date
}[]
}

const trades: PublicAPI<TradesOptions, TradesResponse> = async (
{ pair },
init
) => {
const url = new URL(API_TRADES, BASE_URL)

url.search = new URLSearchParams({
pair
}).toString()
const res = await fetch(url.toString(), init)

if (!res.ok) {
throw Error(res.statusText)
}

const text = await res.text()

return JSON.parse(text, reciever) as TradesResponse
}

export { trades, ALL_TRADES_PAIRS }
14 changes: 0 additions & 14 deletions lib/base.ts

This file was deleted.

Loading

0 comments on commit 8818753

Please sign in to comment.