import {z} from 'zod' export type GetApiV2DocumentsFormsArgs = {status: string, per_page?: number | undefined, page_no?: number | undefined}; export type GetApiV2DocumentsFormsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2DocumentsFormsResponse = z.object({forms: z.array(z.object({form: z.object({id: z.number().int().optional(), updated_at: z.string().optional(), hidden_from_drivers: z.boolean().optional(), editable: z.boolean().optional(), created_at: z.string().optional(), category: z.string().optional(), created_by: z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional()}).optional(), modified_by: z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional()}).optional()}).optional()})).optional(), per_page: z.number().int().optional(), page_no: z.number().int().optional(), total: z.number().int().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 }; /** * Fetch the document forms of your company * * GET /v2/documents/forms */ export const getApiV2DocumentsForms = async (args: GetApiV2DocumentsFormsArgs, options?: GetApiV2DocumentsFormsOptions) => { const {baseUrl = "https://api.gomotive.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.page_no != null) query.append("page_no", String(args.page_no)) if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/v2/documents/forms${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV2DocumentsFormsResponse, await res.json()) };