@gandalan/weblibs 1.0.12 → 1.0.14

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.
Files changed (2) hide show
  1. package/api/RESTClient.js +30 -39
  2. package/package.json +1 -1
package/api/RESTClient.js CHANGED
@@ -1,49 +1,40 @@
1
1
  import axios from "axios";
2
- import { jwtTokenInvalid, jwtTokenRenew } from './authUtils';
2
+ import { jwtTokenInvalid, jwtTokenRenew } from "./authUtils";
3
3
 
4
4
  export class RESTClient {
5
5
  lastError = "";
6
6
  settings = {};
7
+ axiosInstance = null;
7
8
 
8
- constructor(settings)
9
- {
9
+ constructor(settings) {
10
10
  this.settings = settings;
11
- axios.defaults.baseURL = settings.apiBaseurl;
12
- if (settings.jwtToken)
13
- {
14
- axios.defaults.headers.common["Authorization"] = "Bearer " + this.token;
15
- axios.interceptors.request.use(async (config) => {
16
- await this.checkTokenBeforeRequest(config);
17
- return config;
18
- });
19
- } else {
20
- // might contain the classic token for fallback
21
- axios.defaults.headers.common["X-Gdl-AuthToken"] = settings.jwtRefreshToken;
22
- }
11
+
12
+ this.axiosInstance = axios.create({
13
+ baseURL: settings.apiBaseurl,
14
+ headers: {
15
+ "Authorization" : `Bearer ${ settings.jwtToken }`
16
+ }
17
+ });
18
+
19
+ this.axiosInstance.interceptors.request.use(async (config) => {
20
+ await this.checkTokenBeforeRequest(config);
21
+ return config;
22
+ });
23
23
  }
24
24
 
25
25
  async checkTokenBeforeRequest(config) {
26
- let authHeader = config.headers["Authorization"];
27
- if (authHeader && authHeader.toString().startsWith("Bearer ")) // only check requests containing a Bearer token
28
- {
29
- let parts = authHeader.toString().split(" ");
30
- let jwt = parts[1];
31
- if (this.settings.jwtToken === jwt && jwtTokenInvalid(this.settings)) // ignore custom/different JWT tokens
32
- await jwtTokenRenew(this.settings);
26
+ if (this.settings.jwtToken && jwtTokenInvalid(this.settings)) { // ignore custom/different JWT tokens
27
+ await jwtTokenRenew(this.settings);
33
28
  }
34
29
  }
35
30
 
36
- getUrlOptions(noJWT = false) {
37
- let options = { withCredentials: false }
38
- /*if (this.isJWT && !noJWT) {
39
- options.headers = { Authorization: `Bearer ${this.token}` }
40
- }*/
41
- return options;
31
+ getUrlOptions() {
32
+ return { withCredentials: false };
42
33
  }
43
34
 
44
- async get(uri, noJWT = false) {
35
+ async get(uri) {
45
36
  try {
46
- const response = await axios.get(uri, this.getUrlOptions(noJWT));
37
+ const response = await this.axiosInstance.get(uri, this.getUrlOptions());
47
38
  this.lastError = "";
48
39
  return response.data;
49
40
  } catch (error) {
@@ -53,7 +44,7 @@ export class RESTClient {
53
44
 
54
45
  async getFile(uri) {
55
46
  try {
56
- const response = await axios.get(uri, { responseType: "blob" });
47
+ const response = await this.axiosInstance.get(uri, { responseType: "blob" });
57
48
  let fileName = "1000.pdf";
58
49
  if (response.headers["content-disposition"]) {
59
50
  fileName = response.headers["content-disposition"].split(";")[1];
@@ -66,10 +57,10 @@ export class RESTClient {
66
57
  }
67
58
  }
68
59
 
69
- async getRaw(uri, noJWT = false) {
60
+ async getRaw(uri) {
70
61
  let response = {};
71
62
  try {
72
- response = await axios.get(uri, this.getUrlOptions(noJWT))
63
+ response = await this.axiosInstance.get(uri, this.getUrlOptions())
73
64
  this.lastError = "";
74
65
  } catch (error) {
75
66
  this.handleError(error);
@@ -77,9 +68,9 @@ export class RESTClient {
77
68
  return response;
78
69
  }
79
70
 
80
- async post(uri, formData, noJWT = false) {
71
+ async post(uri, formData) {
81
72
  try {
82
- const response = await axios.post(uri, formData, this.getUrlOptions(noJWT));
73
+ const response = await this.axiosInstance.post(uri, formData, this.getUrlOptions());
83
74
  this.lastError = "";
84
75
  return response;
85
76
  } catch (error) {
@@ -87,9 +78,9 @@ export class RESTClient {
87
78
  }
88
79
  }
89
80
 
90
- async put(uri, formData, noJWT = false) {
81
+ async put(uri, formData) {
91
82
  try {
92
- const response = await axios.put(uri, formData, this.getUrlOptions(noJWT));
83
+ const response = await this.axiosInstance.put(uri, formData, this.getUrlOptions());
93
84
  this.lastError = "";
94
85
  return response;
95
86
  } catch (error) {
@@ -97,9 +88,9 @@ export class RESTClient {
97
88
  }
98
89
  }
99
90
 
100
- async delete(uri, noJWT = false) {
91
+ async delete(uri) {
101
92
  try {
102
- const response = await axios.delete(uri, this.getUrlOptions(noJWT));
93
+ const response = await this.axiosInstance.delete(uri, this.getUrlOptions());
103
94
  this.lastError = "";
104
95
  return response;
105
96
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gandalan/weblibs",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
4
4
  "description": "WebLibs for Gandalan JS/TS/Svelte projects",
5
5
  "keywords": [
6
6
  "gandalan"