|
@@ -1,7 +1,8 @@
|
|
import { Hono } from "hono"
|
|
import { Hono } from "hono"
|
|
-import { Client, isFullPage } from "@notionhq/client"
|
|
|
|
import { z } from "zod"
|
|
import { z } from "zod"
|
|
import { zValidator } from "@hono/zod-validator"
|
|
import { zValidator } from "@hono/zod-validator"
|
|
|
|
+import { NotionProvider } from "./provider/notion"
|
|
|
|
+import { BocProvider } from "./provider/boc"
|
|
|
|
|
|
type Bindings = {
|
|
type Bindings = {
|
|
NOTION_KEY: string
|
|
NOTION_KEY: string
|
|
@@ -42,56 +43,22 @@ app.get(
|
|
const amount = parseFloat(c.req.param("amount"))
|
|
const amount = parseFloat(c.req.param("amount"))
|
|
|
|
|
|
try {
|
|
try {
|
|
- const client = new Client({ auth: notionKey, fetch: fetch.bind(globalThis) })
|
|
|
|
- const query = await client.databases.query({
|
|
|
|
- database_id: notionDatabaseId,
|
|
|
|
- page_size: 2,
|
|
|
|
- filter: {
|
|
|
|
- or: [
|
|
|
|
- {
|
|
|
|
- type: "title",
|
|
|
|
- property: "Code",
|
|
|
|
- title: {
|
|
|
|
- equals: from,
|
|
|
|
- },
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- type: "title",
|
|
|
|
- property: "Code",
|
|
|
|
- title: {
|
|
|
|
- equals: to,
|
|
|
|
- },
|
|
|
|
- },
|
|
|
|
- ],
|
|
|
|
|
|
+ const notion = new NotionProvider(notionKey, notionDatabaseId)
|
|
|
|
+ const ratesMap = await notion.queryRates([from, to])
|
|
|
|
+ return c.json(
|
|
|
|
+ {
|
|
|
|
+ rates: ratesMap,
|
|
|
|
+ exchange_rate: ratesMap[to] / ratesMap[from],
|
|
|
|
+ amount: amount,
|
|
|
|
+ target: (amount * ratesMap[to]) / ratesMap[from],
|
|
},
|
|
},
|
|
- })
|
|
|
|
- const pages = query.results.filter(page => isFullPage(page))
|
|
|
|
-
|
|
|
|
- let ratesMap: {
|
|
|
|
- [key: string]: number
|
|
|
|
- } = {}
|
|
|
|
- for (const page of pages) {
|
|
|
|
- let currencyCodeProp = page.properties["Code"]
|
|
|
|
- let rateProp = page.properties["Rate"]
|
|
|
|
- if (currencyCodeProp!.type !== "title") {
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- if (rateProp!.type !== "number") {
|
|
|
|
- continue
|
|
|
|
|
|
+ 200,
|
|
|
|
+ {
|
|
|
|
+ "Content-Type": "application/json; charset=utf-8",
|
|
|
|
+ "Cache-Control": "s-maxage=300, stale-while-revalidate=60",
|
|
|
|
+ "Access-Control-Allow-Origin": "*",
|
|
}
|
|
}
|
|
- ratesMap[currencyCodeProp.title[0]!.plain_text] = rateProp.number!
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return c.json({
|
|
|
|
- rates: ratesMap,
|
|
|
|
- exchange_rate: ratesMap[to] / ratesMap[from],
|
|
|
|
- amount: amount,
|
|
|
|
- target: amount * ratesMap[to] / ratesMap[from]
|
|
|
|
- }, 200, {
|
|
|
|
- "Content-Type": "application/json; charset=utf-8",
|
|
|
|
- "Cache-Control": "s-maxage=300, stale-while-revalidate=60",
|
|
|
|
- "Access-Control-Allow-Origin": "*",
|
|
|
|
- })
|
|
|
|
|
|
+ )
|
|
} catch (error: any) {
|
|
} catch (error: any) {
|
|
console.log(error)
|
|
console.log(error)
|
|
return c.json({ error: error.message }, 500)
|
|
return c.json({ error: error.message }, 500)
|
|
@@ -99,4 +66,11 @@ app.get(
|
|
}
|
|
}
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+app.get("/boc", async c => {
|
|
|
|
+ const boc = new BocProvider()
|
|
|
|
+ return c.json({
|
|
|
|
+ resp: await boc.queryRates(),
|
|
|
|
+ })
|
|
|
|
+})
|
|
|
|
+
|
|
export default app
|
|
export default app
|