@gandalan/weblibs 1.1.51 → 1.1.52

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
@@ -29,9 +29,9 @@ module.exports = {
29
29
  quotes: ["warn", "double"],
30
30
  semi: ["off", "never"],
31
31
  "no-multi-spaces": ["warn", { ignoreEOLComments: true }],
32
- "curly": "warn",
32
+ "curly": "off",
33
33
  "comma-spacing": "warn",
34
- "brace-style": ["warn"],
34
+ "brace-style": ["off"],
35
35
  "no-var": "warn",
36
36
  "key-spacing": "warn",
37
37
  "keyword-spacing": "warn",
@@ -42,7 +42,7 @@ module.exports = {
42
42
  "no-unused-vars": ["warn", {
43
43
  "args": "none",
44
44
  }],
45
- "no-console": "warn",
45
+ "no-console": "off",
46
46
  "no-extra-boolean-cast": "off",
47
47
  "no-multiple-empty-lines": ["warn", { "max": 1, "maxBOF": 0 }],
48
48
  "lines-between-class-members": ["warn", "always", { exceptAfterSingleLine: true }],
@@ -52,6 +52,6 @@ module.exports = {
52
52
  "eol-last": ["warn", "always"],
53
53
  "prefer-template": "warn",
54
54
  "template-curly-spacing": ["warn", "never"],
55
- "comma-dangle": ["warn", "always-multiline"],
56
- },
55
+ "comma-dangle": ["off"],
56
+ }
57
57
  };
package/api/fluentApi.js CHANGED
@@ -2,6 +2,8 @@ import { jwtDecode } from "jwt-decode";
2
2
 
3
3
  const envs = {};
4
4
 
5
+ const JWT_SAFE_RENEWAL = 30; // seconds before token expiry to renew
6
+
5
7
  export async function fetchEnv(env = "") {
6
8
  if (!(env in envs)) {
7
9
  const hubUrl = `https://connect.idas-cloudservices.net/api/Endpoints?env=${env}`;
@@ -25,7 +27,7 @@ export function isTokenValid(token)
25
27
  const decoded = jwtDecode(token);
26
28
  if (!decoded)
27
29
  throw new Error("Invalid token");
28
- return (decoded.exp - 10 > Date.now() / 1000);
30
+ return (decoded.exp - JWT_SAFE_RENEWAL > Date.now() / 1000);
29
31
  }
30
32
  catch {
31
33
  return false;
@@ -42,12 +44,15 @@ export function authBuilder() {
42
44
  useAppToken(appToken = "") {
43
45
  this.appToken = appToken; return this;
44
46
  },
47
+
45
48
  useBaseUrl(authUrl = "") {
46
49
  this.authUrl = authUrl; return this;
47
50
  },
51
+
48
52
  useToken(jwtToken = "") {
49
53
  this.token = jwtToken; return this;
50
54
  },
55
+
51
56
  useRefreshToken(storedRefreshToken = "") {
52
57
  this.refreshToken = storedRefreshToken; return this;
53
58
  },
@@ -123,28 +128,25 @@ export function restClient()
123
128
  const res = await fetch(finalUrl, { method: "GET", headers });
124
129
  if (res.ok)
125
130
  return await res.json();
126
- else
127
- throw new Error(`GET ${finalUrl} failed: ${res.status} ${res.statusText}`);
131
+ throw new Error(`GET ${finalUrl} failed: ${res.status} ${res.statusText}`);
128
132
  },
129
133
 
130
134
  async put(url = "", payload = {}) {
131
135
  const finalUrl = `${this.baseUrl}/${url}`;
132
- const headers = this.token ? { "Authorization": `Bearer ${this.token}`, "Content-Type" : "application/json" } : {};
136
+ const headers = this.token ? { "Authorization": `Bearer ${this.token}`, "Content-Type": "application/json" } : {};
133
137
  const res = await fetch(finalUrl, { method: "PUT", body: JSON.stringify(payload), headers });
134
138
  if (res.ok)
135
139
  return await res.json();
136
- else
137
- throw new Error(`PUT ${finalUrl} failed: ${res.status} ${res.statusText}`);
140
+ throw new Error(`PUT ${finalUrl} failed: ${res.status} ${res.statusText}`);
138
141
  },
139
142
 
140
143
  async post(url = "", payload = {}) {
141
144
  const finalUrl = `${this.baseUrl}/${url}`;
142
- const headers = this.token ? { "Authorization": `Bearer ${this.token}`, "Content-Type" : "application/json" } : {};
145
+ const headers = this.token ? { "Authorization": `Bearer ${this.token}`, "Content-Type": "application/json" } : {};
143
146
  const res = await fetch(finalUrl, { method: "POST", body: JSON.stringify(payload), headers });
144
147
  if (res.ok)
145
148
  return await res.json();
146
- else
147
- throw new Error(`POST ${finalUrl} failed: ${res.status} ${res.statusText}`);
149
+ throw new Error(`POST ${finalUrl} failed: ${res.status} ${res.statusText}`);
148
150
  },
149
151
 
150
152
  async delete(url = "") {
@@ -153,8 +155,7 @@ export function restClient()
153
155
  const res = await fetch(finalUrl, { method: "DELETE", headers });
154
156
  if (res.ok)
155
157
  return await res.json();
156
- else
157
- throw new Error(`DELETE ${finalUrl} failed: ${res.status} ${res.statusText}`);
158
+ throw new Error(`DELETE ${finalUrl} failed: ${res.status} ${res.statusText}`);
158
159
  }
159
160
  }
160
161
  }
@@ -172,21 +173,27 @@ export function api() {
172
173
  useEnvironment(env = "") {
173
174
  this.env = env; return this;
174
175
  },
176
+
175
177
  useAppToken(newApptoken = "") {
176
178
  this.appToken = newApptoken; return this;
177
179
  },
180
+
178
181
  useBaseUrl(url = "") {
179
182
  this.baseUrl = url; return this;
180
183
  },
184
+
181
185
  useAuthUrl(url = "") {
182
186
  this.authUrl = url; return this;
183
187
  },
188
+
184
189
  useToken(jwtToken = "") {
185
190
  this.token = jwtToken; return this;
186
191
  },
192
+
187
193
  useRefreshToken(storedRefreshToken = "") {
188
194
  this.refreshToken = storedRefreshToken; return this;
189
195
  },
196
+
190
197
  useGlobalAuth() {
191
198
  // eslint-disable-next-line no-undef
192
199
  this.token = globalThis.idasTokens.token; this.refreshToken = globalThis.idasTokens.refreshToken; return this;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gandalan/weblibs",
3
- "version": "1.1.51",
3
+ "version": "1.1.52",
4
4
  "description": "WebLibs for Gandalan JS/TS/Svelte projects",
5
5
  "keywords": [
6
6
  "gandalan"