import {z} from 'zod' export type GetApiProfilesUserIdListsArgs = {user_id: string, cursor?: (string | null) | undefined}; export type GetApiProfilesUserIdListsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paging = z.object({cursor: z.string().nullable().optional(), more: z.boolean()}); export const preferenceStatus = z.enum(["OPTED_IN", "OPTED_OUT", "REQUIRED"]); export const rule = z.object({start: z.string().nullable().optional(), until: z.string()}); export const channelClassification = z.enum(["direct_message", "email", "push", "sms", "webhook", "inbox"]); export const channelPreference = z.object({channel: channelClassification}); export const notificationPreferenceDetails = z.object({status: preferenceStatus, rules: z.array(rule).nullable().optional(), channel_preferences: z.array(channelPreference).nullable().optional()}); export const notificationPreferences = z.record(z.string(), notificationPreferenceDetails); export const recipientPreferences = z.object({categories: notificationPreferences.optional(), notifications: notificationPreferences.optional()}); export const getListSubscriptionsItem = z.object({id: z.string(), name: z.string(), created: z.string(), updated: z.string(), preferences: recipientPreferences.optional()}); export const getListSubscriptionsResponse = z.object({paging: paging, results: z.array(getListSubscriptionsItem)}); 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 list subscriptions * * GET /profiles/{user_id}/lists */ export const getApiProfilesUserIdLists = async (args: GetApiProfilesUserIdListsArgs, options?: GetApiProfilesUserIdListsOptions) => { 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.cursor != null) query.append("cursor", String(args.cursor)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/profiles/${encodeURIComponent(String(args.user_id))}/lists${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(getListSubscriptionsResponse, await res.json()) };