apinow-sdk 0.12.0 → 0.12.2

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 +50 -10
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -45,13 +45,52 @@ async function fetchJson(url, options) {
45
45
  else {
46
46
  console.error(`fetchJson (using node-fetch): Called with no options.`);
47
47
  }
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);
48
+ let response;
49
+ try {
50
+ // @ts-ignore options might not perfectly match node-fetch's expected type if RequestInit from lib.dom.d.ts is too different
51
+ response = await fetch(url, options);
52
+ }
53
+ catch (networkError) {
54
+ let requestBodySummary = "No body provided";
55
+ if (options?.body) {
56
+ if (typeof options.body === 'string') {
57
+ requestBodySummary = `String body (len ${options.body.length}): ${options.body.substring(0, 100)}...`;
58
+ }
59
+ else if (options.body instanceof Buffer) {
60
+ requestBodySummary = `Buffer body (len ${options.body.length})`;
61
+ }
62
+ else if (options.body instanceof Uint8Array) { // Should not happen with current txResponse
63
+ requestBodySummary = `Uint8Array body (len ${options.body.length})`;
64
+ }
65
+ else {
66
+ requestBodySummary = `Body of type ${options.body?.constructor?.name || 'unknown'}`;
67
+ }
68
+ }
69
+ const errorMessage = `Network/fetch error for URL: ${url}, Method: ${options?.method || 'GET'}, Request: ${requestBodySummary}. Original error: ${networkError instanceof Error ? networkError.message : String(networkError)}`;
70
+ console.error("fetchJson (using node-fetch): Fetch execution error - ", errorMessage);
71
+ throw new Error(errorMessage);
72
+ }
50
73
  console.error(`fetchJson (using node-fetch): Response status: ${response.status}, ok: ${response.ok}`);
51
74
  if (!response.ok) {
52
- const errorBody = await response.text();
53
- console.error(`fetchJson: Error response body: ${errorBody}`);
54
- throw new Error(`HTTP error ${response.status}: ${errorBody}`);
75
+ const errorBodyText = await response.text();
76
+ console.error(`fetchJson (using node-fetch): Error response body for ${url} (status ${response.status}): ${errorBodyText}`);
77
+ let requestBodySummary = "No body provided";
78
+ if (options?.body) {
79
+ if (typeof options.body === 'string') {
80
+ requestBodySummary = `String body (len ${options.body.length}): ${options.body.substring(0, 100)}...`;
81
+ }
82
+ else if (options.body instanceof Buffer) {
83
+ requestBodySummary = `Buffer body (len ${options.body.length})`;
84
+ }
85
+ else if (options.body instanceof Uint8Array) { // Should not happen with current txResponse
86
+ requestBodySummary = `Uint8Array body (len ${options.body.length})`;
87
+ }
88
+ else {
89
+ requestBodySummary = `Body of type ${options.body?.constructor?.name || 'unknown'}`;
90
+ }
91
+ }
92
+ const detailedErrorMessage = `HTTP error ${response.status} for URL: ${url}, Method: ${options?.method || 'GET'}, Request: ${requestBodySummary}. Response: ${errorBodyText}`;
93
+ throw new Error(detailedErrorMessage);
55
94
  }
56
95
  const responseData = await response.json();
57
96
  console.error(`fetchJson: Successfully fetched and parsed JSON (first 500 chars): ${JSON.stringify(responseData).substring(0, 500)}`);
@@ -170,7 +209,9 @@ class ApiNow {
170
209
  throw new Error('Invalid transaction hash');
171
210
  }
172
211
  const url = new URL(endpoint);
173
- console.error(`txResponse: Constructed URL for fetch: ${url.toString()}`);
212
+ // Add txHash as a query parameter
213
+ url.searchParams.append('txHash', txHash);
214
+ console.error(`txResponse: Constructed URL (with txHash query param) for fetch: ${url.toString()}`);
174
215
  // Determine method reliably
175
216
  const method = (opts.method || 'GET').toUpperCase();
176
217
  console.error(`txResponse: Preparing ${method} request to ${endpoint}`);
@@ -208,11 +249,10 @@ class ApiNow {
208
249
  if (opts.data) {
209
250
  console.error(`txResponse: Setting data as body for ${method} request (to be used with node-fetch):`, opts.data);
210
251
  const requestBodyString = JSON.stringify(opts.data);
211
- // node-fetch can handle string bodies directly. For Content-Length:
212
- const bodyBytes = new TextEncoder().encode(requestBodyString);
213
252
  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);
253
+ // Let node-fetch set the Content-Length automatically for string bodies
254
+ // const bodyBytes = new TextEncoder().encode(requestBodyString);
255
+ // (fetchOptions.headers as Record<string, string>)['Content-Length'] = String(bodyBytes.length);
216
256
  }
217
257
  }
218
258
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apinow-sdk",
3
- "version": "0.12.0",
3
+ "version": "0.12.2",
4
4
  "description": "ApiNow SDK · The endpoint vending machine",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",