import {z} from 'zod' export type GetApiNotificationsArgs = {cursor?: (string | null) | undefined, notes?: (boolean | null) | undefined, event_id?: string | undefined}; export type GetApiNotificationsOptions = {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 messageRoutingMethod = z.enum(["all", "single"]); export const messageRoutingChannel = z.union([z.string(), z.lazy(() => messageRouting)]); export const messageRouting: z.ZodType = z.object({method: messageRoutingMethod, channels: z.array(messageRoutingChannel)}); export const notificationTagData = z.object({id: z.string(), name: z.string()}); export const notificationTag = z.object({data: z.array(notificationTagData)}); export const notification = z.object({created_at: z.number().int(), updated_at: z.number().int(), id: z.string(), routing: messageRouting, tags: notificationTag.optional(), title: z.string().nullable().optional(), topic_id: z.string(), note: z.string().optional(), event_ids: z.array(z.string())}); export const notificationTemplateSummary = z.object({id: z.string(), name: z.string(), tags: z.array(z.string()), state: z.enum(["DRAFT", "PUBLISHED"]), created: z.number().int(), creator: z.string(), updated: z.number().int().optional(), updater: z.string().optional(), subscription_topic_id: z.string().optional(), topic_id: z.string().optional()}); export const notificationListResponse = z.object({paging: paging, results: z.array(z.union([notification, notificationTemplateSummary]))}); 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 }; /** * List Notification Templates * * GET /notifications */ export const getApiNotifications = async (args: GetApiNotificationsArgs, options?: GetApiNotificationsOptions) => { 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 (args.notes != null) query.append("notes", String(args.notes)) if (args.event_id != null) query.append("event_id", String(args.event_id)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/notifications${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(notificationListResponse, await res.json()) };