import {z} from 'zod' export type GetApiUsersUserIdPreferencesTopicIdArgs = {user_id: string, topic_id: string, tenant_id?: (string | null) | undefined}; export type GetApiUsersUserIdPreferencesTopicIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const channelClassification = z.enum(["direct_message", "email", "push", "sms", "webhook", "inbox"]); export const preferenceStatus = z.enum(["OPTED_IN", "OPTED_OUT", "REQUIRED"]); export const usersTopicPreference = z.object({custom_routing: z.array(channelClassification).nullable().optional(), default_status: preferenceStatus, has_custom_routing: z.boolean().nullable().optional(), status: preferenceStatus, topic_id: z.string(), topic_name: z.string()}); export const usersUserPreferencesGetResponse = z.object({topic: usersTopicPreference}); 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 }; /** * Get user subscription topic * * GET /users/{user_id}/preferences/{topic_id} */ export const getApiUsersUserIdPreferencesTopicId = async (args: GetApiUsersUserIdPreferencesTopicIdArgs, options?: GetApiUsersUserIdPreferencesTopicIdOptions) => { const {baseUrl = "https://api.courier.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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/users/${encodeURIComponent(String(args.user_id))}/preferences/${encodeURIComponent(String(args.topic_id))}${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(usersUserPreferencesGetResponse, await res.json()) };