import {z} from 'zod' export type UpdateApiV2DocumentsIdArgs = {id: number, body: {upload_status: string}}; export type UpdateApiV2DocumentsIdOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const updateApiV2DocumentsIdResponse = 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 }; /** * Update the status of a document (v2) * * PUT /v2/documents/{id} */ export const updateApiV2DocumentsId = async (args: UpdateApiV2DocumentsIdArgs, options?: UpdateApiV2DocumentsIdOptions) => { 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/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiV2DocumentsIdResponse, await res.json()) };