apinow-sdk 0.20.2 → 0.21.1
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 +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +32 -9
- package/package.json +1 -1
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.
|
|
10
|
+
.version('0.21.1');
|
|
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:
|
|
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
|
|
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
|
/**
|
|
@@ -302,14 +326,13 @@ export function createClient(config) {
|
|
|
302
326
|
* });
|
|
303
327
|
*/
|
|
304
328
|
async callExternal(url, opts = {}) {
|
|
305
|
-
|
|
329
|
+
const params = new URLSearchParams({ url });
|
|
330
|
+
if (opts.method)
|
|
331
|
+
params.set('method', opts.method);
|
|
332
|
+
return this.call(`/api/x402-proxy?${params}`, {
|
|
306
333
|
method: 'POST',
|
|
307
|
-
body:
|
|
308
|
-
|
|
309
|
-
method: opts.method || 'POST',
|
|
310
|
-
body: opts.body,
|
|
311
|
-
headers: opts.headers,
|
|
312
|
-
},
|
|
334
|
+
body: opts.body,
|
|
335
|
+
headers: opts.headers,
|
|
313
336
|
});
|
|
314
337
|
},
|
|
315
338
|
/**
|
package/package.json
CHANGED