import {z} from 'zod' export type GetApiApiV1WeatherMarineSeaLevelArgs = {latitude: number, longitude: number, hours?: number | undefined, past_days?: number | undefined, forecast_days?: number | undefined}; export type GetApiApiV1WeatherMarineSeaLevelOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const seaLevelRow = z.object({timestamp: z.string(), sea_level_height_msl_m: z.number().nullable().optional(), invert_barometer_height_m: z.number().nullable().optional()}).and(z.record(z.string(), z.unknown())); export const marineSeaLevelData = z.object({latitude: z.number(), longitude: z.number(), elevation_m: z.number().nullable().optional(), timezone: z.string().nullable().optional(), units: z.record(z.string(), z.string()), hours_returned: z.number().int(), past_days: z.number().int(), forecast_days: z.number().int(), data: z.array(seaLevelRow)}).and(z.record(z.string(), z.unknown())); export const sugraMeta = z.object({endpoint: z.string(), data_time: z.string(), response_time: z.string(), provider: z.string(), data_age_days: z.number().nullable().optional(), source: z.string().nullable().optional(), attribution: z.string().nullable().optional(), source_url: z.string().nullable().optional(), citation: z.string().nullable().optional(), fallback_used: z.boolean().nullable().optional(), fallback_chain: z.array(z.string()).nullable().optional(), cached: z.boolean().nullable().optional(), stale: z.boolean().nullable().optional(), stale_since: z.string().nullable().optional(), period: z.string().nullable().optional(), notes: z.string().nullable().optional()}); export const envelopeMarineSeaLevelData = z.object({data: marineSeaLevelData, meta: sugraMeta}); 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 }; /** * Hourly sea-level and storm-surge forecast * * GET /api/v1/weather/marine/sea-level */ export const getApiApiV1WeatherMarineSeaLevel = async (args: GetApiApiV1WeatherMarineSeaLevelArgs, options?: GetApiApiV1WeatherMarineSeaLevelOptions) => { const {baseUrl = "https://sugra.ai", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.latitude != null) query.append("latitude", String(args.latitude)) if (args.longitude != null) query.append("longitude", String(args.longitude)) if (args.hours != null) query.append("hours", String(args.hours)) if (args.past_days != null) query.append("past_days", String(args.past_days)) if (args.forecast_days != null) query.append("forecast_days", String(args.forecast_days)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/weather/marine/sea-level${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(envelopeMarineSeaLevelData, await res.json()) };