import {z} from 'zod' export type GetApiApiV1CompareArgs = {cities: string, resource: 'weather' | 'air' | 'indicators' | 'demographics' | 'unemployment' | 'tourism' | 'charging-status' | 'weather-warnings', page?: number | undefined, limit?: number | undefined, offset?: number | undefined, sort?: ('city' | 'source_status') | undefined, order?: ('asc' | 'desc') | undefined, 'If-None-Match'?: string | undefined}; export type GetApiApiV1CompareOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const meta = z.object({correlation_id: z.string().nullable().optional(), source_status: z.enum(["ok", "no_data", "disabled", "not_ingested", "not_covered"]).optional(), cache_status: z.string().nullable().optional(), covered_cities: z.array(z.string()).optional(), pagination: z.object({total: z.number().int().optional(), returned: z.number().int().optional(), limit: z.number().int().nullable().optional(), offset: z.number().int().optional(), truncated: z.boolean().optional()}).optional()}); export const getApiApiV1CompareResponse = z.object({data: z.array(z.object({city: z.string(), data: z.object({}).nullable().optional(), source_status: z.enum(["ok", "disabled", "no_data", "error", "not_found"])})), meta: meta}); 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 }; /** * Eine Ressource über mehrere Städte vergleichen * * GET /api/v1/compare */ export const getApiApiV1Compare = async (args: GetApiApiV1CompareArgs, options?: GetApiApiV1CompareOptions) => { const {baseUrl = "https://infranode.dev", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cities != null) query.append("cities", String(args.cities)) if (args.resource != null) query.append("resource", String(args.resource)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.order != null) query.append("order", String(args.order)) if (args['If-None-Match'] != null) headers.set("If-None-Match", String(args['If-None-Match'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/compare${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(getApiApiV1CompareResponse, await res.json()) };