@clairejs/client 3.4.5 → 3.4.8

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/README.md CHANGED
@@ -1,5 +1,14 @@
1
1
  ## Change Log
2
2
 
3
+ #### 3.4.8:
4
+
5
+ - refactor http client
6
+
7
+ #### 3.4.6:
8
+
9
+ - update RouterConfig to allow arbitrary field
10
+ - update packages
11
+
3
12
  #### 3.4.5:
4
13
 
5
14
  - fix Translator awaitingTranslations
@@ -9,7 +9,7 @@ export interface RequestData {
9
9
  headers?: object;
10
10
  options?: RequestOptions;
11
11
  }
12
- export declare class DefaultHttpClient extends AbstractHttpClient {
12
+ export declare abstract class DefaultHttpClient extends AbstractHttpClient {
13
13
  protected readonly apiServerUrl: string;
14
14
  protected readonly logger?: LogHandler | undefined;
15
15
  protected readonly maxRetryCount: number;
@@ -18,7 +18,7 @@ export declare class DefaultHttpClient extends AbstractHttpClient {
18
18
  private readonly api;
19
19
  constructor(apiServerUrl: string, logger?: LogHandler | undefined, maxRetryCount?: number, delayMsBetweenRetry?: number, storage?: AbstractStorage | undefined);
20
20
  protected resolveUrl(url: string): Promise<string>;
21
- protected getAuthorization(): Promise<string>;
21
+ protected abstract getAuthorizationHeader(): Promise<Record<string, string>>;
22
22
  protected errorHandler<T = any>(_operation: () => Promise<T>, err: any): Promise<T | undefined>;
23
23
  protected retry<T = any>(apiCall: () => Promise<T>, retryCount?: number): Promise<T | undefined>;
24
24
  protected performRequest<T = any>(data: RequestData): Promise<T | undefined>;
@@ -53,9 +53,6 @@ export class DefaultHttpClient extends AbstractHttpClient {
53
53
  async resolveUrl(url) {
54
54
  return this.apiServerUrl + url;
55
55
  }
56
- async getAuthorization() {
57
- return "";
58
- }
59
56
  async errorHandler(_operation, err) {
60
57
  throw err;
61
58
  }
@@ -79,7 +76,7 @@ export class DefaultHttpClient extends AbstractHttpClient {
79
76
  async performRequest(data) {
80
77
  const finalUrl = await this.resolveUrl(data.url);
81
78
  const operation = async () => {
82
- const authHeader = !data.options?.noAuthorization ? { authorization: await this.getAuthorization() } : {};
79
+ const authHeader = !data.options?.noAuthorization ? await this.getAuthorizationHeader() : {};
83
80
  const result = await this.api({
84
81
  method: data.method,
85
82
  url: finalUrl,
@@ -2,7 +2,7 @@ import { AccessToken, LogHandler } from "@clairejs/core";
2
2
  import { AbstractStorage } from "../system/AbstractStorage";
3
3
  import { AbstractTokenManager } from "./AbstractTokenManager";
4
4
  import { DefaultHttpClient } from "./DefaultHttpClient";
5
- export declare class RefreshHttpClient extends DefaultHttpClient {
5
+ export declare abstract class RefreshHttpClient extends DefaultHttpClient {
6
6
  protected readonly apiServerUrl: string;
7
7
  protected readonly tokenManager: AbstractTokenManager;
8
8
  protected readonly logger?: LogHandler | undefined;
@@ -13,9 +13,8 @@ export declare class RefreshHttpClient extends DefaultHttpClient {
13
13
  private refreshQueue;
14
14
  private tokenQueue;
15
15
  constructor(apiServerUrl: string, tokenManager: AbstractTokenManager, logger?: LogHandler | undefined, maxRetryCount?: number, delayMsBetweenRetry?: number, storage?: AbstractStorage | undefined);
16
- protected getRefreshedAccessToken(): Promise<AccessToken>;
17
- protected reauthenticate(): Promise<void>;
18
16
  protected getAuthorization(): Promise<string>;
17
+ protected abstract getRefreshedAccessToken(): Promise<AccessToken>;
18
+ protected getAuthorizationHeader(): Promise<Record<string, string>>;
19
19
  protected refreshToken(token?: AccessToken): Promise<void>;
20
- protected errorHandler<T = any>(operation: () => Promise<T>, err: any): Promise<T | undefined>;
21
20
  }
@@ -1,7 +1,5 @@
1
1
  import { Errors, LogLevel } from "@clairejs/core";
2
2
  import { DefaultHttpClient } from "./DefaultHttpClient";
3
- const tokenExpiredError = Errors.TOKEN_EXPIRED().name;
4
- const invalidTokenError = Errors.INVALID_TOKEN().name;
5
3
  export class RefreshHttpClient extends DefaultHttpClient {
6
4
  apiServerUrl;
7
5
  tokenManager;
@@ -21,12 +19,6 @@ export class RefreshHttpClient extends DefaultHttpClient {
21
19
  this.delayMsBetweenRetry = delayMsBetweenRetry;
22
20
  this.storage = storage;
23
21
  }
24
- async getRefreshedAccessToken() {
25
- throw Errors.INVALID_TOKEN();
26
- }
27
- async reauthenticate() {
28
- throw Errors.INVALID_CREDENTIALS();
29
- }
30
22
  async getAuthorization() {
31
23
  if (this.refreshing) {
32
24
  const promise = new Promise((resolver) => {
@@ -39,6 +31,9 @@ export class RefreshHttpClient extends DefaultHttpClient {
39
31
  return accessToken?.token || "";
40
32
  }
41
33
  }
34
+ async getAuthorizationHeader() {
35
+ return { authorization: await this.getAuthorization() };
36
+ }
42
37
  async refreshToken(token) {
43
38
  if (this.refreshing) {
44
39
  this.logger?.log(LogLevel.DEBUG, "Awaiting refresh token, queued");
@@ -50,7 +45,7 @@ export class RefreshHttpClient extends DefaultHttpClient {
50
45
  //-- call to api server to refresh token
51
46
  if (!token || !token.refreshToken) {
52
47
  //-- there is no refresh token to refresh
53
- return await this.reauthenticate();
48
+ throw Errors.SESSION_EXPIRED();
54
49
  }
55
50
  try {
56
51
  this.refreshing = true;
@@ -71,29 +66,4 @@ export class RefreshHttpClient extends DefaultHttpClient {
71
66
  this.tokenQueue = [];
72
67
  }
73
68
  }
74
- async errorHandler(operation, err) {
75
- const errorName = err.name;
76
- if (errorName === tokenExpiredError) {
77
- return await this.refreshToken()
78
- .catch(() => this.reauthenticate())
79
- .catch(() => {
80
- throw Errors.SESSION_EXPIRED();
81
- })
82
- .then(() => operation())
83
- .catch((err) => {
84
- throw err.response?.data || Errors.HTTP_REQUEST_ERROR();
85
- });
86
- }
87
- else if (errorName === invalidTokenError) {
88
- return await this.reauthenticate()
89
- .catch(() => {
90
- throw Errors.SESSION_EXPIRED();
91
- })
92
- .then(() => operation())
93
- .catch((err) => {
94
- throw err.response?.data || Errors.HTTP_REQUEST_ERROR();
95
- });
96
- }
97
- throw err;
98
- }
99
69
  }
@@ -10,4 +10,5 @@ export interface RouterConfig {
10
10
  middleware?: AbstractConstructor<AbstractViewMiddleware>[];
11
11
  children?: RouterConfig[];
12
12
  redirect?: string;
13
+ [key: string]: any;
13
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clairejs/client",
3
- "version": "3.4.5",
3
+ "version": "3.4.8",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -13,7 +13,7 @@
13
13
  "axios": "^1.3.4"
14
14
  },
15
15
  "peerDependencies": {
16
- "@clairejs/core": "^3.8.4"
16
+ "@clairejs/core": "^3.8.10"
17
17
  },
18
18
  "devDependencies": {
19
19
  "@types/mocha": "^10.0.1",