@gandalan/weblibs 0.0.37 → 0.0.39

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 +26 -13
  2. package/package.json +1 -1
package/api/RESTClient.js CHANGED
@@ -12,12 +12,13 @@ export class RESTClient {
12
12
  token = '';
13
13
  baseurl = '';
14
14
 
15
- constructor(url, token) {
15
+ constructor(url, token, isJWT = false) {
16
16
  this.lastError = '';
17
17
  this.baseurl = url;
18
18
  this.token = token;
19
+ this.isJWT = isJWT;
19
20
 
20
- if (this.token) {
21
+ if (this.token && !isJWT) {
21
22
  axios.defaults.headers.common['X-Gdl-AuthToken'] = this.token;
22
23
  }
23
24
 
@@ -26,9 +27,20 @@ export class RESTClient {
26
27
  });
27
28
  }
28
29
 
29
- async get(uri) {
30
+ updateToken(token) {
31
+ this.token = token;
32
+ }
33
+
34
+ getUrlOptions(noJWT = false) {
35
+ let options = { withCredentials: false }
36
+ if (this.isJWT && !noJWT)
37
+ options.headers = { Authorization: `Bearer ${this.token}` }
38
+ return options
39
+ }
40
+
41
+ async get(uri, noJWT = false) {
30
42
  try {
31
- const response = await axios.get(this.baseurl + uri, { withCredentials: false });
43
+ const response = await axios.get(this.baseurl + uri, this.getUrlOptions(noJWT));
32
44
  this.lastError = '';
33
45
  return response.data;
34
46
  } catch (error) {
@@ -36,6 +48,7 @@ export class RESTClient {
36
48
  }
37
49
  }
38
50
 
51
+
39
52
  async getFile(uri) {
40
53
  try {
41
54
  const response = await axios.get(this.baseurl + uri, { responseType: 'blob' });
@@ -51,10 +64,10 @@ export class RESTClient {
51
64
  }
52
65
  }
53
66
 
54
- async getRaw(uri) {
67
+ async getRaw(uri, noJWT = false) {
55
68
  let response = {};
56
69
  try {
57
- response = await axios.get(this.baseurl + uri, { withCredentials: false })
70
+ response = await axios.get(this.baseurl + uri, this.getUrlOptions(noJWT))
58
71
  this.lastError = '';
59
72
  } catch (error) {
60
73
  this.handleError(error);
@@ -62,9 +75,9 @@ export class RESTClient {
62
75
  return response;
63
76
  }
64
77
 
65
- async post(uri, formData) {
78
+ async post(uri, formData, noJWT = false) {
66
79
  try {
67
- const response = await axios.post(this.baseurl + uri, formData, { withCredentials: false });
80
+ const response = await axios.post(this.baseurl + uri, formData, this.getUrlOptions(noJWT));
68
81
  this.lastError = '';
69
82
  return response;
70
83
  } catch (error) {
@@ -72,9 +85,9 @@ export class RESTClient {
72
85
  }
73
86
  }
74
87
 
75
- async put(uri, formData) {
88
+ async put(uri, formData, noJWT = false) {
76
89
  try {
77
- const response = await axios.put(this.baseurl + uri, formData, { withCredentials: false });
90
+ const response = await axios.put(this.baseurl + uri, formData, this.getUrlOptions(noJWT));
78
91
  this.lastError = '';
79
92
  return response;
80
93
  } catch (error) {
@@ -82,9 +95,9 @@ export class RESTClient {
82
95
  }
83
96
  }
84
97
 
85
- async delete(uri) {
98
+ async delete(uri, noJWT = false) {
86
99
  try {
87
- const response = await axios.delete(this.baseurl + uri, { withCredentials: false });
100
+ const response = await axios.delete(this.baseurl + uri, this.getUrlOptions(noJWT));
88
101
  this.lastError = '';
89
102
  return response;
90
103
  } catch (error) {
@@ -93,7 +106,7 @@ export class RESTClient {
93
106
  }
94
107
 
95
108
  // eslint-disable-next-line no-unused-vars
96
- onError = (error, message) => {};
109
+ onError = (error, message) => { };
97
110
 
98
111
  handleError(error) {
99
112
  let message = error ? error.message : '?';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gandalan/weblibs",
3
- "version": "0.0.37",
3
+ "version": "0.0.39",
4
4
  "description": "WebLibs for Gandalan JS/TS/Svelte projects",
5
5
  "author": "Philipp Reif",
6
6
  "license": "ISC",