import {z} from 'zod' export type ShipsFind = {id?: Record | undefined, name?: string | undefined, model?: string | undefined, type?: string | undefined, role?: string | undefined, active?: boolean | undefined, imo?: number | undefined, mmsi?: number | undefined, abs?: number | undefined, class?: number | undefined, weight_lbs?: number | undefined, weight_kg?: number | undefined, year_built?: number | undefined, home_port?: string | undefined, status?: string | undefined, speed_kn?: number | undefined, course_deg?: number | undefined, latitude?: number | undefined, longitude?: number | undefined, successful_landings?: number | undefined, attempted_landings?: number | undefined, mission?: string | undefined}; export type GetApiRestShipsArgs = {find?: ShipsFind | undefined, limit?: number | undefined, offset?: number | undefined, order?: string | undefined, sort?: string | undefined}; export type GetApiRestShipsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const shipMission = z.object({flight: z.string().optional(), name: z.string().optional()}); export const shipLocation = z.object({latitude: z.number().optional(), longitude: z.number().optional()}); export const ship = z.object({abs: z.number().int().optional(), active: z.boolean().optional(), attempted_landings: z.number().int().optional(), class: z.number().int().optional(), course_deg: z.number().int().optional(), home_port: z.string().optional(), id: z.object({}).optional(), image: z.string().optional(), imo: z.number().int().optional(), missions: z.array(shipMission).optional(), mmsi: z.number().int().optional(), model: z.string().optional(), name: z.string().optional(), position: shipLocation.optional(), roles: z.array(z.string()).optional(), speed_kn: z.number().optional(), status: z.string().optional(), successful_landings: z.number().int().optional(), type: z.string().optional(), url: z.string().optional(), weight_kg: z.number().int().optional(), weight_lbs: z.number().int().optional(), year_built: z.number().int().optional()}); export const getApiRestShipsResponse = z.array(ship); 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 }; /** * GET /rest/ships */ export const getApiRestShips = async (args: GetApiRestShipsArgs, options: GetApiRestShipsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.find != null) query.append("find", JSON.stringify(args.find)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.order != null) query.append("order", String(args.order)) if (args.sort != null) query.append("sort", String(args.sort)) const url = `${baseUrl.replace(/\/+$/, '')}/rest/ships${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(getApiRestShipsResponse, await res.json()) };