@wefunder/sdk 0.1.0-beta.2 → 0.1.0-beta.4

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.js CHANGED
@@ -838,13 +838,6 @@ function backoffMs(attempt, opts, random) {
838
838
  const jitter = exp * 0.5 * random();
839
839
  return Math.min(exp + jitter, opts.maxDelayMs);
840
840
  }
841
- function withBearer(input, init, token) {
842
- const headers = new Headers(
843
- init?.headers ?? (input instanceof Request ? input.headers : void 0)
844
- );
845
- headers.set("Authorization", `Bearer ${token}`);
846
- return { ...init, headers };
847
- }
848
841
  function createFetch(deps) {
849
842
  const baseFetch = deps.fetch ?? fetch;
850
843
  const sleep = deps.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
@@ -858,21 +851,37 @@ function createFetch(deps) {
858
851
  return async function wrappedFetch(input, init) {
859
852
  const method = methodOf(input, init);
860
853
  const idempotent = IDEMPOTENT.has(method);
854
+ const isRequest = input instanceof Request;
861
855
  let refreshed = false;
862
856
  let attempt = 0;
857
+ let retryToken;
858
+ const dispatch = (authToken) => {
859
+ if (isRequest) {
860
+ const fresh = input.clone();
861
+ if (authToken === void 0) return baseFetch(fresh);
862
+ const headers = new Headers(fresh.headers);
863
+ headers.set("Authorization", `Bearer ${authToken}`);
864
+ return baseFetch(new Request(fresh, { headers }));
865
+ }
866
+ const nextInit = authToken === void 0 ? init : (() => {
867
+ const headers = new Headers(init?.headers);
868
+ headers.set("Authorization", `Bearer ${authToken}`);
869
+ return { ...init, headers };
870
+ })();
871
+ return baseFetch(input, nextInit);
872
+ };
863
873
  for (; ; ) {
864
874
  let response;
865
875
  let networkError;
866
876
  try {
867
- response = await baseFetch(input, init);
877
+ response = await dispatch(retryToken);
868
878
  } catch (err) {
869
879
  networkError = err;
870
880
  }
871
- const replayable = !(input instanceof Request && input.bodyUsed);
872
- if (response?.status === 401 && !refreshed && replayable && deps.tokenManager?.canRefresh) {
881
+ if (response?.status === 401 && !refreshed && deps.tokenManager?.canRefresh) {
873
882
  refreshed = true;
874
883
  const next = await deps.tokenManager.refresh();
875
- init = withBearer(input, init, next.accessToken);
884
+ retryToken = next.accessToken;
876
885
  continue;
877
886
  }
878
887
  if (response?.status === 429 && attempt < retry.maxRetries) {