@devopness/sdk-js 2.134.0 → 2.135.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.
@@ -63,4 +63,12 @@ export declare class DevopnessApiClient {
63
63
  constructor(options?: ConfigurationOptions);
64
64
  get accessToken(): string;
65
65
  set accessToken(accessToken: string);
66
+ /**
67
+ *
68
+ * @summary Sets the callback function to be executed when the access token expires.
69
+ * @static
70
+ * @param {function(string)} callback - The callback function to be executed.
71
+ * This function should accept a single string parameter representing the expired access token.
72
+ */
73
+ set onTokenExpired(callback: (accessToken: string) => void);
66
74
  }
@@ -68,6 +68,9 @@ class DevopnessApiClient {
68
68
  this.users = new UserService_1.UserService();
69
69
  this.variables = new VariableService_1.VariableService();
70
70
  this.virtualHosts = new VirtualHostService_1.VirtualHostService();
71
+ this.onTokenExpired = () => {
72
+ // do nothing.
73
+ };
71
74
  }
72
75
  get accessToken() {
73
76
  return ApiBaseService_1.ApiBaseService.accessToken;
@@ -84,5 +87,15 @@ class DevopnessApiClient {
84
87
  }
85
88
  ApiBaseService_1.ApiBaseService.accessToken = accessToken;
86
89
  }
90
+ /**
91
+ *
92
+ * @summary Sets the callback function to be executed when the access token expires.
93
+ * @static
94
+ * @param {function(string)} callback - The callback function to be executed.
95
+ * This function should accept a single string parameter representing the expired access token.
96
+ */
97
+ set onTokenExpired(callback) {
98
+ ApiBaseService_1.ApiBaseService.onTokenExpired = callback;
99
+ }
87
100
  }
88
101
  exports.DevopnessApiClient = DevopnessApiClient;
@@ -11,6 +11,7 @@ export declare class Configuration implements ConfigurationOptions {
11
11
  export declare class ApiBaseService {
12
12
  private api;
13
13
  private static _accessToken;
14
+ private static _onTokenExpired;
14
15
  static configuration: Configuration;
15
16
  private static SDK_VERSION;
16
17
  private defaultAxiosSettings;
@@ -21,6 +22,8 @@ export declare class ApiBaseService {
21
22
  private setupAxiosResponseInterceptors;
22
23
  static get accessToken(): string;
23
24
  static set accessToken(value: string);
25
+ static set onTokenExpired(callback: (accessToken: string) => void);
26
+ protected isTokenExpired(response: AxiosResponse | undefined): boolean;
24
27
  baseURL(): string;
25
28
  protected post<T, B = undefined, R = AxiosResponse<T>>(endpoint: string, data?: B): Promise<R>;
26
29
  protected put<T, B, R = AxiosResponse<T>>(endpoint: string, data?: B): Promise<R>;
@@ -82,6 +82,11 @@ class ApiBaseService {
82
82
  this.api.interceptors.response.use((response) => {
83
83
  return response;
84
84
  }, (error) => {
85
+ if (this.isTokenExpired(error.response)) {
86
+ // access_token is expired
87
+ ApiBaseService._onTokenExpired(ApiBaseService._accessToken);
88
+ return error.response;
89
+ }
85
90
  if (error.response) {
86
91
  // server responded, but with a status code other than 2xx
87
92
  throw new Exceptions_1.ApiError(error);
@@ -102,6 +107,17 @@ class ApiBaseService {
102
107
  static set accessToken(value) {
103
108
  ApiBaseService._accessToken = value;
104
109
  }
110
+ static set onTokenExpired(callback) {
111
+ ApiBaseService._onTokenExpired = callback;
112
+ }
113
+ isTokenExpired(response) {
114
+ var _a, _b;
115
+ if (!ApiBaseService.accessToken) {
116
+ return false;
117
+ }
118
+ const decodedToken = JSON.parse(Buffer.from((_b = (_a = ApiBaseService.accessToken) === null || _a === void 0 ? void 0 : _a.split(".")) === null || _b === void 0 ? void 0 : _b[1], "base64").toString());
119
+ return (response === null || response === void 0 ? void 0 : response.status) === 401 && decodedToken.exp < (new Date().getTime() / 1000);
120
+ }
105
121
  baseURL() {
106
122
  return this.api.defaults.baseURL ? this.api.defaults.baseURL : "";
107
123
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devopness/sdk-js",
3
- "version": "2.134.0",
3
+ "version": "2.135.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },