@routstr/sdk 0.1.6 → 0.1.7

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.
@@ -1183,6 +1183,14 @@ var BalanceManager = class {
1183
1183
  console.log(response.status);
1184
1184
  const data = await response.json();
1185
1185
  console.log("FAILED ", data);
1186
+ const isInvalidApiKey = response.status === 401 && data?.code === "invalid_api_key" && data?.message?.includes("proofs already spent");
1187
+ return {
1188
+ amount: -1,
1189
+ reserved: data.reserved ?? 0,
1190
+ unit: "msat",
1191
+ apiKey: data.api_key,
1192
+ isInvalidApiKey
1193
+ };
1186
1194
  }
1187
1195
  } catch (error) {
1188
1196
  console.error("ERRORR IN RESTPONSE", error);
@@ -2075,7 +2083,8 @@ var RoutstrClient = class {
2075
2083
  response.status,
2076
2084
  requestId,
2077
2085
  this.mode === "xcashu" ? response.headers.get("x-cashu") ?? void 0 : void 0,
2078
- bodyText
2086
+ bodyText,
2087
+ params.retryCount ?? 0
2079
2088
  );
2080
2089
  }
2081
2090
  return response;
@@ -2084,8 +2093,12 @@ var RoutstrClient = class {
2084
2093
  return await this._handleErrorResponse(
2085
2094
  params,
2086
2095
  token,
2087
- -1
2096
+ -1,
2088
2097
  // just for Network Error to skip all statuses
2098
+ void 0,
2099
+ void 0,
2100
+ void 0,
2101
+ params.retryCount ?? 0
2089
2102
  );
2090
2103
  }
2091
2104
  throw error;
