import {z} from 'zod' export type NotifyRecipient = string | {id?: string | undefined, email?: string | undefined, phone?: string | undefined}; export type NotifySendPolicy = {verification_level?: ('none' | 'identifier' | 'passkey') | undefined, interaction?: ('view' | 'acknowledge' | 'sign' | 'approve' | 'reply') | undefined, reveal_freshness_minutes?: number | undefined, action_hash?: string | undefined}; export type NotifySendRequest = {send_id?: string | undefined, recipients: Array, content?: ({payload: string, content_type?: string | undefined, filename?: string | undefined, ttl_minutes?: number | undefined}) | undefined, ingot?: {ingot_id: string, vault_id: string} | undefined, delivery?: ('sealed' | 'plaintext') | undefined, policy?: NotifySendPolicy | undefined, channels?: (Array<'in_app' | 'email'>) | undefined, escalation?: {delays: Array} | undefined, title?: string | undefined, category: string, type?: string | undefined, instructions?: string | undefined, history_ttl_days?: number | undefined}; export type CreateApiProductsNotifySendArgs = {'X-Vault-Access-Token'?: string | undefined, body: NotifySendRequest}; export type CreateApiProductsNotifySendOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const notifySendResponse = z.object({send_id: z.string(), recipients: z.number().int(), status: z.literal("pending"), idempotent: z.literal(true).optional()}); 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 createApiProductsNotifySendResponse = z.object({data: notifySendResponse, 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 }; /** * Send a notification * * POST /products/notify/send * * Also accepts auth: BearerAuth */ export const createApiProductsNotifySend = async (args: CreateApiProductsNotifySendArgs, options?: CreateApiProductsNotifySendOptions) => { const {baseUrl = "https://api.sparkvault.com/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Vault-Access-Token'] != null) headers.set("X-Vault-Access-Token", String(args['X-Vault-Access-Token'])) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/products/notify/send${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiProductsNotifySendResponse, await res.json()) };