import {z} from 'zod' export type Radius = number; export type Coordinates = Array; export type Circle = {radius: Radius, center: Coordinates}; export type StationName = string; export type GeoJson = Record; export type StationsArea = {circle?: Circle | undefined, stationName?: StationName | undefined, geoJson?: GeoJson | undefined}; export type YearMonthDate = string; export type StationsTime = {start?: YearMonthDate | undefined, end?: YearMonthDate | undefined}; export type StationsModel = string; export type StationsRequest = {area: StationsArea, time: StationsTime, model: StationsModel}; export type CreateApiStationsArgs = {body: StationsRequest}; export type CreateApiStationsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const stationName = z.string(); export const longitude = z.number(); export const latitude = z.number(); export const referencesMatrixFile = z.string(); export const yearMonthDate = z.string(); export const link = z.string(); export const linkExpirationTime = z.string(); export const modelVersion = z.string(); export const gridName = z.string(); export const companyName = z.string(); export const data = z.object({time: yearMonthDate, link: link.optional(), linkExpirationTime: linkExpirationTime.optional(), modelVersion: modelVersion, gridName: gridName, companyName: companyName}); export const dataArray = z.array(data); export const station = z.object({stationName: stationName, longitude: longitude, latitude: latitude, referencesMatrixFile: referencesMatrixFile.optional(), data: dataArray.optional()}); export const stations = z.array(station); export const stationsResponse = z.object({stations: stations.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 }; /** * Retrieve station * * POST /stations */ export const createApiStations = async (args: CreateApiStationsArgs, options?: CreateApiStationsOptions) => { const {baseUrl = "https://api.sinay.ai/metocean/api/v1", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("API_KEY", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/stations${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(stationsResponse, await res.json()) };