import {z} from 'zod' export type GetApiNotificationsNotificationIdArgs = {notification_id: string}; export type GetApiNotificationsNotificationIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const notification = z.object({action_url: z.string().max(2048).nullable().optional(), archived_at: z.string().nullable().optional(), category: z.string().max(100).nullable().optional(), content: z.string().max(10485760).nullable().optional(), created_at: z.string(), custom_attributes: z.object({}).nullable().optional(), discarded_at: z.string().nullable().optional(), id: z.string(), read_at: z.string().nullable().optional(), seen_at: z.string().nullable().optional(), sent_at: z.string().nullable().optional(), status: z.enum(["unseen", "unread", "read", "archived"]), title: z.string(), topic: z.string().max(100).nullable().optional(), updated_at: z.string(), user_id: z.string()}); 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 }; /** * Fetch a notification * * GET /notifications/{notification_id} */ export const getApiNotificationsNotificationId = async (args: GetApiNotificationsNotificationIdArgs, options?: GetApiNotificationsNotificationIdOptions) => { const {baseUrl = "https://api.magicbell.com/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/notifications/${encodeURIComponent(String(args.notification_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(notification, await res.json()) };