import {z} from 'zod' export type GetApiListsListIdSubscriptionsArgs = {list_id: string, cursor?: (string | null) | undefined}; export type GetApiListsListIdSubscriptionsOptions = {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 listSubscriptionRecipient = z.object({recipientId: z.string(), created: z.string().nullable().optional(), preferences: recipientPreferences.optional()}); export const listGetSubscriptionsResponse = z.object({paging: paging, items: z.array(listSubscriptionRecipient)}); 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 the subscriptions for a list * * GET /lists/{list_id}/subscriptions */ export const getApiListsListIdSubscriptions = async (args: GetApiListsListIdSubscriptionsArgs, options?: GetApiListsListIdSubscriptionsOptions) => { 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(/\/+$/, '')}/lists/${encodeURIComponent(String(args.list_id))}/subscriptions${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(listGetSubscriptionsResponse, await res.json()) };