import {z} from 'zod' export type FiatFeeRequest = {value: number, currency_code: string, role_type: 'SENDING' | 'RECEIVING' | 'INSTITUTION_SENDING' | 'INSTITUTION_RECEIVING', payment_type: 'REGULAR' | 'RETURN', node_type: 'INITIAL' | 'INTERMEDIATE' | 'TERMINAL', fee_type: 'PERCENTAGE' | 'FLAT_RATE', partner_name?: string | undefined, account_name?: string | undefined, upper_limit?: number | undefined, lower_limit?: number | undefined, payout_method?: string | undefined, fee_category?: ('NONE' | 'OVERSEAS_DELIVERY_CHARGES' | 'BENEFICIARY_BANK_CHARGE' | 'PROCESSING_HANDLING_CHARGE' | 'LOCAL_GOVERNMENT_CHARGE' | 'OTHER') | undefined, fee_description?: string | undefined}; export type CreateApiConfigFeesArgs = {body: FiatFeeRequest}; export type CreateApiConfigFeesOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const fiatFee = z.object({value: z.number(), currency_code: z.string(), role_type: z.enum(["SENDING", "RECEIVING", "INSTITUTION_SENDING", "INSTITUTION_RECEIVING"]), payment_type: z.enum(["REGULAR", "RETURN"]), node_type: z.enum(["INITIAL", "INTERMEDIATE", "TERMINAL"]), fee_type: z.enum(["PERCENTAGE", "FLAT_RATE"]), partner_name: z.string().optional(), account_name: z.string().optional(), upper_limit: z.number().optional(), lower_limit: z.number().optional(), feeId: z.string(), payout_method: z.string().optional(), fee_category: z.enum(["NONE", "OVERSEAS_DELIVERY_CHARGES", "BENEFICIARY_BANK_CHARGE", "PROCESSING_HANDLING_CHARGE", "LOCAL_GOVERNMENT_CHARGE", "OTHER"]).optional(), fee_description: 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 }; /** * Create fee * * POST /config/fees */ export const createApiConfigFees = async (args: CreateApiConfigFeesArgs, options: CreateApiConfigFeesOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/config/fees${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(fiatFee, await res.json()) };