@iankibetsh/vue-streamline 1.1.6 → 1.1.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iankibetsh/vue-streamline",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "description": "Vue library for streamlining laravel backend services with @iankibet/streamline composer package",
5
5
  "type": "module",
6
6
  "scripts": {
package/src/App.vue CHANGED
@@ -2,19 +2,19 @@
2
2
  import useStreamline from './composables/useStreamline.js'
3
3
  import { onMounted, ref, toRefs } from 'vue'
4
4
 
5
- const {loading, service:paybillService, getActionUrl} = useStreamline('mpesa/paybill',3)
5
+ const {loading, service:paybillService, getActionUrl} = useStreamline('mpesa/paybill',32)
6
6
 
7
7
 
8
8
  const foundPaybill = ref(null)
9
9
 
10
10
  onMounted(()=>{
11
- paybillService.callAnyMethod(3).then(res=>{
12
- foundPaybill.value = res
13
- })
11
+ // paybillService.getPaybill(32).then(res=>{
12
+ // foundPaybill.value = res
13
+ // })
14
14
  })
15
15
  const findPaybill = async ()=>{
16
- foundPaybill.value = await paybillService.getPaybill(3)
17
- console.log(getActionUrl('getPaybill',3))
16
+ foundPaybill.value = await paybillService.getPaybill(32)
17
+ console.log(getActionUrl('getPaybill',32))
18
18
  }
19
19
  </script>
20
20
 
@@ -22,7 +22,9 @@ const findPaybill = async ()=>{
22
22
  <div>
23
23
  <hr/>
24
24
  <h3 class="text-success" >Loading : {{ loading }}</h3>
25
+ <h5>Found Paybill : {{ paybillService.paybill }}</h5>
25
26
  <h1 @click="findPaybill">Get Paybill</h1>
27
+ {{ foundPaybill }}
26
28
  </div>
27
29
  </template>
28
30
 
@@ -1,111 +1,135 @@
1
- import Axios from 'axios'
2
- import { inject, onMounted, reactive, ref, toRefs } from 'vue'
1
+ import Axios from 'axios';
2
+ import { inject, onMounted, reactive, ref } from 'vue';
3
3
 
4
4
  const useStreamline = (stream, ...initialArgs) => {
5
- let formData = {}
6
- const loading = ref(false)
5
+ let formData = {};
6
+ const loading = ref(false);
7
+ const propertiesFetched = ref(false);
8
+ const fetching = ref(false);
9
+ // cache key for local storage, include initialArgs in the key
10
+ const cacheKey = `streamline_${stream}_${initialArgs.join('_')}`;
7
11
 
8
- // Inject headers and apiEndpoint
9
- const streamlineUrl = inject('streamlineUrl')
10
- const streamlineHeaders = inject('streamlineHeaders')
12
+ // Inject headers and API endpoint
13
+ const streamlineUrl = inject('streamlineUrl');
14
+ const streamlineHeaders = inject('streamlineHeaders');
15
+ const enableCache = inject('enableCache');
11
16
 
12
17
  const axios = Axios.create({
13
18
  headers: {
14
- ...streamlineHeaders
15
- }
16
- })
17
- const handler = {
18
- get(target, prop, receiver) {
19
- if (prop in target) {
20
- return target[prop]
21
- }
22
- return async (...args) => {
23
- loading.value = true
24
- try {
25
- const response = await axios.post(streamlineUrl, {
26
- action: prop,
27
- stream,
28
- ...formData,
29
- params: args
30
- })
31
- return response.data
32
- } catch (error) {
33
- console.error(`Error calling ${prop} on stream ${stream}`, error)
34
- throw error
35
- } finally {
36
- loading.value = false
37
- }
38
- }
39
- }
40
- }
41
-
42
- const service = reactive({
43
- }) // Make the service object reactive
19
+ ...streamlineHeaders,
20
+ },
21
+ });
44
22
 
45
- // Function to fetch the public properties of the service class
46
23
  const fetchServiceProperties = async () => {
24
+ if (loading.value || fetching.value || propertiesFetched.value) return;
25
+ fetching.value = true;
26
+
47
27
  try {
48
- loading.value = true
28
+ loading.value = true;
49
29
  const response = await axios.post(streamlineUrl, {
50
30
  action: 'onMounted',
51
31
  stream,
52
- params: initialArgs
53
- })
54
- assignPropertiesAndMethods(response.data)
32
+ params: initialArgs,
33
+ });
34
+ assignPropertiesAndMethods(response.data);
35
+ enableCache && localStorage.setItem(cacheKey, JSON.stringify(response.data));
55
36
  } catch (error) {
56
- console.error(`Error fetching properties for stream ${stream}`, error)
57
- throw error
37
+ console.error(`Error fetching properties for stream ${stream}`, error);
38
+ throw error;
58
39
  } finally {
59
- loading.value = false
40
+ propertiesFetched.value = true;
41
+ fetching.value = false;
42
+ loading.value = false;
60
43
  }
