import {z} from 'zod' export type GetApiProductsNotifyConfigOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const notifyEventsConfig = z.object({webhook_url: z.string().max(2048).optional(), secret: z.string().min(16).max(256).optional(), secret_set: z.boolean().optional()}); export const notifyConfigCategory = z.object({label: z.string().max(60).optional(), description: z.string().max(200).optional(), required: z.boolean().optional()}); export const notifyEffectiveConfig = z.object({delivery: z.object({default_channels: z.array(z.enum(["in_app", "email", "sms", "push", "web_push", "voice", "whatsapp", "rcs", "webhook", "slack", "teams"])).optional(), channel_priority: z.record(z.string(), z.array(z.enum(["in_app", "email", "sms", "push", "web_push", "voice", "whatsapp", "rcs", "webhook", "slack", "teams"]))).optional(), realtime_fallback_delay: z.object({email_minutes: z.number().int().gte(0).lte(1440).optional()}).optional(), escalation_enabled: z.boolean().optional()}).optional(), reliability: z.object({rate_limit_per_recipient_per_minute: z.number().int().gte(1).lte(600).optional()}).optional(), history: z.object({history_ttl_days: z.number().int().gte(1).lte(3650).optional(), receipt_retention_days: z.number().int().gte(1).lte(3650).optional()}).optional(), security: z.object({verification_level: z.enum(["none", "identifier", "passkey"]).optional(), reveal_freshness_minutes: z.number().int().gte(0).optional(), mandatory_seal_categories: z.array(z.string()).optional()}).optional(), events: notifyEventsConfig.optional(), email: z.object({sending_domain: z.string().nullable().optional()}).optional(), categories: z.record(z.string(), notifyConfigCategory).optional()}); export const notifyConfigResponse = z.object({effective: notifyEffectiveConfig, overrides: z.object({})}); 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 getApiProductsNotifyConfigResponse = z.object({data: notifyConfigResponse, 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 }; /** * Read Notify configuration * * GET /products/notify/config * * Also accepts auth: BearerAuth */ export const getApiProductsNotifyConfig = async (options?: GetApiProductsNotifyConfigOptions) => { 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 (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/products/notify/config${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(getApiProductsNotifyConfigResponse, await res.json()) };