@jcbuisson/express-x 1.3.3 → 1.3.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/server.mjs +20 -20
package/package.json
CHANGED
package/src/server.mjs
CHANGED
|
@@ -19,9 +19,9 @@ export function expressX(options = {}) {
|
|
|
19
19
|
let lastConnectionId = 1
|
|
20
20
|
|
|
21
21
|
// logging function - a winston logger must be configured first
|
|
22
|
-
app.log = (severity,
|
|
22
|
+
app.log = (severity, message) => {
|
|
23
23
|
const logger = app.get('logger')
|
|
24
|
-
if (logger) logger.log(severity,
|
|
24
|
+
if (logger) logger.log(severity, message)
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
/*
|
|
@@ -74,7 +74,7 @@ export function expressX(options = {}) {
|
|
|
74
74
|
|
|
75
75
|
// call method
|
|
76
76
|
const result = await method(...context.args)
|
|
77
|
-
app.log('debug',
|
|
77
|
+
app.log('debug', `result ${result}`)
|
|
78
78
|
|
|
79
79
|
// call 'after' hooks
|
|
80
80
|
const afterMethodHooks = service?.hooks?.after && service.hooks.after[methodName] || []
|
|
@@ -152,20 +152,20 @@ export function expressX(options = {}) {
|
|
|
152
152
|
|
|
153
153
|
|
|
154
154
|
app.post(path, async (req, res) => {
|
|
155
|
-
app.log('verbose',
|
|
155
|
+
app.log('verbose', `http request POST ${req.url}`)
|
|
156
156
|
context.http.req = req
|
|
157
157
|
try {
|
|
158
158
|
const value = await service.__create(context, { data: req.body })
|
|
159
159
|
publish(service, 'create', value)
|
|
160
160
|
res.json(value)
|
|
161
161
|
} catch(err) {
|
|
162
|
-
|
|
162
|
+
app.log('error', err)
|
|
163
163
|
res.status(500).send(err.toString())
|
|
164
164
|
}
|
|
165
165
|
})
|
|
166
166
|
|
|
167
167
|
app.get(path, async (req, res) => {
|
|
168
|
-
app.log('verbose',
|
|
168
|
+
app.log('verbose', `http request GET ${req.url}`)
|
|
169
169
|
context.http.req = req
|
|
170
170
|
const query = { ...req.query }
|
|
171
171
|
try {
|
|
@@ -195,13 +195,13 @@ export function expressX(options = {}) {
|
|
|
195
195
|
publish(service, 'findMany', values)
|
|
196
196
|
res.json(values)
|
|
197
197
|
} catch(err) {
|
|
198
|
-
|
|
198
|
+
app.log('error', err)
|
|
199
199
|
res.status(500).send(err.toString())
|
|
200
200
|
}
|
|
201
201
|
})
|
|
202
202
|
|
|
203
203
|
app.get(`${path}/:id`, async (req, res) => {
|
|
204
|
-
app.log('verbose',
|
|
204
|
+
app.log('verbose', `http request GET ${req.url}`)
|
|
205
205
|
context.http.req = req
|
|
206
206
|
try {
|
|
207
207
|
const value = await service.__findUnique(context, {
|
|
@@ -212,13 +212,13 @@ export function expressX(options = {}) {
|
|
|
212
212
|
publish(service, 'findUnique', value)
|
|
213
213
|
res.json(value)
|
|
214
214
|
} catch(err) {
|
|
215
|
-
|
|
215
|
+
app.log('error', err)
|
|
216
216
|
res.status(500).send(err.toString())
|
|
217
217
|
}
|
|
218
218
|
})
|
|
219
219
|
|
|
220
220
|
app.patch(`${path}/:id`, async (req, res) => {
|
|
221
|
-
app.log('verbose',
|
|
221
|
+
app.log('verbose', `http request PATCH ${req.url}`)
|
|
222
222
|
context.http.req = req
|
|
223
223
|
try {
|
|
224
224
|
const value = await service.__update(context, {
|
|
@@ -230,13 +230,13 @@ export function expressX(options = {}) {
|
|
|
230
230
|
publish(service, 'update', value)
|
|
231
231
|
res.json(value)
|
|
232
232
|
} catch(err) {
|
|
233
|
-
|
|
233
|
+
app.log('error', err)
|
|
234
234
|
res.status(500).send(err.toString())
|
|
235
235
|
}
|
|
236
236
|
})
|
|
237
237
|
|
|
238
238
|
app.delete(`${path}/:id`, async (req, res) => {
|
|
239
|
-
app.log('verbose',
|
|
239
|
+
app.log('verbose', `http request DELETE ${req.url}`)
|
|
240
240
|
context.http.req = req
|
|
241
241
|
try {
|
|
242
242
|
const value = await service.__delete(context, {
|
|
@@ -247,7 +247,7 @@ export function expressX(options = {}) {
|
|
|
247
247
|
publish(service, 'delete', value)
|
|
248
248
|
res.json(value)
|
|
249
249
|
} catch(err) {
|
|
250
|
-
|
|
250
|
+
app.log('error', err)
|
|
251
251
|
res.status(500).send(err.toString())
|
|
252
252
|
}
|
|
253
253
|
})
|
|
@@ -275,7 +275,7 @@ export function expressX(options = {}) {
|
|
|
275
275
|
}
|
|
276
276
|
// store connection in cache
|
|
277
277
|
connections[connection.id] = connection
|
|
278
|
-
app.log('verbose',
|
|
278
|
+
app.log('verbose', `active connections ${Object.keys(connections)}`)
|
|
279
279
|
|
|
280
280
|
// emit 'connection' event for app (expressjs extends EventEmitter)
|
|
281
281
|
app.emit('connection', connection)
|
|
@@ -284,7 +284,7 @@ export function expressX(options = {}) {
|
|
|
284
284
|
socket.emit('connected', connection.id)
|
|
285
285
|
|
|
286
286
|
socket.on('disconnect', () => {
|
|
287
|
-
app.log('verbose',
|
|
287
|
+
app.log('verbose', `Client disconnected ${connection.id}`)
|
|
288
288
|
delete connections[connection.id]
|
|
289
289
|
})
|
|
290
290
|
|
|
@@ -294,7 +294,7 @@ export function expressX(options = {}) {
|
|
|
294
294
|
* Emit in return a 'client-response' message
|
|
295
295
|
*/
|
|
296
296
|
socket.on('client-request', async ({ uid, name, action, args }) => {
|
|
297
|
-
app.log('verbose',
|
|
297
|
+
app.log('verbose', `client-request ${uid} ${name} ${action} ${args}`)
|
|
298
298
|
if (name in services) {
|
|
299
299
|
const service = services[name]
|
|
300
300
|
try {
|
|
@@ -314,7 +314,7 @@ export function expressX(options = {}) {
|
|
|
314
314
|
// pub/sub: send event on associated channels
|
|
315
315
|
publish(service, action, result)
|
|
316
316
|
} catch(err) {
|
|
317
|
-
|
|
317
|
+
app.log('error', err)
|
|
318
318
|
io.emit('client-response', {
|
|
319
319
|
uid,
|
|
320
320
|
error: err.toString(),
|
|
@@ -347,12 +347,12 @@ export function expressX(options = {}) {
|
|
|
347
347
|
const publishFunc = service.publishCallback
|
|
348
348
|
if (publishFunc) {
|
|
349
349
|
const channelNames = await publishFunc(result, app)
|
|
350
|
-
app.log('verbose',
|
|
350
|
+
app.log('verbose', `publish channels ${service.name} ${action} ${channelNames}`)
|
|
351
351
|
for (const channelName of channelNames) {
|
|
352
|
-
app.log('verbose',
|
|
352
|
+
app.log('verbose', `service-event ${service.name} ${action} ${channelName}`)
|
|
353
353
|
const connectionList = Object.values(connections).filter(cnx => cnx.channelNames.has(channelName))
|
|
354
354
|
for (const connection of connectionList) {
|
|
355
|
-
app.log('verbose',
|
|
355
|
+
app.log('verbose', `emit to ${connection.id} ${service.name} ${action} ${result}`)
|
|
356
356
|
connection.socket.emit('service-event', {
|
|
357
357
|
name: service.name,
|
|
358
358
|
action,
|