import {z} from 'zod' export type SettingType = 'boolean' | 'string' | 'int' | 'double'; export type CreatePredefinedVariationValueModel = {boolValue?: (boolean | null) | undefined, stringValue?: (string | null) | undefined, intValue?: (number | null) | undefined, doubleValue?: (number | null) | undefined}; export type CreatePredefinedVariationModel = {value: CreatePredefinedVariationValueModel, name?: (string | null) | undefined, hint?: (string | null) | undefined}; export type SettingValueType = boolean | string | number; export type InitialValue = {environmentId?: string | undefined, value: SettingValueType}; export type CreateSettingInitialValues = {name: string, hint?: (string | null) | undefined, tags?: (Array | null) | undefined, order?: (number | null) | undefined, isJson?: (boolean | null) | undefined, key: string, settingType: SettingType, predefinedVariations?: (Array | null) | undefined, initialValues?: (Array | null) | undefined, settingIdToInitFrom?: (number | null) | undefined}; export type CreateApiV1ConfigsConfigIdSettingsArgs = {configId: string, body: CreateSettingInitialValues}; export type CreateApiV1ConfigsConfigIdSettingsOptions = {baseUrl?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const settingType = z.enum(["boolean", "string", "int", "double"]); export const organizationModel = z.object({organizationId: z.string(), name: z.string()}); export const productModel = z.object({organization: organizationModel, productId: z.string(), name: z.string(), description: z.string().nullable(), order: z.number().int(), reasonRequired: z.boolean(), approveRequired: z.boolean()}); export const tagModel = z.object({product: productModel, tagId: z.number().int(), name: z.string(), color: z.string().nullable()}); export const predefinedVariationValueModel = z.object({boolValue: z.boolean().nullable(), stringValue: z.string().nullable(), intValue: z.number().int().nullable(), doubleValue: z.number().nullable()}); export const predefinedVariationModel = z.object({value: predefinedVariationValueModel, name: z.string().max(255).nullable(), hint: z.string().max(1000).nullable(), predefinedVariationId: z.string()}); export const settingModel = z.object({settingId: z.number().int(), key: z.string(), name: z.string(), hint: z.string().nullable(), order: z.number().int(), settingType: settingType, isJson: z.boolean(), configId: z.string(), configName: z.string(), createdAt: z.string().nullable(), tags: z.array(tagModel), predefinedVariations: z.array(predefinedVariationModel)}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create Flag * * POST /v1/configs/{configId}/settings */ export const createApiV1ConfigsConfigIdSettings = async (args: CreateApiV1ConfigsConfigIdSettingsArgs, options?: CreateApiV1ConfigsConfigIdSettingsOptions) => { const {baseUrl = "https://api.configcat.com", fetch: fetchFn = fetch, Basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/configs/${encodeURIComponent(String(args.configId))}/settings${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(settingModel, await res.json()) };