apinow-sdk 0.11.19 → 0.12.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.
Files changed (2) hide show
  1. package/dist/index.js +29 -26
  2. package/package.json +3 -2
package/dist/index.js CHANGED
@@ -1,51 +1,53 @@
1
1
  import { ethers, Wallet, parseUnits, isAddress } from 'ethers';
2
+ import fetch from 'node-fetch'; // Import node-fetch
2
3
  // Default RPC URLs
3
4
  const DEFAULT_ETH_RPC = 'https://rpc.ankr.com/eth';
4
5
  const DEFAULT_BASE_RPC = 'https://mainnet.base.org';
5
6
  // --- Helper function for fetch (Keep this) ---
6
7
  async function fetchJson(url, options) {
7
- console.error(`fetchJson: Called with URL: ${url}`);
8
+ console.error(`fetchJson (using node-fetch): Called with URL: ${url}`);
8
9
  // Safer logging for options to avoid mutating Uint8Array body
9
10
  if (options) {
10
11
  const { body, ...optionsWithoutBody } = options; // Destructure to separate body for logging
11
- console.error(`fetchJson: Called with options (metadata):`, JSON.stringify(optionsWithoutBody, null, 2));
12
+ console.error(`fetchJson (using node-fetch): Called with options (metadata):`, JSON.stringify(optionsWithoutBody, null, 2));
12
13
  if (options.headers) { // Log headers from original options
13
14
  const headers = options.headers;
14
15
  const contentLengthHeader = headers['Content-Length'] || headers['content-length'];
15
- console.error(`fetchJson: Content-Length in options.headers before fetch: ${contentLengthHeader}`);
16
+ console.error(`fetchJson (using node-fetch): Content-Length in options.headers before fetch: ${contentLengthHeader}`);
16
17
  }
17
18
  if (body) {
18
- if (body instanceof Uint8Array) {
19
- // Log info about Uint8Array without converting it with String() or full JSON.stringify
20
- const firstNBytes = body.slice(0, Math.min(body.length, 100)); // Get first 100 bytes or less
21
- let snippet = "";
22
- try {
23
- snippet = new TextDecoder("utf-8", { fatal: false }).decode(firstNBytes);
24
- }
25
- catch (e) {
26
- snippet = "(binary data, could not decode as UTF-8)";
27
- }
28
- console.error(`fetchJson: Body in options is Uint8Array, length: ${body.length}. Snippet (first ~100 bytes): ${snippet}`);
19
+ // For node-fetch, string or Buffer bodies are common.
20
+ // If we are passing a string, its length in bytes is what Content-Length should be.
21
+ // If it's a Buffer, Buffer.length.
22
+ if (typeof body === 'string') {
23
+ console.error(`fetchJson (using node-fetch): Body in options is string, length: ${body.length}. Snippet (first ~100 chars): ${body.substring(0, 100)}`);
24
+ }
25
+ else if (body instanceof Buffer) {
26
+ console.error(`fetchJson (using node-fetch): Body in options is Buffer, length: ${body.length}. Snippet (first ~100 bytes as hex): ${body.slice(0, 100).toString('hex')}`);
27
+ }
28
+ else if (body instanceof Uint8Array) {
29
+ // This case should ideally not be hit if txResponse prepares a string or Buffer
30
+ console.error(`fetchJson (using node-fetch): Body in options is Uint8Array, length: ${body.length}.`);
29
31
  }
30
32
  else {
31
- // For string or other stringifiable bodies
32
33
  try {
33
- console.error(`fetchJson: Body in options before fetch (first 500 chars): ${String(body).substring(0, 500)}`);
34
+ console.error(`fetchJson (using node-fetch): Body in options before fetch (first 500 chars): ${String(body).substring(0, 500)}`);
34
35
  }
35
36
  catch (e) {
36
- console.error(`fetchJson: Body in options before fetch: (Could not be easily stringified for logging)`);
37
+ console.error(`fetchJson (using node-fetch): Body in options before fetch: (Could not be easily stringified for logging)`);
37
38
  }
38
39
  }
39
40
  }
40
41
  else {
41
- console.error(`fetchJson: No body in options.`);
42
+ console.error(`fetchJson (using node-fetch): No body in options.`);
42
43
  }
43
44
  }
44
45
  else {
45
- console.error(`fetchJson: Called with no options.`);
46
+ console.error(`fetchJson (using node-fetch): Called with no options.`);
46
47
  }
47
- const response = await fetch(url, options); // IMPORTANT: Pass original options to fetch
48
- console.error(`fetchJson: Response status: ${response.status}, ok: ${response.ok}`);
48
+ // @ts-ignore options might not perfectly match node-fetch's expected type if RequestInit from lib.dom.d.ts is too different
49
+ const response = await fetch(url, options);
50
+ console.error(`fetchJson (using node-fetch): Response status: ${response.status}, ok: ${response.ok}`);
49
51
  if (!response.ok) {
50
52
  const errorBody = await response.text();
51
53
  console.error(`fetchJson: Error response body: ${errorBody}`);
@@ -204,12 +206,13 @@ class ApiNow {
204
206
  else {
205
207
  // --- POST/PUT/etc.: Set data as body ---
206
208
  if (opts.data) {
207
- console.error(`txResponse: Setting data as body for ${method} request:`, opts.data);
209
+ console.error(`txResponse: Setting data as body for ${method} request (to be used with node-fetch):`, opts.data);
208
210
  const requestBodyString = JSON.stringify(opts.data);
209
- const bodyBytes = new TextEncoder().encode(requestBodyString); // Convert to Uint8Array
210
- fetchOptions.body = bodyBytes; // Use Uint8Array as the body
211
- // Let fetch/undici set the Content-Length for Uint8Array
212
- // (fetchOptions.headers as Record<string, string>)['Content-Length'] = String(bodyBytes.length);
211
+ // node-fetch can handle string bodies directly. For Content-Length:
212
+ const bodyBytes = new TextEncoder().encode(requestBodyString);
213
+ fetchOptions.body = requestBodyString; // Use the string as the body for node-fetch
214
+ // Explicitly set Content-Length from the byte length of the string
215
+ fetchOptions.headers['Content-Length'] = String(bodyBytes.length);
213
216
  }
214
217
  }
215
218
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apinow-sdk",
3
- "version": "0.11.19",
3
+ "version": "0.12.0",
4
4
  "description": "ApiNow SDK · The endpoint vending machine",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -22,7 +22,8 @@
22
22
  "license": "MIT",
23
23
  "dependencies": {
24
24
  "apinow-sdk": "^0.11.19",
25
- "ethers": "^6.13.5"
25
+ "ethers": "^6.13.5",
26
+ "node-fetch": "^3.3.2"
26
27
  },
27
28
  "devDependencies": {
28
29
  "@types/jest": "^29.5.12",