import {z} from 'zod' export type GetApiDataFlowKeyProviderArgs = {flow: string, key: string, provider: string, startPeriod?: string | undefined, endPeriod?: string | undefined, updatedAfter?: string | undefined, firstNObservations?: number | undefined, lastNObservations?: number | undefined, dimensionAtObservation?: string | undefined, detail?: ('full' | 'dataonly' | 'serieskeysonly' | 'nodata') | undefined, includeHistory?: boolean | undefined, 'Accept-Encoding'?: ('br' | 'compress' | 'deflate' | 'exi' | 'gzip' | 'identity' | 'pack200-gzip' | 'zstd') | undefined, 'Accept-Language'?: string | undefined, 'If-Modified-Since'?: string | undefined}; export type GetApiDataFlowKeyProviderOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiDataFlowKeyProviderResponse = z.string(); 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 data * * GET /data/{flow}/{key}/{provider} */ export const getApiDataFlowKeyProvider = async (args: GetApiDataFlowKeyProviderArgs, options?: GetApiDataFlowKeyProviderOptions) => { const {baseUrl = "https://sdw-wsrest.ecb.europa.eu/service", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.sdmx.data+json;version=1.0.0") if (args.startPeriod != null) query.append("startPeriod", String(args.startPeriod)) if (args.endPeriod != null) query.append("endPeriod", String(args.endPeriod)) if (args.updatedAfter != null) query.append("updatedAfter", String(args.updatedAfter)) if (args.firstNObservations != null) query.append("firstNObservations", String(args.firstNObservations)) if (args.lastNObservations != null) query.append("lastNObservations", String(args.lastNObservations)) if (args.dimensionAtObservation != null) query.append("dimensionAtObservation", String(args.dimensionAtObservation)) if (args.detail != null) query.append("detail", String(args.detail)) if (args.includeHistory != null) query.append("includeHistory", String(args.includeHistory)) if (args['Accept-Encoding'] != null) headers.set("Accept-Encoding", String(args['Accept-Encoding'])) if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (args['If-Modified-Since'] != null) headers.set("If-Modified-Since", String(args['If-Modified-Since'])) const url = `${baseUrl.replace(/\/+$/, '')}/data/${encodeURIComponent(String(args.flow))}/${encodeURIComponent(String(args.key))}/${encodeURIComponent(String(args.provider))}${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(getApiDataFlowKeyProviderResponse, await res.json()) };