forked from BETAIL-BOYS/TradeFlow-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
103 lines (86 loc) · 3.04 KB
/
Copy pathroute.ts
File metadata and controls
103 lines (86 loc) · 3.04 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { NextRequest, NextResponse } from "next/server";
import { getApiBaseUrl } from "../../../lib/env";
function getCorsHeaders(request: NextRequest): Record<string, string> {
const origin = request.headers.get("origin");
const allowList = (process.env.CORS_ALLOW_ORIGINS || "")
.split(",")
.map((s) => s.trim())
.filter(Boolean);
const headers: Record<string, string> = {
"Access-Control-Allow-Methods": "GET,OPTIONS",
"Access-Control-Allow-Headers": "Authorization,Content-Type",
"Access-Control-Max-Age": "86400",
Vary: "Origin",
};
if (origin && (allowList.length === 0 ? origin.startsWith("http://localhost") : allowList.includes(origin))) {
headers["Access-Control-Allow-Origin"] = origin;
}
return headers;
}
function isSafeInvoiceId(invoiceId: string): boolean {
if (!invoiceId) return false;
if (invoiceId.length > 128) return false;
return /^[a-zA-Z0-9._:-]+$/.test(invoiceId);
}
export async function OPTIONS(request: NextRequest) {
return new NextResponse(null, { status: 204, headers: getCorsHeaders(request) });
}
export async function GET(request: NextRequest) {
const corsHeaders = getCorsHeaders(request);
const invoiceId = request.nextUrl.searchParams.get("invoiceId") || "";
if (!isSafeInvoiceId(invoiceId)) {
return NextResponse.json(
{ error: { message: "Invalid invoiceId. Expected 1-128 chars: letters, numbers, . _ : -" } },
{ status: 400, headers: corsHeaders },
);
}
const apiUrl = getApiBaseUrl();
if (!apiUrl) {
const score =
invoiceId.split("").reduce((sum, ch) => sum + ch.charCodeAt(0), 0) % 101;
return NextResponse.json(
{
invoiceId,
riskScore: score,
updatedAt: new Date().toISOString(),
},
{ status: 200, headers: corsHeaders },
);
}
const upstreamUrl = new URL(`${apiUrl}/v1/risk`);
upstreamUrl.searchParams.set("invoiceId", invoiceId);
const auth = request.headers.get("authorization") || undefined;
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 10000);
try {
const upstreamRes = await fetch(upstreamUrl.toString(), {
method: "GET",
headers: {
Accept: "application/json",
...(auth ? { Authorization: auth } : {}),
},
signal: controller.signal,
cache: "no-store",
});
const text = await upstreamRes.text();
const contentType = upstreamRes.headers.get("content-type") || "application/json";
return new NextResponse(text, {
status: upstreamRes.status,
headers: {
...corsHeaders,
"Content-Type": contentType,
},
});
} catch (error) {
const message =
error instanceof DOMException && error.name === "AbortError"
? "Upstream /v1/risk request timed out"
: error instanceof Error
? error.message
: "Upstream /v1/risk request failed";
return NextResponse.json({ error: { message } }, { status: 502, headers: corsHeaders });
} finally {
clearTimeout(timeout);
}
}
// Maintenance: minor update