import {z} from 'zod' export type GetApiApiV0WeatherArgs = {lat?: number | undefined, lon?: number | undefined, mmsi?: string | undefined, imo?: string | undefined, uuid?: string | undefined, port_unlocode?: string | undefined, port_uuid?: string | undefined, mode?: (Array<'current' | 'daily' | 'hourly'>) | undefined}; export type GetApiApiV0WeatherOptions = {baseUrl?: string | undefined, ApiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const weatherReading = z.object({time: z.string().optional(), interval: z.number().int().optional(), temperature_2m: z.number().optional(), precipitation: z.number().optional(), weather_code: z.number().int().nullable().optional(), wind_speed_10m: z.number().optional(), wind_direction_10m: z.number().optional(), wind_gusts_10m: z.number().optional(), cloud_cover: z.number().optional(), relative_humidity_2m: z.number().optional(), dew_point_2m: z.number().optional(), apparent_temperature: z.number().optional(), visibility: z.number().optional(), pressure_msl: z.number().optional(), wave_height: z.number().optional(), wave_direction: z.number().optional(), wave_period: z.number().optional(), wind_wave_height: z.number().optional(), wind_wave_direction: z.number().optional(), wind_wave_period: z.number().optional(), wind_wave_peak_period: z.number().optional(), swell_wave_height: z.number().optional(), swell_wave_direction: z.number().optional(), swell_wave_period: z.number().optional(), swell_wave_peak_period: z.number().optional(), secondary_swell_wave_height: z.number().optional(), secondary_swell_wave_direction: z.number().optional(), secondary_swell_wave_period: z.number().optional(), tertiary_swell_wave_height: z.number().optional(), tertiary_swell_wave_direction: z.number().optional(), tertiary_swell_wave_period: z.number().optional(), sea_surface_temperature: z.number().optional(), invert_barometer_height: z.number().optional(), sea_level_height_msl: z.number().optional(), ocean_current_velocity: z.number().optional(), ocean_current_direction: z.number().optional()}); export const weather = z.object({location: z.object({type: z.string().optional(), geometry: z.object({type: z.string().optional(), coordinates: z.array(z.number()).optional()}).optional(), properties: z.record(z.string(), z.unknown()).optional()}).optional(), weather: z.object({latitude: z.number().optional(), longitude: z.number().optional(), generationtime_ms: z.number().optional(), utc_offset_seconds: z.number().int().optional(), timezone: z.string().optional(), timezone_abbreviation: z.string().optional(), elevation: z.number().optional(), current: weatherReading.optional(), hourly: z.record(z.string(), z.unknown()).optional(), daily: z.record(z.string(), z.unknown()).optional()}).optional()}); export const meta = z.object({duration: z.number().optional(), endpoint: z.string().optional(), success: z.boolean().optional(), next: z.string().optional()}); export const getApiApiV0WeatherResponse = z.object({data: weather.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 }; /** * Marine and atmospheric weather for a location * * GET /api/v0/weather * * Also accepts auth: ApiKeyQuery */ export const getApiApiV0Weather = async (args: GetApiApiV0WeatherArgs, options?: GetApiApiV0WeatherOptions) => { 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.lat != null) query.append("lat", String(args.lat)) if (args.lon != null) query.append("lon", String(args.lon)) 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.port_unlocode != null) query.append("port_unlocode", String(args.port_unlocode)) if (args.port_uuid != null) query.append("port_uuid", String(args.port_uuid)) for (const item of args.mode ?? []) query.append("mode", String(item)) if (ApiKeyHeader !== undefined) headers.set("x-api-key", ApiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v0/weather${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(getApiApiV0WeatherResponse, await res.json()) };