import {z} from 'zod' export type CreateApiDistanceJobsArgs = {body: {name: string, origins: string, destinations: string, distance_mode?: ('"driving"' | '"haversine"' | '"straightline"' | null) | undefined, calculation_type?: ('"matrix"' | '"pairs"' | null) | undefined, units?: ('"miles"' | '"km"' | null) | undefined, max_results?: (number | null) | undefined, max_distance?: (number | null) | undefined, max_duration?: (number | null) | undefined, min_distance?: (number | null) | undefined, min_duration?: (number | null) | undefined, order_by?: ('"distance"' | '"duration"' | null) | undefined, sort_order?: ('"asc"' | '"desc"' | null) | undefined, fields?: (string | null) | undefined, callback_url?: (string | null) | undefined}}; export type CreateApiDistanceJobsOptions = {baseUrl?: string | undefined, geocodioApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const warnings = z.array(z.string()); export const distanceMatrixJobCreated = z.object({id: z.number().int().optional(), identifier: z.string().optional(), status: z.string().optional(), name: z.string().optional(), created_at: z.string().optional(), origins_count: z.number().int().optional(), destinations_count: z.number().int().optional(), calculation_type: z.string().optional(), total_calculations: z.number().int().optional(), _warnings: warnings.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 async distance matrix or one-to-one pairs job * * POST /distance-jobs */ export const createApiDistanceJobs = async (args: CreateApiDistanceJobsArgs, options?: CreateApiDistanceJobsOptions) => { const {baseUrl = "https://api.geocod.io/v2.1", fetch: fetchFn = fetch, geocodioApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (geocodioApiKey !== undefined) query.set("api_key", geocodioApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/distance-jobs${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(distanceMatrixJobCreated, await res.json()) };