@iankibetsh/vue-streamline 1.3.4 → 1.3.6
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 +24 -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,8 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
11
12
|
const cacheKey = `streamline_${stream}_${initialArgs.join('_')}`
|
|
12
13
|
|
|
13
14
|
// Inject headers and API endpoint
|
|
14
|
-
const streamlineUrl = inject('streamlineUrl',window.streamlineUrl)
|
|
15
|
-
const enableCache = inject('enableCache',window.enableCache)
|
|
15
|
+
const streamlineUrl = inject('streamlineUrl', window.streamlineUrl)
|
|
16
|
+
const enableCache = inject('enableCache', window.enableCache ?? true)
|
|
16
17
|
|
|
17
18
|
const originalProps = reactive({})
|
|
18
19
|
|
|
@@ -30,7 +31,7 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
30
31
|
})
|
|
31
32
|
|
|
32
33
|
const fetchServiceProperties = async (force) => {
|
|
33
|
-
if(!force){
|
|
34
|
+
if (!force) {
|
|
34
35
|
if (loading.value || propertiesFetched.value) return
|
|
35
36
|
}
|
|
36
37
|
|
|
@@ -42,7 +43,9 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
42
43
|
params: initialArgs
|
|
43
44
|
})
|
|
44
45
|
assignProperties(response.data)
|
|
45
|
-
|
|
46
|
+
if (enableCache) {
|
|
47
|
+
await Cache.set(cacheKey, response.data)
|
|
48
|
+
}
|
|
46
49
|
} catch (error) {
|
|
47
50
|
console.error(`Error fetching properties for stream ${stream}`, error)
|
|
48
51
|
throw error
|
|
@@ -64,10 +67,10 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
64
67
|
}
|
|
65
68
|
|
|
66
69
|
return (...args) => {
|
|
67
|
-
if(prop === 'refresh' || prop === 'reload'){
|
|
70
|
+
if (prop === 'refresh' || prop === 'reload') {
|
|
68
71
|
return fetchServiceProperties(true)
|
|
69
72
|
}
|
|
70
|
-
if(prop === 'confirm'){
|
|
73
|
+
if (prop === 'confirm') {
|
|
71
74
|
return confirmAction(args[0] ?? 'Are you sure?')
|
|
72
75
|
}
|
|
73
76
|
let repo = null
|
|
@@ -84,7 +87,7 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
84
87
|
}
|
|
85
88
|
})
|
|
86
89
|
if (confirmationMessage.value) {
|
|
87
|
-
|
|
90
|
+
repo = shRepo.runPlainRequest(streamlineUrl, null, confirmationMessage.value, data)
|
|
88
91
|
} else {
|
|
89
92
|
repo = shApis
|
|
90
93
|
.doPost(streamlineUrl, data);
|
|
@@ -93,13 +96,13 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
93
96
|
loading.value = true
|
|
94
97
|
return repo.then((response) => {
|
|
95
98
|
loading.value = false
|
|
96
|
-
if(confirmationMessage.value){
|
|
99
|
+
if (confirmationMessage.value) {
|
|
97
100
|
confirmationMessage.value = null
|
|
98
101
|
|
|
99
|
-
if(!response.isConfirmed){
|
|
102
|
+
if (!response.isConfirmed) {
|
|
100
103
|
return
|
|
101
104
|
}
|
|
102
|
-
if(response.value?.success){
|
|
105
|
+
if (response.value?.success) {
|
|
103
106
|
return response.value.response
|
|
104
107
|
} else {
|
|
105
108
|
// throw error
|
|
@@ -108,8 +111,8 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
108
111
|
|
|
109
112
|
}
|
|
110
113
|
|
|
111
|
-
|
|
112
|
-
|
|
114
|
+
return response.data
|
|
115
|
+
})
|
|
113
116
|
.catch((error) => {
|
|
114
117
|
loading.value = false
|
|
115
118
|
console.error(`Error calling ${prop} on stream ${stream}`, error)
|
|
@@ -120,28 +123,15 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
120
123
|
}
|
|
121
124
|
const getActionUrl = (action, ...args) => {
|
|
122
125
|
let newStream = stream
|
|
123
|
-
if(action.includes(':')){
|
|
126
|
+
if (action.includes(':')) {
|
|
124
127
|
[newStream, action] = action.split(':')
|
|
125
128
|
}
|
|
126
129
|
const post = {
|
|
127
130
|
action,
|
|
128
|
-
stream:newStream,
|
|
131
|
+
stream: newStream,
|
|
129
132
|
params: args
|
|
130
133
|
}
|
|
131
134
|
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
135
|
}
|
|
146
136
|
|
|
147
137
|
const service = reactive({})
|
|
@@ -152,15 +142,16 @@ const useStreamline = (stream, ...initialArgs) => {
|
|
|
152
142
|
return new Proxy(service, handler)
|
|
153
143
|
}
|
|
154
144
|
|
|
155
|
-
onMounted(() => {
|
|
145
|
+
onMounted(async () => {
|
|
146
|
+
if (enableCache) {
|
|
147
|
+
const cachedData = await Cache.get(cacheKey)
|
|
148
|
+
if (cachedData) {
|
|
149
|
+
assignProperties(cachedData)
|
|
150
|
+
}
|
|
151
|
+
}
|
|
156
152
|
if (initialArgs.length > 0) {
|
|
157
153
|
fetchServiceProperties()
|
|
158
154
|
}
|
|
159
|
-
if (!enableCache) return
|
|
160
|
-
const cachedData = localStorage.getItem(cacheKey)
|
|
161
|
-
if (cachedData) {
|
|
162
|
-
assignProperties(JSON.parse(cachedData))
|
|
163
|
-
}
|
|
164
155
|
})
|
|
165
156
|
|
|
166
157
|
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
|