import {z} from 'zod' export type CreateApiV2DocumentsArgs = {body: {uploader_id?: number | undefined, document_form_id?: number | undefined, doc_date?: string | undefined, ref_no?: string | undefined, notes?: string | undefined, doc_json_fields?: unknown | undefined, attachments?: (Array<{doc_name?: string | undefined, doc_size?: number | undefined, doc_type?: string | undefined}>) | undefined, doc_time?: string | undefined}}; export type CreateApiV2DocumentsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2DocumentsResponse = z.object({document: z.object({id: z.number().int().optional(), category: z.string().optional(), ref_no: z.string().optional(), notes: z.string().optional(), doc_date: z.string().optional(), doc_fields: z.object({notes: z.string().optional()}).optional(), doc_time: z.string().optional(), uploader: z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional(), email: z.string().optional(), role: z.string().optional(), deactivated_at: z.unknown(), driver_company_id: z.unknown()}).optional(), attachments: z.array(z.object({id: z.number().int().optional(), name: z.string().optional(), upload_url: z.string().optional(), doc_type: z.string().optional(), offline_id: z.unknown()})).optional(), field_docs: z.unknown(), created_at: z.string().optional(), updated_at: z.string().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 a new document (v2) * * POST /v2/documents */ export const createApiV2Documents = async (args: CreateApiV2DocumentsArgs, options?: CreateApiV2DocumentsOptions) => { const {baseUrl = "https://api.gomotive.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/v2/documents${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(createApiV2DocumentsResponse, await res.json()) };