|
| 1 | +import type { ReadonlyHeaders, ReadonlyRequestCookies } from 'flags'; |
1 | 2 | import { flag } from 'flags/sveltekit';
|
2 | 3 |
|
3 |
| -export const showDashboard = flag<boolean>({ |
4 |
| - key: 'showDashboard', |
5 |
| - description: 'Show the dashboard', // optional |
6 |
| - origin: 'https://example.com/#showdashbord', // optional |
| 4 | +export const showNewDashboard = flag<boolean>({ |
| 5 | + key: 'showNewDashboard', |
| 6 | + description: 'Show the new dashboard', // optional |
| 7 | + origin: 'https://example.com/#shownewdashbord', // optional |
7 | 8 | options: [{ value: true }, { value: false }], // optional
|
8 |
| - // can be async and has access to the event |
9 |
| - decide(_event) { |
10 |
| - return false; |
| 9 | + // can be async and has access to entities (see below for an example), headers and cookies |
| 10 | + decide({ cookies }) { |
| 11 | + return cookies.get('showNewDashboard')?.value === 'true'; |
| 12 | + } |
| 13 | +}); |
| 14 | + |
| 15 | +export const marketingABTestManualApproach = flag<boolean>({ |
| 16 | + key: 'marketingABTestManualApproach', |
| 17 | + description: 'Marketing AB Test Manual Approach', |
| 18 | + decide({ cookies, headers }) { |
| 19 | + return (cookies.get('marketingManual')?.value ?? headers.get('x-marketingManual')) === 'true'; |
| 20 | + } |
| 21 | +}); |
| 22 | + |
| 23 | +interface Entities { |
| 24 | + visitorId?: string; |
| 25 | +} |
| 26 | + |
| 27 | +function identify({ |
| 28 | + cookies, |
| 29 | + headers |
| 30 | +}: { |
| 31 | + cookies: ReadonlyRequestCookies; |
| 32 | + headers: ReadonlyHeaders; |
| 33 | +}): Entities { |
| 34 | + const visitorId = cookies.get('visitorId')?.value ?? headers.get('x-visitorId'); |
| 35 | + |
| 36 | + if (!visitorId) { |
| 37 | + throw new Error( |
| 38 | + 'Visitor ID not found - should have been set by middleware or within api/reroute' |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + return { visitorId }; |
| 43 | +} |
| 44 | + |
| 45 | +export const firstMarketingABTest = flag<boolean, Entities>({ |
| 46 | + key: 'firstMarketingABTest', |
| 47 | + description: 'Example of a precomputed flag', |
| 48 | + identify, |
| 49 | + decide({ entities }) { |
| 50 | + if (!entities?.visitorId) return false; |
| 51 | + |
| 52 | + // Use any kind of deterministic method that runs on the visitorId |
| 53 | + return /^[a-n0-5]/i.test(entities?.visitorId); |
| 54 | + } |
| 55 | +}); |
| 56 | + |
| 57 | +export const secondMarketingABTest = flag<boolean, Entities>({ |
| 58 | + key: 'secondMarketingABTest', |
| 59 | + description: 'Example of a precomputed flag', |
| 60 | + identify, |
| 61 | + decide({ entities }) { |
| 62 | + if (!entities?.visitorId) return false; |
| 63 | + |
| 64 | + // Use any kind of deterministic method that runs on the visitorId |
| 65 | + return /[a-n0-5]$/i.test(entities.visitorId); |
11 | 66 | }
|
12 | 67 | });
|
0 commit comments