import {z} from 'zod' export type GetApiMiscPushPullNotificationBoxBoxIdNotificationsArgs = {Accept: 'application/vnd.hmrc.1.0+json', boxId: string, status?: string | undefined, fromDate?: string | undefined, toDate?: string | undefined, count?: number | undefined}; export type GetApiMiscPushPullNotificationBoxBoxIdNotificationsOptions = {baseUrl?: string | undefined, applicationRestricted?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiMiscPushPullNotificationBoxBoxIdNotificationsResponse = z.array(z.object({notificationId: z.string(), boxId: z.string(), messageContentType: z.enum(["application/json", "application/xml"]), message: z.string(), status: z.enum(["PENDING", "FAILED", "ACKNOWLEDGED"]), createdDateTime: z.string()})); 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 a list of notifications * * GET /misc/push-pull-notification/box/{boxId}/notifications */ export const getApiMiscPushPullNotificationBoxBoxIdNotifications = async (args: GetApiMiscPushPullNotificationBoxBoxIdNotificationsArgs, options?: GetApiMiscPushPullNotificationBoxBoxIdNotificationsOptions) => { const {baseUrl = "https://test-api.service.hmrc.gov.uk", fetch: fetchFn = fetch, applicationRestricted} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Accept != null) headers.set("Accept", String(args.Accept)) if (args.status != null) query.append("status", String(args.status)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.count != null) query.append("count", String(args.count)) if (applicationRestricted !== undefined) headers.set('authorization', `Bearer ${applicationRestricted}`) const url = `${baseUrl.replace(/\/+$/, '')}/misc/push-pull-notification/box/${encodeURIComponent(String(args.boxId))}/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(getApiMiscPushPullNotificationBoxBoxIdNotificationsResponse, await res.json()) };