Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Use HttpBaseClient for CurrencyAPI FX rates

Kiran K6b3c4d7a87eb
diff --git a/apps/web/app/(ee)/api/cron/fx-rates/route.ts b/apps/web/app/(ee)/api/cron/fx-rates/route.ts
index 20d6fedcfb..e38c7a050d 100644
--- a/apps/web/app/(ee)/api/cron/fx-rates/route.ts
+++ b/apps/web/app/(ee)/api/cron/fx-rates/route.ts
@@ -1,54 +1,26 @@
-import { handleAndReturnErrorResponse } from "@/lib/api/errors";
-import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { withCron } from "@/lib/cron/with-cron";
+import { currencyApiClient } from "@/lib/currencyapi/client";
import { redis } from "@/lib/upstash";
-import { log } from "@dub/utils";
-import { NextResponse } from "next/server";
+import { logAndRespond } from "../utils";
export const dynamic = "force-dynamic";
// Cron to update the Foreign Exchange Rates in Redis
// Runs once every day at 08:00 AM UTC (0 8 * * *)
-export async function POST(req: Request) {
- try {
- const rawBody = await req.text();
+// POST /api/cron/fx-rates
+export const POST = withCron(async () => {
+ const { data } = await currencyApiClient.getLatest();
- await verifyQstashSignature({
- req,
- rawBody,
- });
+ const transformedRates: Record<string, number> = {};
- const res = await fetch("https://api.currencyapi.com/v3/latest", {
- headers: {
- apikey: process.env.CURRENCY_API_KEY || "",
- },
- });
-
- const { data } = (await res.json()) as {
- data: Record<string, { value: number }>;
- };
-
- if (!data) {
- return NextResponse.json({
- message: "Failed to fetch FX rates",
- });
- }
-
- const transformedRates: Record<string, number> = {};
-
- for (const [ticker, details] of Object.entries(data)) {
- transformedRates[ticker] = details.value;
- }
-
- // // Store FX rates in Redis (with USD as the base currency)
- await redis.hset("fxRates:usd", transformedRates);
+ for (const [ticker, details] of Object.entries(data)) {
+ transformedRates[ticker] = details.value;
+ }
- return NextResponse.json(transformedRates);
- } catch (error) {
- await log({
- message: `Error updating FX rates: ${error.message}`,
- type: "cron",
- });
+ // Store FX rates in Redis (with USD as the base currency)
+ await redis.hset("fxRates:usd", transformedRates);
- return handleAndReturnErrorResponse(error);
- }
-}
+ return logAndRespond(
+ `Updated ${Object.keys(transformedRates).length} FX rates.`,
+ );
+});
diff --git a/apps/web/lib/currencyapi/client.ts b/apps/web/lib/currencyapi/client.ts
new file mode 100644
index 0000000000..eee5bd02c2
--- /dev/null
+++ b/apps/web/lib/currencyapi/client.ts
@@ -0,0 +1,23 @@
+import { assertEnv } from "@/lib/assert-env";
+import { HttpBaseClient } from "@/lib/http/base-client";
+import { getLatestOutputSchema } from "./schema";
+
+class CurrencyApiClient extends HttpBaseClient {
+ protected readonly vendor = "CurrencyAPI";
+ protected readonly baseUrl = "https://api.currencyapi.com/v3";
+
+ protected buildAuthHeaders() {
+ return {
+ apikey: assertEnv("CURRENCY_API_KEY"),
+ };
+ }
+
+ // GET /v3/latest
+ async getLatest() {
+ return await this.get("/latest", {
+ outputSchema: getLatestOutputSchema,
+ });
+ }
+}
+
+export const currencyApiClient = new CurrencyApiClient();
diff --git a/apps/web/lib/currencyapi/schema.ts b/apps/web/lib/currencyapi/schema.ts
new file mode 100644
index 0000000000..6f856fc188
--- /dev/null
+++ b/apps/web/lib/currencyapi/schema.ts
@@ -0,0 +1,11 @@
+import * as z from "zod/v4";
+
+// GET /v3/latest response
+export const getLatestOutputSchema = z.object({
+ data: z.record(
+ z.string(),
+ z.object({
+ value: z.number(),
+ }),
+ ),
+});