@gandalan/weblibs 1.1.67 → 1.1.69

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/api/fluentApi.js CHANGED
@@ -96,7 +96,7 @@ export function isTokenValid(token)
96
96
  * @property {string} storageEntry - The storage entry.
97
97
  * @property {string} token - The JWT token for authorization.
98
98
  * @property {string} refreshToken - The refresh token.
99
- * @property {function(string) : FluentApi} useEnvironment - Sets the environment and returns the FluentApi object.
99
+ * @property {function(EnvironmentConfig) : FluentApi} useEnvironment - Sets the environment and returns the FluentApi object.
100
100
  * @property {function(string) : FluentApi} useAppToken - Sets the application token and returns the FluentApi object.
101
101
  * @property {function(string) : FluentApi} useBaseUrl - Sets the base URL for API requests and returns the FluentApi object.
102
102
  * @property {function(string) : FluentApi} useAuthUrl - Sets the authentication URL and returns the FluentApi object.
@@ -130,11 +130,14 @@ export function createApi() {
130
130
  /**
131
131
  * set the environment to use
132
132
  *
133
- * @param {string} env
133
+ * @param {EnvironmentConfig} env
134
134
  * @return {FluentApi}
135
135
  */
136
- useEnvironment(env = "") {
137
- this.env = env; return this;
136
+ useEnvironment(env = {}) {
137
+ this.env = env;
138
+ this.baseUrl = env.idas;
139
+ this.authUrl = env.idas;
140
+ return this;
138
141
  },
139
142
 
140
143
  /**
@@ -154,7 +157,8 @@ export function createApi() {
154
157
  * @return {FluentApi}
155
158
  */
156
159
  useBaseUrl(url = "") {
157
- this.baseUrl = url; return this;
160
+ this.baseUrl = url;
161
+ return this;
158
162
  },
159
163
 
160
164
  /**
@@ -270,12 +274,10 @@ export function createApi() {
270
274
  if (this.token && isTokenValid(this.token))
271
275
  return;
272
276
 
273
- await this.ensureBaseUrlIsSet();
274
-
275
277
  try {
276
278
  const temptoken = await authBuilder()
277
279
  .useAppToken(this.appToken)
278
- .useBaseUrl(this.authUrl)
280
+ .useBaseUrl(this.authUrl || this.env.idas)
279
281
  .useToken(this.token)
280
282
  .useRefreshToken(this.refreshToken)
281
283
  .authenticate() || "";
@@ -297,30 +299,6 @@ export function createApi() {
297
299
  //this.redirectToLogin();
298
300
  console.error("not authenticated", e);
299
301
  }
300
- },
301
-
302
- /**
303
- * Ensure the base URL is set before making a request. If not set,
304
- * retrieve the environment data and set the base URL.
305
- *
306
- * @async
307
- * @private
308
- */
309
- async ensureBaseUrlIsSet() {
310
- if (this.env && (!this.baseUrl || !this.authUrl)) {
311
- const envInfo = await fetchEnv(this.env);
312
- this.baseUrl = this.baseUrl || envInfo.idas;
313
- this.authUrl = this.authUrl || envInfo.idas;
314
- console.log("envInfo", envInfo);
315
- }
316
-
317
- if (!this.baseUrl) {
318
- throw new Error("apiBaseurl not set");
319
- }
320
-
321
- if (!this.authUrl) {
322
- throw new Error("authUrl not set");
323
- }
324
302
  }
325
303
  };
326
304
  }
@@ -335,8 +313,7 @@ export function createApi() {
335
313
  export function idasApi(appToken = "") {
336
314
  return createApi()
337
315
  .useGlobalAuth()
338
- .useAppToken(appToken)
339
- .useEnvironment("dev");
316
+ .useAppToken(appToken);
340
317
  }
341
318
 
342
319
  /**
@@ -348,6 +325,5 @@ export function idasApi(appToken = "") {
348
325
  export function localApi() {
349
326
  return createApi()
350
327
  .useGlobalAuth()
351
- .useBaseUrl("/api")
352
- .useEnvironment("dev");
328
+ .useBaseUrl("api");
353
329
  }
@@ -48,7 +48,7 @@ export function restClient() {
48
48
  * @returns {Promise<any>}
49
49
  */
50
50
  async get(url = "", auth = true) {
51
- const finalUrl = `${this.baseUrl}/${url}`;
51
+ const finalUrl = `${this.baseUrl}${url}`;
52
52
  const headers = this.token ? { "Authorization": `Bearer ${this.token}` } : {};
53
53
  const res = await fetch(finalUrl, { method: "GET", headers });
54
54
  if (res.ok)
@@ -65,7 +65,7 @@ export function restClient() {
65
65
  * @returns {Promise<any>}
66
66
  */
67
67
  async put(url = "", payload = {}) {
68
- const finalUrl = `${this.baseUrl}/${url}`;
68
+ const finalUrl = `${this.baseUrl}${url}`;
69
69
  const headers = this.token ? { "Authorization": `Bearer ${this.token}`, "Content-Type": "application/json" } : {};
70
70
  const res = await fetch(finalUrl, { method: "PUT", body: JSON.stringify(payload), headers });
71
71
  if (res.ok)
@@ -82,7 +82,7 @@ export function restClient() {
82
82
  * @returns {Promise<any>}
83
83
  */
84
84
  async post(url = "", payload = {}) {
85
- const finalUrl = `${this.baseUrl}/${url}`;
85
+ const finalUrl = `${this.baseUrl}${url}`;
86
86
  const headers = this.token ? { "Authorization": `Bearer ${this.token}`, "Content-Type": "application/json" } : {};
87
87
  const res = await fetch(finalUrl, { method: "POST", body: JSON.stringify(payload), headers });
88
88
  if (res.ok)
@@ -98,7 +98,7 @@ export function restClient() {
98
98
  * @returns {Promise<any>}
99
99
  */
100
100
  async delete(url = "") {
101
- const finalUrl = `${this.baseUrl}/${url}`;
101
+ const finalUrl = `${this.baseUrl}${url}`;
102
102
  const headers = this.token ? { "Authorization": `Bearer ${this.token}` } : {};
103
103
  const res = await fetch(finalUrl, { method: "DELETE", headers });
104
104
  if (res.ok)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gandalan/weblibs",
3
- "version": "1.1.67",
3
+ "version": "1.1.69",
4
4
  "description": "WebLibs for Gandalan JS/TS/Svelte projects",
5
5
  "keywords": [
6
6
  "gandalan"