apinow-sdk 0.20.1 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -7,7 +7,7 @@ const program = new Command();
7
7
  program
8
8
  .name('apinow')
9
9
  .description('CLI for APINow.fun — search, inspect, and call pay-per-request APIs')
10
- .version('0.20.0');
10
+ .version('0.21.0');
11
11
  // ─── Helpers ───
12
12
  function getPrivateKey(opts) {
13
13
  const raw = opts.key || process.env.APINOW_WALLET_PKEY || process.env.PRIVATE_KEY;
package/dist/index.d.ts CHANGED
@@ -21,6 +21,7 @@ export interface PriceDiscovery {
21
21
  export interface ApinowConfig {
22
22
  privateKey: `0x${string}`;
23
23
  baseUrl?: string;
24
+ fetch?: typeof globalThis.fetch;
24
25
  }
25
26
  export declare function createClient(config: ApinowConfig): {
26
27
  wallet: `0x${string}`;
@@ -194,6 +195,6 @@ export declare function createClient(config: ApinowConfig): {
194
195
  /**
195
196
  * The underlying x402-wrapped fetch, for advanced use.
196
197
  */
197
- fetch: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
198
+ fetch: typeof fetch;
198
199
  };
199
200
  export default createClient;
package/dist/index.js CHANGED
@@ -3,12 +3,36 @@ import { registerExactEvmScheme } from '@x402/evm/exact/client';
3
3
  import { privateKeyToAccount } from 'viem/accounts';
4
4
  const APINOW_BASE = 'https://apinow.fun';
5
5
  // ─── SDK ───
6
+ // Prevent undici (Node 20+ / Vercel) from crashing when @x402/fetch retries
7
+ // a POST with a cloned Request body stream and the server returns a 3xx redirect.
8
+ function makeSafeFetch(baseFetch) {
9
+ const safeFetch = ((input, init) => {
10
+ if (input instanceof Request) {
11
+ return baseFetch(new Request(input, { redirect: 'manual' }), init);
12
+ }
13
+ return baseFetch(input, { ...init, redirect: 'manual' });
14
+ });
15
+ return safeFetch;
16
+ }
17
+ async function followRedirects(res) {
18
+ if (res.status >= 300 && res.status < 400) {
19
+ const location = res.headers.get('location');
20
+ if (location)
21
+ return fetch(location, { redirect: 'manual' });
22
+ }
23
+ return res;
24
+ }
6
25
  export function createClient(config) {
7
- const { privateKey, baseUrl = APINOW_BASE } = config;
26
+ const { privateKey, baseUrl = APINOW_BASE, fetch: customFetch } = config;
8
27
  const account = privateKeyToAccount(privateKey);
9
28
  const client = new x402Client();
10
29
  registerExactEvmScheme(client, { signer: account });
11
- const fetchWithPayment = wrapFetchWithPayment(fetch, client);
30
+ const safeFetch = makeSafeFetch(customFetch ?? fetch);
31
+ const rawFetchWithPayment = wrapFetchWithPayment(safeFetch, client);
32
+ const fetchWithPayment = (async (input, init) => {
33
+ const res = await rawFetchWithPayment(input, init);
34
+ return followRedirects(res);
35
+ });
12
36
  return {
13
37
  wallet: account.address,
14
38
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apinow-sdk",
3
- "version": "0.20.1",
3
+ "version": "0.21.0",
4
4
  "description": "Pay-per-call API SDK & CLI for APINow.fun — endpoints + workflows, wraps x402 so you don't have to",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",