61
- }
62
-
44
+ };
63
45
 
64
- // Function to assign properties and methods to the service object
65
46
  const assignPropertiesAndMethods = (data) => {
66
- Object.assign(service, data.properties) // Assign the properties to the reactive service object
47
+ Object.assign(service, data.properties); // Assign the properties to the reactive service object
67
48
  for (const method of data.methods) {
68
49
  const fn = async (...args) => {
69
- loading.value = true
50
+ loading.value = true;
70
51
  try {
71
52
  const response = await axios.post(streamlineUrl, {
72
53
  action: method,
73
54
  stream,
74
55
  ...formData,
75
- params: args
76
- })
77
- return response.data
56
+ params: args,
57
+ });
58
+ return response.data;
78
59
  } catch (error) {
79
- console.error(`Error calling ${method} on stream ${stream}`, error)
80
- throw error
60
+ console.error(`Error calling ${method} on stream ${stream}`, error);
61
+ throw error;
81
62
  } finally {
82
- loading.value = false
63
+ loading.value = false;
83
64
  }
84
- }
85
- service[method] = fn
65
+ };
66
+ service[method] = fn;
86
67
  }
87
- }
68
+ };
88
69
 
89
- const getActionUrl = (action, ...args) => {
90
- // console log all arguments to this
70
+ const handler = {
71
+ get(target, prop, receiver) {
72
+ // Fetch properties if not already fetched
73
+ if (!propertiesFetched.value && !loading.value) {
74
+ fetchServiceProperties().then(() => target[prop]);
75
+ }
76
+
77
+ // Handle existing properties
78
+ if (prop in target) {
79
+ return target[prop];
80
+ }
81
+ // Handle nonexistent properties or dynamic methods
82
+ return (...args) => {
83
+ loading.value = true;
84
+ return axios
85
+ .post(streamlineUrl, {
86
+ action: prop,
87
+ stream,
88
+ ...formData,
89
+ params: args,
90
+ })
91
+ .then((response) => {
92
+ loading.value = false;
93
+ return response.data;
94
+ })
95
+ .catch((error) => {
96
+ loading.value = false;
97
+ console.error(`Error calling ${prop} on stream ${stream}`, error);
98
+ throw error;
99
+ });
100
+ };
101
+ },
102
+ };
91
103
 
104
+ const getActionUrl = (action, ...args) => {
105
+ console.log('getActionUrl called with:', action, args);
92
106
  const post = {
93
107
  action,
94
108
  stream,
95
- params: args
109
+ params: args,
110
+ };
111
+ return `${streamlineUrl}?${new URLSearchParams(post).toString()}`;
112
+ };
113
+
114
+ const service = reactive({}); // Make the service object reactive
115
+
116
+ onMounted(() => {
117
+ if (!enableCache) return;
118
+ const cachedData = localStorage.getItem(cacheKey);
119
+ if (cachedData) {
120
+ assignPropertiesAndMethods(JSON.parse(cachedData));
121
+ }
122
+ if (initialArgs.length > 0) {
123
+ fetchServiceProperties();
96
124
  }
97
- return streamlineUrl + '?' + new URLSearchParams(post).toString()
98
125
 
99
- }
100
- onMounted(()=>{
101
- fetchServiceProperties()
102
- })
126
+ });
103
127
 
104
128
  return {
105
129
  loading,
106
130
  service: new Proxy(service, handler),
107
- getActionUrl
108
- }
109
- }
131
+ getActionUrl,
132
+ };
133
+ };
110
134
 
111
- export default useStreamline
135
+ export default useStreamline;
package/src/main.js CHANGED
@@ -10,7 +10,8 @@ const streamlineUrl = 'http://2022-sharasms.test/api/streamline'
10
10
  const app = createApp(App)
11
11
  app.use(streamline,{
12
12
  streamlineHeaders,
13
- streamlineUrl
13
+ streamlineUrl,
14
+ enableCache: true
14
15
  })
15
16
 
16
17
  app.mount('#app')
@@ -2,6 +2,7 @@ const streamline = {
2
2
  install(app, options) {
3
3
  app.provide('streamlineUrl', options.streamlineUrl)
4
4
  app.provide('streamlineHeaders', options.streamlineHeaders)
5
+ app.provide('enableCache', options.enableCache)
5
6
  }
6
7
  }
7
8