import {z} from 'zod' export type GetApiRoutingDefaultOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const routableConnectors = z.enum(["absa_sanlam", "authipay", "adyenplatform", "stripe_billing_test", "phonypay", "fauxpay", "pretendpay", "stripe_test", "adyen_test", "checkout_test", "paypal_test", "aci", "adyen", "affirm", "airwallex", "amazonpay", "archipel", "authorizedotnet", "bankofamerica", "barclaycard", "billwerk", "bitpay", "bambora", "blackhawknetwork", "bamboraapac", "bluesnap", "calida", "boku", "braintree", "breadpay", "cashtocode", "celero", "chargebee", "custombilling", "checkbook", "checkout", "coinbase", "coingate", "cryptopay", "cybersource", "cybersourcedecisionmanager", "datatrans", "deutschebank", "digitalvirgo", "dlocal", "dwolla", "ebanx", "elavon", "envoy", "facilitapay", "finix", "fiserv", "fiservemea", "fiservcommercehub", "fiuu", "flexiti", "forte", "getnet", "gigadat", "givepayments", "globalpay", "globepay", "gocardless", "hipay", "helcim", "hyperpg", "iatapay", "imerchantsolutions", "inespay", "interpayments", "itaubank", "jpmorgan", "klarna", "loonio", "mifinity", "mollie", "moneris", "multisafepay", "nexinets", "nexixpay", "nmi", "nomupay", "noon", "nordea", "novalnet", "nuvei", "opennode", "paybox", "payme", "payload", "payone", "paypal", "paysafe", "paystack", "paytm", "payconex", "payu", "peachpayments", "payjustnow", "payjustnowinstore", "phonepe", "placetopay", "powertranz", "prophetpay", "rapyd", "razorpay", "recurly", "redsys", "revolv3", "riskified", "santander", "shift4", "signifyd", "silverflow", "square", "stax", "stripe", "stripebilling", "tesouro", "truelayer", "trustly", "trustpay", "trustpayments", "tokenio", "tsys", "tsys_transit", "volt", "wellsfargo", "wise", "worldline", "worldpay", "worldpaymodular", "worldpayvantiv", "worldpayxml", "xendit", "zen", "zift", "plaid", "zsl", "juspaythreedsserver", "ctp_mastercard", "ctp_visa", "netcetera", "cardinal", "threedsecureio"]); export const routableConnectorChoice = z.object({connector: routableConnectors, merchant_connector_id: z.string().nullable().optional()}); export const getApiRoutingDefaultResponse = z.array(routableConnectorChoice); 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 }; /** * Routing - Retrieve Default Config * * GET /routing/default * * Also accepts auth: jwt_key */ export const getApiRoutingDefault = async (options: GetApiRoutingDefaultOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/routing/default${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(getApiRoutingDefaultResponse, await res.json()) };