forked from Northgate-Systems/RemitX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
39 lines (33 loc) · 1.13 KB
/
Copy pathroute.ts
File metadata and controls
39 lines (33 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { NextRequest } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { getRate } from "@/lib/rates";
import { rateQuerySchema } from "@/lib/validations";
import { successResponse, errorResponse } from "@/lib/api-response";
export async function GET(request: NextRequest) {
try {
const user = await getCurrentUser();
if (!user) {
return errorResponse("Unauthorized", 401);
}
const { searchParams } = new URL(request.url);
const parsed = rateQuerySchema.safeParse({
from: searchParams.get("from"),
to: searchParams.get("to"),
});
if (!parsed.success) {
return errorResponse("Invalid query parameters. Required: from, to", 400);
}
const { from, to } = parsed.data;
const result = await getRate(from.toUpperCase(), to.toUpperCase());
return successResponse({
rate: result.rate,
fromAsset: result.fromAsset,
toAsset: result.toAsset,
fetchedAt: result.fetchedAt,
source: result.source,
});
} catch (err) {
console.error("Rate fetch error:", err);
return errorResponse("Failed to fetch exchange rate", 500);
}
}