@flashbacktech/tsclient 0.4.72 → 0.4.73

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.d.cts CHANGED
@@ -1,5 +1,13 @@
1
1
  type TokenProvider = () => string | null | Promise<string | null>;
2
- type UnauthorizedHandler = () => void | Promise<void>;
2
+ /**
3
+ * Called when a request comes back 401. Typically refreshes the access token.
4
+ * Return `true` when the refresh succeeded so the BaseClient transparently
5
+ * retries the original request once with the new token. Return `false` when
6
+ * the session is unrecoverable (e.g. a dead refresh token) so the original
7
+ * 401 is surfaced without a wasted retry. Returning `void` is treated as
8
+ * "retry once" for backwards compatibility.
9
+ */
10
+ type UnauthorizedHandler = () => void | boolean | Promise<void | boolean>;
3
11
 
4
12
  /**
5
13
  * Returns the org id the platform admin is currently impersonating, or
@@ -45,7 +53,7 @@ declare class BaseClient {
45
53
  put<T>(path: string, options?: RequestOptions): Promise<T>;
46
54
  patch<T>(path: string, options?: RequestOptions): Promise<T>;
47
55
  delete<T>(path: string, options?: RequestOptions): Promise<T>;
48
- request<T>(method: string, path: string, options?: RequestOptions): Promise<T>;
56
+ request<T>(method: string, path: string, options?: RequestOptions, retrying?: boolean): Promise<T>;
49
57
  private buildUrl;
50
58
  }
51
59
 
package/dist/index.d.ts CHANGED
@@ -1,5 +1,13 @@
1
1
  type TokenProvider = () => string | null | Promise<string | null>;
2
- type UnauthorizedHandler = () => void | Promise<void>;
2
+ /**
3
+ * Called when a request comes back 401. Typically refreshes the access token.
4
+ * Return `true` when the refresh succeeded so the BaseClient transparently
5
+ * retries the original request once with the new token. Return `false` when
6
+ * the session is unrecoverable (e.g. a dead refresh token) so the original
7
+ * 401 is surfaced without a wasted retry. Returning `void` is treated as
8
+ * "retry once" for backwards compatibility.
9
+ */
10
+ type UnauthorizedHandler = () => void | boolean | Promise<void | boolean>;
3
11
 
4
12
  /**
5
13
  * Returns the org id the platform admin is currently impersonating, or
@@ -45,7 +53,7 @@ declare class BaseClient {
45
53
  put<T>(path: string, options?: RequestOptions): Promise<T>;
46
54
  patch<T>(path: string, options?: RequestOptions): Promise<T>;
47
55
  delete<T>(path: string, options?: RequestOptions): Promise<T>;
48
- request<T>(method: string, path: string, options?: RequestOptions): Promise<T>;
56
+ request<T>(method: string, path: string, options?: RequestOptions, retrying?: boolean): Promise<T>;
49
57
  private buildUrl;
50
58
  }
51
59
 
package/dist/index.js CHANGED
@@ -59,7 +59,7 @@ var BaseClient = class {
59
59
  delete(path, options) {
60
60
  return this.request("DELETE", path, options);
61
61
  }
62
- async request(method, path, options = {}) {
62
+ async request(method, path, options = {}, retrying = false) {
63
63
  const url = this.buildUrl(path, options.query);
64
64
  const headers = { ...this.defaultHeaders, ...options.headers ?? {} };
65
65
  if (!options.skipAuth && this.getToken) {
@@ -98,11 +98,15 @@ var BaseClient = class {
98
98
  console.debug("[flashbacktech/tsclient] \u2190", res.status, url, parsed);
99
99
  }
100
100
  if (!res.ok) {
101
- if (res.status === 401 && this.onUnauthorized) {
101
+ if (res.status === 401 && this.onUnauthorized && !options.skipAuth && !retrying) {
102
+ let refreshed = false;
102
103
  try {
103
- await this.onUnauthorized();
104
+ refreshed = await this.onUnauthorized() !== false;
104
105
  } catch {
105
106
  }
107
+ if (refreshed) {
108
+ return this.request(method, path, options, true);
109
+ }
106
110
  }
107
111
  throw new HttpError(res.status, res.statusText, parsed);
108
112
  }