@iankibetsh/vue-streamline 1.3.4 → 1.3.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 +5 -2
- package/package.json +1 -1
- package/src/App.vue +8 -2
- package/src/composables/useStreamline.js +27 -33
- package/src/main.js +5 -1
- package/src/plugins/streamline.js +2 -2
- package/src/utils/cache.js +113 -0
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ A robust Vue 3 plugin designed for seamless integration with Streamline backend
|
|
|
8
8
|
- **Stateful Streams**: Initial arguments are persisted across all subsequent action calls.
|
|
9
9
|
- **Object Argument Mapping**: Pass objects that automatically populate backend `request()` data and map to method parameters.
|
|
10
10
|
- **Reactive State**: Automatically syncs public properties from your backend Stream classes.
|
|
11
|
-
- **Smart Caching**:
|
|
11
|
+
- **Smart Caching**: IndexedDB caching for instant data availability (enabled by default).
|
|
12
12
|
- **Security & Privacy**: Automatically filters out internal methods and properties from the response payload.
|
|
13
13
|
|
|
14
14
|
## 📦 Installation
|
|
@@ -30,12 +30,15 @@ const app = createApp(App);
|
|
|
30
30
|
|
|
31
31
|
app.use(streamline, {
|
|
32
32
|
streamlineUrl: "https://your-api.com/api/streamline",
|
|
33
|
-
enableCache: true, //
|
|
33
|
+
enableCache: true, // Enabled by default, set to false to disable
|
|
34
34
|
});
|
|
35
35
|
|
|
36
36
|
app.mount("#app");
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
+
> [!NOTE]
|
|
40
|
+
> Caching uses **IndexedDB** (`streamline_cache` database) for persistence, offering higher storage limits and better performance than standard local storage.
|
|
41
|
+
|
|
39
42
|
### 2. Use in Components
|
|
40
43
|
|
|
41
44
|
```vue
|
package/package.json
CHANGED
package/src/App.vue
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
<script setup>
|
|
2
2
|
import useStreamline from './composables/useStreamline.js'
|
|
3
|
-
import { onMounted, ref, toRefs } from 'vue'
|
|
3
|
+
import { onMounted, ref, toRefs,computed } from 'vue'
|
|
4
4
|
import SimpleGetActionUrl from './SimpleGetActionUrl.vue'
|
|
5
5
|
|
|
6
6
|
const {loading, service:paybillService, getActionUrl,props,confirmAction} = useStreamline('mpesa/paybill',32)
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
const existing = computed(()=>props.paybill)
|
|
9
9
|
const foundPaybill = ref(null)
|
|
10
10
|
|
|
11
11
|
onMounted(()=>{
|
|
@@ -41,6 +41,12 @@ const refresh = ()=>{
|
|
|
41
41
|
<h1 @click="refresh">Refresh</h1>
|
|
42
42
|
{{ foundPaybill }}
|
|
43
43
|
</div>
|
|
44
|
+
<div class="alert alert-info">
|
|
45
|
+
<h2>Existing</h2>
|
|
46
|
+
<p>
|
|
47
|
+
{{ existing }}
|
|
48
|
+
</p>
|
|
49
|
+
</div>
|
|
44
50
|
<simple-get-action-url/>
|
|
45
51
|
</template>
|
|
46
52
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { inject, onMounted, reactive, ref } from 'vue'
|
|
2
2
|
import { shApis, shRepo } from '@iankibetsh/shframework'
|
|
3
|
+
import Cache from '../utils/cache'
|
|
3
4
|
|
|
4
5
|
const useStreamline = (stream, ...initialArgs) => {
|
|
5
6
|
let formData = {}
|
|
@@ -11,8 +12,11 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
11
12
|
const cacheKey = `streamline_${stream}_${initialArgs.join('_')}`
|
|
12
13
|
|
|
13
14
|
// Inject headers and API endpoint
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
let streamlineUrl = inject('streamlineUrl', window.streamlineUrl)
|
|
16
|
+
if (streamlineUrl && streamlineUrl.startsWith('/') && !streamlineUrl.startsWith('//') && typeof window !== 'undefined') {
|
|
17
|
+
streamlineUrl = window.location.origin + streamlineUrl
|
|
18
|
+
}
|
|
19
|
+
const enableCache = inject('enableCache', window.enableCache ?? true)
|
|
16
20
|
|
|
17
21
|
const originalProps = reactive({})
|
|
18
22
|
|
|
@@ -30,7 +34,7 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
30
34
|
})
|
|
31
35
|
|
|
32
36
|
const fetchServiceProperties = async (force) => {
|
|
33
|
-
if(!force){
|
|
37
|
+
if (!force) {
|
|
34
38
|
if (loading.value || propertiesFetched.value) return
|
|
35
39
|
}
|
|
36
40
|
|
|
@@ -42,7 +46,9 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
42
46
|
params: initialArgs
|
|
43
47
|
})
|
|
44
48
|
assignProperties(response.data)
|
|
45
|
-
|
|
49
|
+
if (enableCache) {
|
|
50
|
+
await Cache.set(cacheKey, response.data)
|
|
51
|
+
}
|
|
46
52
|
} catch (error) {
|
|
47
53
|
console.error(`Error fetching properties for stream ${stream}`, error)
|
|
48
54
|
throw error
|
|
@@ -64,10 +70,10 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
64
70
|
}
|
|
65
71
|
|
|
66
72
|
return (...args) => {
|
|
67
|
-
if(prop === 'refresh' || prop === 'reload'){
|
|
73
|
+
if (prop === 'refresh' || prop === 'reload') {
|
|
68
74
|
return fetchServiceProperties(true)
|
|
69
75
|
}
|
|
70
|
-
if(prop === 'confirm'){
|
|
76
|
+
if (prop === 'confirm') {
|
|
71
77
|
return confirmAction(args[0] ?? 'Are you sure?')
|
|
72
78
|
}
|
|
73
79
|
let repo = null
|
|
@@ -84,7 +90,7 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
84
90
|
}
|
|
85
91
|
})
|
|
86
92
|
if (confirmationMessage.value) {
|
|
87
|
-
|
|
93
|
+
repo = shRepo.runPlainRequest(streamlineUrl, null, confirmationMessage.value, data)
|
|
88
94
|
} else {
|
|
89
95
|
repo = shApis
|
|
90
96
|
.doPost(streamlineUrl, data);
|
|
@@ -93,13 +99,13 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
93
99
|
loading.value = true
|
|
94
100
|
return repo.then((response) => {
|
|
95
101
|
loading.value = false
|
|
96
|
-
if(confirmationMessage.value){
|
|
102
|
+
if (confirmationMessage.value) {
|
|
97
103
|
confirmationMessage.value = null
|
|
98
104
|
|
|
99
|
-
if(!response.isConfirmed){
|
|
105
|
+
if (!response.isConfirmed) {
|
|
100
106
|
return
|
|
101
107
|
}
|
|
102
|
-
if(response.value?.success){
|
|
108
|
+
if (response.value?.success) {
|
|
103
109
|
return response.value.response
|
|
104
110
|
} else {
|
|
105
111
|
// throw error
|
|
@@ -108,8 +114,8 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
108
114
|
|
|
109
115
|
}
|
|
110
116
|
|
|
111
|
-
|
|
112
|
-
|
|
117
|
+
return response.data
|
|
118
|
+
})
|
|
113
119
|
.catch((error) => {
|
|
114
120
|
loading.value = false
|
|
115
121
|
console.error(`Error calling ${prop} on stream ${stream}`, error)
|
|
@@ -120,28 +126,15 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
120
126
|
}
|
|
121
127
|
const getActionUrl = (action, ...args) => {
|
|
122
128
|
let newStream = stream
|
|
123
|
-
if(action.includes(':')){
|
|
129
|
+
if (action.includes(':')) {
|
|
124
130
|
[newStream, action] = action.split(':')
|
|
125
131
|
}
|
|
126
132
|
const post = {
|
|
127
133
|
action,
|
|
128
|
-
stream:newStream,
|
|
134
|
+
stream: newStream,
|
|
129
135
|
params: args
|
|
130
136
|
}
|
|
131
137
|
return `${streamlineUrl}?${new URLSearchParams(post).toString()}`
|
|
132
|
-
|
|
133
|
-
// // Add each arg as a separate param
|
|
134
|
-
// args.forEach((arg, index) => {
|
|
135
|
-
// let value = arg;
|
|
136
|
-
// if (typeof arg === 'object' && !(arg instanceof Date)) {
|
|
137
|
-
// value = JSON.stringify(arg);
|
|
138
|
-
// } else if (arg instanceof Date) {
|
|
139
|
-
// value = arg.toISOString();
|
|
140
|
-
// }
|
|
141
|
-
// params.append(`params[${index}]`, value);
|
|
142
|
-
// })
|
|
143
|
-
|
|
144
|
-
// return `${streamlineUrl}?${params.toString()}`
|
|
145
138
|
}
|
|
146
139
|
|
|
147
140
|
const service = reactive({})
|
|
@@ -152,15 +145,16 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
152
145
|
return new Proxy(service, handler)
|
|
153
146
|
}
|
|
154
147
|
|
|
155
|
-
onMounted(() => {
|
|
148
|
+
onMounted(async () => {
|
|
149
|
+
if (enableCache) {
|
|
150
|
+
const cachedData = await Cache.get(cacheKey)
|
|
151
|
+
if (cachedData) {
|
|
152
|
+
assignProperties(cachedData)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
156
155
|
if (initialArgs.length > 0) {
|
|
157
156
|
fetchServiceProperties()
|
|
158
157
|
}
|
|
159
|
-
if (!enableCache) return
|
|
160
|
-
const cachedData = localStorage.getItem(cacheKey)
|
|
161
|
-
if (cachedData) {
|
|
162
|
-
assignProperties(JSON.parse(cachedData))
|
|
163
|
-
}
|
|
164
158
|
})
|
|
165
159
|
|
|
166
160
|
return {
|
package/src/main.js
CHANGED
|
@@ -2,16 +2,20 @@ import { createApp } from 'vue'
|
|
|
2
2
|
import './style.css'
|
|
3
3
|
import App from './App.vue'
|
|
4
4
|
import streamline from './plugins/streamline.js'
|
|
5
|
+
import { ShFrontend } from '@iankibetsh/shframework'
|
|
5
6
|
|
|
6
7
|
const streamlineHeaders = {
|
|
7
8
|
Authorization: 'Bearer ' + localStorage.getItem('access_token')
|
|
8
9
|
}
|
|
9
10
|
const streamlineUrl = 'http://2022-sharasms.test/api/streamline'
|
|
10
11
|
const app = createApp(App)
|
|
12
|
+
app.use(ShFrontend, {
|
|
13
|
+
baseApiUrl: 'http://2022-sharasms.test/api'
|
|
14
|
+
})
|
|
11
15
|
app.use(streamline,{
|
|
12
16
|
streamlineHeaders,
|
|
13
17
|
streamlineUrl,
|
|
14
|
-
enableCache:
|
|
18
|
+
enableCache: true
|
|
15
19
|
})
|
|
16
20
|
|
|
17
21
|
app.mount('#app')
|
|
@@ -2,11 +2,11 @@ const streamline = {
|
|
|
2
2
|
install(app, options) {
|
|
3
3
|
app.provide('streamlineUrl', options.streamlineUrl ?? '/api/streamline')
|
|
4
4
|
// app.provide('streamlineHeaders', options.streamlineHeaders)
|
|
5
|
-
app.provide('enableCache', options.enableCache)
|
|
5
|
+
app.provide('enableCache', options.enableCache ?? true)
|
|
6
6
|
|
|
7
7
|
// put them on window
|
|
8
8
|
window.streamlineUrl = options.streamlineUrl ?? '/api/streamline'
|
|
9
|
-
window.enableCache = options.enableCache ??
|
|
9
|
+
window.enableCache = options.enableCache ?? true
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
const DB_NAME = 'streamline_cache'
|
|
2
|
+
const STORE_NAME = 'properties'
|
|
3
|
+
const DB_VERSION = 1
|
|
4
|
+
|
|
5
|
+
const openDB = () => {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
const request = indexedDB.open(DB_NAME, DB_VERSION)
|
|
8
|
+
|
|
9
|
+
request.onupgradeneeded = (event) => {
|
|
10
|
+
const db = event.target.result
|
|
11
|
+
if (!db.objectStoreNames.contains(STORE_NAME)) {
|
|
12
|
+
db.createObjectStore(STORE_NAME)
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
request.onsuccess = (event) => {
|
|
17
|
+
resolve(event.target.result)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
request.onerror = (event) => {
|
|
21
|
+
reject(event.target.error)
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const Cache = {
|
|
27
|
+
async get(key) {
|
|
28
|
+
try {
|
|
29
|
+
const db = await openDB()
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
const transaction = db.transaction(STORE_NAME, 'readonly')
|
|
32
|
+
const store = transaction.objectStore(STORE_NAME)
|
|
33
|
+
const request = store.get(key)
|
|
34
|
+
|
|
35
|
+
request.onsuccess = () => {
|
|
36
|
+
resolve(request.result)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
request.onerror = () => {
|
|
40
|
+
reject(request.error)
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.error('Error getting from IndexedDB cache', error)
|
|
45
|
+
return null
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
async set(key, value) {
|
|
50
|
+
try {
|
|
51
|
+
const db = await openDB()
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
const transaction = db.transaction(STORE_NAME, 'readwrite')
|
|
54
|
+
const store = transaction.objectStore(STORE_NAME)
|
|
55
|
+
const request = store.put(value, key)
|
|
56
|
+
|
|
57
|
+
request.onsuccess = () => {
|
|
58
|
+
resolve()
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
request.onerror = () => {
|
|
62
|
+
reject(request.error)
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
} catch (error) {
|
|
66
|
+
console.error('Error setting in IndexedDB cache', error)
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
async delete(key) {
|
|
71
|
+
try {
|
|
72
|
+
const db = await openDB()
|
|
73
|
+
return new Promise((resolve, reject) => {
|
|
74
|
+
const transaction = db.transaction(STORE_NAME, 'readwrite')
|
|
75
|
+
const store = transaction.objectStore(STORE_NAME)
|
|
76
|
+
const request = store.delete(key)
|
|
77
|
+
|
|
78
|
+
request.onsuccess = () => {
|
|
79
|
+
resolve()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
request.onerror = () => {
|
|
83
|
+
reject(request.error)
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.error('Error deleting from IndexedDB cache', error)
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
async clear() {
|
|
92
|
+
try {
|
|
93
|
+
const db = await openDB()
|
|
94
|
+
return new Promise((resolve, reject) => {
|
|
95
|
+
const transaction = db.transaction(STORE_NAME, 'readwrite')
|
|
96
|
+
const store = transaction.objectStore(STORE_NAME)
|
|
97
|
+
const request = store.clear()
|
|
98
|
+
|
|
99
|
+
request.onsuccess = () => {
|
|
100
|
+
resolve()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
request.onerror = () => {
|
|
104
|
+
reject(request.error)
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
} catch (error) {
|
|
108
|
+
console.error('Error clearing IndexedDB cache', error)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export default Cache
|