import {z} from 'zod' export type GetApiPreferencesSectionsSectionIdArgs = {section_id: string}; export type GetApiPreferencesSectionsSectionIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const channelClassification = z.enum(["direct_message", "email", "push", "sms", "webhook", "inbox"]); export const subscriptionTopicStatus = z.enum(["OPTED_OUT", "OPTED_IN", "REQUIRED"]); export const allowedPreference = z.enum(["snooze", "channel_preferences"]); export const workspacePreferenceTopicGetResponse = z.object({id: z.string(), name: z.string(), description: z.string().nullable().optional(), default_status: subscriptionTopicStatus, routing_options: z.array(channelClassification), allowed_preferences: z.array(allowedPreference), include_unsubscribe_header: z.boolean(), topic_data: z.record(z.string(), z.unknown()), created: z.string(), creator: z.string().nullable().optional(), updated: z.string(), updater: z.string().nullable().optional()}); export const workspacePreferenceGetResponse = z.object({id: z.string(), name: z.string(), description: z.string().nullable().optional(), routing_options: z.array(channelClassification), has_custom_routing: z.boolean(), created: z.string(), creator: z.string().nullable().optional(), updated: z.string().nullable().optional(), updater: z.string().nullable().optional(), topics: z.array(workspacePreferenceTopicGetResponse)}); 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 a preference section * * GET /preferences/sections/{section_id} */ export const getApiPreferencesSectionsSectionId = async (args: GetApiPreferencesSectionsSectionIdArgs, options?: GetApiPreferencesSectionsSectionIdOptions) => { 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/preferences/sections/${encodeURIComponent(String(args.section_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(workspacePreferenceGetResponse, await res.json()) };