apinow-sdk 0.11.2 → 0.11.3
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/index.js +46 -7
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -122,15 +122,48 @@ class ApiNow {
|
|
|
122
122
|
}
|
|
123
123
|
const url = new URL(endpoint);
|
|
124
124
|
url.searchParams.append('txHash', txHash);
|
|
125
|
+
// Determine method reliably
|
|
126
|
+
const method = (opts.method || 'GET').toUpperCase();
|
|
127
|
+
console.log(`txResponse: Preparing ${method} request to ${endpoint}`);
|
|
128
|
+
const fetchOptions = {
|
|
129
|
+
method: method,
|
|
130
|
+
headers: { 'Content-Type': 'application/json' },
|
|
131
|
+
// body is set conditionally below
|
|
132
|
+
};
|
|
133
|
+
if (method === 'GET' || method === 'HEAD') {
|
|
134
|
+
// --- GET/HEAD: Append data as query params ---
|
|
135
|
+
if (opts.data && typeof opts.data === 'object' && Object.keys(opts.data).length > 0) {
|
|
136
|
+
console.log(`txResponse: Appending data as query params for GET request:`, opts.data);
|
|
137
|
+
// Convert potential non-string values in opts.data to strings for URLSearchParams
|
|
138
|
+
const paramsData = {};
|
|
139
|
+
for (const key in opts.data) {
|
|
140
|
+
if (Object.prototype.hasOwnProperty.call(opts.data, key)) {
|
|
141
|
+
paramsData[key] = String(opts.data[key]);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const params = new URLSearchParams(paramsData);
|
|
145
|
+
params.forEach((value, key) => {
|
|
146
|
+
url.searchParams.append(key, value);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
// Ensure no body is sent for GET/HEAD
|
|
150
|
+
delete fetchOptions.body; // Or just don't set it
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
// --- POST/PUT/etc.: Set data as body ---
|
|
154
|
+
if (opts.data) {
|
|
155
|
+
console.log(`txResponse: Setting data as body for ${method} request:`, opts.data);
|
|
156
|
+
fetchOptions.body = JSON.stringify(opts.data);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
125
159
|
try {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
body: opts.data ? JSON.stringify(opts.data) : undefined
|
|
130
|
-
});
|
|
160
|
+
console.log(`txResponse: Fetching ${url.toString()} with options:`, fetchOptions);
|
|
161
|
+
// Use the potentially modified URL and fetchOptions
|
|
162
|
+
return await fetchJson(url.toString(), fetchOptions);
|
|
131
163
|
}
|
|
132
164
|
catch (error) {
|
|
133
|
-
|
|
165
|
+
// ... error logging ...
|
|
166
|
+
console.error(`Failed to get txResponse from ${url.toString()} for tx ${txHash} using method ${method}:`, error);
|
|
134
167
|
throw new Error(`Could not get transaction response: ${error instanceof Error ? error.message : String(error)}`);
|
|
135
168
|
}
|
|
136
169
|
}
|
|
@@ -162,7 +195,13 @@ class ApiNow {
|
|
|
162
195
|
await new Promise(resolve => setTimeout(resolve, 500));
|
|
163
196
|
}
|
|
164
197
|
console.log(`Fetching response for tx: ${txHash}`);
|
|
165
|
-
|
|
198
|
+
// Create specific options for txResponse
|
|
199
|
+
const txResponseOpts = {
|
|
200
|
+
method: info.httpMethod || 'POST', // Use the method from info, default to POST
|
|
201
|
+
data: opts.data // Pass the original data payload intended for the API
|
|
202
|
+
};
|
|
203
|
+
// Call txResponse with the tailored options
|
|
204
|
+
return this.txResponse(endpoint, txHash, txResponseOpts);
|
|
166
205
|
}
|
|
167
206
|
}
|
|
168
207
|
const apiNow = new ApiNow();
|