import {z} from 'zod' export type DocId = string; export type Currency = string; export type Money = {amount?: number | undefined, currency?: Currency | undefined}; export type ReversalRequest = {id: DocId, partialAmount?: Money | undefined}; export type CreateApiV1AuthorizationsReversalArgs = {auditUser: string, body: ReversalRequest}; export type CreateApiV1AuthorizationsReversalOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const mti = z.string().max(4); export const docId = z.string().max(18); export const authResponseCode = z.string().max(2); export const retrievalReferenceNumber = z.string().max(12); export const authResponse = z.object({metadata: z.object({mti: mti, id: docId}), transactionData: z.object({authResponseCode: authResponseCode, retrievalReferenceNumber: retrievalReferenceNumber})}); 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 }; /** * Send test authorization reversal request * * POST /v1/authorizations/reversal */ export const createApiV1AuthorizationsReversal = async (args: CreateApiV1AuthorizationsReversalArgs, options: CreateApiV1AuthorizationsReversalOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.auditUser != null) query.append("auditUser", String(args.auditUser)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/authorizations/reversal${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(authResponse, await res.json()) };