import {z} from 'zod' export type NotifyEventsConfig = {webhook_url?: string | undefined, secret?: string | undefined, secret_set?: boolean | undefined}; export type NotifyConfigCategory = {label?: string | undefined, description?: string | undefined, required?: boolean | undefined}; export type NotifyConfigPatch = {delivery?: ({default_channels?: (Array<'in_app' | 'email'>) | undefined, channel_priority?: ({[key: string]: Array<'in_app' | 'email'> | null}) | undefined, realtime_fallback_delay?: ({email_minutes?: number | undefined}) | undefined, escalation_enabled?: boolean | undefined}) | undefined, reliability?: ({rate_limit_per_recipient_per_minute?: number | undefined}) | undefined, history?: ({history_ttl_days?: number | undefined, receipt_retention_days?: number | undefined}) | undefined, security?: ({verification_level?: ('none' | 'identifier' | 'passkey') | undefined, reveal_freshness_minutes?: number | undefined, mandatory_seal_categories?: Array | undefined}) | undefined, events?: NotifyEventsConfig | undefined, email?: ({sending_domain?: (string | null) | undefined}) | undefined, categories?: ({[key: string]: NotifyConfigCategory | null}) | undefined}; export type UpdateApiProductsNotifyConfigArgs = {body: NotifyConfigPatch}; export type UpdateApiProductsNotifyConfigOptions = {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 updateApiProductsNotifyConfigResponse = 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 }; /** * Update Notify configuration * * PUT /products/notify/config * * Also accepts auth: BearerAuth */ export const updateApiProductsNotifyConfig = async (args: UpdateApiProductsNotifyConfigArgs, options?: UpdateApiProductsNotifyConfigOptions) => { 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 (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: "PUT", 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(updateApiProductsNotifyConfigResponse, await res.json()) };