import {z} from 'zod' export type ResendEmailInput = {id?: (string | null) | undefined, from: string, to: Array, subject: string, html?: (string | null) | undefined, text?: (string | null) | undefined, reply_to?: (string | null) | undefined, cc?: (Array | null) | undefined, bcc?: (Array | null) | undefined, tags?: (Array<{[key: string]: string}> | null) | undefined, created_at?: (string | null) | undefined}; export type ResendContactInput = {id?: (string | null) | undefined, email: string, first_name?: (string | null) | undefined, last_name?: (string | null) | undefined, unsubscribed?: (boolean | null) | undefined, created_at?: (string | null) | undefined}; export type ResendBroadcastInput = {id?: (string | null) | undefined, from: string, subject: string, name?: (string | null) | undefined, html?: (string | null) | undefined, text?: (string | null) | undefined, reply_to?: (string | null) | undefined, status?: (string | null) | undefined, created_at?: (string | null) | undefined, scheduled_at?: (string | null) | undefined, sent_at?: (string | null) | undefined, topic_id?: (string | null) | undefined}; export type ResendSegmentInput = {id?: (string | null) | undefined, name: string, created_at?: (string | null) | undefined, broadcasts?: (Array | null) | undefined}; export type ResendDataInput = {emails?: (Array | null) | undefined, contacts?: (Array | null) | undefined, segments?: (Array | null) | undefined}; export type CreateApiSandboxResendSandboxIdInitializeArgs = {sandbox_id: string, init_default_data?: boolean | undefined, body: ResendDataInput | null}; export type CreateApiSandboxResendSandboxIdInitializeOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const sandboxStatus = z.enum(["idle", "occupied", "error"]); export const initializeSandboxResponse = z.object({sandbox_id: z.string(), status: sandboxStatus, message: z.string()}); 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 }; /** * Initialize resend sandbox with data * * POST /sandbox/resend/{sandbox_id}/initialize */ export const createApiSandboxResendSandboxIdInitialize = async (args: CreateApiSandboxResendSandboxIdInitializeArgs, options?: CreateApiSandboxResendSandboxIdInitializeOptions) => { const {baseUrl = "https://api.klavis.ai", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.init_default_data != null) query.append("init_default_data", String(args.init_default_data)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/sandbox/resend/${encodeURIComponent(String(args.sandbox_id))}/initialize${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(initializeSandboxResponse, await res.json()) };