@jcbuisson/express-x-client 1.0.2 → 1.0.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/package.json +1 -1
- package/src/express-x-client.js +27 -20
package/package.json
CHANGED
package/src/express-x-client.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
|
|
2
|
-
import { io } from 'socket.io-client'
|
|
3
2
|
import { v4 } from 'uuid'
|
|
4
3
|
|
|
5
|
-
function expressxClient() {
|
|
6
|
-
|
|
7
|
-
const socket = io()
|
|
4
|
+
function expressxClient(socket) {
|
|
8
5
|
|
|
9
6
|
const waitingPromises = {}
|
|
10
7
|
const action2service2handlers = {}
|
|
@@ -12,11 +9,13 @@ function expressxClient() {
|
|
|
12
9
|
// on connection
|
|
13
10
|
socket.on("connected", async (connectionId) => {
|
|
14
11
|
console.log('connected', connectionId)
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
|
|
13
|
+
// TODO: configurable
|
|
14
|
+
const accessToken = window.sessionStorage.getItem('feathers-jwt')
|
|
15
|
+
if (accessToken) {
|
|
17
16
|
// disconnect/reconnect: reauthenticate
|
|
18
|
-
console.log('reauthenticate with',
|
|
19
|
-
serviceMethodRequest('authenticate', '
|
|
17
|
+
console.log('reauthenticate with', accessToken)
|
|
18
|
+
serviceMethodRequest('authenticate', 'reauthenticate', accessToken)
|
|
20
19
|
}
|
|
21
20
|
})
|
|
22
21
|
|
|
@@ -41,7 +40,7 @@ function expressxClient() {
|
|
|
41
40
|
if (handler) handler(result)
|
|
42
41
|
})
|
|
43
42
|
|
|
44
|
-
async function serviceMethodRequest(name, action,
|
|
43
|
+
async function serviceMethodRequest(name, action, ...args) {
|
|
45
44
|
const uid = v4()
|
|
46
45
|
const promise = new Promise((resolve, reject) => {
|
|
47
46
|
waitingPromises[uid] = [resolve, reject]
|
|
@@ -50,33 +49,41 @@ function expressxClient() {
|
|
|
50
49
|
uid,
|
|
51
50
|
name,
|
|
52
51
|
action,
|
|
53
|
-
|
|
52
|
+
args,
|
|
54
53
|
})
|
|
55
54
|
return promise
|
|
56
55
|
}
|
|
57
56
|
|
|
58
57
|
function service(name) {
|
|
59
|
-
|
|
60
|
-
create: (data) => serviceMethodRequest(name, 'create', [data]),
|
|
61
|
-
get: (id) => serviceMethodRequest(name, 'create', [id]),
|
|
62
|
-
patch: (id, data) => serviceMethodRequest(name, 'create', [id, data]),
|
|
63
|
-
update: (id, data) => serviceMethodRequest(name, 'update', [id, data]),
|
|
64
|
-
remove: (id) => serviceMethodRequest(name, 'remove', [id]),
|
|
65
|
-
find: (data) => serviceMethodRequest(name, 'find', [data]),
|
|
66
|
-
|
|
58
|
+
const service = {
|
|
67
59
|
// associate a handler to a pub/sub event for this service
|
|
68
60
|
on: (action, handler) => {
|
|
69
61
|
if (!action2service2handlers[action]) action2service2handlers[action] = {}
|
|
70
62
|
const serviceHandlers = action2service2handlers[action]
|
|
71
63
|
serviceHandlers[name] = handler
|
|
72
|
-
console.log('action2service2handlers', action2service2handlers)
|
|
73
64
|
},
|
|
74
65
|
}
|
|
66
|
+
// use a proxy to allow for any method name for a service
|
|
67
|
+
const handler = {
|
|
68
|
+
get(service, action) {
|
|
69
|
+
if (!(action in service)) {
|
|
70
|
+
// newly used property: define it as the service method request function
|
|
71
|
+
service[action] = (...args) => serviceMethodRequest(name, action, ...args)
|
|
72
|
+
}
|
|
73
|
+
return service[action]
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return new Proxy(service, handler)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function logout() {
|
|
80
|
+
// TODO: configurable
|
|
81
|
+
window.sessionStorage.removeItem('feathers-jwt')
|
|
75
82
|
}
|
|
76
83
|
|
|
77
84
|
return {
|
|
78
85
|
service,
|
|
79
|
-
|
|
86
|
+
logout,
|
|
80
87
|
}
|
|
81
88
|
}
|
|
82
89
|
|