import { z } from "zod"; import { FeatureSchema } from "./data/schema.base"; /** * For flags whose `params` are unknown. * * @param defaults * The default values for the feature. * * @returns * The feature schema. */ export function flag(defaults: Partial> = {}) { return FeatureSchema.extend({ params: z.unknown().optional() }).default({ enabled: false, ...defaults, }); } /** * For flags whose `params` are a static object (fixed keys). * * @param params * The schema for the parameters. It should be a Zod object with fixed keys. * * @param defaults * The default values for the feature. * * @returns * The feature schema. */ export function flagWith

( params: P, defaults: Partial> & { params?: z.output> } = {}, ) { return FeatureSchema.extend({ params: z.object(params).optional() }).default({ enabled: false, ...defaults, }) as z.ZodDefault< z.ZodType & { params?: z.output> }> >; } /** * For flags whose `params` are a record/dictionary (dynamic keys). * * @param paramsSchema * The schema for the parameters. It should be a Zod object with dynamic keys. * * @param defaults * The default values for the feature. * * @returns * The feature schema. */ export function flagWithRecord( paramsSchema: T, defaults: Partial> & { params?: z.infer } = {}, ) { return FeatureSchema.extend({ params: paramsSchema.optional() }).default({ enabled: false, ...defaults, }); }