-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #210 from midday-ai/feature/exhange-rates
Feature/exchange rates
- Loading branch information
Showing
5 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { GeneralErrorSchema } from "@/common/schema"; | ||
import { getRates } from "@/utils/rates"; | ||
import { OpenAPIHono, createRoute } from "@hono/zod-openapi"; | ||
import type { Bindings } from "hono/types"; | ||
import { RatesSchema } from "./schema"; | ||
|
||
const app = new OpenAPIHono<{ Bindings: Bindings }>(); | ||
|
||
const indexRoute = createRoute({ | ||
method: "get", | ||
path: "/", | ||
summary: "Get rates", | ||
responses: { | ||
200: { | ||
content: { | ||
"application/json": { | ||
schema: RatesSchema, | ||
}, | ||
}, | ||
description: "Retrieve rates", | ||
}, | ||
400: { | ||
content: { | ||
"application/json": { | ||
schema: GeneralErrorSchema, | ||
}, | ||
}, | ||
description: "Returns an error", | ||
}, | ||
}, | ||
}); | ||
|
||
app.openapi(indexRoute, async (c) => { | ||
try { | ||
const data = await getRates(); | ||
|
||
return c.json( | ||
{ | ||
data, | ||
}, | ||
200, | ||
); | ||
} catch (error) { | ||
return c.json( | ||
{ | ||
error: "Internal server error", | ||
message: "Internal server error", | ||
requestId: c.get("requestId"), | ||
code: "400", | ||
}, | ||
400, | ||
); | ||
} | ||
}); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { z } from "zod"; | ||
|
||
export const RatesSchema = z | ||
.object({ | ||
data: z.array( | ||
z.object({ | ||
source: z.string().openapi({ | ||
example: "USD", | ||
}), | ||
rates: z.record(z.string(), z.number()).openapi({ | ||
example: { | ||
EUR: 0.925393, | ||
GBP: 0.792256, | ||
SEK: 10.0, | ||
BDT: 200.0, | ||
}, | ||
}), | ||
}), | ||
), | ||
}) | ||
|
||
.openapi("RatesSchema"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { uniqueCurrencies } from "@midday/location/src/currencies"; | ||
|
||
const ENDPOINT = | ||
"https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@2024.8.2/v1"; | ||
|
||
async function getCurrency(currency: string) { | ||
const response = await fetch(`${ENDPOINT}/currencies/${currency}.json`); | ||
|
||
return response.json(); | ||
} | ||
|
||
function transformKeysToUppercase(obj: Record<string, number>) { | ||
const entries = Object.entries(obj); | ||
|
||
// Transform each entry's key to uppercase | ||
const upperCaseEntries = entries | ||
.map(([key, value]) => { | ||
return [key.toUpperCase(), value]; | ||
}) | ||
.filter(([key]) => uniqueCurrencies.includes(key as string)); | ||
|
||
// Convert the transformed entries back into an object | ||
const transformedObject = Object.fromEntries(upperCaseEntries); | ||
|
||
return transformedObject; | ||
} | ||
|
||
export async function getRates() { | ||
const rates = await Promise.allSettled( | ||
uniqueCurrencies.map((currency) => getCurrency(currency.toLowerCase())), | ||
); | ||
|
||
return rates | ||
.filter( | ||
(rate): rate is PromiseFulfilledResult<Record<string, unknown>> => | ||
rate.status === "fulfilled", | ||
) | ||
.map((rate) => rate.value) | ||
.map((value) => { | ||
const currency = Object.keys(value).at(1); | ||
|
||
if (!currency) { | ||
return null; | ||
} | ||
|
||
const currencyData = value[currency]; | ||
if (typeof currencyData !== "object" || currencyData === null) { | ||
return null; | ||
} | ||
|
||
return { | ||
source: currency.toUpperCase(), | ||
rates: transformKeysToUppercase(currencyData as Record<string, number>), | ||
}; | ||
}) | ||
.filter((item) => item !== null); | ||
} |