import {z} from 'zod' export type GetApiApiV2TransportPortsCongestionArgs = {country?: (string | null) | undefined, ports?: (string | null) | undefined, top?: number | undefined, min_abs_z?: (number | null) | undefined, sort?: ('anomaly' | 'activity') | undefined}; export type GetApiApiV2TransportPortsCongestionOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const portCongestionQuery = z.object({country: z.string().nullable().optional(), ports: z.string().nullable().optional(), top: z.number().int(), min_abs_z: z.number().nullable().optional(), sort: z.string()}); export const portSetInfo = z.object({requested: z.string(), scorable_universe: z.number().int(), resolved: z.number().int(), capped: z.boolean()}); export const portThroughput = z.object({activity: z.number().nullable().optional(), baseline_mean: z.number().nullable().optional(), baseline_stddev: z.number().nullable().optional(), baseline_days: z.number().int().nullable().optional(), baseline_rows: z.number().int().nullable().optional(), baseline_window_days: z.number().int().nullable().optional(), z_score: z.number().nullable().optional(), pct_deviation: z.number().nullable().optional(), direction: z.string().nullable().optional(), band: z.string().nullable().optional(), available: z.boolean(), reason: z.string().nullable().optional(), reason_code: z.string().nullable().optional()}); export const portRiskOverlay = z.object({index: z.number().nullable().optional(), band: z.string().nullable().optional(), dominant_driver: z.string().nullable().optional(), components_count: z.number().int().nullable().optional(), sub_scores: z.record(z.string(), z.unknown()).optional(), wave_available: z.boolean().optional(), unavailable_factors: z.array(z.string()).optional(), available: z.boolean(), reason: z.string().nullable().optional()}); export const portCongestionEntry = z.object({port_id: z.string(), name: z.string().nullable().optional(), country: z.string().nullable().optional(), iso3: z.string().nullable().optional(), lat: z.number().nullable().optional(), lon: z.number().nullable().optional(), throughput: portThroughput, risk_overlay: portRiskOverlay}); export const portCongestionDataQuality = z.object({as_of: z.string().nullable(), published_at: z.string().nullable().optional(), staleness_days: z.number().int().nullable(), directional_ambiguity: z.string(), ais_confounds: z.string()}); export const portCongestionFeed = z.object({query: portCongestionQuery, port_set: portSetInfo, total_matched: z.number().int(), count: z.number().int(), ports: z.array(portCongestionEntry).optional(), methodology: z.record(z.string(), z.unknown()).optional(), data_quality: portCongestionDataQuality, attribution: z.record(z.string(), z.unknown()).optional(), disclaimer: z.string()}); 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 envelopePortCongestionFeed = z.object({data: portCongestionFeed, 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 }; /** * Port-call throughput deviation with a weather risk overlay - IMF PortWatch * * GET /api/v2/transport/ports/congestion */ export const getApiApiV2TransportPortsCongestion = async (args: GetApiApiV2TransportPortsCongestionArgs, options?: GetApiApiV2TransportPortsCongestionOptions) => { 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.country != null) query.append("country", String(args.country)) if (args.ports != null) query.append("ports", String(args.ports)) if (args.top != null) query.append("top", String(args.top)) if (args.min_abs_z != null) query.append("min_abs_z", String(args.min_abs_z)) if (args.sort != null) query.append("sort", String(args.sort)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/transport/ports/congestion${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(envelopePortCongestionFeed, await res.json()) };