import {z} from 'zod' export type GetApiV3OrdersOrderIdArgs = {MID: string, orderId: string}; export type GetApiV3OrdersOrderIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiV3OrdersOrderIdResponse = z.object({status: z.string().optional(), data: z.object({id: z.string().optional(), status: z.string().optional(), type: z.string().optional(), purposeCode: z.string().optional(), source: z.object({currency: z.string().optional(), amount: z.string().optional()}).optional(), destination: z.object({currency: z.string().optional(), amount: z.string().optional()}).optional(), userId: z.string().optional(), senderName: z.object({firstName: z.string().optional(), lastName: z.string().optional()}).optional(), mid: z.string().optional(), partnerContext: z.object({testfield1: z.string().optional(), testfield2: z.string().optional(), testfield3: z.number().int().optional(), testfield4: z.string().optional(), testfield7: z.string().optional()}).optional(), fees: z.object({feeMode: z.string().optional(), fixedFee: z.object({totalFixedFees: z.number().int().optional(), totalFixedTfFees: z.number().int().optional(), totalFixedCxFees: z.number().int().optional(), fixedFeesCurrency: z.string().optional(), fixedFeeDetails: z.object({baseFeeFixedRate: z.number().int().optional(), tfFeeFixedRate: z.number().int().optional(), cxFeeFixedRate: z.number().int().optional(), currency: z.string().optional()}).optional()}).optional(), networkFee: z.number().int().optional(), processingFee: z.number().optional(), rrFee: z.string().optional()}).optional(), failureCode: z.string().optional(), failureMessage: z.string().optional(), purposeCodeReason: 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 }; /** * Get Order by ID * * GET /v3/orders/{orderId} */ export const getApiV3OrdersOrderId = async (args: GetApiV3OrdersOrderIdArgs, options?: GetApiV3OrdersOrderIdOptions) => { const {baseUrl = "https://sandbox-api.transfi.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.MID != null) headers.set("MID", String(args.MID)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/orders/${encodeURIComponent(String(args.orderId))}${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(getApiV3OrdersOrderIdResponse, await res.json()) };