import {z} from 'zod' export type GetApiV0NotificationsArgs = {created_before?: string | undefined, created_after?: string | undefined, status?: ('NEW' | 'ACKNOWLEDGED') | undefined, occurred_before?: string | undefined, occurred_after?: string | undefined, notification_type?: string | undefined, subject?: string | undefined, additional_provider_filter?: string | undefined, limit?: number | undefined}; export type GetApiV0NotificationsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const notificationResponse = z.object({notification_id: z.string().optional(), created_datetime: z.string().optional(), event_datetime: z.string().optional(), payload_schema: z.string().optional(), notification_type: z.string().optional(), subject_type: z.string().optional(), subject: z.string().optional(), additional_provider_filter: z.string().optional(), status: z.enum(["NEW", "ACKNOWLEDGED"]).optional(), payload: z.record(z.string(), z.unknown()).optional()}); export const getApiV0NotificationsResponse = z.object({data: z.array(notificationResponse).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 }; /** * Receive notifications API - Get any unacknowledged notifications * * GET /v0/notifications */ export const getApiV0Notifications = async (args: GetApiV0NotificationsArgs, options?: GetApiV0NotificationsOptions) => { const {baseUrl = "https://businessgateway.landregistry.gov.uk/bg2/api", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.status != null) query.append("status", String(args.status)) if (args.occurred_before != null) query.append("occurred_before", String(args.occurred_before)) if (args.occurred_after != null) query.append("occurred_after", String(args.occurred_after)) if (args.notification_type != null) query.append("notification_type", String(args.notification_type)) if (args.subject != null) query.append("subject", String(args.subject)) if (args.additional_provider_filter != null) query.append("additional_provider_filter", String(args.additional_provider_filter)) if (args.limit != null) query.append("limit", String(args.limit)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v0/notifications${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(getApiV0NotificationsResponse, await res.json()) };