@gandalan/weblibs 0.0.36 → 0.0.38

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 -9
  2. package/package.json +6 -6
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,16 @@ export class RESTClient {
26
27
  });
27
28
  }
28
29
 
29
- async get(uri) {
30
+ updateToken(token) {
31
+ this.token = token;
32
+ }
33
+
34
+ async get(uri, noJWT = false) {
30
35
  try {
31
- const response = await axios.get(this.baseurl + uri, { withCredentials: false });
36
+ let options = { withCredentials: false }
37
+ if (this.isJWT && !noJWT)
38
+ options.headers = { Authorization: `Bearer ${this.token}` }
39
+ const response = await axios.get(this.baseurl + uri, options);
32
40
  this.lastError = '';
33
41
  return response.data;
34
42
  } catch (error) {
@@ -36,6 +44,7 @@ export class RESTClient {
36
44
  }
37
45
  }
38
46
 
47
+
39
48
  async getFile(uri) {
40
49
  try {
41
50
  const response = await axios.get(this.baseurl + uri, { responseType: 'blob' });
@@ -54,7 +63,10 @@ export class RESTClient {
54
63
  async getRaw(uri) {
55
64
  let response = {};
56
65
  try {
57
- response = await axios.get(this.baseurl + uri, { withCredentials: false })
66
+ let options = { withCredentials: false }
67
+ if (this.isJWT)
68
+ options.headers = { Authorization: `Bearer ${this.token}` }
69
+ response = await axios.get(this.baseurl + uri, options)
58
70
  this.lastError = '';
59
71
  } catch (error) {
60
72
  this.handleError(error);
@@ -64,7 +76,10 @@ export class RESTClient {
64
76
 
65
77
  async post(uri, formData) {
66
78
  try {
67
- const response = await axios.post(this.baseurl + uri, formData, { withCredentials: false });
79
+ let options = { withCredentials: false }
80
+ if (this.isJWT)
81
+ options.headers = { Authorization: `Bearer ${this.token}` }
82
+ const response = await axios.post(this.baseurl + uri, formData, options);
68
83
  this.lastError = '';
69
84
  return response;
70
85
  } catch (error) {
@@ -74,7 +89,10 @@ export class RESTClient {
74
89
 
75
90
  async put(uri, formData) {
76
91
  try {
77
- const response = await axios.put(this.baseurl + uri, formData, { withCredentials: false });
92
+ let options = { withCredentials: false }
93
+ if (this.isJWT)
94
+ options.headers = { Authorization: `Bearer ${this.token}` }
95
+ const response = await axios.put(this.baseurl + uri, formData, options);
78
96
  this.lastError = '';
79
97
  return response;
80
98
  } catch (error) {
@@ -84,7 +102,10 @@ export class RESTClient {
84
102
 
85
103
  async delete(uri) {
86
104
  try {
87
- const response = await axios.delete(this.baseurl + uri, { withCredentials: false });
105
+ let options = { withCredentials: false }
106
+ if (this.isJWT)
107
+ options.headers = { Authorization: `Bearer ${this.token}` }
108
+ const response = await axios.delete(this.baseurl + uri, options);
88
109
  this.lastError = '';
89
110
  return response;
90
111
  } catch (error) {
@@ -93,7 +114,7 @@ export class RESTClient {
93
114
  }
94
115
 
95
116
  // eslint-disable-next-line no-unused-vars
96
- onError = (error, message) => {};
117
+ onError = (error, message) => { };
97
118
 
98
119
  handleError(error) {
99
120
  let message = error ? error.message : '?';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gandalan/weblibs",
3
- "version": "0.0.36",
3
+ "version": "0.0.38",
4
4
  "description": "WebLibs for Gandalan JS/TS/Svelte projects",
5
5
  "author": "Philipp Reif",
6
6
  "license": "ISC",
@@ -12,18 +12,18 @@
12
12
  "lint": "eslint ."
13
13
  },
14
14
  "devDependencies": {
15
- "@babel/eslint-parser": "^7.18.9",
15
+ "@babel/eslint-parser": "^7.19.1",
16
16
  "chota": "^0.8.0",
17
- "eslint": "^8.21.0",
17
+ "eslint": "^8.23.1",
18
18
  "eslint-plugin-svelte3": "^4.0.0",
19
- "svelte": "^3.49.0",
20
- "svelte-check": "^2.8.0",
19
+ "svelte": "^3.50.1",
20
+ "svelte-check": "^2.9.0",
21
21
  "svelte-chota": "^1.8.6"
22
22
  },
23
23
  "dependencies": {
24
24
  "@mdi/js": "^7.0.96",
25
25
  "axios": "^0.27.2",
26
- "svelte-table": "^0.5.0"
26
+ "svelte-table": "^0.5.1"
27
27
  },
28
28
  "peerDependencies": {
29
29
  "svelte-chota": "^1.8.6"