{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { AxiosInstance, AxiosError } from \"axios\";\nimport {\n  ChainIdToNetwork,\n  PaymentRequirements,\n  PaymentRequirementsSchema,\n  Signer,\n  MultiNetworkSigner,\n  isMultiNetworkSigner,\n  isSvmSignerWallet,\n  Network,\n  evm,\n  X402Config,\n} from \"x402/types\";\nimport {\n  createPaymentHeader,\n  PaymentRequirementsSelector,\n  selectPaymentRequirements,\n} from \"x402/client\";\n\n/**\n * Enables the payment of APIs using the x402 payment protocol.\n *\n * When a request receives a 402 response:\n * 1. Extracts payment requirements from the response\n * 2. Verifies the payment amount is within the allowed maximum\n * 3. Creates a payment header using the provided wallet client\n * 4. Retries the original request with the payment header\n * 5. Exposes the X-PAYMENT-RESPONSE header in the final response\n *\n * @param axiosClient - The Axios instance to add the interceptor to\n * @param walletClient - A wallet client that can sign transactions and create payment headers\n * @param maxValue - The maximum allowed payment amount in base units (defaults to 0.1 USDC)\n * @param paymentRequirementsSelector - A function that selects the payment requirements from the response\n * @param config - Optional configuration for X402 operations (e.g., custom RPC URLs)\n * @returns The modified Axios instance with the payment interceptor\n *\n * @example\n * ```typescript\n * const client = withPaymentInterceptor(\n *   axios.create(),\n *   signer\n * );\n *\n * // With custom RPC configuration\n * const client = withPaymentInterceptor(\n *   axios.create(),\n *   signer,\n *   undefined,\n *   undefined,\n *   { svmConfig: { rpcUrl: \"http://localhost:8899\" } }\n * );\n *\n * // The client will automatically handle 402 responses\n * const response = await client.get('https://api.example.com/premium-content');\n * ```\n *\n * @throws {Error} If the payment amount exceeds the maximum allowed value\n */\nexport function withPaymentInterceptor(\n  axiosClient: AxiosInstance,\n  walletClient: Signer | MultiNetworkSigner,\n  maxValue: bigint = BigInt(0.1 * 10 ** 6), // Default to 0.10 USDC\n  paymentRequirementsSelector: PaymentRequirementsSelector = selectPaymentRequirements,\n  config?: X402Config,\n) {\n  axiosClient.interceptors.response.use(\n    response => response,\n    async (error: AxiosError) => {\n      if (!error.response || error.response.status !== 402) {\n        return Promise.reject(error);\n      }\n\n      try {\n        const originalConfig = error.config;\n        if (!originalConfig || !originalConfig.headers) {\n          return Promise.reject(new Error(\"Missing axios request configuration\"));\n        }\n\n        if ((originalConfig as { __is402Retry?: boolean }).__is402Retry) {\n          return Promise.reject(error);\n        }\n\n        const { x402Version, accepts } = error.response.data as {\n          x402Version: number;\n          accepts: PaymentRequirements[];\n        };\n        const parsed = accepts.map(x => PaymentRequirementsSchema.parse(x));\n\n        const network = isMultiNetworkSigner(walletClient)\n          ? undefined\n          : evm.isSignerWallet(walletClient as typeof evm.EvmSigner)\n            ? ChainIdToNetwork[(walletClient as typeof evm.EvmSigner).chain?.id]\n            : isSvmSignerWallet(walletClient as Signer)\n              ? ([\"solana\", \"solana-devnet\"] as Network[])\n              : undefined;\n\n        const selectedPaymentRequirements = paymentRequirementsSelector(parsed, network, \"exact\");\n\n        if (BigInt(selectedPaymentRequirements.maxAmountRequired) > maxValue) {\n          throw new Error(\"Payment amount exceeds maximum allowed\");\n        }\n\n        const paymentHeader = await createPaymentHeader(\n          walletClient,\n          x402Version,\n          selectedPaymentRequirements,\n          config,\n        );\n\n        (originalConfig as { __is402Retry?: boolean }).__is402Retry = true;\n\n        originalConfig.headers[\"X-PAYMENT\"] = paymentHeader;\n        originalConfig.headers[\"Access-Control-Expose-Headers\"] = \"X-PAYMENT-RESPONSE\";\n\n        const secondResponse = await axiosClient.request(originalConfig);\n        return secondResponse;\n      } catch (paymentError) {\n        return Promise.reject(paymentError);\n      }\n    },\n  );\n\n  return axiosClient;\n}\n\nexport { decodeXPaymentResponse } from \"x402/shared\";\nexport { createSigner, type Signer, type MultiNetworkSigner, type X402Config } from \"x402/types\";\nexport { type PaymentRequirementsSelector } from \"x402/client\";\nexport type { Hex } from \"viem\";\n"],"mappings":";AACA;AAAA,EACE;AAAA,EAEA;AAAA,EAGA;AAAA,EACA;AAAA,EAEA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EAEA;AAAA,OACK;AA4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