import {z} from 'zod' export type BatchMessagesStatusRequest = {message_ids: Array}; export type CreateApiV1MessagesBatchUnseenArgs = {body: BatchMessagesStatusRequest}; export type CreateApiV1MessagesBatchUnseenOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const recipientReference = z.union([z.string(), z.object({collection: z.string().optional(), id: z.string().optional()})]); export const message = z.object({__typename: z.string(), actors: z.array(recipientReference).optional(), archived_at: z.string().nullable().optional(), channel: z.object({created_at: z.string(), id: z.string(), key: z.string().nullable().optional(), name: z.string().nullable().optional(), provider: z.string(), type: z.enum(["email", "in_app", "in_app_feed", "in_app_guide", "sms", "push", "chat", "http"]), updated_at: z.string()}).optional(), channel_id: z.string(), clicked_at: z.string().nullable().optional(), data: z.record(z.string(), z.unknown()).nullable().optional(), engagement_statuses: z.array(z.enum(["seen", "read", "interacted", "link_clicked", "archived"])), id: z.string(), inserted_at: z.string(), interacted_at: z.string().nullable().optional(), link_clicked_at: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), read_at: z.string().nullable().optional(), recipient: recipientReference, recipient_snapshot: z.object({channel_id: z.string().nullable().optional(), email: z.string().nullable().optional(), name: z.string().nullable().optional(), user_id: z.string().nullable().optional(), via_incoming_webhook: z.boolean().nullable().optional()}).nullable().optional(), scheduled_at: z.string().nullable().optional(), seen_at: z.string().nullable().optional(), source: z.object({__typename: z.string(), branch_path: z.array(z.object({branch_id: z.string(), step_ref: z.string(), step_type: z.string()})).nullable().optional(), categories: z.array(z.string()), key: z.string(), step_ref: z.string().nullable().optional(), type: z.enum(["broadcast", "workflow", "guide"]).optional(), version_id: z.string(), workflow_recipient_run_id: z.string().nullable().optional(), workflow_run_id: z.string().nullable().optional()}), status: z.enum(["queued", "sent", "delivered", "delivery_attempted", "undelivered", "not_sent", "bounced"]), tenant: z.string().nullable().optional(), updated_at: z.string(), workflow: z.string().nullable().optional()}); export const createApiV1MessagesBatchUnseenResponse = z.array(message); 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 }; /** * Mark messages as unseen * * POST /v1/messages/batch/unseen */ export const createApiV1MessagesBatchUnseen = async (args: CreateApiV1MessagesBatchUnseenArgs, options?: CreateApiV1MessagesBatchUnseenOptions) => { const {baseUrl = "https://api.knock.app", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/messages/batch/unseen${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(createApiV1MessagesBatchUnseenResponse, await res.json()) };