import {z} from 'zod' export type GetApiApiV0VesselHistoryArgs = {mmsi?: string | undefined, imo?: string | undefined, uuid?: string | undefined, days?: number | undefined, from?: string | undefined, to?: string | undefined}; export type GetApiApiV0VesselHistoryOptions = {baseUrl?: string | undefined, ApiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const vesselHistoryRecord = z.object({mmsi: z.string().optional(), lat: z.number().optional(), lon: z.number().optional(), speed: z.number().optional(), course: z.number().optional(), heading: z.number().optional(), navigational_status: z.string().nullable().optional(), destination: z.string().nullable().optional(), timestamp_UTC: z.string().optional(), timestamp_epoch: z.number().int().optional()}); export const meta = z.object({duration: z.number().optional(), endpoint: z.string().optional(), success: z.boolean().optional(), next: z.string().optional()}); export const getApiApiV0VesselHistoryResponse = z.object({data: z.array(vesselHistoryRecord).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 }; /** * Historical AIS positions for a vessel * * GET /api/v0/vessel_history * * Also accepts auth: ApiKeyQuery */ export const getApiApiV0VesselHistory = async (args: GetApiApiV0VesselHistoryArgs, options?: GetApiApiV0VesselHistoryOptions) => { 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.mmsi != null) query.append("mmsi", String(args.mmsi)) if (args.imo != null) query.append("imo", String(args.imo)) if (args.uuid != null) query.append("uuid", String(args.uuid)) if (args.days != null) query.append("days", String(args.days)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (ApiKeyHeader !== undefined) headers.set("x-api-key", ApiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v0/vessel_history${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(getApiApiV0VesselHistoryResponse, await res.json()) };