import {z} from 'zod' export type GetApiV2ServicesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currency = z.object({code: z.string().optional(), description: z.string().optional()}); export const feeRuleFixedAmount = z.object({feeRuleId: z.number().int().optional(), fixedAmount: z.number().int().optional()}); export const feeRuleMaxMin = z.object({feeRuleId: z.number().int().optional(), minimumAmount: z.number().int().optional(), maximumAmount: z.number().int().optional(), fixedPercentage4d: z.number().int().optional()}); export const feeRule = z.union([feeRuleFixedAmount, feeRuleMaxMin]); export const businessService = z.object({service: z.enum(["PAYMENT", "WITHDRAWAL", "LODGEMENT", "ADD_ACCOUNT", "INTERNAL_TRANSFER", "PAYMENT_REQUEST", "FX_INT_TRANS_FROM", "FX_INT_TRANS_TO", "CARD_TOP_UP", "CREATE_CARD", "NB2P_TO", "NP2B_FROM", "NP2P_TO", "NP2P_FROM", "CARAPAY_TRANSFER_FROM", "CARAPAY_TRANSFER_TO", "CARD_POS_CONTACT_DEBIT", "CARD_POS_CONTACT_CREDIT", "CARD_POS_CONTACTLESS_DEBIT", "CARD_POS_CONTACTLESS_CREDIT", "CARD_ECOMMERCE_DEBIT", "CARD_ECOMMERCE_CREDIT", "CARD_ATM_DEBIT", "CARD_ATM_CREDIT", "CARD_INTERNATIONAL_POS_CONTACT_DEBIT", "CARD_INTERNATIONAL_POS_CONTACT_CREDIT", "CARD_INTERNATIONAL_POS_CONTACTLESS_DEBIT", "CARD_INTERNATIONAL_POS_CONTACTLESS_CREDIT", "CARD_INTERNATIONAL_ECOMMERCE_DEBIT", "CARD_INTERNATIONAL_ECOMMERCE_CREDIT", "CARD_INTERNATIONAL_ATM_DEBIT", "CARD_INTERNATIONAL_ATM_CREDIT", "CARD_POS_CONTACT_DEBIT_REVERSAL", "CARD_POS_CONTACT_CREDIT_REVERSAL", "CARD_POS_CONTACTLESS_DEBIT_REVERSAL", "CARD_POS_CONTACTLESS_CREDIT_REVERSAL", "CARD_ECOMMERCE_DEBIT_REVERSAL", "CARD_ECOMMERCE_CREDIT_REVERSAL", "CARD_ATM_DEBIT_REVERSAL", "CARD_ATM_CREDIT_REVERSAL", "CARD_INTERNATIONAL_POS_CONTACT_DEBIT_REVERSAL", "CARD_INTERNATIONAL_POS_CONTACT_CREDIT_REVERSAL", "CARD_INTERNATIONAL_POS_CONTACTLESS_DEBIT_REVERSAL", "CARD_INTERNATIONAL_POS_CONTACTLESS_CREDIT_REVERSAL", "CARD_INTERNATIONAL_ECOMMERCE_DEBIT_REVERSAL", "CARD_INTERNATIONAL_ECOMMERCE_CREDIT_REVERSAL", "CARD_INTERNATIONAL_ATM_DEBIT_REVERSAL", "CARD_INTERNATIONAL_ATM_CREDIT_REVERSAL", "INSTANT_LODGEMENT", "INSTANT_WITHDRAWAL", "INSTANT_WITHDRAWAL_REVERSAL", "INSTANT_WITHDRAWAL_RETURNED", "INSTANT_LODGEMENT_REVERSAL", "INSTANT_LODGEMENT_RETURNED"]).optional(), currency: currency.optional(), feeRule: feeRule.optional()}); export const getApiV2ServicesResponse = z.array(businessService); 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 service Fees and info * * GET /v2/services */ export const getApiV2Services = async (options?: GetApiV2ServicesOptions) => { const {baseUrl = "https://api.fire.com/business", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/services${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(getApiV2ServicesResponse, await res.json()) };