|
| 1 | +import { NextResponse } from 'next/server'; |
| 2 | +import { createClient } from '@vercel/edge-config'; |
| 3 | +import { init as initLD } from '@launchdarkly/vercel-server-sdk'; |
| 4 | + |
| 5 | +export const config = { |
| 6 | + runtime: 'edge', |
| 7 | +}; |
| 8 | + |
| 9 | +export async function GET() { |
| 10 | + const sdkKey = 'test-sdk-key'; |
| 11 | + const flagKey = 'testFlag1'; |
| 12 | + |
| 13 | + // default fallthrough evaluates to true |
| 14 | + const contextFallthrough = { kind: 'user', key: 'test-user-key-1' }; |
| 15 | + |
| 16 | + // matches email targeting rule evaluates to false |
| 17 | + const contextEmailRule = { kind: 'user', key: 'test-user-key-1', email: '[email protected]' }; |
| 18 | + |
| 19 | + const vercelClient = createClient(process.env.EDGE_CONFIG); |
| 20 | + |
| 21 | + // start using ld |
| 22 | + const client = initLD(sdkKey, vercelClient); |
| 23 | + await client.waitForInitialization(); |
| 24 | + const fallthrough = await client.variation(flagKey, contextFallthrough, false); |
| 25 | + const emailRule = await client.variation(flagKey, contextEmailRule, false); |
| 26 | + const flagDetail = await client.variationDetail(flagKey, contextEmailRule, false); |
| 27 | + const allFlags = await client.allFlagsState(contextEmailRule); |
| 28 | + |
| 29 | + return NextResponse.json({ |
| 30 | + flagKey: `${flagKey}`, |
| 31 | + contextKey: `${contextFallthrough.key}`, |
| 32 | + fallthrough, |
| 33 | + emailRule, |
| 34 | + emailRuleDetail: `${JSON.stringify(flagDetail)}`, |
| 35 | + allFlags: `${JSON.stringify(allFlags)}`, |
| 36 | + }); |
| 37 | +} |
0 commit comments