|
1 | 1 | import { z } from "zod"; |
2 | 2 |
|
3 | 3 | import { authRequest } from "../utils/auth.js"; |
4 | | -import { booleanish, EnvironmentQuerySchema } from "../utils/schemas.js"; |
5 | | -import { PaginatedResponse } from "../utils/types.js"; |
| 4 | +import type { FlagForTypes } from "../utils/gen.js"; |
| 5 | +import { EnvironmentQuerySchema } from "../utils/schemas.js"; |
6 | 6 |
|
7 | | -export type Stage = { |
8 | | - id: string; |
9 | | - name: string; |
10 | | - order: number; |
11 | | -}; |
12 | | - |
13 | | -export type RemoteConfigVariant = { |
14 | | - key?: string; |
15 | | - payload?: any; |
16 | | -}; |
17 | | - |
18 | | -export type RemoteConfig = { |
19 | | - variants: [ |
20 | | - { |
21 | | - variant: RemoteConfigVariant; |
22 | | - }, |
23 | | - ]; |
24 | | -}; |
25 | | - |
26 | | -export type FlagName = { |
27 | | - id: string; |
28 | | - name: string; |
29 | | - key: string; |
30 | | -}; |
31 | | - |
32 | | -export type Flag = FlagName & { |
33 | | - description: string | null; |
34 | | - remoteConfigs: RemoteConfig[]; |
35 | | - stage: Stage | null; |
36 | | -}; |
37 | | - |
38 | | -export type FlagsResponse = PaginatedResponse<Flag>; |
| 7 | +const FlagSchema = z.object({ |
| 8 | + id: z.string().min(1), |
| 9 | + name: z.string(), |
| 10 | + key: z.string().min(1), |
| 11 | + flagType: z.enum(["boolean", "multivariate"]), |
| 12 | + stage: z.object({ name: z.string() }).optional(), |
| 13 | +}); |
| 14 | +const FlagsResponseSchema = z.object({ |
| 15 | + data: z.array(FlagSchema), |
| 16 | + totalCount: z.number().int().nonnegative(), |
| 17 | +}); |
| 18 | +const MultivariateVersionsSchema = z.object({ |
| 19 | + flagId: z.string(), |
| 20 | + flagType: z.literal("multivariate"), |
| 21 | + versions: z.array( |
| 22 | + z.object({ |
| 23 | + environment: z.object({ id: z.string() }), |
| 24 | + currentVersion: z.boolean(), |
| 25 | + variants: z.record(z.string(), z.json()), |
| 26 | + }), |
| 27 | + ), |
| 28 | +}); |
39 | 29 |
|
40 | 30 | export const FlagsQuerySchema = EnvironmentQuerySchema.extend({ |
41 | | - sortBy: z.string().default("key").describe("Field to sort features by"), |
42 | | - sortOrder: z |
43 | | - .enum(["asc", "desc"]) |
44 | | - .default("asc") |
45 | | - .describe("Sort direction (ascending or descending)"), |
46 | | - includeRemoteConfigs: booleanish |
47 | | - .default(false) |
48 | | - .describe("Include remote configuration data"), |
| 31 | + sortBy: z |
| 32 | + .enum(["key", "name", "stage"]) |
| 33 | + .default("key") |
| 34 | + .describe("Field to sort flags by"), |
| 35 | + sortOrder: z.enum(["asc", "desc"]).default("asc").describe("Sort direction"), |
49 | 36 | }).strict(); |
50 | | - |
51 | 37 | export const FlagCreateSchema = z |
52 | 38 | .object({ |
53 | | - name: z |
54 | | - .string() |
55 | | - .min(1, "Flag name is required") |
56 | | - .describe("Name of the flag"), |
57 | | - key: z |
58 | | - .string() |
59 | | - .min(1, "Flag key is required") |
60 | | - .describe("Unique identifier key for the flag"), |
61 | | - description: z |
62 | | - .string() |
63 | | - .optional() |
64 | | - .describe("Optional description of the flag"), |
| 39 | + name: z.string().min(1, "Flag name is required"), |
| 40 | + key: z.string().min(1, "Flag key is required"), |
| 41 | + description: z.string().optional(), |
65 | 42 | }) |
66 | 43 | .strict(); |
| 44 | +const flagsPath = (appId: string) => `/apps/${encodeURIComponent(appId)}/flags`; |
67 | 45 |
|
68 | | -export type FlagsQuery = z.input<typeof FlagsQuerySchema>; |
69 | | -export type FlagCreate = z.input<typeof FlagCreateSchema>; |
70 | | - |
71 | | -export async function listFlags(appId: string, query: FlagsQuery) { |
72 | | - return authRequest<FlagsResponse>(`/apps/${appId}/features`, { |
| 46 | +export async function listFlags( |
| 47 | + appId: string, |
| 48 | + query: z.input<typeof FlagsQuerySchema>, |
| 49 | +) { |
| 50 | + const response = await authRequest(flagsPath(appId), { |
73 | 51 | params: FlagsQuerySchema.parse(query), |
74 | 52 | }); |
| 53 | + return FlagsResponseSchema.parse(response); |
75 | 54 | } |
76 | 55 |
|
77 | | -type CreateFlagResponse = { |
78 | | - feature: FlagName & { |
79 | | - description: string | null; |
80 | | - }; |
81 | | -}; |
| 56 | +/** Generate schemas from current values in the selected environment, never legacy remote-config wrappers. */ |
| 57 | +export async function listFlagValues( |
| 58 | + appId: string, |
| 59 | + envId: string, |
| 60 | +): Promise<FlagForTypes[]> { |
| 61 | + const { data, totalCount } = await listFlags(appId, { envId }); |
| 62 | + if (data.length !== totalCount) { |
| 63 | + throw new Error("Cannot generate types from an incomplete flag list"); |
| 64 | + } |
| 65 | + const flags: FlagForTypes[] = []; |
| 66 | + for (const { id, key, flagType } of data) { |
| 67 | + if (flagType === "boolean") { |
| 68 | + flags.push({ key, flagType }); |
| 69 | + continue; |
| 70 | + } |
| 71 | + const { flagId, versions } = MultivariateVersionsSchema.parse( |
| 72 | + await authRequest( |
| 73 | + `${flagsPath(appId)}/${encodeURIComponent(id)}/targeting`, |
| 74 | + { |
| 75 | + params: { envId }, |
| 76 | + }, |
| 77 | + ), |
| 78 | + ); |
| 79 | + const [version] = versions; |
| 80 | + if ( |
| 81 | + flagId !== id || |
| 82 | + versions.length > 1 || |
| 83 | + (version && (version.environment.id !== envId || !version.currentVersion)) |
| 84 | + ) { |
| 85 | + throw new Error( |
| 86 | + `Unexpected current-version response for flag ${JSON.stringify(key)}`, |
| 87 | + ); |
| 88 | + } |
| 89 | + flags.push({ |
| 90 | + key, |
| 91 | + flagType, |
| 92 | + values: Object.values(version?.variants ?? {}), |
| 93 | + }); |
| 94 | + } |
| 95 | + return flags; |
| 96 | +} |
82 | 97 |
|
83 | | -export async function createFlag(appId: string, featureData: FlagCreate) { |
84 | | - return authRequest<CreateFlagResponse>(`/apps/${appId}/features`, { |
| 98 | +export async function createFlag( |
| 99 | + appId: string, |
| 100 | + flag: z.input<typeof FlagCreateSchema>, |
| 101 | +) { |
| 102 | + const response = await authRequest(flagsPath(appId), { |
85 | 103 | method: "POST", |
86 | | - headers: { |
87 | | - "Content-Type": "application/json", |
88 | | - }, |
| 104 | + headers: { "Content-Type": "application/json" }, |
89 | 105 | body: JSON.stringify({ |
90 | 106 | source: "event", |
91 | | - ...FlagCreateSchema.parse(featureData), |
| 107 | + flagType: "boolean", |
| 108 | + ...FlagCreateSchema.parse(flag), |
92 | 109 | }), |
93 | | - }).then(({ feature }) => feature); |
| 110 | + }); |
| 111 | + return z.object({ flag: FlagSchema }).parse(response).flag; |
94 | 112 | } |
0 commit comments