MCPcopy Create free account
hub / github.com/vercel/examples / proxy

Function proxy

flags-sdk/launchdarkly/proxy.ts:11–37  ·  view source on GitHub ↗
(request: NextRequest)

Source from the content-addressed store, hash-verified

9}
10
11export async function proxy(request: NextRequest) {
12 const stableId = await getStableId()
13 const cartId = await getCartId()
14 const code = await precompute(productFlags)
15
16 // rewrites the request to the variant for this flag combination
17 const nextUrl = new URL(
18 `/${code}${request.nextUrl.pathname}${request.nextUrl.search}`,
19 request.url
20 )
21
22 // Add a header to the request to indicate that the stable id is generated,
23 // as it will not be present on the cookie request header on the first-ever request.
24 if (cartId.isFresh) {
25 request.headers.set('x-generated-cart-id', cartId.value)
26 }
27
28 if (stableId.isFresh) {
29 request.headers.set('x-generated-stable-id', stableId.value)
30 }
31
32 // response headers
33 const headers = new Headers()
34 headers.append('set-cookie', `stable-id=${stableId.value}`)
35 headers.append('set-cookie', `cart-id=${cartId.value}`)
36 return NextResponse.rewrite(nextUrl, { request, headers })
37}

Callers

nothing calls this directly

Calls 1

setMethod · 0.80

Tested by

no test coverage detected