import {z} from 'zod' export type GetApiChangesSinceDateArgs = {date: string}; export type GetApiChangesSinceDateOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const premisAgent = z.object({agentType: z.string(), agentName: z.string(), agentRole: z.string().optional()}); export const premisObject = z.object({objectIdentifier: z.string(), objectCategory: z.enum(["software", "hardware"]), objectDescription: z.string()}); export const premisEvent = z.object({eventType: z.string(), eventDateTime: z.string(), eventDetail: z.string(), eventOutcomeInformation: z.string().optional(), linkedAgent: premisAgent.optional(), linkedObject: premisObject.optional()}); export const changeLog = z.object({versionNr: z.string(), typesOfChanges: z.array(z.enum(["Unavailability", "Interface change", "Functionality change", "Behavioural change"])), description: z.string(), systemChanges: z.object({softwareChanges: z.array(premisEvent).optional(), hardwareChanges: z.array(premisEvent).optional()}).optional()}); export const getApiChangesSinceDateResponse = z.array(changeLog); 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 }; /** * List of changes to the web service * * GET /changesSince/{date} */ export const getApiChangesSinceDate = async (args: GetApiChangesSinceDateArgs, options: GetApiChangesSinceDateOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/changesSince/${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(getApiChangesSinceDateResponse, await res.json()) };