import {z} from 'zod' export type ApiPreference = {id?: number | undefined, publicName?: (string | null) | undefined, privateName?: (string | null) | undefined, isPreference?: boolean | undefined, order?: (string | null) | undefined, isPublic?: boolean | undefined, categoryId?: number | undefined, preferenceCount?: number | undefined, created?: string | undefined, lastModified?: string | undefined, isOptedIn?: (boolean | null) | undefined, preferences?: (Array | null) | undefined}; export type UpdateApiV2PreferencesIdArgs = {id: number, body: ApiPreference}; export type UpdateApiV2PreferencesIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const apiPreference: z.ZodType = z.object({id: z.number().int().optional(), publicName: z.string().nullable().optional(), privateName: z.string().nullable().optional(), isPreference: z.boolean().optional(), order: z.string().nullable().optional(), isPublic: z.boolean().optional(), categoryId: z.number().int().optional(), preferenceCount: z.number().int().optional(), created: z.string().optional(), lastModified: z.string().optional(), isOptedIn: z.boolean().nullable().optional(), preferences: z.array(z.lazy(() => apiPreference)).nullable().optional()}); 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 the preference with the specified Id. * * PUT /v2/preferences/{id} */ export const updateApiV2PreferencesId = async (args: UpdateApiV2PreferencesIdArgs, options?: UpdateApiV2PreferencesIdOptions) => { const {baseUrl = "https://api.dotdigital.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/preferences/${encodeURIComponent(String(args.id))}${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(apiPreference, await res.json()) };