Skip to content

Commit b4aafb2

Browse files
committed
Add v2/coinrankAsset support
1 parent d17f403 commit b4aafb2

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

src/v3/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import { pickMethod, pickPath } from 'serverlet'
44
import { makeExpressRoute } from 'serverlet/express'
55

66
import { config } from '../config'
7-
import { ratesV2, rateV2, sendCoinranksV2 } from './legacyRouter'
7+
import {
8+
ratesV2,
9+
rateV2,
10+
sendCoinrankAssetV2,
11+
sendCoinranksV2
12+
} from './legacyRouter'
813
import { heartbeatV3, ratesV3 } from './router'
914

1015
async function main(): Promise<void> {
@@ -17,6 +22,7 @@ function server(): void {
1722
'/v2/exchangeRate': pickMethod({ GET: rateV2 }),
1823
'/v2/exchangeRates': pickMethod({ POST: ratesV2 }),
1924
'/v2/coinrank': pickMethod({ GET: sendCoinranksV2 }),
25+
'/v2/coinrankAsset/([^/]+)': pickMethod({ GET: sendCoinrankAssetV2 }),
2026
'/v3/rates': pickMethod({ POST: ratesV3 })
2127
})
2228

src/v3/legacyRouter.ts

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import type { HttpResponse } from 'serverlet'
22
import type { ExpressRequest } from 'serverlet/express'
33

44
import { asExchangeRatesReq, getRedisMarkets } from '../exchangeRateRouter'
5-
import { asCoinrankReq, type CoinrankReq } from '../types'
5+
import {
6+
asCoinrankAssetReq,
7+
asCoinrankReq,
8+
type CoinrankAssetReq,
9+
type CoinrankReq
10+
} from '../types'
611
import { ratesV3, v2CurrencyCodeMapSyncDoc } from './router'
712
import { asGetRatesParams } from './types'
813
import { convertV2, convertV3ToV2 } from './v2converter'
@@ -141,3 +146,71 @@ export const sendCoinranksV2 = async (
141146
}
142147
}
143148
}
149+
150+
export const sendCoinrankAssetV2 = async (
151+
request: ExpressRequest
152+
): Promise<HttpResponse> => {
153+
if (request.req.query == null) {
154+
return {
155+
status: 400,
156+
headers: { 'content-type': 'application/json' },
157+
body: JSON.stringify({ error: 'Invalid request query' })
158+
}
159+
}
160+
161+
let query: CoinrankAssetReq
162+
try {
163+
query = asCoinrankAssetReq(request.req.query)
164+
} catch (e) {
165+
return {
166+
status: 400,
167+
headers: { 'content-type': 'application/json' },
168+
body: JSON.stringify({ error: `Invalid request query ${String(e)}` })
169+
}
170+
}
171+
const { fiatCode } = query
172+
const pathParts = request.path.split('/')
173+
const assetId = pathParts[pathParts.length - 1]
174+
if (assetId == null) {
175+
return {
176+
status: 400,
177+
headers: { 'content-type': 'application/json' },
178+
body: JSON.stringify({ error: 'Invalid request path' })
179+
}
180+
}
181+
try {
182+
const redisResult = await getRedisMarkets(fiatCode)
183+
184+
if (redisResult == null) {
185+
return {
186+
status: 400,
187+
headers: { 'content-type': 'application/json' },
188+
body: JSON.stringify({
189+
error: `Unable to get results for fiatCode ${fiatCode}`
190+
})
191+
}
192+
}
193+
194+
const { markets } = redisResult
195+
const market = markets.find(m => m.assetId === assetId)
196+
if (market == null) {
197+
return {
198+
status: 404,
199+
headers: { 'content-type': 'application/json' },
200+
body: JSON.stringify({ error: `assetId ${assetId} not found` })
201+
}
202+
}
203+
204+
return {
205+
status: 200,
206+
headers: { 'content-type': 'application/json' },
207+
body: JSON.stringify({ data: market })
208+
}
209+
} catch (e: unknown) {
210+
return {
211+
status: 500,
212+
headers: { 'content-type': 'application/json' },
213+
body: JSON.stringify({ error: `Internal server error ${String(e)}` })
214+
}
215+
}
216+
}

0 commit comments

Comments
 (0)