@iankibetsh/vue-streamline 1.1.5 → 1.1.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/README.md CHANGED
@@ -32,7 +32,7 @@ Vue.use(streamline, {
32
32
  import { useStreamline } from '@iankibet/vue-streamline'
33
33
 
34
34
 
35
- const {service:tasksService} = useStreamline('tasks')
35
+ const {getActionUrl, service:tasksService} = useStreamline('tasks')
36
36
 
37
37
  // call the service methods
38
38
 
@@ -46,5 +46,5 @@ tasksServices.getTasks().then(response => {
46
46
 
47
47
  You can get the url to an action/method in the service by calling the getActionUrl method on the service object passing the action name and any parameters
48
48
  ```js
49
- tasksService.getActionUrl('getTasks', 'active')
49
+ getActionUrl('getTasks', 'active')
50
50
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iankibetsh/vue-streamline",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
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
@@ -1,25 +1,30 @@
1
1
  <script setup>
2
2
  import useStreamline from './composables/useStreamline.js'
3
- import { ref, toRefs } from 'vue'
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.getPaybill(32).then(res=>{
12
+ // foundPaybill.value = res
13
+ // })
14
+ })
11
15
  const findPaybill = async ()=>{
12
- foundPaybill.value = await paybillService.getPaybill(3)
13
- console.log(getActionUrl('getPaybill',3))
16
+ foundPaybill.value = await paybillService.getPaybill(32)
17
+ console.log(getActionUrl('getPaybill',32))
14
18
  }
15
19
  </script>
16
20
 
17
21
  <template>
18
22
  <div>
19
- {{ paybill }}
20
23
  <hr/>
21
24
  <h3 class="text-success" >Loading : {{ loading }}</h3>
25
+ <h5>Found Paybill : {{ paybillService.paybill }}</h5>
22
26
  <h1 @click="findPaybill">Get Paybill</h1>
27
+ {{ foundPaybill }}
23
28
  </div>
24
29
  </template>
25
30
 
@@ -1,86 +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
-
18
- const service = reactive({}) // Make the service object reactive
19
+ ...streamlineHeaders,
20
+ },
21
+ });
19
22
 
20
- // Function to fetch the public properties of the service class
21
23
  const fetchServiceProperties = async () => {
24
+ if (loading.value || fetching.value || propertiesFetched.value) return;
25
+ fetching.value = true;
26
+
22
27
  try {
23
- loading.value = true
28
+ loading.value = true;
24
29
  const response = await axios.post(streamlineUrl, {
25
30
  action: 'onMounted',
26
31
  stream,
27
- params: initialArgs
28
- })
29
- assignPropertiesAndMethods(response.data)
32
+ params: initialArgs,
33
+ });
34
+ assignPropertiesAndMethods(response.data);
35
+ enableCache && localStorage.setItem(cacheKey, JSON.stringify(response.data));
30
36
  } catch (error) {
31
- console.error(`Error fetching properties for stream ${stream}`, error)
32
- throw error
37
+ console.error(`Error fetching properties for stream ${stream}`, error);
38
+ throw error;
33
39
  } finally {
34
- loading.value = false
40
+ propertiesFetched.value = true;
41
+ fetching.value = false;
42
+ loading.value = false;
35
43
  }
36
- }
44
+ };
37
45
 
38
-
39
- // Function to assign properties and methods to the service object
40
46
  const assignPropertiesAndMethods = (data) => {
41
- 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
42
48
  for (const method of data.methods) {
43
49
  const fn = async (...args) => {
44
- loading.value = true
50
+ loading.value = true;
45
51
  try {
46
52
  const response = await axios.post(streamlineUrl, {
47
53
  action: method,
48
54
  stream,
49
55
  ...formData,
50
- params: args
51
- })
52
- return response.data
56
+ params: args,
57
+ });
58
+ return response.data;
53
59
  } catch (error) {
54
- console.error(`Error calling ${method} on stream ${stream}`, error)
55
- throw error
60
+ console.error(`Error calling ${method} on stream ${stream}`, error);
61
+ throw error;
56
62
  } finally {
57
- loading.value = false
63
+ loading.value = false;
58
64
  }
59
- }
60
- service[method] = fn
65
+ };
66
+ service[method] = fn;
61
67
  }
62
- }
68
+ };
63
69
 
64
- const getActionUrl = (action, ...args) => {
65
- // 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
+ };
66
103
 
104
+ const getActionUrl = (action, ...args) => {
105
+ console.log('getActionUrl called with:', action, args);
67
106
  const post = {
68
107
  action,
69
108
  stream,
70
- 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 (initialArgs.length > 0) {
118
+ fetchServiceProperties();
119
+ }
120
+ if (!enableCache) return;
121
+ const cachedData = localStorage.getItem(cacheKey);
122
+ if (cachedData) {
123
+ assignPropertiesAndMethods(JSON.parse(cachedData));
71
124
  }
72
- return streamlineUrl + '?' + new URLSearchParams(post).toString()
73
125
 
74
- }
75
- onMounted(()=>{
76
- fetchServiceProperties()
77
- })
126
+ });
78
127
 
79
128
  return {
80
129
  loading,
81
- service,
82
- getActionUrl
83
- }
84
- }
130
+ service: new Proxy(service, handler),
131
+ getActionUrl,
132
+ };
133
+ };
85
134
 
86
- 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: false
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
 
package/dist/vite.svg DELETED
@@ -1 +0,0 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>