@iankibetsh/shframework 4.5.6 → 4.5.7

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/dist/library.js CHANGED
@@ -38,7 +38,7 @@ function getItem (key) {
38
38
  function removeItem (key) {
39
39
  return localStorage.removeItem(key)
40
40
  }
41
- var ShStorage = {
41
+ var shStorage = {
42
42
  setItem,
43
43
  getItem,
44
44
  removeItem
@@ -144,21 +144,21 @@ const signOutUser = () => {
144
144
  const logoutApiEndPoint = getShConfig('logoutApiEndpoint', 'auth/logout');
145
145
  console.log(loginUrl, logoutApiEndPoint);
146
146
  shApis.doPost(logoutApiEndPoint).then(res => {
147
- ShStorage.removeItem('access_token');
148
- ShStorage.removeItem('user');
149
- ShStorage.removeItem('last_activity');
147
+ shStorage.removeItem('access_token');
148
+ shStorage.removeItem('user');
149
+ shStorage.removeItem('last_activity');
150
150
  window.location.href = loginUrl;
151
151
  }).catch(ex => {
152
- ShStorage.removeItem('access_token');
153
- ShStorage.removeItem('user');
154
- ShStorage.removeItem('last_activity');
152
+ shStorage.removeItem('access_token');
153
+ shStorage.removeItem('user');
154
+ shStorage.removeItem('last_activity');
155
155
  window.location.href = loginUrl;
156
156
  });
157
157
  };
158
158
 
159
159
 
160
160
  function getShConfig(key = null, def = ''){
161
- const config = ShStorage.getItem('ShConfig') ?? {};
161
+ const config = shStorage.getItem('ShConfig') ?? {};
162
162
  if (key) {
163
163
  return config[key] ?? def
164
164
  }
@@ -296,17 +296,17 @@ function logoutUser(){
296
296
  }
297
297
  }
298
298
  function sessionRestored(){
299
- const timeout = ShStorage.getItem('sessionTimeout') * 60;
300
- const last_activity = ShStorage.getItem('last_activity');
299
+ const timeout = shStorage.getItem('sessionTimeout') * 60;
300
+ const last_activity = shStorage.getItem('last_activity');
301
301
  const pastSeconds = moment__default["default"]().diff(last_activity, 'seconds');
302
- if(!ShStorage.getItem('access_token'))
302
+ if(!shStorage.getItem('access_token'))
303
303
  return false
304
304
  return pastSeconds < timeout
305
305
  }
306
306
  const checkSession = function (isCheking) {
307
- const timeout = ShStorage.getItem('sessionTimeout');
308
- const last_activity = ShStorage.getItem('last_activity');
309
- if (ShStorage.getItem('access_token')) {
307
+ const timeout = shStorage.getItem('sessionTimeout');
308
+ const last_activity = shStorage.getItem('last_activity');
309
+ if (shStorage.getItem('access_token')) {
310
310
  const pastMinutes = moment__default["default"]().diff(last_activity, 'minutes');
311
311
  const pastSeconds = moment__default["default"]().diff(last_activity, 'seconds');
312
312
  if(pastMinutes >= timeout) {
@@ -364,17 +364,17 @@ async function shSwalLogout (seconds = 30) {
364
364
  window.ShConfirmation = null;
365
365
  clearInterval(window.shInterval);
366
366
  const timeNow = moment__default["default"]().toISOString();
367
- ShStorage.setItem('last_activity', timeNow);
367
+ shStorage.setItem('last_activity', timeNow);
368
368
  startSession();
369
369
  }
370
370
  })
371
371
  }
372
372
  function startSession () {
373
373
  const timeNow = moment__default["default"]().toISOString();
374
- const accessToken = ShStorage.getItem('access_token');
374
+ const accessToken = shStorage.getItem('access_token');
375
375
  if (accessToken) {
376
- ShStorage.setItem('last_activity', timeNow);
377
- const timout = ShStorage.getItem('sessionTimeout');
376
+ shStorage.setItem('last_activity', timeNow);
377
+ const timout = shStorage.getItem('sessionTimeout');
378
378
  const interval = (timout * 60 *1000) / 3;
379
379
  window.shInterval = setInterval(()=>{
380
380
  checkSession();
@@ -386,7 +386,7 @@ const updateSession = () =>{
386
386
  startSession();
387
387
  }
388
388
  const timeNow = moment__default["default"]().toISOString();
389
- ShStorage.setItem('last_activity', timeNow);
389
+ shStorage.setItem('last_activity', timeNow);
390
390
  };
391
391
 
392
392
  const graphQlEndpoint = 'sh-ql';
@@ -407,7 +407,7 @@ function doGet (endPoint, data,extraConfig) {
407
407
  updateSession();
408
408
  let config = {
409
409
  headers: {
410
- Authorization: 'Bearer ' + ShStorage.getItem('access_token')
410
+ Authorization: 'Bearer ' + shStorage.getItem('access_token')
411
411
  }
412
412
  };
413
413
  if (extraConfig) {
@@ -423,7 +423,7 @@ function doPost (endPoint, data, extraConfig) {
423
423
  updateSession();
424
424
  const config = {
425
425
  headers: {
426
- Authorization: 'Bearer ' + ShStorage.getItem('access_token')
426
+ Authorization: 'Bearer ' + shStorage.getItem('access_token')
427
427
  }
428
428
  };
429
429
  if (extraConfig) {
@@ -438,7 +438,7 @@ function doDelete (endPoint, data, extraConfig) {
438
438
  updateSession();
439
439
  const config = {
440
440
  headers: {
441
- Authorization: 'Bearer ' + ShStorage.getItem('access_token')
441
+ Authorization: 'Bearer ' + shStorage.getItem('access_token')
442
442
  }
443
443
  };
444
444
  if (extraConfig) {
@@ -453,7 +453,7 @@ function doPut (endPoint, data, extraConfig) {
453
453
  updateSession();
454
454
  const config = {
455
455
  headers: {
456
- Authorization: 'Bearer ' + ShStorage.getItem('access_token')
456
+ Authorization: 'Bearer ' + shStorage.getItem('access_token')
457
457
  }
458
458
  };
459
459
  if (extraConfig) {
@@ -469,7 +469,7 @@ function doPatch (endPoint, data, extraConfig) {
469
469
  updateSession();
470
470
  const config = {
471
471
  headers: {
472
- Authorization: 'Bearer ' + ShStorage.getItem('access_token')
472
+ Authorization: 'Bearer ' + shStorage.getItem('access_token')
473
473
  }
474
474
  };
475
475
  if (extraConfig) {
@@ -3757,7 +3757,7 @@ var script$m = {
3757
3757
  },
3758
3758
  centered: {
3759
3759
  type: Boolean,
3760
- default: false
3760
+ default: true
3761
3761
  }
3762
3762
  },
3763
3763
  emits: ['modalClosed'],
@@ -4054,7 +4054,7 @@ const useUserStore = pinia.defineStore('user-store', {
4054
4054
  setUser (){
4055
4055
  let user = null;
4056
4056
  try {
4057
- user = ShStorage.getItem('user') ? ShStorage.getItem('user') : null;
4057
+ user = shStorage.getItem('user') ? shStorage.getItem('user') : null;
4058
4058
  } catch (error) {
4059
4059
  user= null;
4060
4060
  }
@@ -4078,7 +4078,7 @@ const useUserStore = pinia.defineStore('user-store', {
4078
4078
  if (typeof(user) === 'undefined') {
4079
4079
  user = res.data;
4080
4080
  }
4081
- ShStorage.setItem('user',user);
4081
+ shStorage.setItem('user',user);
4082
4082
  user.signOut = this.signOut;
4083
4083
  user.logout = this.signOut;
4084
4084
  user.logOut = this.signOut;
@@ -4101,7 +4101,7 @@ const useUserStore = pinia.defineStore('user-store', {
4101
4101
  }).catch((reason) => {
4102
4102
  if (reason.response && reason.response.status) {
4103
4103
  if(reason.response.status === 401) {
4104
- ShStorage.setItem('user',null);
4104
+ shStorage.setItem('user',null);
4105
4105
  this.user = null;
4106
4106
  }
4107
4107
  this.loggedOut = true;
@@ -4115,7 +4115,7 @@ const useUserStore = pinia.defineStore('user-store', {
4115
4115
  }
4116
4116
  }
4117
4117
  const timeNow = moment__default["default"]().toISOString();
4118
- ShStorage.setItem('session_start',timeNow);
4118
+ shStorage.setItem('session_start',timeNow);
4119
4119
  },
4120
4120
  signOut () {
4121
4121
  shRepo.signOutUser();
@@ -4127,7 +4127,7 @@ const useUserStore = pinia.defineStore('user-store', {
4127
4127
  this.setUser();
4128
4128
  },
4129
4129
  setAccessToken (accessToken) {
4130
- ShStorage.setItem('access_token', accessToken);
4130
+ shStorage.setItem('access_token', accessToken);
4131
4131
  this.setUser();
4132
4132
  }
4133
4133
  },
@@ -5097,7 +5097,7 @@ const __default__ = {
5097
5097
  },
5098
5098
  setCachedData: function (){
5099
5099
  if (this.cacheKey) {
5100
- this.records = ShStorage.getItem('sh_table_cache_' + this.cacheKey, null);
5100
+ this.records = shStorage.getItem('sh_table_cache_' + this.cacheKey, null);
5101
5101
  }
5102
5102
  },
5103
5103
  reloadData: function (page, append){
@@ -5136,7 +5136,7 @@ const __default__ = {
5136
5136
  const response = req.data.data;
5137
5137
  this.$emit('dataLoaded', response);
5138
5138
  if (this.page < 2 && this.cacheKey) {
5139
- ShStorage.setItem('sh_table_cache_' + this.cacheKey, response.data);
5139
+ shStorage.setItem('sh_table_cache_' + this.cacheKey, response.data);
5140
5140
  }
5141
5141
  this.pagination_data = {
5142
5142
  current: response.current_page,
@@ -7139,7 +7139,7 @@ const ShFrontend = {
7139
7139
  install: (app, options) => {
7140
7140
  if(options.sessionTimeout){
7141
7141
  app.provide('sessionTimeout',options.sessionTimeout);
7142
- ShStorage.setItem('sessionTimeout',options.sessionTimeout);
7142
+ shStorage.setItem('sessionTimeout',options.sessionTimeout);
7143
7143
  }
7144
7144
  const shFormElements = options.shFormElementClasses ?? {};
7145
7145
 
@@ -7207,7 +7207,7 @@ const ShFrontend = {
7207
7207
  const allowKeys = [];
7208
7208
  Object.keys(options).map(key=> ((!['string','integer','number'].includes(typeof options[key]) && !allowKeys.includes(key)) || removeKeys.includes(key)) && delete options[key]);
7209
7209
 
7210
- ShStorage.setItem('ShConfig',options);
7210
+ shStorage.setItem('ShConfig',options);
7211
7211
  }
7212
7212
  };
7213
7213
 
@@ -7224,6 +7224,69 @@ var shGql = {
7224
7224
  mutate
7225
7225
  };
7226
7226
 
7227
+ /**
7228
+ * useShFetch is a custom hook for fetching data from a given URL and caching it.
7229
+ * @param {string} url - The URL to fetch data from.
7230
+ * @param {string} path - The path to drill down to the data in the response.
7231
+ * @param {string} cacheKey - The key to use when caching the data.
7232
+ * @returns {object} An object containing the status, loading state, error, data, and a function to refetch the data.
7233
+ */
7234
+ const useShFetch = (url, path, cacheKey) => {
7235
+ const status = vue.ref('pending');
7236
+ const loading = vue.ref(false);
7237
+ const error = vue.ref(null);
7238
+ const data = vue.ref(null);
7239
+
7240
+ vue.onMounted(() => {
7241
+ reFetchData();
7242
+ });
7243
+
7244
+ /**
7245
+ * Fetches data from the given URL and updates the status, loading state, error, and data refs.
7246
+ * If a cacheKey is provided and there is cached data for that key, the cached data is used instead of fetching.
7247
+ */
7248
+ const reFetchData = () => {
7249
+ loading.value = true;
7250
+ status.value = 'loading';
7251
+ if (cacheKey && shStorage.getItem(cacheKey)) {
7252
+ data.value = shStorage.getItem(cacheKey);
7253
+ status.value = 'success';
7254
+ loading.value = false;
7255
+ } else {
7256
+ shApis.doGet(url).then(response => {
7257
+ status.value = 'success';
7258
+ let res = response.data;
7259
+ if (path) {
7260
+ let pathArr = path.split('.');
7261
+ for (let i = 0; i < pathArr.length; i++) {
7262
+ res = res[pathArr[i]];
7263
+ }
7264
+ }
7265
+ data.value = res;
7266
+ if (cacheKey) {
7267
+ shStorage.setItem(cacheKey, res);
7268
+ }
7269
+ })
7270
+ .catch(res => {
7271
+ status.value = 'error';
7272
+ error.value = res.message ? res.message : (res.error ? res.error : 'An unexpected error occurred');
7273
+ shRepo.showToast(error.value, 'error');
7274
+ })
7275
+ .finally(() => {
7276
+ loading.value = false;
7277
+ });
7278
+ }
7279
+ };
7280
+
7281
+ return {
7282
+ status,
7283
+ loading,
7284
+ error,
7285
+ data,
7286
+ reFetchData
7287
+ }
7288
+ };
7289
+
7227
7290
  exports.Countries = countries;
7228
7291
  exports.ManagePermissions = script$8;
7229
7292
  exports.ShAutoForm = script$o;
@@ -7250,6 +7313,7 @@ exports.ShTabs = script$c;
7250
7313
  exports.shApis = shApis;
7251
7314
  exports.shGql = shGql;
7252
7315
  exports.shRepo = shRepo;
7253
- exports.shStorage = ShStorage;
7316
+ exports.shStorage = shStorage;
7254
7317
  exports.useAppStore = useAppStore;
7318
+ exports.useShFetch = useShFetch;
7255
7319
  exports.useUserStore = useUserStore;
package/dist/library.mjs CHANGED
@@ -26,7 +26,7 @@ function getItem (key) {
26
26
  function removeItem (key) {
27
27
  return localStorage.removeItem(key)
28
28
  }
29
- var ShStorage = {
29
+ var shStorage = {
30
30
  setItem,
31
31
  getItem,
32
32
  removeItem
@@ -132,21 +132,21 @@ const signOutUser = () => {
132
132
  const logoutApiEndPoint = getShConfig('logoutApiEndpoint', 'auth/logout');
133
133
  console.log(loginUrl, logoutApiEndPoint);
134
134
  shApis.doPost(logoutApiEndPoint).then(res => {
135
- ShStorage.removeItem('access_token');
136
- ShStorage.removeItem('user');
137
- ShStorage.removeItem('last_activity');
135
+ shStorage.removeItem('access_token');
136
+ shStorage.removeItem('user');
137
+ shStorage.removeItem('last_activity');
138
138
  window.location.href = loginUrl;
139
139
  }).catch(ex => {
140
- ShStorage.removeItem('access_token');
141
- ShStorage.removeItem('user');
142
- ShStorage.removeItem('last_activity');
140
+ shStorage.removeItem('access_token');
141
+ shStorage.removeItem('user');
142
+ shStorage.removeItem('last_activity');
143
143
  window.location.href = loginUrl;
144
144
  });
145
145
  };
146
146
 
147
147
 
148
148
  function getShConfig(key = null, def = ''){
149
- const config = ShStorage.getItem('ShConfig') ?? {};
149
+ const config = shStorage.getItem('ShConfig') ?? {};
150
150
  if (key) {
151
151
  return config[key] ?? def
152
152
  }
@@ -284,17 +284,17 @@ function logoutUser(){
284
284
  }
285
285
  }
286
286
  function sessionRestored(){
287
- const timeout = ShStorage.getItem('sessionTimeout') * 60;
288
- const last_activity = ShStorage.getItem('last_activity');
287
+ const timeout = shStorage.getItem('sessionTimeout') * 60;
288
+ const last_activity = shStorage.getItem('last_activity');
289
289
  const pastSeconds = moment().diff(last_activity, 'seconds');
290
- if(!ShStorage.getItem('access_token'))
290
+ if(!shStorage.getItem('access_token'))
291
291
  return false
292
292
  return pastSeconds < timeout
293
293
  }
294
294
  const checkSession = function (isCheking) {
295
- const timeout = ShStorage.getItem('sessionTimeout');
296
- const last_activity = ShStorage.getItem('last_activity');
297
- if (ShStorage.getItem('access_token')) {
295
+ const timeout = shStorage.getItem('sessionTimeout');
296
+ const last_activity = shStorage.getItem('last_activity');
297
+ if (shStorage.getItem('access_token')) {
298
298
  const pastMinutes = moment().diff(last_activity, 'minutes');
299
299
  const pastSeconds = moment().diff(last_activity, 'seconds');
300
300
  if(pastMinutes >= timeout) {
@@ -352,17 +352,17 @@ async function shSwalLogout (seconds = 30) {
352
352
  window.ShConfirmation = null;
353
353
  clearInterval(window.shInterval);
354
354
  const timeNow = moment().toISOString();
355
- ShStorage.setItem('last_activity', timeNow);
355
+ shStorage.setItem('last_activity', timeNow);
356
356
  startSession();
357
357
  }
358
358
  })
359
359
  }
360
360
  function startSession () {
361
361
  const timeNow = moment().toISOString();
362
- const accessToken = ShStorage.getItem('access_token');
362
+ const accessToken = shStorage.getItem('access_token');
363
363
  if (accessToken) {
364
- ShStorage.setItem('last_activity', timeNow);
365
- const timout = ShStorage.getItem('sessionTimeout');
364
+ shStorage.setItem('last_activity', timeNow);
365
+ const timout = shStorage.getItem('sessionTimeout');
366
366
  const interval = (timout * 60 *1000) / 3;
367
367
  window.shInterval = setInterval(()=>{
368
368
  checkSession();
@@ -374,7 +374,7 @@ const updateSession = () =>{
374
374
  startSession();
375
375
  }
376
376
  const timeNow = moment().toISOString();
377
- ShStorage.setItem('last_activity', timeNow);
377
+ shStorage.setItem('last_activity', timeNow);
378
378
  };
379
379
 
380
380
  const graphQlEndpoint = 'sh-ql';
@@ -395,7 +395,7 @@ function doGet (endPoint, data,extraConfig) {
395
395
  updateSession();
396
396
  let config = {
397
397
  headers: {
398
- Authorization: 'Bearer ' + ShStorage.getItem('access_token')
398
+ Authorization: 'Bearer ' + shStorage.getItem('access_token')
399
399
  }
400
400
  };
401
401
  if (extraConfig) {
@@ -411,7 +411,7 @@ function doPost (endPoint, data, extraConfig) {
411
411
  updateSession();
412
412
  const config = {
413
413
  headers: {
414
- Authorization: 'Bearer ' + ShStorage.getItem('access_token')
414
+ Authorization: 'Bearer ' + shStorage.getItem('access_token')
415
415
  }
416
416
  };
417
417
  if (extraConfig) {
@@ -426,7 +426,7 @@ function doDelete (endPoint, data, extraConfig) {
426
426
  updateSession();
427
427
  const config = {
428
428
  headers: {
429
- Authorization: 'Bearer ' + ShStorage.getItem('access_token')
429
+ Authorization: 'Bearer ' + shStorage.getItem('access_token')
430
430
  }
431
431
  };
432
432
  if (extraConfig) {
@@ -441,7 +441,7 @@ function doPut (endPoint, data, extraConfig) {
441
441
  updateSession();
442
442
  const config = {
443
443
  headers: {
444
- Authorization: 'Bearer ' + ShStorage.getItem('access_token')
444
+ Authorization: 'Bearer ' + shStorage.getItem('access_token')
445
445
  }
446
446
  };
447
447
  if (extraConfig) {
@@ -457,7 +457,7 @@ function doPatch (endPoint, data, extraConfig) {
457
457
  updateSession();
458
458
  const config = {
459
459
  headers: {
460
- Authorization: 'Bearer ' + ShStorage.getItem('access_token')
460
+ Authorization: 'Bearer ' + shStorage.getItem('access_token')
461
461
  }
462
462
  };
463
463
  if (extraConfig) {
@@ -3745,7 +3745,7 @@ var script$m = {
3745
3745
  },
3746
3746
  centered: {
3747
3747
  type: Boolean,
3748
- default: false
3748
+ default: true
3749
3749
  }
3750
3750
  },
3751
3751
  emits: ['modalClosed'],
@@ -4042,7 +4042,7 @@ const useUserStore = defineStore('user-store', {
4042
4042
  setUser (){
4043
4043
  let user = null;
4044
4044
  try {
4045
- user = ShStorage.getItem('user') ? ShStorage.getItem('user') : null;
4045
+ user = shStorage.getItem('user') ? shStorage.getItem('user') : null;
4046
4046
  } catch (error) {
4047
4047
  user= null;
4048
4048
  }
@@ -4066,7 +4066,7 @@ const useUserStore = defineStore('user-store', {
4066
4066
  if (typeof(user) === 'undefined') {
4067
4067
  user = res.data;
4068
4068
  }
4069
- ShStorage.setItem('user',user);
4069
+ shStorage.setItem('user',user);
4070
4070
  user.signOut = this.signOut;
4071
4071
  user.logout = this.signOut;
4072
4072
  user.logOut = this.signOut;
@@ -4089,7 +4089,7 @@ const useUserStore = defineStore('user-store', {
4089
4089
  }).catch((reason) => {
4090
4090
  if (reason.response && reason.response.status) {
4091
4091
  if(reason.response.status === 401) {
4092
- ShStorage.setItem('user',null);
4092
+ shStorage.setItem('user',null);
4093
4093
  this.user = null;
4094
4094
  }
4095
4095
  this.loggedOut = true;
@@ -4103,7 +4103,7 @@ const useUserStore = defineStore('user-store', {
4103
4103
  }
4104
4104
  }
4105
4105
  const timeNow = moment().toISOString();
4106
- ShStorage.setItem('session_start',timeNow);
4106
+ shStorage.setItem('session_start',timeNow);
4107
4107
  },
4108
4108
  signOut () {
4109
4109
  shRepo.signOutUser();
@@ -4115,7 +4115,7 @@ const useUserStore = defineStore('user-store', {
4115
4115
  this.setUser();
4116
4116
  },
4117
4117
  setAccessToken (accessToken) {
4118
- ShStorage.setItem('access_token', accessToken);
4118
+ shStorage.setItem('access_token', accessToken);
4119
4119
  this.setUser();
4120
4120
  }
4121
4121
  },
@@ -5085,7 +5085,7 @@ const __default__ = {
5085
5085
  },
5086
5086
  setCachedData: function (){
5087
5087
  if (this.cacheKey) {
5088
- this.records = ShStorage.getItem('sh_table_cache_' + this.cacheKey, null);
5088
+ this.records = shStorage.getItem('sh_table_cache_' + this.cacheKey, null);
5089
5089
  }
5090
5090
  },
5091
5091
  reloadData: function (page, append){
@@ -5124,7 +5124,7 @@ const __default__ = {
5124
5124
  const response = req.data.data;
5125
5125
  this.$emit('dataLoaded', response);
5126
5126
  if (this.page < 2 && this.cacheKey) {
5127
- ShStorage.setItem('sh_table_cache_' + this.cacheKey, response.data);
5127
+ shStorage.setItem('sh_table_cache_' + this.cacheKey, response.data);
5128
5128
  }
5129
5129
  this.pagination_data = {
5130
5130
  current: response.current_page,
@@ -7127,7 +7127,7 @@ const ShFrontend = {
7127
7127
  install: (app, options) => {
7128
7128
  if(options.sessionTimeout){
7129
7129
  app.provide('sessionTimeout',options.sessionTimeout);
7130
- ShStorage.setItem('sessionTimeout',options.sessionTimeout);
7130
+ shStorage.setItem('sessionTimeout',options.sessionTimeout);
7131
7131
  }
7132
7132
  const shFormElements = options.shFormElementClasses ?? {};
7133
7133
 
@@ -7195,7 +7195,7 @@ const ShFrontend = {
7195
7195
  const allowKeys = [];
7196
7196
  Object.keys(options).map(key=> ((!['string','integer','number'].includes(typeof options[key]) && !allowKeys.includes(key)) || removeKeys.includes(key)) && delete options[key]);
7197
7197
 
7198
- ShStorage.setItem('ShConfig',options);
7198
+ shStorage.setItem('ShConfig',options);
7199
7199
  }
7200
7200
  };
7201
7201
 
@@ -7212,4 +7212,67 @@ var shGql = {
7212
7212
  mutate
7213
7213
  };
7214
7214
 
7215
- export { countries as Countries, script$8 as ManagePermissions, script$o as ShAutoForm, script$j as ShCanvas, script$9 as ShCanvasBtn, script$4 as ShCardLayout, script$g as ShConfirmAction, script$n as ShDropDownForm, script$b as ShDynamicTabs, script$w as ShForm, ShFrontend, script$m as ShModal, script$a as ShModalBtn, script$l as ShModalForm, script$k as ShModalFormAuto, script$y as ShPhone, script$5 as ShQueryPopups, script$e as ShRange, script$7 as ShRoutePopups, script$f as ShSilentAction, script$x as ShSuggest, script$d as ShTable, script$c as ShTabs, shApis, shGql, shRepo, ShStorage as shStorage, useAppStore, useUserStore };
7215
+ /**
7216
+ * useShFetch is a custom hook for fetching data from a given URL and caching it.
7217
+ * @param {string} url - The URL to fetch data from.
7218
+ * @param {string} path - The path to drill down to the data in the response.
7219
+ * @param {string} cacheKey - The key to use when caching the data.
7220
+ * @returns {object} An object containing the status, loading state, error, data, and a function to refetch the data.
7221
+ */
7222
+ const useShFetch = (url, path, cacheKey) => {
7223
+ const status = ref('pending');
7224
+ const loading = ref(false);
7225
+ const error = ref(null);
7226
+ const data = ref(null);
7227
+
7228
+ onMounted(() => {
7229
+ reFetchData();
7230
+ });
7231
+
7232
+ /**
7233
+ * Fetches data from the given URL and updates the status, loading state, error, and data refs.
7234
+ * If a cacheKey is provided and there is cached data for that key, the cached data is used instead of fetching.
7235
+ */
7236
+ const reFetchData = () => {
7237
+ loading.value = true;
7238
+ status.value = 'loading';
7239
+ if (cacheKey && shStorage.getItem(cacheKey)) {
7240
+ data.value = shStorage.getItem(cacheKey);
7241
+ status.value = 'success';
7242
+ loading.value = false;
7243
+ } else {
7244
+ shApis.doGet(url).then(response => {
7245
+ status.value = 'success';
7246
+ let res = response.data;
7247
+ if (path) {
7248
+ let pathArr = path.split('.');
7249
+ for (let i = 0; i < pathArr.length; i++) {
7250
+ res = res[pathArr[i]];
7251
+ }
7252
+ }
7253
+ data.value = res;
7254
+ if (cacheKey) {
7255
+ shStorage.setItem(cacheKey, res);
7256
+ }
7257
+ })
7258
+ .catch(res => {
7259
+ status.value = 'error';
7260
+ error.value = res.message ? res.message : (res.error ? res.error : 'An unexpected error occurred');
7261
+ shRepo.showToast(error.value, 'error');
7262
+ })
7263
+ .finally(() => {
7264
+ loading.value = false;
7265
+ });
7266
+ }
7267
+ };
7268
+
7269
+ return {
7270
+ status,
7271
+ loading,
7272
+ error,
7273
+ data,
7274
+ reFetchData
7275
+ }
7276
+ };
7277
+
7278
+ export { countries as Countries, script$8 as ManagePermissions, script$o as ShAutoForm, script$j as ShCanvas, script$9 as ShCanvasBtn, script$4 as ShCardLayout, script$g as ShConfirmAction, script$n as ShDropDownForm, script$b as ShDynamicTabs, script$w as ShForm, ShFrontend, script$m as ShModal, script$a as ShModalBtn, script$l as ShModalForm, script$k as ShModalFormAuto, script$y as ShPhone, script$5 as ShQueryPopups, script$e as ShRange, script$7 as ShRoutePopups, script$f as ShSilentAction, script$x as ShSuggest, script$d as ShTable, script$c as ShTabs, shApis, shGql, shRepo, shStorage, useAppStore, useShFetch, useUserStore };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iankibetsh/shframework",
3
- "version": "4.5.6",
3
+ "version": "4.5.7",
4
4
  "description": "Vue library for handling laravel backend",
5
5
  "main": "dist/library.js",
6
6
  "module": "dist/library.mjs",