import {z} from 'zod' export type GetApiChangeLogsDateArgs = {date: string, search?: string | undefined, title?: string | undefined, type?: string | undefined, page?: number | undefined, Accept: 'application/json'}; export type GetApiChangeLogsDateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const changeLogs = z.object({data: z.array(z.object({body: z.string(), date: z.string(), details: z.string(), display_banner: z.boolean(), heading: z.string(), high_priority: z.boolean(), id: z.string(), applicable_to: z.array(z.enum(["UK", "England", "England regions", "England UTLAs", "England LTLAs", "England MSOAs", "Scotland", "Scotland local authorities", "Northern Ireland", "Northern Ireland local authorities", "Wales local authorities", "All NHS regions", "All NHS trusts", "Unspecified"])), metrics: z.array(z.object({metric: z.string(), metric_name: z.string()})), page: z.object({title: z.string().optional(), uri: z.string().optional()}), type: z.string()})).optional(), length: z.number().int().optional(), page: z.number().int().optional(), total_length: z.number().int().optional(), total_pages: z.number().int().optional()}); 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 }; /** * Change logs: dated * * GET /change_logs/{date} */ export const getApiChangeLogsDate = async (args: GetApiChangeLogsDateArgs, options?: GetApiChangeLogsDateOptions) => { const {baseUrl = "https://api.coronavirus.data.gov.uk/generic", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.title != null) query.append("title", String(args.title)) if (args.type != null) query.append("type", String(args.type)) if (args.page != null) query.append("page", String(args.page)) if (args.Accept != null) headers.set("Accept", String(args.Accept)) const url = `${baseUrl.replace(/\/+$/, '')}/change_logs/${encodeURIComponent(String(args.date))}${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(changeLogs, await res.json()) };