@routstr/sdk 0.1.5 → 0.1.6

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.
@@ -2164,8 +2164,7 @@ var RoutstrClient = class {
2164
2164
  );
2165
2165
  }
2166
2166
  }
2167
- if ((status === 402 || status === 413 && responseBody?.includes("Insufficient balance")) && !tryNextProvider && (this.mode === "apikeys" || this.mode === "lazyrefund")) {
2168
- console.log("RESPONSFE ", responseBody);
2167
+ if (status === 402 && !tryNextProvider && (this.mode === "apikeys" || this.mode === "lazyrefund")) {
2169
2168
  const topupResult = await this.balanceManager.topUp({
2170
2169
  mintUrl,
2171
2170
  baseUrl,
@@ -2208,6 +2207,41 @@ var RoutstrClient = class {
2208
2207
  headers: this._withAuthHeader(params.baseHeaders, params.token)
2209
2208
  });
2210
2209
  }
2210
+ const isInsufficientBalance413 = status === 413 && responseBody?.includes("Insufficient balance");
2211
+ if (isInsufficientBalance413 && !tryNextProvider && this.mode === "apikeys") {
2212
+ let retryToken = params.token;
2213
+ try {
2214
+ const latestBalanceInfo = await this.balanceManager.getTokenBalance(
2215
+ params.token,
2216
+ baseUrl
2217
+ );
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);
2224
+ }
2225
+ this.storageAdapter.setApiKey(baseUrl, latestBalanceInfo.apiKey);
2226
+ }
2227
+ retryToken = latestBalanceInfo.apiKey;
2228
+ }
2229
+ if (latestTokenBalance >= 0) {
2230
+ this.storageAdapter.updateApiKeyBalance(baseUrl, latestTokenBalance);
2231
+ }
2232
+ } catch (error) {
2233
+ this._log(
2234
+ "WARN",
2235
+ `[RoutstrClient] _handleErrorResponse: Failed to refresh API key after 413 insufficient balance for ${baseUrl}`,
2236
+ error
2237
+ );
2238
+ }
2239
+ return this._makeRequest({
2240
+ ...params,
2241
+ token: retryToken,
2242
+ headers: this._withAuthHeader(params.baseHeaders, retryToken)
2243
+ });
2244
+ }
2211
2245
  if ((status === 401 || status === 403 || status === 413 || status === 400 || status === 500 || status === 502 || status === 503 || status === 504 || status === 521) && !tryNextProvider) {
2212
2246
  this._log(
2213
2247
  "DEBUG",