@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x-client",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "",
5
5
  "main": "src/express-x-client.js",
6
6
  "repository": {
@@ -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
- const token = window.sessionStorage.getItem('feathers-jwt')
16
- if (token) {
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', token)
19
- serviceMethodRequest('authenticate', 'patch', [token])
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, argList) {
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
- argList,
52
+ args,
54
53
  })
55
54
  return promise
56
55
  }
57
56
 
58
57
  function service(name) {
59
- return {
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
- socket,
86
+ logout,
80
87
  }
81
88
  }
82
89