@webtypen/webframez-core 0.3.82 → 0.3.83

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.
@@ -5,9 +5,12 @@ export type BearerAuthTokens = {
5
5
  refresh_expires_at: number;
6
6
  };
7
7
 
8
+ /** Override one request's transport (e.g. XHR upload progress); refreshes keep the original fetch and shared refresh lock. */
9
+ export type BearerAuthFetch = (input: Parameters<typeof fetch>[0], init?: RequestInit, requestFetch?: typeof fetch) => Promise<Response>;
10
+
8
11
  export function createBearerAuthFetch(options: {
9
12
  configuration: (url: URL) => { key: string; refreshUrl: string } | null;
10
13
  load: (key: string) => Promise<BearerAuthTokens | null>;
11
14
  clear?: (key: string) => Promise<void>;
12
15
  save: (key: string, tokens: BearerAuthTokens) => Promise<void>;
13
- }, originalFetch?: typeof fetch): typeof fetch;
16
+ }, originalFetch?: typeof fetch): BearerAuthFetch;
@@ -41,27 +41,27 @@ exports.createBearerAuthFetch = function createBearerAuthFetch(options, original
41
41
  }
42
42
  return pending.get(key);
43
43
  };
44
- return async (input, init) => {
44
+ return async (input, init, requestFetch = originalFetch) => {
45
45
  let url;
46
46
  try { url = new URL(typeof Request !== "undefined" && input instanceof Request ? input.url : String(input), globalThis.location?.href); }
47
- catch { return originalFetch(input, init); }
47
+ catch { return requestFetch(input, init); }
48
48
  const configuration = options.configuration(url);
49
- if (!configuration) return originalFetch(input, init);
49
+ if (!configuration) return requestFetch(input, init);
50
50
  const refreshUrl = new URL(configuration.refreshUrl);
51
51
  if (refreshUrl.origin !== url.origin) throw new Error("Bearer refresh must use the request origin.");
52
52
  let session = await options.load(configuration.key);
53
- if (!session || url.href === refreshUrl.href) return originalFetch(input, init);
53
+ if (!session || url.href === refreshUrl.href) return requestFetch(input, init);
54
54
  if (session.auth_expires_at <= Date.now() + 30000) session = await refresh(configuration, session.auth_token) || session;
55
55
  const headers = new Headers(init?.headers || (typeof Request !== "undefined" && input instanceof Request ? input.headers : undefined));
56
56
  headers.set("Authorization", `Bearer ${session.auth_token}`);
57
57
  const retryInput = typeof Request !== "undefined" && input instanceof Request ? input.clone() : input;
58
- const response = await originalFetch(input, { ...init, headers, credentials: "omit", redirect: "error" });
58
+ const response = await requestFetch(input, { ...init, headers, credentials: "omit", redirect: "error" });
59
59
  if (response.status !== 401) return response;
60
60
  const failure = await response.clone().json().catch(() => null);
61
61
  if (failure?.code !== "unauthorized") return response;
62
62
  const renewed = await refresh(configuration, session.auth_token);
63
63
  if (!renewed) return response;
64
64
  headers.set("Authorization", `Bearer ${renewed.auth_token}`);
65
- return originalFetch(retryInput, { ...init, headers, credentials: "omit", redirect: "error" });
65
+ return requestFetch(retryInput, { ...init, headers, credentials: "omit", redirect: "error" });
66
66
  };
67
67
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.3.82",
3
+ "version": "0.3.83",
4
4
  "description": "The core package of the typescript based webframez (web-framework) by webtypen.de",
5
5
  "homepage": "https://webtypen.de/",
6
6
  "license": "MIT",