import {z} from 'zod' export type GetApiApiExtRouteArgs = {port_unlocode_from?: string | undefined, port_uuid_from?: string | undefined, lat_from?: number | undefined, lon_from?: number | undefined, port_unlocode_to?: string | undefined, port_uuid_to?: string | undefined, lat_to?: number | undefined, lon_to?: number | undefined}; export type GetApiApiExtRouteOptions = {baseUrl?: string | undefined, ApiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const meta = z.object({duration: z.number().optional(), endpoint: z.string().optional(), success: z.boolean().optional(), next: z.string().optional()}); export const getApiApiExtRouteResponse = z.object({data: z.object({distance_nm: z.number().optional(), waypoints: z.array(z.object({lat: z.number().optional(), lon: z.number().optional()})).optional()}).optional(), meta: meta.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 }; /** * Calculate sea route between two points * * GET /api/ext/route * * Also accepts auth: ApiKeyQuery */ export const getApiApiExtRoute = async (args: GetApiApiExtRouteArgs, options?: GetApiApiExtRouteOptions) => { const {baseUrl = "https://api.datalastic.com", fetch: fetchFn = fetch, ApiKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.port_unlocode_from != null) query.append("port_unlocode_from", String(args.port_unlocode_from)) if (args.port_uuid_from != null) query.append("port_uuid_from", String(args.port_uuid_from)) if (args.lat_from != null) query.append("lat_from", String(args.lat_from)) if (args.lon_from != null) query.append("lon_from", String(args.lon_from)) if (args.port_unlocode_to != null) query.append("port_unlocode_to", String(args.port_unlocode_to)) if (args.port_uuid_to != null) query.append("port_uuid_to", String(args.port_uuid_to)) if (args.lat_to != null) query.append("lat_to", String(args.lat_to)) if (args.lon_to != null) query.append("lon_to", String(args.lon_to)) if (ApiKeyHeader !== undefined) headers.set("x-api-key", ApiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/ext/route${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(getApiApiExtRouteResponse, await res.json()) };