import {z} from 'zod' export type GetApiRoutesArgs = {status?: ('draft' | 'published' | 'blocked' | 'started' | 'finished' | Array<'draft' | 'published' | 'blocked' | 'started' | 'finished'>) | undefined, created_from?: string | undefined, created_to?: string | undefined}; export type GetApiRoutesOptions = {baseUrl?: string | undefined, orgToken?: string | undefined, fetch?: typeof fetch | undefined}; export const routeSummary = z.object({id: z.number().int().optional(), external_id: z.string().nullable().optional(), shift_date: z.string().optional(), status: z.string().optional(), driver: z.object({id: z.number().int().optional(), firstname: z.string().optional(), lastname: z.string().optional()}).nullable().optional(), vehicle: z.object({id: z.number().int().optional(), name: z.string().optional()}).nullable().optional(), created_at: z.string().optional()}); export const pagination = z.object({total: z.number().int().optional(), per_page: z.number().int().optional(), current_page: z.number().int().optional(), last_page: z.number().int().optional(), from: z.number().int().optional(), to: z.number().int().optional()}); export const getApiRoutesResponse = z.object({routes: z.object({data: z.array(routeSummary).optional(), links: z.object({first: z.string().nullable().optional(), last: z.string().nullable().optional(), prev: z.string().nullable().optional(), next: z.string().nullable().optional()}).optional(), meta: pagination.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 }; /** * Listar rutas * * GET /routes */ export const getApiRoutes = async (args: GetApiRoutesArgs, options?: GetApiRoutesOptions) => { const {baseUrl = "https://mi-organizacion.routes.zipnova.app/api/v1", fetch: fetchFn = fetch, orgToken} = 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.created_from != null) query.append("created_from", String(args.created_from)) if (args.created_to != null) query.append("created_to", String(args.created_to)) if (orgToken !== undefined) headers.set('authorization', `Bearer ${orgToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/routes${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(getApiRoutesResponse, await res.json()) };