@@ -2094,7 +2107,8 @@ var RoutstrClient = class {
2094
2107
  /**
2095
2108
  * Handle error responses with failover
2096
2109
  */
2097
- async _handleErrorResponse(params, token, status, requestId, xCashuRefundToken, responseBody) {
2110
+ async _handleErrorResponse(params, token, status, requestId, xCashuRefundToken, responseBody, retryCount = 0) {
2111
+ const MAX_RETRIES_PER_PROVIDER = 2;
2098
2112
  const { path, method, body, selectedModel, baseUrl, mintUrl } = params;
2099
2113
  let tryNextProvider = false;
2100
2114
  this._log(
@@ -2200,12 +2214,26 @@ var RoutstrClient = class {
2200
2214
  `[RoutstrClient] _handleErrorResponse: Topup successful, will retry with new token`
2201
2215
  );
2202
2216
  }
2203
- if (!tryNextProvider)
2204
- return this._makeRequest({
2205
- ...params,
2206
- token: params.token,
2207
- headers: this._withAuthHeader(params.baseHeaders, params.token)
2208
- });
2217
+ if (!tryNextProvider) {
2218
+ if (retryCount < MAX_RETRIES_PER_PROVIDER) {
2219
+ this._log(
2220
+ "DEBUG",
2221
+ `[RoutstrClient] _handleErrorResponse: Retrying 402 (attempt ${retryCount + 1}/${MAX_RETRIES_PER_PROVIDER})`
2222
+ );
2223
+ return this._makeRequest({
2224
+ ...params,
2225
+ token: params.token,
2226
+ headers: this._withAuthHeader(params.baseHeaders, params.token),
2227
+ retryCount: retryCount + 1
2228
+ });
2229
+ } else {
2230
+ this._log(
2231
+ "DEBUG",
2232
+ `[RoutstrClient] _handleErrorResponse: 402 retry limit reached (${retryCount}/${MAX_RETRIES_PER_PROVIDER}), failing over to next provider`
2233
+ );
2234
+ tryNextProvider = true;
2235
+ }
2236
+ }
2209
2237
  }
2210
2238
  const isInsufficientBalance413 = status === 413 && responseBody?.includes("Insufficient balance");
2211
2239
  if (isInsufficientBalance413 && !tryNextProvider && this.mode === "apikeys") {
@@ -2215,19 +2243,28 @@ var RoutstrClient = class {
2215
2243
  params.token,
2216
2244
  baseUrl
2217
2245
  );
2218
- const latestTokenBalance = latestBalanceInfo.unit === "msat" ? latestBalanceInfo.amount / 1e3 : latestBalanceInfo.amount;
2219
- if (latestBalanceInfo.apiKey) {
2220
- const storedApiKeyEntry = this.storageAdapter.getApiKey(baseUrl);
2221
- if (storedApiKeyEntry?.key !== latestBalanceInfo.apiKey) {
2222
- if (storedApiKeyEntry) {
2223
- this.storageAdapter.removeApiKey(baseUrl);
2246
+ if (latestBalanceInfo.isInvalidApiKey) {
2247
+ this._log(
2248
+ "DEBUG",
2249
+ `[RoutstrClient] _handleErrorResponse: Invalid API key (proofs already spent), removing for ${baseUrl}`
2250
+ );
2251
+ this.storageAdapter.removeApiKey(baseUrl);
2252
+ tryNextProvider = true;
2253
+ } else {
2254
+ const latestTokenBalance = latestBalanceInfo.unit === "msat" ? latestBalanceInfo.amount / 1e3 : latestBalanceInfo.amount;
2255
+ if (latestBalanceInfo.apiKey) {
2256
+ const storedApiKeyEntry = this.storageAdapter.getApiKey(baseUrl);
2257
+ if (storedApiKeyEntry?.key !== latestBalanceInfo.apiKey) {
2258
+ if (storedApiKeyEntry) {
2259
+ this.storageAdapter.removeApiKey(baseUrl);
2260
+ }
2261
+ this.storageAdapter.setApiKey(baseUrl, latestBalanceInfo.apiKey);
2224
2262
  }
2225
- this.storageAdapter.setApiKey(baseUrl, latestBalanceInfo.apiKey);
2263
+ retryToken = latestBalanceInfo.apiKey;
2264
+ }
2265
+ if (latestTokenBalance >= 0) {
2266
+ this.storageAdapter.updateApiKeyBalance(baseUrl, latestTokenBalance);
2226
2267
  }
2227
- retryToken = latestBalanceInfo.apiKey;
2228
- }
2229
- if (latestTokenBalance >= 0) {
2230
- this.storageAdapter.updateApiKeyBalance(baseUrl, latestTokenBalance);
2231
2268
  }
2232
2269
  } catch (error) {
2233
2270
  this._log(
@@ -2236,11 +2273,24 @@ var RoutstrClient = class {
2236
2273
  error
2237
2274
  );
2238
2275
  }
2239
- return this._makeRequest({
2240
- ...params,
2241
- token: retryToken,
2242
- headers: this._withAuthHeader(params.baseHeaders, retryToken)
2243
- });
2276
+ if (retryCount < MAX_RETRIES_PER_PROVIDER) {
2277
+ this._log(
2278
+ "DEBUG",
2279
+ `[RoutstrClient] _handleErrorResponse: Retrying 413 (attempt ${retryCount + 1}/${MAX_RETRIES_PER_PROVIDER})`
2280
+ );
2281
+ return this._makeRequest({
2282
+ ...params,
2283
+ token: retryToken,
2284
+ headers: this._withAuthHeader(params.baseHeaders, retryToken),
2285
+ retryCount: retryCount + 1
2286
+ });
2287
+ } else {
2288
+ this._log(
2289
+ "DEBUG",
2290
+ `[RoutstrClient] _handleErrorResponse: 413 retry limit reached (${retryCount}/${MAX_RETRIES_PER_PROVIDER}), failing over to next provider`
2291
+ );
2292
+ tryNextProvider = true;
2293
+ }
2244
2294
  }
2245
2295
  if ((status === 401 || status === 403 || status === 413 || status === 400 || status === 500 || status === 502 || status === 503 || status === 504 || status === 521) && !tryNextProvider) {
2246
2296
  this._log(
@@ -2356,7 +2406,8 @@ var RoutstrClient = class {
2356
2406
  selectedModel: newModel,
2357
2407
  token: spendResult.token,
2358
2408
  requiredSats: newRequiredSats,
2359
- headers: this._withAuthHeader(params.baseHeaders, spendResult.token)
2409
+ headers: this._withAuthHeader(params.baseHeaders, spendResult.token),
2410
+ retryCount: 0
2360
2411
  });
2361
2412
  }
2362
2413
  throw new FailoverError(baseUrl, Array.from(this.providerManager));