@routstr/sdk 0.1.5 → 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 +94 -9
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.mjs +94 -9
- package/dist/client/index.mjs.map +1 -1
- package/dist/discovery/index.d.mts +3 -1
- package/dist/discovery/index.d.ts +3 -1
- package/dist/discovery/index.js +17 -13
- package/dist/discovery/index.js.map +1 -1
- package/dist/discovery/index.mjs +17 -13
- package/dist/discovery/index.mjs.map +1 -1
- package/dist/index.js +115 -26
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +115 -26
- 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(
|
|
@@ -2162,8 +2176,7 @@ var RoutstrClient = class {
|
|
|
2162
2176
|
);
|
|
2163
2177
|
}
|
|
2164
2178
|
}
|
|
2165
|
-
if (
|
|
2166
|
-
console.log("RESPONSFE ", responseBody);
|
|
2179
|
+
if (status === 402 && !tryNextProvider && (this.mode === "apikeys" || this.mode === "lazyrefund")) {
|
|
2167
2180
|
const topupResult = await this.balanceManager.topUp({
|
|
2168
2181
|
mintUrl,
|
|
2169
2182
|
baseUrl,
|
|
@@ -2199,12 +2212,83 @@ var RoutstrClient = class {
|
|
|
2199
2212
|
`[RoutstrClient] _handleErrorResponse: Topup successful, will retry with new token`
|
|
2200
2213
|
);
|
|
2201
2214
|
}
|
|
2202
|
-
if (!tryNextProvider)
|
|
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
|
+
}
|
|
2235
|
+
}
|
|
2236
|
+
const isInsufficientBalance413 = status === 413 && responseBody?.includes("Insufficient balance");
|
|
2237
|
+
if (isInsufficientBalance413 && !tryNextProvider && this.mode === "apikeys") {
|
|
2238
|
+
let retryToken = params.token;
|
|
2239
|
+
try {
|
|
2240
|
+
const latestBalanceInfo = await this.balanceManager.getTokenBalance(
|
|
2241
|
+
params.token,
|
|
2242
|
+
baseUrl
|
|
2243
|
+
);
|
|
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);
|
|
2260
|
+
}
|
|
2261
|
+
retryToken = latestBalanceInfo.apiKey;
|
|
2262
|
+
}
|
|
2263
|
+
if (latestTokenBalance >= 0) {
|
|
2264
|
+
this.storageAdapter.updateApiKeyBalance(baseUrl, latestTokenBalance);
|
|
2265
|
+
}
|
|
2266
|
+
}
|
|
2267
|
+
} catch (error) {
|
|
2268
|
+
this._log(
|
|
2269
|
+
"WARN",
|
|
2270
|
+
`[RoutstrClient] _handleErrorResponse: Failed to refresh API key after 413 insufficient balance for ${baseUrl}`,
|
|
2271
|
+
error
|
|
2272
|
+
);
|
|
2273
|
+
}
|
|
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
|
+
);
|
|
2203
2279
|
return this._makeRequest({
|
|
2204
2280
|
...params,
|
|
2205
|
-
token:
|
|
2206
|
-
headers: this._withAuthHeader(params.baseHeaders,
|
|
2281
|
+
token: retryToken,
|
|
2282
|
+
headers: this._withAuthHeader(params.baseHeaders, retryToken),
|
|
2283
|
+
retryCount: retryCount + 1
|
|
2207
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
|
+
}
|
|
2208
2292
|
}
|
|
2209
2293
|
if ((status === 401 || status === 403 || status === 413 || status === 400 || status === 500 || status === 502 || status === 503 || status === 504 || status === 521) && !tryNextProvider) {
|
|
2210
2294
|
this._log(
|
|
@@ -2320,7 +2404,8 @@ var RoutstrClient = class {
|
|
|
2320
2404
|
selectedModel: newModel,
|
|
2321
2405
|
token: spendResult.token,
|
|
2322
2406
|
requiredSats: newRequiredSats,
|
|
2323
|
-
headers: this._withAuthHeader(params.baseHeaders, spendResult.token)
|
|
2407
|
+
headers: this._withAuthHeader(params.baseHeaders, spendResult.token),
|
|
2408
|
+
retryCount: 0
|
|
2324
2409
|
});
|
|
2325
2410
|
}
|
|
2326
2411
|
throw new FailoverError(baseUrl, Array.from(this.providerManager));
|