@iankibetsh/vue-streamline 1.2.2 → 1.2.4
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 +8 -0
- package/package.json +27 -29
- package/src/App.vue +9 -3
- package/src/composables/useStreamline.js +93 -81
- package/src/plugins/streamline.js +1 -1
package/README.md
CHANGED
|
@@ -48,6 +48,14 @@ tasksService.getTasks().then(response => {
|
|
|
48
48
|
console.log(response)
|
|
49
49
|
})
|
|
50
50
|
```
|
|
51
|
+
A swal popup asking the user to confirm the action will be shown before the action is called
|
|
52
|
+
|
|
53
|
+
## Pre Confirm Calling a action/method
|
|
54
|
+
|
|
55
|
+
You can pre confirm calling an action/method in the service by calling the method on the service object
|
|
56
|
+
```js
|
|
57
|
+
const res = await tasksService.confirm('Are you sure you want to delete this task?').deleteTask(1)
|
|
58
|
+
```
|
|
51
59
|
|
|
52
60
|
|
|
53
61
|
## Getting url to an action/method in the service
|
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.4",
|
|
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
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import useStreamline from './composables/useStreamline.js'
|
|
3
3
|
import { onMounted, ref, toRefs } from 'vue'
|
|
4
4
|
|
|
5
|
-
const {loading, service:paybillService, getActionUrl,props} = useStreamline('mpesa/paybill')
|
|
5
|
+
const {loading, service:paybillService, getActionUrl,props,confirmAction} = useStreamline('mpesa/paybill')
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
const foundPaybill = ref(null)
|
|
@@ -13,8 +13,14 @@ onMounted(()=>{
|
|
|
13
13
|
// })
|
|
14
14
|
})
|
|
15
15
|
const findPaybill = async ()=>{
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
// const res = confirmAction('Are you sure?')
|
|
17
|
+
// res.getPaybill(32).then(res=>{
|
|
18
|
+
// foundPaybill.value = res
|
|
19
|
+
// })
|
|
20
|
+
foundPaybill.value = await paybillService.confirm().getPaybill(32)
|
|
21
|
+
.catch(err=>{
|
|
22
|
+
console.log(err)
|
|
23
|
+
})
|
|
18
24
|
}
|
|
19
25
|
</script>
|
|
20
26
|
|
|
@@ -1,131 +1,143 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { inject, onMounted, reactive, ref } from 'vue'
|
|
2
|
+
import { shApis, shRepo } 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
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
let formData = {}
|
|
6
|
+
const loading = ref(false)
|
|
7
|
+
const propertiesFetched = ref(false)
|
|
8
|
+
const confirmationMessage = ref(null)
|
|
9
|
+
|
|
10
|
+
// Cache key for local storage, include initialArgs in the key
|
|
11
|
+
const cacheKey = `streamline_${stream}_${initialArgs.join('_')}`
|
|
11
12
|
|
|
12
13
|
// Inject headers and API endpoint
|
|
13
|
-
const streamlineUrl = inject('streamlineUrl')
|
|
14
|
-
const
|
|
15
|
-
const enableCache = inject('enableCache');
|
|
14
|
+
const streamlineUrl = inject('streamlineUrl')
|
|
15
|
+
const enableCache = inject('enableCache')
|
|
16
16
|
|
|
17
|
-
const originalProps = reactive({})
|
|
17
|
+
const originalProps = reactive({})
|
|
18
18
|
|
|
19
|
-
// Proxy to intercept access
|
|
19
|
+
// Proxy to intercept access
|
|
20
20
|
const props = new Proxy(originalProps, {
|
|
21
21
|
get(target, property, receiver) {
|
|
22
|
-
if(!propertiesFetched.value) {
|
|
23
|
-
fetchServiceProperties().then(() => target[property])
|
|
22
|
+
if (!propertiesFetched.value) {
|
|
23
|
+
fetchServiceProperties().then(() => target[property])
|
|
24
24
|
}
|
|
25
|
-
return Reflect.get(target, property, receiver)
|
|
25
|
+
return Reflect.get(target, property, receiver) // Return the actual value
|
|
26
26
|
},
|
|
27
27
|
set(target, property, value, receiver) {
|
|
28
|
-
return Reflect.set(target, property, value, receiver)
|
|
28
|
+
return Reflect.set(target, property, value, receiver) // Update the value
|
|
29
29
|
}
|
|
30
30
|
})
|
|
31
|
-
const axios = Axios.create({
|
|
32
|
-
headers: {
|
|
33
|
-
...streamlineHeaders,
|
|
34
|
-
},
|
|
35
|
-
});
|
|
36
31
|
|
|
37
32
|
const fetchServiceProperties = async () => {
|
|
38
|
-
if (loading.value ||
|
|
39
|
-
fetching.value = true;
|
|
33
|
+
if (loading.value || propertiesFetched.value) return
|
|
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
|
-
|
|
56
|
-
loading.value = false;
|
|
48
|
+
propertiesFetched.value = true
|
|
49
|
+
loading.value = false
|
|
57
50
|
}
|
|
58
|
-
}
|
|
51
|
+
}
|
|
59
52
|
|
|
60
53
|
const assignProperties = (data) => {
|
|
61
|
-
Object.assign(service, data.properties)
|
|
62
|
-
Object.assign(originalProps, data.properties)
|
|
63
|
-
}
|
|
54
|
+
Object.assign(service, data.properties)
|
|
55
|
+
Object.assign(originalProps, data.properties)
|
|
56
|
+
}
|
|
64
57
|
|
|
65
58
|
const handler = {
|
|
66
59
|
get(target, prop, receiver) {
|
|
67
60
|
// Fetch properties if not already fetched
|
|
68
61
|
if (!propertiesFetched.value && !loading.value) {
|
|
69
|
-
fetchServiceProperties().then(() => target[prop])
|
|
62
|
+
fetchServiceProperties().then(() => target[prop])
|
|
70
63
|
}
|
|
71
64
|
|
|
72
|
-
// Handle existing properties
|
|
73
65
|
if (prop in target) {
|
|
74
|
-
return target[prop]
|
|
66
|
+
return target[prop]
|
|
75
67
|
}
|
|
76
|
-
|
|
68
|
+
|
|
77
69
|
return (...args) => {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
70
|
+
if(prop === 'confirm'){
|
|
71
|
+
return confirmAction(args[0] ?? 'Are you sure?')
|
|
72
|
+
}
|
|
73
|
+
let repo = null
|
|
74
|
+
const data = {
|
|
75
|
+
action: prop,
|
|
76
|
+
stream,
|
|
77
|
+
...formData,
|
|
78
|
+
params: args
|
|
79
|
+
}
|
|
80
|
+
if (confirmationMessage.value) {
|
|
81
|
+
repo = shRepo.runPlainRequest(streamlineUrl,null, confirmationMessage.value,data)
|
|
82
|
+
} else {
|
|
83
|
+
repo = shApis
|
|
84
|
+
.doPost(streamlineUrl, data);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
loading.value = true
|
|
88
|
+
return repo.then((response) => {
|
|
89
|
+
loading.value = false
|
|
90
|
+
if(confirmationMessage.value){
|
|
91
|
+
confirmationMessage.value = null
|
|
92
|
+
|
|
93
|
+
if(!response.isConfirmed){
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
if(response.value?.success){
|
|
97
|
+
return response.value.response
|
|
98
|
+
} else {
|
|
99
|
+
// throw error
|
|
100
|
+
throw response.value.error
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return response.data
|
|
89
106
|
})
|
|
90
107
|
.catch((error) => {
|
|
91
|
-
loading.value = false
|
|
92
|
-
console.error(`Error calling ${prop} on stream ${stream}`, error)
|
|
93
|
-
throw error
|
|
94
|
-
})
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
108
|
+
loading.value = false
|
|
109
|
+
console.error(`Error calling ${prop} on stream ${stream}`, error)
|
|
110
|
+
throw error
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
98
115
|
|
|
99
|
-
const
|
|
100
|
-
// console.log('getActionUrl called with:', action, args);
|
|
101
|
-
const post = {
|
|
102
|
-
action,
|
|
103
|
-
stream,
|
|
104
|
-
params: args,
|
|
105
|
-
};
|
|
106
|
-
return `${streamlineUrl}?${new URLSearchParams(post).toString()}`;
|
|
107
|
-
};
|
|
116
|
+
const service = reactive({})
|
|
108
117
|
|
|
109
|
-
|
|
118
|
+
// Confirmation wrapper
|
|
119
|
+
const confirmAction = (message) => {
|
|
120
|
+
confirmationMessage.value = message
|
|
121
|
+
return new Proxy(service, handler)
|
|
122
|
+
}
|
|
110
123
|
|
|
111
124
|
onMounted(() => {
|
|
112
|
-
if (!enableCache) return
|
|
113
|
-
const cachedData = localStorage.getItem(cacheKey)
|
|
125
|
+
if (!enableCache) return
|
|
126
|
+
const cachedData = localStorage.getItem(cacheKey)
|
|
114
127
|
if (cachedData) {
|
|
115
|
-
assignProperties(JSON.parse(cachedData))
|
|
128
|
+
assignProperties(JSON.parse(cachedData))
|
|
116
129
|
}
|
|
117
130
|
if (initialArgs.length > 0) {
|
|
118
|
-
fetchServiceProperties()
|
|
131
|
+
fetchServiceProperties()
|
|
119
132
|
}
|
|
120
|
-
|
|
121
|
-
});
|
|
133
|
+
})
|
|
122
134
|
|
|
123
135
|
return {
|
|
124
136
|
loading,
|
|
125
137
|
service: new Proxy(service, handler),
|
|
126
|
-
|
|
138
|
+
confirmAction,
|
|
127
139
|
props
|
|
128
|
-
}
|
|
129
|
-
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
130
142
|
|
|
131
|
-
export default useStreamline
|
|
143
|
+
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
|
}
|