@usebridge/sdk-react 0.7.2 → 0.8.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.cjs +45 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +45 -14
- package/dist/index.js.map +1 -1
- package/package.json +7 -2
package/dist/index.cjs
CHANGED
|
@@ -549,6 +549,9 @@ function useIsMounted() {
|
|
|
549
549
|
return (0, import_react4.useCallback)(() => isMounted.current, []);
|
|
550
550
|
}
|
|
551
551
|
|
|
552
|
+
// src/payer-search/use-payer-autocomplete.ts
|
|
553
|
+
var import_api = require("@usebridge/api");
|
|
554
|
+
|
|
552
555
|
// src/lib/retry-loop.ts
|
|
553
556
|
var RetryLoopCancelledError = class extends Error {
|
|
554
557
|
constructor() {
|
|
@@ -556,20 +559,22 @@ var RetryLoopCancelledError = class extends Error {
|
|
|
556
559
|
this.name = "RetryLoopCancelledError";
|
|
557
560
|
}
|
|
558
561
|
};
|
|
559
|
-
async function retryLoop(fn,
|
|
560
|
-
let
|
|
561
|
-
|
|
562
|
+
async function retryLoop(fn, getRetryDecision, delayMs = 500) {
|
|
563
|
+
let decision = "retry";
|
|
564
|
+
let lastError;
|
|
565
|
+
while (decision === "retry") {
|
|
562
566
|
try {
|
|
563
567
|
return await fn();
|
|
564
|
-
} catch (
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
+
} catch (err) {
|
|
569
|
+
lastError = err;
|
|
570
|
+
decision = getRetryDecision(err);
|
|
571
|
+
if (decision === "retry") {
|
|
568
572
|
await new Promise((r) => setTimeout(r, delayMs));
|
|
569
573
|
}
|
|
570
574
|
}
|
|
571
575
|
}
|
|
572
|
-
throw new RetryLoopCancelledError();
|
|
576
|
+
if (decision === "cancel") throw new RetryLoopCancelledError();
|
|
577
|
+
throw lastError;
|
|
573
578
|
}
|
|
574
579
|
|
|
575
580
|
// src/payer-search/use-payer-search.ts
|
|
@@ -2128,45 +2133,71 @@ var omit_default = omit;
|
|
|
2128
2133
|
|
|
2129
2134
|
// src/payer-search/use-payer-autocomplete.ts
|
|
2130
2135
|
var resultCache = /* @__PURE__ */ new Map();
|
|
2136
|
+
function toCacheKey(normalizedQuery, limit) {
|
|
2137
|
+
return `${limit}:${normalizedQuery}`;
|
|
2138
|
+
}
|
|
2139
|
+
function toError(err) {
|
|
2140
|
+
if (err instanceof Error) return err;
|
|
2141
|
+
return new Error(String(err));
|
|
2142
|
+
}
|
|
2143
|
+
function isRetryablePayerSearchError(err) {
|
|
2144
|
+
if (err instanceof import_api.BridgeApiError && err.statusCode != null) {
|
|
2145
|
+
if (err.statusCode === 429 || err.statusCode === 408) return true;
|
|
2146
|
+
if (err.statusCode >= 400 && err.statusCode < 500) return false;
|
|
2147
|
+
}
|
|
2148
|
+
return true;
|
|
2149
|
+
}
|
|
2131
2150
|
function usePayerAutocomplete(query, {
|
|
2132
2151
|
limit = 10
|
|
2133
2152
|
} = {}) {
|
|
2134
2153
|
const payerSearch = usePayerSearch();
|
|
2135
2154
|
const [isLoading, setIsLoading] = (0, import_react6.useState)(false);
|
|
2136
2155
|
const [results, setResults] = (0, import_react6.useState)([]);
|
|
2156
|
+
const [error, setError] = (0, import_react6.useState)();
|
|
2137
2157
|
const normalizedQuery = query.trim().toLowerCase();
|
|
2138
2158
|
const reqId = (0, import_react6.useRef)(0);
|
|
2139
2159
|
const isMounted = useIsMounted();
|
|
2140
2160
|
(0, import_react6.useEffect)(() => {
|
|
2141
2161
|
const thisId = ++reqId.current;
|
|
2142
|
-
const
|
|
2162
|
+
const cacheKey = toCacheKey(normalizedQuery, limit);
|
|
2163
|
+
const cachedResults = resultCache.get(cacheKey);
|
|
2143
2164
|
if (cachedResults) {
|
|
2144
2165
|
setIsLoading(false);
|
|
2145
2166
|
setResults(cachedResults);
|
|
2167
|
+
setError(void 0);
|
|
2146
2168
|
return;
|
|
2147
2169
|
}
|
|
2148
2170
|
async function run() {
|
|
2149
2171
|
setIsLoading(true);
|
|
2172
|
+
setError(void 0);
|
|
2150
2173
|
try {
|
|
2151
2174
|
const response = await retryLoop(
|
|
2152
2175
|
() => payerSearch({ query: normalizedQuery, limit }),
|
|
2153
|
-
() =>
|
|
2176
|
+
(err) => {
|
|
2177
|
+
if (!isMounted() || thisId !== reqId.current) return "cancel";
|
|
2178
|
+
if (!isRetryablePayerSearchError(err)) return "fail";
|
|
2179
|
+
return "retry";
|
|
2180
|
+
}
|
|
2154
2181
|
);
|
|
2155
2182
|
if (!isMounted()) return;
|
|
2156
|
-
resultCache.set(
|
|
2183
|
+
resultCache.set(cacheKey, response.items);
|
|
2157
2184
|
if (thisId !== reqId.current) return;
|
|
2158
2185
|
setIsLoading(false);
|
|
2159
2186
|
setResults(response.items);
|
|
2187
|
+
setError(void 0);
|
|
2160
2188
|
} catch (err) {
|
|
2161
2189
|
if (err instanceof RetryLoopCancelledError) return;
|
|
2162
|
-
|
|
2190
|
+
if (!isMounted() || thisId !== reqId.current) return;
|
|
2191
|
+
setResults([]);
|
|
2192
|
+
setIsLoading(false);
|
|
2193
|
+
setError(toError(err));
|
|
2163
2194
|
}
|
|
2164
2195
|
}
|
|
2165
2196
|
void run();
|
|
2166
2197
|
}, [payerSearch, normalizedQuery, limit]);
|
|
2167
2198
|
return (0, import_react6.useMemo)(
|
|
2168
|
-
() => ({ isLoading, results: results.map((r) => omit_default(r, "code")) }),
|
|
2169
|
-
[isLoading, results]
|
|
2199
|
+
() => ({ isLoading, results: results.map((r) => omit_default(r, "code")), error }),
|
|
2200
|
+
[isLoading, results, error]
|
|
2170
2201
|
);
|
|
2171
2202
|
}
|
|
2172
2203
|
|