Skip to content

Commit f644838

Browse files
committed
Add heartbeat path in v3
1 parent d1577b7 commit f644838

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

src/v3/index.ts

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

66
import { config } from '../config'
7-
import { ratesV3 } from './router'
7+
import { heartbeatV3, ratesV3 } from './router'
88

99
async function main(): Promise<void> {
1010
server()
1111
}
1212

1313
function server(): void {
1414
const server = pickPath({
15+
'/': pickMethod({ GET: heartbeatV3 }),
1516
'/v3/rates': pickMethod({ POST: ratesV3 })
1617
})
1718

src/v3/router.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { syncedDocument } from 'edge-server-tools'
22
import type { HttpResponse } from 'serverlet'
33
import type { ExpressRequest } from 'serverlet/express'
44

5+
import { config } from '../config'
56
import { ONE_MINUTE } from './constants'
67
import { getRates } from './getRates'
78
import { dbSettings } from './providers/couch'
89
import {
910
asCrossChainMapping,
11+
asGetRatesParams,
1012
asIncomingGetRatesParams,
1113
type CrossChainMapping,
1214
type GetRatesParams,
@@ -148,3 +150,61 @@ export const ratesV3 = async (
148150
}
149151
}
150152
}
153+
154+
export const heartbeatV3 = async (
155+
request: ExpressRequest
156+
): Promise<HttpResponse> => {
157+
const testData = {
158+
targetFiat: 'USD',
159+
crypto: [
160+
{
161+
asset: {
162+
pluginId: 'bitcoin',
163+
tokenId: null
164+
}
165+
}
166+
],
167+
fiat: [
168+
{
169+
fiatCode: 'EUR'
170+
}
171+
]
172+
}
173+
174+
const response = await fetch(
175+
`http://${config.httpHost}:${config.httpPort}/v3/rates`,
176+
{
177+
method: 'POST',
178+
headers: {
179+
'Content-Type': 'application/json'
180+
},
181+
body: JSON.stringify(testData)
182+
}
183+
)
184+
185+
if (!response.ok) {
186+
return {
187+
status: 500,
188+
headers: { 'content-type': 'application/json' },
189+
body: JSON.stringify({ error: 'Failed to fetch rates' })
190+
}
191+
}
192+
193+
const json = await response.json()
194+
const data = asGetRatesParams(json)
195+
const btcRate = data.crypto.find(c => c.asset.pluginId === 'bitcoin')
196+
const eurRate = data.fiat.find(f => f.fiatCode === 'EUR')
197+
if (btcRate == null || eurRate == null) {
198+
return {
199+
status: 500,
200+
headers: { 'content-type': 'application/json' },
201+
body: JSON.stringify({ error: 'Failed to fetch rates' })
202+
}
203+
}
204+
205+
return {
206+
status: response.status,
207+
headers: { 'content-type': 'application/json' },
208+
body: JSON.stringify(json)
209+
}
210+
}

0 commit comments

Comments
 (0)