Skip to content

Commit b828058

Browse files
committed
refine path and naming
1 parent 65f199e commit b828058

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

node/proxy/api/src/app.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import swaggerUi from 'swagger-ui-express'
44
import { middleware } from '@shapeshiftoss/common-api'
55
import { Logger } from '@shapeshiftoss/logger'
66
import { RegisterRoutes } from './routes'
7-
import { Markets } from './coingecko'
7+
import { CoinGecko } from './coingecko'
88

99
const PORT = process.env.PORT ?? 3000
1010

@@ -32,8 +32,8 @@ app.use('/docs', swaggerUi.serve, swaggerUi.setup(undefined, options))
3232

3333
RegisterRoutes(app)
3434

35-
const markets = new Markets()
36-
app.get('/markets/*', markets.handler.bind(markets))
35+
const coingecko = new CoinGecko()
36+
app.get('/api/v1/markets/*', coingecko.handler.bind(coingecko))
3737

3838
// redirect any unmatched routes to docs
3939
app.get('/', async (_, res) => {

node/proxy/api/src/coingecko.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios, { AxiosResponse, isAxiosError } from 'axios'
22
import { Axios } from 'axios'
3-
import { NextFunction, Request, Response } from 'express'
3+
import { Request, Response } from 'express'
44

55
const COINGECKO_API_KEY = process.env.COINGECKO_API_KEY
66

@@ -10,24 +10,22 @@ const CACHE_TTL_MS = 60_000
1010

1111
type RequestCache = Partial<Record<string, AxiosResponse>>
1212

13-
export class Markets {
13+
export class CoinGecko {
1414
private axiosInstance: Axios
1515
private requestCache: RequestCache
1616

1717
constructor() {
1818
this.requestCache = {}
1919
this.axiosInstance = axios.create({
20-
baseURL: 'https://pro-api.coingecko.com/',
20+
baseURL: 'https://pro-api.coingecko.com/api/v3/',
2121
headers: {
2222
'x-cg-pro-api-key': COINGECKO_API_KEY,
2323
},
2424
})
2525
}
2626

27-
async handler(req: Request, res: Response, next: NextFunction): Promise<void> {
28-
const url = req.url.substring('/markets/'.length)
29-
30-
if (!url.startsWith('api/v3/')) return next()
27+
async handler(req: Request, res: Response): Promise<void> {
28+
const url = req.url.substring('/api/v1/markets/'.length)
3129

3230
const cachedResponse = this.requestCache[req.url]
3331
if (cachedResponse) {
@@ -40,7 +38,7 @@ export class Markets {
4038

4139
try {
4240
const response = await this.axiosInstance.get(url)
43-
this.requestCache[url] = response
41+
this.requestCache[req.url] = response
4442
Object.entries(response.headers).forEach(([k, v]) => res.set(k, v))
4543
res.status(response.status).send(response.data)
4644
} catch (err) {
@@ -52,7 +50,7 @@ export class Markets {
5250
res.status(500).send('Internal Server Error')
5351
}
5452
} finally {
55-
setInterval(() => delete this.requestCache[url], CACHE_TTL_MS)
53+
setInterval(() => delete this.requestCache[req.url], CACHE_TTL_MS)
5654
}
5755
}
5856
}

0 commit comments

Comments
 (0)