import {z} from 'zod' export type DocumentTemplateContentRequest = {label: string, name?: string | undefined, type: 'string' | 'number' | 'date_time' | 'photos' | 'multi_choices' | 'signature' | 'barcode' | 'single_choice', required?: boolean | undefined, validation?: ({min?: number | undefined, max?: number | undefined, min_items?: number | undefined, max_items?: number | undefined}) | undefined, meta?: {options: Array<{label: string, value: string}>} | undefined}; export type CreateApiFleetifyDocumentTemplatesArgs = {key: string, body: {name: string, content: Array}}; export type CreateApiFleetifyDocumentTemplatesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const documentTemplateContentResponse = z.object({label: z.string().optional(), name: z.string().optional(), type: z.string().optional(), required: z.boolean().optional(), validation: z.object({min: z.number().int().optional(), max: z.number().int().optional(), min_items: z.string().optional(), max_items: z.string().optional()}).optional(), meta: z.object({options: z.array(z.object({label: z.string().optional(), value: z.string().optional()})).optional()}).optional()}); export const createApiFleetifyDocumentTemplatesResponse = z.object({status: z.number().int().optional(), msg: z.string().optional(), data: z.object({id: z.string().optional(), name: z.string().optional(), content: z.array(documentTemplateContentResponse).optional()}).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 Document template * * POST /fleetify/document_templates */ export const createApiFleetifyDocumentTemplates = async (args: CreateApiFleetifyDocumentTemplatesArgs, options?: CreateApiFleetifyDocumentTemplatesOptions) => { const {baseUrl = "https://api.nextbillion.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.key != null) query.append("key", String(args.key)) if (apiKey !== undefined) query.set("key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/fleetify/document_templates${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(createApiFleetifyDocumentTemplatesResponse, await res.json()) };