import {z} from 'zod' export type UpdatePredefinedVariationValueModel = {boolValue?: (boolean | null) | undefined, stringValue?: (string | null) | undefined, intValue?: (number | null) | undefined, doubleValue?: (number | null) | undefined}; export type UpdatePredefinedVariationModel = {value: UpdatePredefinedVariationValueModel, name?: (string | null) | undefined, hint?: (string | null) | undefined, predefinedVariationId?: (string | null) | undefined}; export type UpdatePredefinedVariationsRequest = {predefinedVariations: Array}; export type UpdateApiV1SettingsSettingIdPredefinedVariationsArgs = {settingId: number, body: UpdatePredefinedVariationsRequest}; export type UpdateApiV1SettingsSettingIdPredefinedVariationsOptions = {baseUrl?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; 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 predefinedVariationsModel = z.object({predefinedVariations: z.array(predefinedVariationModel), maxPredefinedVariations: z.number().int()}); 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 }; /** * Update predefined variations * * PUT /v1/settings/{settingId}/predefined-variations */ export const updateApiV1SettingsSettingIdPredefinedVariations = async (args: UpdateApiV1SettingsSettingIdPredefinedVariationsArgs, options?: UpdateApiV1SettingsSettingIdPredefinedVariationsOptions) => { 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/settings/${encodeURIComponent(String(args.settingId))}/predefined-variations${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(predefinedVariationsModel, await res.json()) };