import {z} from 'zod' export type GetApiV1UserDistinctIdPreferenceCategoryArgs = {distinct_id: string, tenant_id?: string | undefined, show_opt_out_channels?: boolean | undefined, tags?: string | undefined, locale?: string | undefined}; export type GetApiV1UserDistinctIdPreferenceCategoryOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1UserDistinctIdPreferenceCategoryResponse = z.object({meta: z.object({count: z.number().int().optional(), limit: z.number().int().optional(), offset: z.number().int().optional()}).optional(), results: z.array(z.object({name: z.string().optional(), category: z.string().optional(), description: z.string().optional(), original_preference: z.string().nullable().optional(), preference: z.enum(["opt_in", "opt_out"]).optional(), is_editable: z.boolean().optional(), channels: z.array(z.object({channel: z.string().optional(), preference: z.string().optional(), is_editable: z.boolean().optional()})).optional(), digest_schedule_options: z.object({options: z.array(z.object({id: z.string().optional(), label: z.string().optional(), frequency: z.enum(["instantly", "minutely", "hourly", "daily", "weekly", "weekly_mo2fr", "monthly"]).optional(), interval: z.number().int().optional(), time: z.object({edit_policy: z.enum(["locked", "editable"]).optional(), default_value: z.string().optional(), value: z.string().nullable().optional()}).optional(), weekdays: z.object({edit_policy: z.enum(["locked", "editable"]).optional(), default_value: z.array(z.string()).optional(), value: z.array(z.string()).nullable().optional()}).optional(), monthdays: z.object({edit_policy: z.enum(["locked", "editable"]).optional(), default_value: z.array(z.object({pos: z.number().int().optional(), day: z.string().optional()})).optional(), value: z.array(z.object({})).nullable().optional()}).optional(), dtstart: z.object({edit_policy: z.enum(["locked", "editable"]).optional(), default_value: z.string().optional(), value: z.string().nullable().optional()}).optional(), is_default: z.boolean().optional(), is_user_selected: z.boolean().optional()})).optional()}).nullable().optional(), digest_schedule: z.object({id: z.string().optional(), label: z.string().optional(), frequency: z.enum(["instantly", "minutely", "hourly", "daily", "weekly", "weekly_mo2fr", "monthly"]).optional(), interval: z.number().int().optional(), time: z.string().optional(), weekdays: z.array(z.string()).optional(), monthdays: z.array(z.object({pos: z.number().int().optional(), day: z.string().optional()})).optional(), is_default: z.boolean().optional(), is_user_selected: z.boolean().optional()}).nullable().optional(), properties: z.array(z.object({key: z.string().optional(), label: z.string().optional(), value_type: z.enum(["integer", "string", "string_choice", "list_choice", "string_dynamic", "list_dynamic"]).optional(), default_value: z.unknown(), is_optional: z.boolean().optional(), edit_policy: z.enum(["locked", "editable"]).optional(), is_overridden: z.boolean().optional(), value: z.unknown()})).nullable().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 }; /** * All Categories * * GET /v1/user/{distinct_id}/preference/category/ */ export const getApiV1UserDistinctIdPreferenceCategory = async (args: GetApiV1UserDistinctIdPreferenceCategoryArgs, options?: GetApiV1UserDistinctIdPreferenceCategoryOptions) => { const {baseUrl = "https://hub.suprsend.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.tenant_id != null) query.append("tenant_id", String(args.tenant_id)) if (args.show_opt_out_channels != null) query.append("show_opt_out_channels", String(args.show_opt_out_channels)) if (args.tags != null) query.append("tags", String(args.tags)) if (args.locale != null) query.append("locale", String(args.locale)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/user/${encodeURIComponent(String(args.distinct_id))}/preference/category/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1UserDistinctIdPreferenceCategoryResponse, await res.json()) };