@gandalan/weblibs 1.1.67 → 1.1.68

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,9 @@ export function createApi() {
154
157
  * @return {FluentApi}
155
158
  */
156
159
  useBaseUrl(url = "") {
157
- this.baseUrl = url; return this;
160
+ this.baseUrl = url;
161
+ console.log("url!!!");
162
+ return this;
158
163
  },
159
164
 
160
165
  /**
@@ -270,12 +275,10 @@ export function createApi() {
270
275
  if (this.token && isTokenValid(this.token))
271
276
  return;
272
277
 
273
- await this.ensureBaseUrlIsSet();
274
-
275
278
  try {
276
279
  const temptoken = await authBuilder()
277
280
  .useAppToken(this.appToken)
278
- .useBaseUrl(this.authUrl)
281
+ .useBaseUrl(this.authUrl || this.env.idas)
279
282
  .useToken(this.token)
280
283
  .useRefreshToken(this.refreshToken)
281
284
  .authenticate() || "";
@@ -297,30 +300,6 @@ export function createApi() {
297
300
  //this.redirectToLogin();
298
301
  console.error("not authenticated", e);
299
302
  }
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
303
  }
325
304
  };
326
305
  }
@@ -335,8 +314,7 @@ export function createApi() {
335
314
  export function idasApi(appToken = "") {
336
315
  return createApi()
337
316
  .useGlobalAuth()
338
- .useAppToken(appToken)
339
- .useEnvironment("dev");
317
+ .useAppToken(appToken);
340
318
  }
341
319
 
342
320
  /**
@@ -348,6 +326,5 @@ export function idasApi(appToken = "") {
348
326
  export function localApi() {
349
327
  return createApi()
350
328
  .useGlobalAuth()
351
- .useBaseUrl("/api")
352
- .useEnvironment("dev");
329
+ .useBaseUrl("api");
353
330
  }
@@ -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.68",
4
4
  "description": "WebLibs for Gandalan JS/TS/Svelte projects",
5
5
  "keywords": [
6
6
  "gandalan"