import {z} from 'zod' export type GetApiRecordsV1CollectionReferenceArgs = {reference: string, source?: ('TNA' | 'A2A' | 'PA' | 'All') | undefined, direction?: ('NEXT' | 'PREV') | undefined, includeCursor?: boolean | undefined, batchStartRecordId?: string | undefined, batchStartMark?: string | undefined, limit?: number | undefined}; export type GetApiRecordsV1CollectionReferenceOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const scopeContentViewModel = z.object({PlaceNames: z.array(z.string()).optional(), Description: z.string().optional(), Ephemera: z.string().optional(), Schema: z.string().optional()}); export const xReferenceViewModel = z.object({XReferenceId: z.string().optional(), XReferenceCode: z.string().optional(), XReferenceName: z.string().optional(), XReferenceType: z.string().optional(), XReferenceURL: z.string().optional(), XReferenceDescription: z.string().optional(), XReferenceSortWord: z.string().optional()}); export const informationAssetIdentityViewModel = z.object({AccessConditions: z.string().optional(), CatalogueId: z.number().int().optional(), CitableReference: z.string().optional(), ClosureCode: z.string().optional(), ClosureStatus: z.string().optional(), ClosureType: z.string().optional(), CoveringDates: z.string().optional(), CoveringFromDate: z.number().int().optional(), CoveringToDate: z.number().int().optional(), ScopeContent: scopeContentViewModel.optional(), Digitised: z.boolean().optional(), HeldBy: z.array(xReferenceViewModel).optional(), Id: z.string().optional(), IsParent: z.boolean().optional(), CatalogueLevel: z.number().int().optional(), ParentId: z.string().optional(), RecordOpeningDate: z.string().optional(), ReferencePart: z.string().optional(), ReferenceParentId: z.string().optional(), SortKey: z.string().optional(), Source: z.string().optional(), Title: z.string().optional()}); export const assetCollectionViewModelInformationAssetIdentityViewModel = z.object({Assets: z.array(informationAssetIdentityViewModel).optional(), HasMoreAfterLast: z.boolean().optional(), HasMoreBeforeFirst: z.boolean().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 }; /** * Get collection of records with the same citable reference * * GET /records/v1/collection/{reference} */ export const getApiRecordsV1CollectionReference = async (args: GetApiRecordsV1CollectionReferenceArgs, options?: GetApiRecordsV1CollectionReferenceOptions) => { const {baseUrl = "https://discovery.nationalarchives.gov.uk/API", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.source != null) query.append("source", String(args.source)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.includeCursor != null) query.append("includeCursor", String(args.includeCursor)) if (args.batchStartRecordId != null) query.append("batchStartRecordId", String(args.batchStartRecordId)) if (args.batchStartMark != null) query.append("batchStartMark", String(args.batchStartMark)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/records/v1/collection/${encodeURIComponent(String(args.reference))}${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(assetCollectionViewModelInformationAssetIdentityViewModel, await res.json()) };