import {z} from 'zod' export type FeeType = 'MARKUP_FEE' | 'ATM_FEE' | 'CASH_FEE' | 'UNKNOWN'; export type Fee = {type: FeeType, settlementAmount: string, settlementCurrency: string}; export type Currency = string; export type Money = {amount?: number | undefined, currency?: Currency | undefined}; export type AuthorisationRequestV1 = {metadata: {systemTimeStamp: string, traceId?: string | undefined, transactionId?: string | undefined, linkId?: string | undefined, cardId?: string | undefined, customerId?: string | undefined, institutionId?: string | undefined, fees?: Array | undefined, available?: Money | undefined, authLengthInMinutes?: string | undefined, retry?: boolean | undefined, partialApprovalCapableMerchant?: boolean | undefined, incremental?: boolean | undefined}, authorisation: {MTI: string, DE03: string, DE04: string, DE05?: string | undefined, DE06?: string | undefined, DE07: string, DE10?: string | undefined, DE11: string, DE12: string, DE13: string, DE14?: string | undefined, DE15?: string | undefined, DE18: string, DE19?: string | undefined, DE20?: string | undefined, DE22: string, DE23?: string | undefined, DE25?: string | undefined, DE26?: string | undefined, DE28?: string | undefined, DE32: string, DE33?: string | undefined, DE37: string, DE38?: string | undefined, DE39?: string | undefined, DE41?: string | undefined, DE42?: string | undefined, DE43?: string | undefined, DE46?: string | undefined, DE47?: string | undefined, DE48?: string | undefined, DE49: string, DE50?: string | undefined, DE51?: string | undefined, DE90?: string | undefined, DE95?: string | undefined}}; export type CreateApiAuthArgs = {body: AuthorisationRequestV1}; export type CreateApiAuthOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const authorisationResponseV1 = z.object({authorisationResponse: z.object({DE39: z.string(), DE05: z.string().optional(), DE04: z.string().optional(), DE06: z.string().optional(), DE54: z.string().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 }; /** * Request which is being send to customers decision making systems * * POST /auth */ export const createApiAuth = async (args: CreateApiAuthArgs, options: CreateApiAuthOptions) => { 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") const url = `${baseUrl.replace(/\/+$/, '')}/auth${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(authorisationResponseV1, await res.json()) };