import {z} from 'zod' export type GetApiLastUpdatesArgs = {'datasets.limit'?: number | undefined, 'datasets.offset'?: number | undefined, 'providers.limit'?: number | undefined, 'providers.offset'?: number | undefined}; export type GetApiLastUpdatesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiLastUpdatesResponse = z.object({_meta: z.object({version: z.string()}), datasets: z.object({docs: z.array(z.object({code: z.string(), indexed_at: z.string().optional(), json_data_commit_ref: z.string().optional(), name: z.string().optional(), nb_series: z.number().int().gte(0).optional(), provider_code: z.string().optional(), provider_name: z.string().optional()})), limit: z.number().int(), num_found: z.number().int(), offset: z.number().int()}), providers: z.object({docs: z.array(z.object({code: z.string(), indexed_at: z.string().optional(), name: z.string().optional(), region: z.string().optional(), terms_of_use: z.string().optional(), website: z.string().optional()})), limit: z.number().int(), num_found: z.number().int(), offset: z.number().int()})}); 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 }; /** * Respond a list of providers and a list of datasets sorted by creation/conversion date, most recent first. * * GET /last-updates */ export const getApiLastUpdates = async (args: GetApiLastUpdatesArgs, options: GetApiLastUpdatesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['datasets.limit'] != null) query.append("datasets.limit", String(args['datasets.limit'])) if (args['datasets.offset'] != null) query.append("datasets.offset", String(args['datasets.offset'])) if (args['providers.limit'] != null) query.append("providers.limit", String(args['providers.limit'])) if (args['providers.offset'] != null) query.append("providers.offset", String(args['providers.offset'])) const url = `${baseUrl.replace(/\/+$/, '')}/last-updates${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(getApiLastUpdatesResponse, await res.json()) };