import {z} from 'zod' export type CreateApiMdmCreateArgs = {key: unknown, option: 'flexible', spliter?: ('od_number_spliter' | 'straight_distance_spliter' | 'location_spliter') | undefined, body: {origins: string, destinations?: string | undefined, area?: ('singapore' | 'usa' | 'india') | undefined, mode?: ('car' | 'truck') | undefined, avoid?: ('toll' | 'ferry' | 'highway' | 'sharp_turn' | 'service_road' | 'bbox' | 'left_turn' | 'right_turn' | 'none') | undefined, departure_time?: number | undefined, route_type?: ('fastest' | 'shortest') | undefined, truck_size?: string | undefined, truck_weight?: number | undefined, origins_approach?: ('unrestricted' | 'curb') | undefined, destinations_approach?: ('unrestricted' | 'curb') | undefined, hazmat_type?: ('general' | 'circumstantial' | 'explosive' | 'harmful_to_water') | undefined, cross_border?: boolean | undefined, truck_axle_load?: number | undefined}}; export type CreateApiMdmCreateOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiMdmCreateResponse = z.object({task_id: z.string().optional(), code: z.string().optional(), message: z.string().optional(), warning: z.array(z.string()).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 Asynchronous Distance Matrix * * POST /mdm/create */ export const createApiMdmCreate = async (args: CreateApiMdmCreateArgs, options?: CreateApiMdmCreateOptions) => { const {baseUrl = "https://api.nextbillion.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.key != null) query.append("key", String(args.key)) if (args.option != null) query.append("option", String(args.option)) if (args.spliter != null) query.append("spliter", String(args.spliter)) if (apiKey !== undefined) query.set("key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/mdm/create${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(createApiMdmCreateResponse, await res.json()) };