import {z} from 'zod' export type GetApiSchemaContextAgencyIDResourceIDVersionArgs = {context: 'datastructure' | 'metadatastructure' | 'dataflow' | 'metadataflow' | 'provisionagreement', agencyID: string, resourceID: string, version: string, dimensionAtObservation?: string | undefined, explicitMeasure?: boolean | undefined, 'Accept-Encoding'?: ('br' | 'compress' | 'deflate' | 'exi' | 'gzip' | 'identity' | 'pack200-gzip' | 'zstd') | undefined, 'If-Modified-Since'?: string | undefined}; export type GetApiSchemaContextAgencyIDResourceIDVersionOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiSchemaContextAgencyIDResourceIDVersionResponse = 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 XML schemas * * GET /schema/{context}/{agencyID}/{resourceID}/{version} */ export const getApiSchemaContextAgencyIDResourceIDVersion = async (args: GetApiSchemaContextAgencyIDResourceIDVersionArgs, options?: GetApiSchemaContextAgencyIDResourceIDVersionOptions) => { 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.structure+json;version=1.0.0") if (args.dimensionAtObservation != null) query.append("dimensionAtObservation", String(args.dimensionAtObservation)) if (args.explicitMeasure != null) query.append("explicitMeasure", String(args.explicitMeasure)) if (args['Accept-Encoding'] != null) headers.set("Accept-Encoding", String(args['Accept-Encoding'])) if (args['If-Modified-Since'] != null) headers.set("If-Modified-Since", String(args['If-Modified-Since'])) const url = `${baseUrl.replace(/\/+$/, '')}/schema/${encodeURIComponent(String(args.context))}/${encodeURIComponent(String(args.agencyID))}/${encodeURIComponent(String(args.resourceID))}/${encodeURIComponent(String(args.version))}${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(getApiSchemaContextAgencyIDResourceIDVersionResponse, await res.json()) };