@jcbuisson/express-x 1.5.35 → 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.
- package/package.json +1 -1
- package/src/server.mjs +29 -18
package/package.json
CHANGED
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
|
|
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,21 +102,20 @@ 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
|
-
|
|
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
|
-
|
|
113
|
-
|
|
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
|
-
console.log('connections', connections)
|
|
118
119
|
for (const channelName of channelNames) {
|
|
119
120
|
app.log('verbose', `service-event ${service.name} ${methodName} ${channelName}`)
|
|
120
121
|
const connectionList = connections.filter(connection => {
|
|
@@ -126,11 +127,8 @@ export function expressX(prisma, options = {}) {
|
|
|
126
127
|
const trimmedResult = result ? JSON.stringify(result).slice(0, 300) : ''
|
|
127
128
|
app.log('verbose', `emit to ${connection.id} ${service.name} ${methodName} ${trimmedResult}`)
|
|
128
129
|
const socket = cnx2Socket[connection.id]
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
continue // SHOULD NOT HAPPEN
|
|
132
|
-
}
|
|
133
|
-
socket.emit('service-event', {
|
|
130
|
+
// emit service event
|
|
131
|
+
socket && socket.emit('service-event', {
|
|
134
132
|
name: service.name,
|
|
135
133
|
action: methodName,
|
|
136
134
|
result,
|
|
@@ -138,20 +136,27 @@ export function expressX(prisma, options = {}) {
|
|
|
138
136
|
}
|
|
139
137
|
}
|
|
140
138
|
}
|
|
141
|
-
|
|
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
|
+
|
|
142
147
|
return context.result
|
|
143
148
|
}
|
|
144
149
|
|
|
150
|
+
// TODO: NOT CLEAR, CREATE ISSUES
|
|
145
151
|
// hooked version of method: `create`, etc., to be called from backend with no context
|
|
146
152
|
service[methodName] = method
|
|
147
|
-
|
|
148
153
|
// un-hooked version of method: `_create`, etc., to be called from backend with no context
|
|
149
154
|
service['_' + methodName] = method
|
|
150
155
|
}
|
|
151
156
|
|
|
152
157
|
// attach pub/sub publish callback
|
|
153
158
|
service.publish = async (func) => {
|
|
154
|
-
service.
|
|
159
|
+
service.publishFunction = func
|
|
155
160
|
},
|
|
156
161
|
|
|
157
162
|
// attach hooks
|
|
@@ -175,6 +180,11 @@ export function expressX(prisma, options = {}) {
|
|
|
175
180
|
callback(app)
|
|
176
181
|
}
|
|
177
182
|
|
|
183
|
+
// set application hooks
|
|
184
|
+
function hooks(hooks) {
|
|
185
|
+
appHooks = hooks
|
|
186
|
+
}
|
|
187
|
+
|
|
178
188
|
/*
|
|
179
189
|
* add an HTTP REST endpoint at `path`, based on `service`
|
|
180
190
|
*/
|
|
@@ -431,6 +441,7 @@ export function expressX(prisma, options = {}) {
|
|
|
431
441
|
createService,
|
|
432
442
|
service,
|
|
433
443
|
configure,
|
|
444
|
+
hooks,
|
|
434
445
|
addHttpRest,
|
|
435
446
|
server,
|
|
436
447
|
joinChannel,
|