@gandalan/weblibs 1.0.12 → 1.0.13

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