import {z} from 'zod' export type GetApiApiV0PortFindArgs = {name?: string | undefined, country_iso?: string | undefined, unlocode?: string | undefined, uuid?: string | undefined, fuzzy?: (0 | 1) | undefined, port_type?: ('Port' | 'Anchorage' | 'Marina' | 'Offshore Terminal' | 'Shelter' | 'Demolition Yard' | 'Canal' | 'Fishing Harbour') | undefined, lat?: number | undefined, lon?: number | undefined, radius?: number | undefined}; export type GetApiApiV0PortFindOptions = {baseUrl?: string | undefined, ApiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const terminal = z.object({terminal_code: z.string().optional(), terminal_name: z.string().optional(), company_name: z.string().nullable().optional(), lat: z.number().optional(), lon: z.number().optional(), url: z.string().nullable().optional(), address: z.string().nullable().optional()}); export const port = z.object({uuid: z.string().optional(), port_name: z.string().optional(), country_iso: z.string().optional(), country_name: z.string().optional(), unlocode: z.string().optional(), port_type: z.string().optional(), lat: z.number().optional(), lon: z.number().optional(), area_lvl1: z.string().nullable().optional(), area_lvl2: z.string().nullable().optional(), terminals: z.array(terminal).optional()}); export const meta = z.object({duration: z.number().optional(), endpoint: z.string().optional(), success: z.boolean().optional(), next: z.string().optional()}); export const getApiApiV0PortFindResponse = z.object({data: z.array(port).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 }; /** * Find ports by name, country, or UNLOCODE * * GET /api/v0/port_find * * Also accepts auth: ApiKeyQuery */ export const getApiApiV0PortFind = async (args: GetApiApiV0PortFindArgs, options?: GetApiApiV0PortFindOptions) => { 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.name != null) query.append("name", String(args.name)) if (args.country_iso != null) query.append("country_iso", String(args.country_iso)) if (args.unlocode != null) query.append("unlocode", String(args.unlocode)) if (args.uuid != null) query.append("uuid", String(args.uuid)) if (args.fuzzy != null) query.append("fuzzy", String(args.fuzzy)) if (args.port_type != null) query.append("port_type", String(args.port_type)) if (args.lat != null) query.append("lat", String(args.lat)) if (args.lon != null) query.append("lon", String(args.lon)) if (args.radius != null) query.append("radius", String(args.radius)) if (ApiKeyHeader !== undefined) headers.set("x-api-key", ApiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v0/port_find${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(getApiApiV0PortFindResponse, await res.json()) };