import {z} from 'zod' export type GetApiMandatesMandateIdArgs = {mandate_id: string, filter?: string | undefined}; export type GetApiMandatesMandateIdOptions = {baseUrl?: string | undefined, token?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiMandatesMandateIdResponse = z.object({mandate_id: z.string().optional(), status: z.enum(["pending", "active", "rejected", "cancelled"]).optional(), mandate_type: z.literal("direct_debit").optional(), reference: z.string().optional(), bacs_reference: z.string().optional(), payer_detail: z.object({account_name: z.string().optional(), account_number_ending: z.string().min(8).max(8).optional()}).optional(), detail: z.object({scheme: z.literal("bacs").optional(), report: z.enum(["AUDDIS", "ADDACS"]).optional(), reason_code: z.enum(["1", "2", "3", "5", "6", "7", "B", "C", "F", "G", "H", "I", "K", "L", "M", "N", "O", "P", "Q", "0", "D", "E", "R"]).optional(), reason_description: z.enum(["Instruction cancelled", "Payer deceased", "Account transferred", "No account", "No instruction", "DDI amount not zero", "Account closed", "Account transferred to a different branch of the bank/building society", "Invalid account type", "Bank will not accept Direct Debits on account", "Instruction has expired", "Payer reference is not unique", "Instruction cancelled by paying bank", "Incorrect payer's account details", "Transaction code/user status incompatible", "Transaction disallowed at payer's branch", "Invalid reference", "Payer's name not present", "Service user's name blank", "Instruction cancelled-refer to payer", "Instruction cancelled by payer", "Advance notice disputed", "Instruction amended", "Instruction reinstated"]).optional(), rejected_date: z.string().optional()}).optional(), created: z.string().optional(), last_updated: z.string().optional(), links: z.array(z.object({rel: z.literal("customer").optional(), href: z.string().optional()})).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 }; /** * Retrieve a mandate * * GET /mandates/{mandate_id} */ export const getApiMandatesMandateId = async (args: GetApiMandatesMandateIdArgs, options?: GetApiMandatesMandateIdOptions) => { const {baseUrl = "https://test-api.acquired.com/v1", fetch: fetchFn = fetch, token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filter != null) query.append("filter", String(args.filter)) if (token !== undefined) headers.set('authorization', `Bearer ${token}`) const url = `${baseUrl.replace(/\/+$/, '')}/mandates/${encodeURIComponent(String(args.mandate_id))}${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(getApiMandatesMandateIdResponse, await res.json()) };