import {z} from 'zod' export type GetApiV1LogSuggestionsArgs = {body: {status?: string | undefined, per_page?: number | undefined, page_no?: number | undefined}}; export type GetApiV1LogSuggestionsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1LogSuggestionsResponse = z.object({log_suggestions: z.array(z.object({log_suggestion: z.object({id: z.number().int().optional(), driver_ids: z.array(z.number().int()).optional(), log_ids: z.array(z.number().int()).optional(), status: z.string().optional(), suggested_at: z.string().optional(), reason: z.unknown(), drivers: z.array(z.object({driver: z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional(), driver_company_id: z.string().optional(), eld_mode: z.string().optional()}).optional()})).optional(), dispatchers: z.array(z.object({dispatcher: z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional()}).optional()})).optional(), suggested_changes: z.array(z.object({field_name: z.string().optional(), old_value: z.unknown(), new_value: z.string().optional()})).optional()}).optional()})).optional(), pagination: z.object({per_page: z.number().int().optional(), page_no: z.number().int().optional(), total: z.number().int().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 }; /** * View all log suggestions * * GET /v1/log_suggestions */ export const getApiV1LogSuggestions = async (args: GetApiV1LogSuggestionsArgs, options?: GetApiV1LogSuggestionsOptions) => { 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(/\/+$/, '')}/v1/log_suggestions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", 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(getApiV1LogSuggestionsResponse, await res.json()) };