-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhello.ts
55 lines (50 loc) · 1.39 KB
/
hello.ts
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
import type { NextRequest } from "next/server";
import {
type Context,
randomSessionId,
evaluateFlags,
getDefinitions,
} from "@unleash/nextjs";
import { UNLEASH_COOKIE_NAME } from "../../utils";
export const config = {
runtime: "edge",
};
export default async function handler(req: NextRequest) {
try {
const sessionId =
req.nextUrl.searchParams.get("sessionId") ||
req.cookies.get(UNLEASH_COOKIE_NAME)?.value ||
randomSessionId();
const remoteAddress =
req.nextUrl.searchParams.get("remoteAddress") ||
req.headers.get("x-forwarded-for") ||
req.ip;
const context: Context = {
userId: req.nextUrl.searchParams.get("userId") || undefined,
sessionId,
remoteAddress,
};
const definitions = await getDefinitions();
const { toggles } = evaluateFlags(definitions, context);
return new Response(JSON.stringify({ toggles }), {
status: 200,
headers: {
"no-cache": "no-cache",
"content-type": "application/json",
"set-cookie": `${UNLEASH_COOKIE_NAME}=${sessionId}; path=/;`,
},
});
} catch (error) {
return new Response(
JSON.stringify({
status: 500,
error: "Internal Server Error",
message: (error as Error)?.message || undefined,
}),
{
status: 500,
headers: { "content-type": "application/json" },
}
);
}
}