@gandalan/weblibs 1.1.49 → 1.1.51

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.
package/.eslintrc.cjs CHANGED
@@ -37,7 +37,7 @@ module.exports = {
37
37
  "keyword-spacing": "warn",
38
38
  "space-infix-ops": "warn",
39
39
  "arrow-spacing": "warn",
40
- "no-trailing-spaces": "warn",
40
+ "no-trailing-spaces": "off",
41
41
  "space-before-blocks": "warn",
42
42
  "no-unused-vars": ["warn", {
43
43
  "args": "none",
package/api/fluentApi.js CHANGED
@@ -5,6 +5,7 @@ const envs = {};
5
5
  export async function fetchEnv(env = "") {
6
6
  if (!(env in envs)) {
7
7
  const hubUrl = `https://connect.idas-cloudservices.net/api/Endpoints?env=${env}`;
8
+ console.log("fetching env", hubUrl);
8
9
  const r = await fetch(hubUrl);
9
10
  const data = await r.json();
10
11
  envs[env] = data;
@@ -17,6 +18,20 @@ export function getRefreshToken(token) {
17
18
  return decoded.refreshToken;
18
19
  }
19
20
 
21
+ export function isTokenValid(token)
22
+ {
23
+ try
24
+ {
25
+ const decoded = jwtDecode(token);
26
+ if (!decoded)
27
+ throw new Error("Invalid token");
28
+ return (decoded.exp - 10 > Date.now() / 1000);
29
+ }
30
+ catch {
31
+ return false;
32
+ }
33
+ }
34
+
20
35
  export function authBuilder() {
21
36
  return {
22
37
  authUrl: "",
@@ -37,28 +52,15 @@ export function authBuilder() {
37
52
  this.refreshToken = storedRefreshToken; return this;
38
53
  },
39
54
 
40
- async authenticate(username = "", password = "") {
55
+ async authenticate(username = "", password = "")
56
+ {
41
57
  console.log("authenticating:", this.token ? `token set, exp: ${jwtDecode(this.token).exp - (Date.now() / 1000)}` : "no token,", this.refreshToken);
42
58
 
43
- if (this.token) {
44
- try {
45
- const decoded = jwtDecode(this.token);
46
- if (!decoded) {
47
- throw new Error("Invalid token");
48
- }
49
-
50
- if (decoded.exp - 60 > Date.now() / 1000) {
51
- return this.token;
52
- }
59
+ if (this.token && isTokenValid(this.token))
60
+ return this.token;
53
61
 
54
- if (decoded.refreshToken && !this.refreshToken) {
55
- this.refreshToken = decoded.refreshToken;
56
- }
57
- } catch (e) {
58
- console.error("Error decoding token", e);
59
- return null;
60
- }
61
- }
62
+ if (this.token && !this.refreshToken)
63
+ this.refreshToken = getRefreshToken(this.token);
62
64
 
63
65
  if (this.refreshToken) {
64
66
  try {
@@ -72,7 +74,6 @@ export function authBuilder() {
72
74
  // - should still be valid for a while
73
75
  }
74
76
  return this.token;
75
-
76
77
  }
77
78
 
78
79
  if (username && password) {
@@ -90,7 +91,7 @@ export function authBuilder() {
90
91
  },
91
92
 
92
93
  async tryRefreshToken(refreshToken = "") {
93
- const payload = { "Token": refreshToken };
94
+ const payload = { "Token": refreshToken };
94
95
  const res = await fetch(`${this.authUrl}/LoginJwt/Refresh`,
95
96
  {
96
97
  method: "PUT",
@@ -102,6 +103,62 @@ export function authBuilder() {
102
103
  }
103
104
  }
104
105
 
106
+ export function restClient()
107
+ {
108
+ return {
109
+ baseUrl: "",
110
+ token: "",
111
+
112
+ useBaseUrl(url = "") {
113
+ this.baseUrl = url; return this;
114
+ },
115
+
116
+ useToken(jwtToken = "") {
117
+ this.token = jwtToken; return this;
118
+ },
119
+
120
+ async get(url = "", auth = true) {
121
+ const finalUrl = `${this.baseUrl}/${url}`;
122
+ const headers = this.token ? { "Authorization": `Bearer ${this.token}` } : {};
123
+ const res = await fetch(finalUrl, { method: "GET", headers });
124
+ if (res.ok)
125
+ return await res.json();
126
+ else
127
+ throw new Error(`GET ${finalUrl} failed: ${res.status} ${res.statusText}`);
128
+ },
129
+
130
+ async put(url = "", payload = {}) {
131
+ const finalUrl = `${this.baseUrl}/${url}`;
132
+ const headers = this.token ? { "Authorization": `Bearer ${this.token}`, "Content-Type" : "application/json" } : {};
133
+ const res = await fetch(finalUrl, { method: "PUT", body: JSON.stringify(payload), headers });
134
+ if (res.ok)
135
+ return await res.json();
136
+ else
137
+ throw new Error(`PUT ${finalUrl} failed: ${res.status} ${res.statusText}`);
138
+ },
139
+
140
+ async post(url = "", payload = {}) {
141
+ const finalUrl = `${this.baseUrl}/${url}`;
142
+ const headers = this.token ? { "Authorization": `Bearer ${this.token}`, "Content-Type" : "application/json" } : {};
143
+ const res = await fetch(finalUrl, { method: "POST", body: JSON.stringify(payload), headers });
144
+ if (res.ok)
145
+ return await res.json();
146
+ else
147
+ throw new Error(`POST ${finalUrl} failed: ${res.status} ${res.statusText}`);
148
+ },
149
+
150
+ async delete(url = "") {
151
+ const finalUrl = `${this.baseUrl}/${url}`;
152
+ const headers = this.token ? { "Authorization": `Bearer ${this.token}` } : {};
153
+ const res = await fetch(finalUrl, { method: "DELETE", headers });
154
+ if (res.ok)
155
+ return await res.json();
156
+ else
157
+ throw new Error(`DELETE ${finalUrl} failed: ${res.status} ${res.statusText}`);
158
+ }
159
+ }
160
+ }
161
+
105
162
  export function api() {
106
163
  return {
107
164
  baseUrl: "",
@@ -132,57 +189,45 @@ export function api() {
132
189
  },
133
190
  useGlobalAuth() {
134
191
  // eslint-disable-next-line no-undef
135
- this.token = globalThis.idas.token; this.refreshToken = globalThis.idas.refreshToken; return this;
192
+ this.token = globalThis.idasTokens.token; this.refreshToken = globalThis.idasTokens.refreshToken; return this;
136
193
  },
137
194
 
138
- async get(url = "") {
139
- console.log("get", url);
140
- await this.ensureAuthenticated();
141
- const headers = this.token ? { "Authorization": `Bearer ${this.token}` } : {};
142
- const res = await fetch(`${this.baseUrl}/${url}`, { method: "GET", headers });
143
- return await res.json();
195
+ async get(url = "", auth = true) {
196
+ if (auth)
197
+ await this.ensureAuthenticated();
198
+ return restClient().useBaseUrl(this.baseUrl).useToken(this.token).get(url);
144
199
  },
145
200
 
146
- async put(url = "", payload = {}) {
147
- console.log("put", url, payload);
148
- await this.ensureAuthenticated();
149
-
150
- const headers = this.token ? { "Authorization": `Bearer ${this.token}`, "Content-Type": "application/json" } : {};
151
- const res = await fetch(`${this.baseUrl}/${url}`,
152
- { method: "PUT", body: JSON.stringify(payload), headers });
153
- return await res.json();
201
+ async put(url = "", payload = {}, auth = true) {
202
+ if (auth)
203
+ await this.ensureAuthenticated();
204
+ return restClient().useBaseUrl(this.baseUrl).useToken(this.token).put(url, payload);
154
205
  },
155
206
 
156
- async post(url = "", payload = {}) {
157
- console.log("post", url, payload);
158
- await this.ensureAuthenticated();
159
-
160
- const headers = this.token ? { "Authorization": `Bearer ${this.token}`, "Content-Type": "application/json" } : {};
161
- const res = await fetch(`${this.baseUrl}/${url}`,
162
- { method: "POST", body: JSON.stringify(payload), headers });
163
- return await res.json();
207
+ async post(url = "", payload = {}, auth = true) {
208
+ if (auth)
209
+ await this.ensureAuthenticated();
210
+ return restClient().useBaseUrl(this.baseUrl).useToken(this.token).post(url, payload);
164
211
  },
165
212
 
166
- async delete(url = "") {
167
- console.log("delete", url);
168
- await this.ensureAuthenticated();
169
-
170
- const headers = this.token ? { "Authorization": `Bearer ${this.token}` } : {};
171
- const res = await fetch(`${this.baseUrl}/${url}`, { method: "DELETE", headers });
172
- return await res.json();
213
+ async delete(url = "", auth = true) {
214
+ if (auth)
215
+ await this.ensureAuthenticated();
216
+ return restClient().useBaseUrl(this.baseUrl).useToken(this.token).delete(url);
173
217
  },
174
218
 
175
219
  async ensureAuthenticated() {
176
- await this.ensureBaseUrlIsSet();
177
- if (!this.token && !this.refreshToken) {
220
+ if (this.token && isTokenValid(this.token))
178
221
  return;
179
- }
222
+
223
+ await this.ensureBaseUrlIsSet();
224
+
180
225
  try {
181
226
  const temptoken = await authBuilder()
182
- .useToken(this.token)
183
- .useRefreshToken(this.refreshToken)
184
227
  .useAppToken(this.appToken)
185
228
  .useBaseUrl(this.authUrl)
229
+ .useToken(this.token)
230
+ .useRefreshToken(this.refreshToken)
186
231
  .authenticate() || "";
187
232
 
188
233
  if (!temptoken) {
@@ -202,15 +247,20 @@ export function api() {
202
247
  },
203
248
 
204
249
  async ensureBaseUrlIsSet() {
205
- if (this.env && !this.baseUrl) {
250
+ if (this.env && (!this.baseUrl || !this.authUrl)) {
206
251
  const envInfo = await fetchEnv(this.env);
207
- this.baseUrl = envInfo.idas;
208
- this.authUrl = envInfo.idas;
252
+ this.baseUrl = this.baseUrl || envInfo.idas;
253
+ this.authUrl = this.authUrl || envInfo.idas;
254
+ console.log("envInfo", envInfo);
209
255
  }
210
256
 
211
257
  if (!this.baseUrl) {
212
258
  throw new Error("apiBaseurl not set");
213
259
  }
260
+
261
+ if (!this.authUrl) {
262
+ throw new Error("authUrl not set");
263
+ }
214
264
  },
215
265
 
216
266
  redirectToLogin(authPath = "") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gandalan/weblibs",
3
- "version": "1.1.49",
3
+ "version": "1.1.51",
4
4
  "description": "WebLibs for Gandalan JS/TS/Svelte projects",
5
5
  "keywords": [
6
6
  "gandalan"