@jcbuisson/express-x 1.5.36 → 1.6.0

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/server.mjs +29 -17
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.5.36",
3
+ "version": "1.6.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
package/src/server.mjs CHANGED
@@ -13,6 +13,7 @@ export function expressX(prisma, options = {}) {
13
13
  if (options.ws === undefined) options.ws = { ws_prefix: "expressx" }
14
14
 
15
15
  const services = {}
16
+ let appHooks = []
16
17
 
17
18
  const cnx2Socket = {}
18
19
 
@@ -89,10 +90,11 @@ export function expressX(prisma, options = {}) {
89
90
  // if a hook or the method throws an error, it will be caught by `socket.on('client-request'` (ws)
90
91
  // or by express (http) and the client will get a rejected promise
91
92
 
92
- // call 'before' hooks, modifying `context.args`
93
+ // call 'before' hooks, modifying `context`
94
+ const beforeAppHooks = appHooks?.before || []
93
95
  const beforeMethodHooks = service?.hooks?.before && service.hooks.before[methodName] || []
94
96
  const beforeAllHooks = service?.hooks?.before?.all || []
95
- for (const hook of [...beforeMethodHooks, ...beforeAllHooks]) {
97
+ for (const hook of [...beforeAppHooks, ...beforeMethodHooks, ...beforeAllHooks]) {
96
98
  await hook(context)
97
99
  }
98
100
 
@@ -100,18 +102,18 @@ export function expressX(prisma, options = {}) {
100
102
  const result = await method(...context.args)
101
103
  // put result into context
102
104
  context.result = result
103
-
104
- // call 'after' hooks
105
+
106
+ // call 'after' hooks, modifying `context`
105
107
  const afterMethodHooks = service?.hooks?.after && service.hooks.after[methodName] || []
106
108
  const afterAllHooks = service?.hooks?.after?.all || []
107
- for (const hook of [...afterMethodHooks, ...afterAllHooks]) {
109
+ const afterAppHooks = appHooks?.after || []
110
+ for (const hook of [...afterMethodHooks, ...afterAllHooks, ...afterAppHooks]) {
108
111
  await hook(context)
109
112
  }
110
113
 
111
- // publish event
112
- const publishFunc = service.publishCallback
113
- if (publishFunc) {
114
- const channelNames = await publishFunc(result, app)
114
+ // publish event (websocket transport)
115
+ if (options.ws && service.publishFunction) {
116
+ const channelNames = await service.publishFunction(result, app)
115
117
  app.log('verbose', `publish channels ${service.name} ${methodName} ${channelNames}`)
116
118
  const connections = await app.prisma.Connection.findMany({})
117
119
  for (const channelName of channelNames) {
@@ -125,11 +127,8 @@ export function expressX(prisma, options = {}) {
125
127
  const trimmedResult = result ? JSON.stringify(result).slice(0, 300) : ''
126
128
  app.log('verbose', `emit to ${connection.id} ${service.name} ${methodName} ${trimmedResult}`)
127
129
  const socket = cnx2Socket[connection.id]
128
- console.log()
129
- if (!socket) {
130
- continue // SHOULD NOT HAPPEN
131
- }
132
- socket.emit('service-event', {
130
+ // emit service event
131
+ socket && socket.emit('service-event', {
133
132
  name: service.name,
134
133
  action: methodName,
135
134
  result,
@@ -137,20 +136,27 @@ export function expressX(prisma, options = {}) {
137
136
  }
138
137
  }
139
138
  }
140
-
139
+
140
+ // AD-HOC, FOR SESSION EXPIRATION
141
+ // emit application event, if any, only to the calling cllient (no pub/sub)
142
+ if (context.appEvent) {
143
+ const socket = cnx2Socket[context?.params?.connectionId]
144
+ socket && socket.emit('app-event', context.appEvent)
145
+ }
146
+
141
147
  return context.result
142
148
  }
143
149
 
150
+ // TODO: NOT CLEAR, CREATE ISSUES
144
151
  // hooked version of method: `create`, etc., to be called from backend with no context
145
152
  service[methodName] = method
146
-
147
153
  // un-hooked version of method: `_create`, etc., to be called from backend with no context
148
154
  service['_' + methodName] = method
149
155
  }
150
156
 
151
157
  // attach pub/sub publish callback
152
158
  service.publish = async (func) => {
153
- service.publishCallback = func
159
+ service.publishFunction = func
154
160
  },
155
161
 
156
162
  // attach hooks
@@ -174,6 +180,11 @@ export function expressX(prisma, options = {}) {
174
180
  callback(app)
175
181
  }
176
182
 
183
+ // set application hooks
184
+ function hooks(hooks) {
185
+ appHooks = hooks
186
+ }
187
+
177
188
  /*
178
189
  * add an HTTP REST endpoint at `path`, based on `service`
179
190
  */
@@ -430,6 +441,7 @@ export function expressX(prisma, options = {}) {
430
441
  createService,
431
442
  service,
432
443
  configure,
444
+ hooks,
433
445
  addHttpRest,
434
446
  server,
435
447
  joinChannel,