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