import {z} from 'zod' export type GetApiProductsNotifyInboxArgs = {recipient_id: string, state?: ('all' | 'unseen' | 'unread' | 'archived') | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiProductsNotifyInboxOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const notifyInboxRow = z.object({notification_id: z.string(), send_id: z.string(), created_at: z.number().int(), title: z.string().nullable().optional(), category: z.string().nullable().optional(), type: z.string().nullable().optional(), seen_at: z.number().int().nullable().optional(), read_at: z.number().int().nullable().optional(), archived_at: z.number().int().nullable().optional(), sparklink_code: z.string().optional(), pointer_url: z.string().optional(), locked: z.boolean(), recalled: z.boolean(), body: z.string().optional()}); export const notifyInboxPage = z.object({notifications: z.array(notifyInboxRow), cursor: z.string().nullable()}); export const responseMeta = z.object({api_version: z.string().optional(), request_id: z.string().optional(), response_ms: z.number().int().optional(), timestamp: z.number().int().optional(), pools: z.object({}).optional(), billing: z.object({past_due: z.boolean().optional()}).optional(), quota: z.object({limit: z.number().int().optional(), used: z.number().int().optional(), remaining: z.number().int().optional(), resets_at: z.number().int().optional()}).optional()}); export const getApiProductsNotifyInboxResponse = z.object({data: notifyInboxPage, meta: responseMeta.optional()}); 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 a recipient inbox * * GET /products/notify/inbox * * Also accepts auth: BearerAuth */ export const getApiProductsNotifyInbox = async (args: GetApiProductsNotifyInboxArgs, options?: GetApiProductsNotifyInboxOptions) => { const {baseUrl = "https://api.sparkvault.com/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.recipient_id != null) query.append("recipient_id", String(args.recipient_id)) if (args.state != null) query.append("state", String(args.state)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/products/notify/inbox${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(getApiProductsNotifyInboxResponse, await res.json()) };