import {z} from 'zod' export type PatchApiV1UserDistinctIdPreferenceArgs = {distinct_id: string, tenant_id?: string | undefined, body: {channel_preferences?: (Array<{channel?: string | undefined, is_restricted?: boolean | undefined}>) | undefined, categories?: (Array<{category?: string | undefined, preference?: ('opt_in' | 'opt_out') | undefined, opt_out_channels?: Array | undefined, opt_in_channels?: Array | undefined, digest_schedule?: ({id?: string | undefined, time?: string | undefined, dtstart?: string | undefined, weekdays?: (Array<'mo' | 'tu' | 'we' | 'th' | 'fr' | 'sa' | 'su'>) | undefined, monthdays?: (Array<{pos?: number | undefined, day?: string | undefined}>) | undefined} | null) | undefined, properties?: (Array<{key?: string | undefined, value: unknown}>) | undefined}>) | undefined}}; export type PatchApiV1UserDistinctIdPreferenceOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const patchApiV1UserDistinctIdPreferenceResponse = z.object({sections: z.array(z.object({name: z.string().nullable().optional(), slug: z.string().nullable().optional(), description: z.string().nullable().optional(), tags: z.array(z.string()).nullable().optional(), subcategories: z.array(z.object({name: z.string().optional(), category: z.string().optional(), description: z.string().nullable().optional(), tags: z.array(z.string()).nullable().optional(), effective_tags: z.array(z.string()).nullable().optional(), original_preference: z.enum(["opt_in", "opt_out", "cant_unsubscribe"]).nullable().optional(), preference: z.enum(["opt_in", "opt_out", "cant_unsubscribe"]).optional(), is_editable: z.boolean().optional(), channels: z.array(z.object({channel: z.string().optional(), preference: z.enum(["opt_in", "opt_out"]).optional(), is_editable: z.boolean().optional()})).optional()})).optional()})).optional(), channel_preferences: z.array(z.object({channel: z.string().optional(), is_restricted: z.boolean().optional()})).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 User Full Preference * * PATCH /v1/user/{distinct_id}/preference/ */ export const patchApiV1UserDistinctIdPreference = async (args: PatchApiV1UserDistinctIdPreferenceArgs, options?: PatchApiV1UserDistinctIdPreferenceOptions) => { const {baseUrl = "https://hub.suprsend.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.tenant_id != null) query.append("tenant_id", String(args.tenant_id)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/user/${encodeURIComponent(String(args.distinct_id))}/preference/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiV1UserDistinctIdPreferenceResponse, await res.json()) };