import {z} from 'zod' export type GetApiTestFraudPreventionHeadersApiValidationFeedbackArgs = {Accept: string, Authorization: string, api: 'business-details-mtd' | 'business-income-source-summary-mtd' | 'business-source-adjustable-summary-mtd' | 'cis-deductions-mtd' | 'individual-calculations-mtd' | 'individual-losses-mtd' | 'individuals-business-end-of-period-statement-mtd' | 'individuals-charges-mtd' | 'individuals-disclosures-mtd' | 'individuals-expenses-mtd' | 'individuals-income-received-mtd' | 'individuals-reliefs-mtd' | 'individuals-state-benefits-mtd' | 'obligations-mtd' | 'other-deductions-mtd' | 'property-business-mtd' | 'self-assessment-mtd' | 'self-assessment-accounts-mtd' | 'self-assessment-assist-mtd' | 'self-employment-business-mtd' | 'vat-mtd' | 'individuals-capital-gains-income-mtd' | 'individuals-dividends-income-mtd' | 'individuals-employments-income-mtd' | 'individuals-foreign-income-mtd' | 'individuals-insurance-policies-income-mtd' | 'individuals-other-income-mtd' | 'individuals-pensions-income-mtd' | 'individuals-savings-income-mtd' | 'self-assessment-individual-details-mtd', connectionMethod?: ('BATCH_PROCESS_DIRECT' | 'DESKTOP_APP_DIRECT' | 'DESKTOP_APP_VIA_SERVER' | 'MOBILE_APP_DIRECT' | 'MOBILE_APP_VIA_SERVER' | 'OTHER_DIRECT' | 'OTHER_VIA_SERVER' | 'WEB_APP_VIA_SERVER') | undefined}; export type GetApiTestFraudPreventionHeadersApiValidationFeedbackOptions = {baseUrl?: string | undefined, applicationRestricted?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiTestFraudPreventionHeadersApiValidationFeedbackResponse = z.object({requests: z.array(z.object({path: z.string(), method: z.string(), requestTimestamp: z.string(), code: z.union([z.literal("VALID_HEADERS"), z.literal("INVALID_HEADERS"), z.literal("POTENTIALLY_INVALID_HEADERS"), z.literal("NO_HEADERS")]), headers: z.array(z.object({header: z.string(), value: z.string().optional(), code: z.union([z.literal("VALID_HEADER"), z.literal("INVALID_HEADER"), z.literal("POTENTIALLY_INVALID_HEADER"), z.literal("MISSING_HEADER"), z.literal("UNEXPECTED_HEADER"), z.literal("TEST_SCENARIO_HEADER")]), errors: z.array(z.string()), warnings: z.array(z.string())})), crossValidation: z.array(z.object({headers: z.array(z.string()), code: z.union([z.literal("VALID_HEADERS"), z.literal("INVALID_HEADERS")]), errors: z.array(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 feedback on requests made to an API * * GET /test/fraud-prevention-headers/{api}/validation-feedback */ export const getApiTestFraudPreventionHeadersApiValidationFeedback = async (args: GetApiTestFraudPreventionHeadersApiValidationFeedbackArgs, options?: GetApiTestFraudPreventionHeadersApiValidationFeedbackOptions) => { 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.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args.connectionMethod != null) query.append("connectionMethod", String(args.connectionMethod)) if (applicationRestricted !== undefined) headers.set('authorization', `Bearer ${applicationRestricted}`) const url = `${baseUrl.replace(/\/+$/, '')}/test/fraud-prevention-headers/${encodeURIComponent(String(args.api))}/validation-feedback${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(getApiTestFraudPreventionHeadersApiValidationFeedbackResponse, await res.json()) };