import {z} from 'zod' export type IFormTypeEnum = 'natural' | 'simple'; export type IFormCreate = {org_id?: (string | null) | undefined, name: string | null, trigger_instructions: string | null, form_type: IFormTypeEnum | null, attributes?: {[key: string]: unknown} | undefined, fields?: Array | undefined, webhook_id?: (string | null) | undefined, trigger_tools?: (Array | null) | undefined, contact_mapping?: ({[key: string]: unknown} | null) | undefined}; export type CreateApiApiV1FormArgs = {body: IFormCreate}; export type CreateApiApiV1FormOptions = {baseUrl: string, YourAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const iFormTypeEnum = z.enum(["natural", "simple"]); export const iFormCreate = z.object({org_id: z.string().nullable().optional(), name: z.string().nullable(), trigger_instructions: z.string().nullable(), form_type: iFormTypeEnum.nullable(), attributes: z.record(z.string(), z.unknown()).optional(), fields: z.array(z.unknown()).optional(), webhook_id: z.string().nullable().optional(), trigger_tools: z.array(z.unknown()).nullable().optional(), contact_mapping: z.record(z.string(), z.unknown()).nullable().optional()}); export const iPostResponseBaseIFormCreate = z.object({message: z.string().nullable().optional(), meta: z.union([z.record(z.string(), z.unknown()), z.unknown()]).nullable().optional(), data: iFormCreate.nullable().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 }; /** * Create Form * * POST /api/v1/form * * Also accepts auth: HTTPBearer */ export const createApiApiV1Form = async (args: CreateApiApiV1FormArgs, options: CreateApiApiV1FormOptions) => { const {baseUrl, fetch: fetchFn = fetch, YourAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (YourAPIKey !== undefined) query.set("api_key", YourAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/form${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(iPostResponseBaseIFormCreate, await res.json()) };