@iankibetsh/vue-streamline 1.2.0 → 1.2.3
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 +22 -4
- package/package.json +27 -29
- package/src/App.vue +3 -0
- package/src/composables/useStreamline.js +63 -64
- package/src/plugins/streamline.js +1 -1
package/README.md
CHANGED
|
@@ -32,11 +32,19 @@ Vue.use(streamline, {
|
|
|
32
32
|
import { useStreamline } from '@iankibet/vue-streamline'
|
|
33
33
|
|
|
34
34
|
|
|
35
|
-
const {getActionUrl, service:tasksService} = useStreamline('tasks')
|
|
35
|
+
const {getActionUrl, service:tasksService, props} = useStreamline('tasks')
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Calling an action/method in the service
|
|
40
|
+
|
|
41
|
+
You can call an action/method in the service by calling the method on the service object
|
|
42
|
+
```js
|
|
43
|
+
const res = await tasksService.getTasks()
|
|
44
|
+
|
|
45
|
+
// or
|
|
38
46
|
|
|
39
|
-
|
|
47
|
+
tasksService.getTasks().then(response => {
|
|
40
48
|
console.log(response)
|
|
41
49
|
})
|
|
42
50
|
```
|
|
@@ -47,4 +55,14 @@ tasksServices.getTasks().then(response => {
|
|
|
47
55
|
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
56
|
```js
|
|
49
57
|
getActionUrl('getTasks', 'active')
|
|
50
|
-
```
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Accessing class properties
|
|
61
|
+
In your template just access the props object to get the class properties
|
|
62
|
+
```html
|
|
63
|
+
<template>
|
|
64
|
+
<div>
|
|
65
|
+
<h1>{{props.title}}</h1>
|
|
66
|
+
</div>
|
|
67
|
+
</template>
|
|
68
|
+
```
|
package/package.json
CHANGED
|
@@ -1,30 +1,28 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
}
|
|
2
|
+
"name": "@iankibetsh/vue-streamline",
|
|
3
|
+
"version": "1.2.3",
|
|
4
|
+
"description": "Vue library for streamlining laravel backend services with @iankibet/streamline composer package",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite",
|
|
8
|
+
"build": "vite build",
|
|
9
|
+
"preview": "vite preview"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": "./src/index.js"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [],
|
|
15
|
+
"author": "Iankibet",
|
|
16
|
+
"license": "ISC",
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@vitejs/plugin-vue": "^5.1.2",
|
|
19
|
+
"vite": "^5.4.1"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"@iankibetsh/shframework": "^5",
|
|
23
|
+
"vue": "^3"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/App.vue
CHANGED
|
@@ -23,6 +23,9 @@ const findPaybill = async ()=>{
|
|
|
23
23
|
<hr/>
|
|
24
24
|
<h3>Destructed: {{ testKey }}</h3>
|
|
25
25
|
<h3>Original: {{ props.testKey }}</h3>
|
|
26
|
+
<h3>Original: {{ props.testKey }}</h3>
|
|
27
|
+
<h3>Original: {{ props.testKey }}</h3>
|
|
28
|
+
<h3>Original: {{ props.testKey }}</h3>
|
|
26
29
|
<h3 class="text-success" >Loading : {{ loading }}</h3>
|
|
27
30
|
<h1 @click="findPaybill">Get Paybill</h1>
|
|
28
31
|
{{ foundPaybill }}
|
|
@@ -1,126 +1,125 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { inject, onMounted, reactive, ref } from 'vue'
|
|
2
|
+
import { shApis } from '@iankibetsh/shframework'
|
|
3
3
|
|
|
4
4
|
const useStreamline = (stream, ...initialArgs) => {
|
|
5
|
-
let formData = {}
|
|
6
|
-
const loading = ref(false)
|
|
7
|
-
const propertiesFetched = ref(false)
|
|
8
|
-
const fetching = ref(false)
|
|
5
|
+
let formData = {}
|
|
6
|
+
const loading = ref(false)
|
|
7
|
+
const propertiesFetched = ref(false)
|
|
8
|
+
const fetching = ref(false)
|
|
9
9
|
// cache key for local storage, include initialArgs in the key
|
|
10
|
-
const cacheKey = `streamline_${stream}_${initialArgs.join('_')}
|
|
10
|
+
const cacheKey = `streamline_${stream}_${initialArgs.join('_')}`
|
|
11
11
|
|
|
12
12
|
// Inject headers and API endpoint
|
|
13
|
-
const streamlineUrl = inject('streamlineUrl')
|
|
14
|
-
const
|
|
15
|
-
const enableCache = inject('enableCache');
|
|
13
|
+
const streamlineUrl = inject('streamlineUrl')
|
|
14
|
+
const enableCache = inject('enableCache')
|
|
16
15
|
|
|
17
|
-
const originalProps = reactive({})
|
|
16
|
+
const originalProps = reactive({})
|
|
18
17
|
|
|
19
18
|
// Proxy to intercept access
|
|
20
19
|
const props = new Proxy(originalProps, {
|
|
21
20
|
get(target, property, receiver) {
|
|
22
|
-
if(!propertiesFetched.value) {
|
|
23
|
-
fetchServiceProperties().then(() => target[property])
|
|
21
|
+
if (!propertiesFetched.value) {
|
|
22
|
+
fetchServiceProperties().then(() => target[property])
|
|
24
23
|
}
|
|
25
|
-
return Reflect.get(target, property, receiver)
|
|
24
|
+
return Reflect.get(target, property, receiver) // Return the actual value
|
|
26
25
|
},
|
|
27
26
|
set(target, property, value, receiver) {
|
|
28
|
-
return Reflect.set(target, property, value, receiver)
|
|
27
|
+
return Reflect.set(target, property, value, receiver) // Update the value
|
|
29
28
|
}
|
|
30
29
|
})
|
|
31
|
-
const axios = Axios.create({
|
|
32
|
-
headers: {
|
|
33
|
-
...streamlineHeaders,
|
|
34
|
-
},
|
|
35
|
-
});
|
|
36
30
|
|
|
37
31
|
const fetchServiceProperties = async () => {
|
|
38
|
-
if (loading.value || fetching.value || propertiesFetched.value) return
|
|
39
|
-
fetching.value = true
|
|
32
|
+
if (loading.value || fetching.value || propertiesFetched.value) return
|
|
33
|
+
fetching.value = true
|
|
40
34
|
|
|
41
35
|
try {
|
|
42
|
-
loading.value = true
|
|
43
|
-
const response = await
|
|
36
|
+
loading.value = true
|
|
37
|
+
const response = await shApis.doPost(streamlineUrl, {
|
|
44
38
|
action: 'onMounted',
|
|
45
39
|
stream,
|
|
46
|
-
params: initialArgs
|
|
47
|
-
})
|
|
48
|
-
assignProperties(response.data)
|
|
49
|
-
enableCache && localStorage.setItem(cacheKey, JSON.stringify(response.data))
|
|
40
|
+
params: initialArgs
|
|
41
|
+
})
|
|
42
|
+
assignProperties(response.data)
|
|
43
|
+
enableCache && localStorage.setItem(cacheKey, JSON.stringify(response.data))
|
|
50
44
|
} catch (error) {
|
|
51
|
-
console.error(`Error fetching properties for stream ${stream}`, error)
|
|
52
|
-
throw error
|
|
45
|
+
console.error(`Error fetching properties for stream ${stream}`, error)
|
|
46
|
+
throw error
|
|
53
47
|
} finally {
|
|
54
|
-
propertiesFetched.value = true
|
|
55
|
-
fetching.value = false
|
|
56
|
-
loading.value = false
|
|
48
|
+
propertiesFetched.value = true
|
|
49
|
+
fetching.value = false
|
|
50
|
+
loading.value = false
|
|
57
51
|
}
|
|
58
|
-
}
|
|
52
|
+
}
|
|
59
53
|
|
|
60
54
|
const assignProperties = (data) => {
|
|
61
|
-
Object.assign(service, data.properties)
|
|
62
|
-
Object.assign(originalProps, data.properties)
|
|
63
|
-
}
|
|
55
|
+
Object.assign(service, data.properties) // Assign the properties to the reactive service object
|
|
56
|
+
Object.assign(originalProps, data.properties) // Assign the properties to the reactive service object
|
|
57
|
+
}
|
|
64
58
|
|
|
65
59
|
const handler = {
|
|
66
60
|
get(target, prop, receiver) {
|
|
61
|
+
// Fetch properties if not already fetched
|
|
62
|
+
if (!propertiesFetched.value && !loading.value) {
|
|
63
|
+
fetchServiceProperties().then(() => target[prop])
|
|
64
|
+
}
|
|
65
|
+
|
|
67
66
|
// Handle existing properties
|
|
68
67
|
if (prop in target) {
|
|
69
|
-
return target[prop]
|
|
68
|
+
return target[prop]
|
|
70
69
|
}
|
|
71
70
|
// Handle nonexistent properties or dynamic methods
|
|
72
71
|
return (...args) => {
|
|
73
|
-
loading.value = true
|
|
74
|
-
return
|
|
75
|
-
.
|
|
72
|
+
loading.value = true
|
|
73
|
+
return shApis
|
|
74
|
+
.doPost(streamlineUrl, {
|
|
76
75
|
action: prop,
|
|
77
76
|
stream,
|
|
78
77
|
...formData,
|
|
79
|
-
params: args
|
|
78
|
+
params: args
|
|
80
79
|
})
|
|
81
80
|
.then((response) => {
|
|
82
|
-
loading.value = false
|
|
83
|
-
return response.data
|
|
81
|
+
loading.value = false
|
|
82
|
+
return response.data
|
|
84
83
|
})
|
|
85
84
|
.catch((error) => {
|
|
86
|
-
loading.value = false
|
|
87
|
-
console.error(`Error calling ${prop} on stream ${stream}`, error)
|
|
88
|
-
throw error
|
|
89
|
-
})
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
85
|
+
loading.value = false
|
|
86
|
+
console.error(`Error calling ${prop} on stream ${stream}`, error)
|
|
87
|
+
throw error
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
93
92
|
|
|
94
93
|
const getActionUrl = (action, ...args) => {
|
|
95
94
|
// console.log('getActionUrl called with:', action, args);
|
|
96
95
|
const post = {
|
|
97
96
|
action,
|
|
98
97
|
stream,
|
|
99
|
-
params: args
|
|
100
|
-
}
|
|
101
|
-
return `${streamlineUrl}?${new URLSearchParams(post).toString()}
|
|
102
|
-
}
|
|
98
|
+
params: args
|
|
99
|
+
}
|
|
100
|
+
return `${streamlineUrl}?${new URLSearchParams(post).toString()}`
|
|
101
|
+
}
|
|
103
102
|
|
|
104
|
-
const service = reactive({})
|
|
103
|
+
const service = reactive({}) // Make the service object reactive
|
|
105
104
|
|
|
106
105
|
onMounted(() => {
|
|
107
|
-
if (!enableCache) return
|
|
108
|
-
const cachedData = localStorage.getItem(cacheKey)
|
|
106
|
+
if (!enableCache) return
|
|
107
|
+
const cachedData = localStorage.getItem(cacheKey)
|
|
109
108
|
if (cachedData) {
|
|
110
|
-
assignProperties(JSON.parse(cachedData))
|
|
109
|
+
assignProperties(JSON.parse(cachedData))
|
|
111
110
|
}
|
|
112
111
|
if (initialArgs.length > 0) {
|
|
113
|
-
fetchServiceProperties()
|
|
112
|
+
fetchServiceProperties()
|
|
114
113
|
}
|
|
115
114
|
|
|
116
|
-
})
|
|
115
|
+
})
|
|
117
116
|
|
|
118
117
|
return {
|
|
119
118
|
loading,
|
|
120
119
|
service: new Proxy(service, handler),
|
|
121
120
|
getActionUrl,
|
|
122
121
|
props
|
|
123
|
-
}
|
|
124
|
-
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
125
124
|
|
|
126
|
-
export default useStreamline
|
|
125
|
+
export default useStreamline
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const streamline = {
|
|
2
2
|
install(app, options) {
|
|
3
3
|
app.provide('streamlineUrl', options.streamlineUrl)
|
|
4
|
-
app.provide('streamlineHeaders', options.streamlineHeaders)
|
|
4
|
+
// app.provide('streamlineHeaders', options.streamlineHeaders)
|
|
5
5
|
app.provide('enableCache', options.enableCache)
|
|
6
6
|
}
|
|
7
7
|
}
|