snaptrade-typescript-sdk 12.1.14 → 12.2.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/index.d.cts CHANGED
@@ -10917,7 +10917,7 @@ declare class SnaptradeError extends Error {
10917
10917
  */
10918
10918
  readonly statusText?: string;
10919
10919
  /**
10920
- * The URL that the original request was sent to
10920
+ * The request URL with authentication query parameter values redacted.
10921
10921
  */
10922
10922
  readonly url?: string;
10923
10923
  /**
package/dist/index.d.mts CHANGED
@@ -10917,7 +10917,7 @@ declare class SnaptradeError extends Error {
10917
10917
  */
10918
10918
  readonly statusText?: string;
10919
10919
  /**
10920
- * The URL that the original request was sent to
10920
+ * The request URL with authentication query parameter values redacted.
10921
10921
  */
10922
10922
  readonly url?: string;
10923
10923
  /**
package/dist/index.d.ts CHANGED
@@ -10917,7 +10917,7 @@ declare class SnaptradeError extends Error {
10917
10917
  */
10918
10918
  readonly statusText?: string;
10919
10919
  /**
10920
- * The URL that the original request was sent to
10920
+ * The request URL with authentication query parameter values redacted.
10921
10921
  */
10922
10922
  readonly url?: string;
10923
10923
  /**
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import globalAxios, { AxiosError } from "axios";
1
+ import globalAxios from "axios";
2
2
  //#region base.ts
3
3
  const BASE_PATH = "https://api.snaptrade.com".replace(/\/+$/, "");
4
4
  /**
@@ -83,17 +83,48 @@ async function requestAfterHook(request) {
83
83
  }
84
84
  //#endregion
85
85
  //#region error.ts
86
+ const REDACTED_QUERY_VALUE = "[REDACTED]";
87
+ const SENSITIVE_QUERY_PARAMETER_NAMES = new Set([
88
+ "clientid",
89
+ "timestamp",
90
+ "userid",
91
+ "usersecret"
92
+ ]);
93
+ function redactSensitiveQueryValues(url) {
94
+ if (url === void 0 || SENSITIVE_QUERY_PARAMETER_NAMES.size === 0) return url;
95
+ try {
96
+ const isAbsoluteUrl = /^[a-z][a-z\d+.-]*:/i.test(url);
97
+ const parsedUrl = new URL(url, "http://konfig.invalid");
98
+ const redactedSearchParams = new URLSearchParams();
99
+ for (const [name, value] of parsedUrl.searchParams.entries()) {
100
+ let redactedValue = value;
101
+ if (SENSITIVE_QUERY_PARAMETER_NAMES.has(name.toLowerCase())) redactedValue = REDACTED_QUERY_VALUE;
102
+ redactedSearchParams.append(name, redactedValue);
103
+ }
104
+ parsedUrl.search = redactedSearchParams.toString();
105
+ if (isAbsoluteUrl) return parsedUrl.toString();
106
+ return parsedUrl.pathname + parsedUrl.search + parsedUrl.hash;
107
+ } catch {
108
+ return;
109
+ }
110
+ }
111
+ function redactUrlFromMessage(message, originalUrl, redactedUrl) {
112
+ if (originalUrl === void 0 || !message.includes(originalUrl)) return message;
113
+ return message.split(originalUrl).join(redactedUrl ?? "[REDACTED URL]");
114
+ }
86
115
  /**
87
116
  * This class provides a wrapper for network errors when making requests to SnapTrade
88
117
  */
89
118
  var SnaptradeError = class extends Error {
90
119
  constructor(axiosError, responseBody, headers) {
91
- const message = axiosError.message + "\nRESPONSE HEADERS:\n" + JSON.stringify(headers, null, 2);
120
+ const originalUrl = axiosError.config?.url;
121
+ const redactedUrl = redactSensitiveQueryValues(originalUrl);
122
+ const message = redactUrlFromMessage(axiosError.message, originalUrl, redactedUrl) + "\nRESPONSE HEADERS:\n" + JSON.stringify(headers, null, 2);
92
123
  super(message);
93
124
  this.name = "SnaptradeError";
94
125
  this.code = axiosError.code;
95
126
  this.method = axiosError.config?.method?.toUpperCase();
96
- this.url = axiosError.config?.url;
127
+ this.url = redactedUrl;
97
128
  this.status = axiosError.response?.status;
98
129
  this.statusText = axiosError.response?.statusText;
99
130
  this.responseBody = responseBody;
@@ -214,7 +245,7 @@ async function wrapAxiosRequest(makeRequest) {
214
245
  while (attempt < maxAttempts) try {
215
246
  return await makeRequest();
216
247
  } catch (e) {
217
- if (e instanceof AxiosError && e.isAxiosError) {
248
+ if (globalAxios.isAxiosError(e)) {
218
249
  if (e.response?.status == 429) {
219
250
  attempt++;
220
251
  console.log(`429 error encountered, retrying in ${delay / 1e3} seconds...`);
@@ -222,13 +253,16 @@ async function wrapAxiosRequest(makeRequest) {
222
253
  delay *= 2;
223
254
  continue;
224
255
  }
256
+ let responseBody;
225
257
  try {
226
- throw new SnaptradeError(e, parseIfJson(e.response?.data instanceof ReadableStream ? await readableStreamToString(e.response.data) : e.response?.data), e.response?.headers);
227
- } catch (innerError) {
228
- if (innerError instanceof ReferenceError) throw new SnaptradeError(e, e.response?.data, e.response?.headers);
229
- if (innerError instanceof SnaptradeError) throw innerError;
230
- throw e;
258
+ const responseData = e.response?.data;
259
+ responseBody = responseData;
260
+ if (typeof ReadableStream !== "undefined" && responseData instanceof ReadableStream) responseBody = await readableStreamToString(responseData);
261
+ responseBody = parseIfJson(responseBody);
262
+ } catch {
263
+ responseBody = void 0;
231
264
  }
265
+ throw new SnaptradeError(e, responseBody, e.response?.headers);
232
266
  }
233
267
  throw e;
234
268
  }
@@ -6973,7 +7007,7 @@ var Configuration = class {
6973
7007
  }
6974
7008
  this.basePath = param.basePath;
6975
7009
  this.baseOptions = param.baseOptions ?? {};
6976
- this.userAgent = param.userAgent === void 0 ? "Konfig/12.1.14/typescript" : param.userAgent;
7010
+ this.userAgent = param.userAgent === void 0 ? "Konfig/12.2.0/typescript" : param.userAgent;
6977
7011
  this.formDataCtor = param.formDataCtor;
6978
7012
  }
6979
7013
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "snaptrade-typescript-sdk",
3
- "version": "12.1.14",
3
+ "version": "12.2.0",
4
4
  "description": "Client for SnapTrade",
5
5
  "author": "Konfig",
6
6
  "engines": {