@jcbuisson/express-x 1.3.0 → 1.3.2
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/prisma/dev.db +0 -0
- package/src/client.mjs +2 -2
- package/src/server.mjs +15 -15
package/package.json
CHANGED
package/prisma/dev.db
CHANGED
|
Binary file
|
package/src/client.mjs
CHANGED
|
@@ -19,13 +19,13 @@ export function expressXClient(socket, options={}) {
|
|
|
19
19
|
|
|
20
20
|
// on connection
|
|
21
21
|
socket.on("connected", async (connectionId) => {
|
|
22
|
-
if (options.
|
|
22
|
+
if (options.debug) console.log('connected', connectionId)
|
|
23
23
|
if (onConnectionCallback) onConnectionCallback(connectionId)
|
|
24
24
|
})
|
|
25
25
|
|
|
26
26
|
// on receiving response from service request
|
|
27
27
|
socket.on('client-response', ({ uid, error, result }) => {
|
|
28
|
-
if (options.
|
|
28
|
+
if (options.debug) console.log('client-response', uid, error, result)
|
|
29
29
|
if (!waitingPromisesByUid[uid]) return // may not exist because a timeout removed it
|
|
30
30
|
const [resolve, reject] = waitingPromisesByUid[uid]
|
|
31
31
|
if (error) {
|
package/src/server.mjs
CHANGED
|
@@ -37,7 +37,7 @@ export function expressX(options = {}) {
|
|
|
37
37
|
service.prisma = prisma
|
|
38
38
|
service.entity = prismaOptions.entity
|
|
39
39
|
|
|
40
|
-
if (
|
|
40
|
+
if (app.get('logger')) app.get('logger').log('info', `created service '${name}' over table '${prismaOptions.entity}'`)
|
|
41
41
|
return service
|
|
42
42
|
}
|
|
43
43
|
|
|
@@ -68,7 +68,7 @@ export function expressX(options = {}) {
|
|
|
68
68
|
|
|
69
69
|
// call method
|
|
70
70
|
const result = await method(...context.args)
|
|
71
|
-
|
|
71
|
+
if (app.get('logger')) app.get('logger').log('debug', 'result', result)
|
|
72
72
|
|
|
73
73
|
// call 'after' hooks
|
|
74
74
|
const afterMethodHooks = service?.hooks?.after && service.hooks.after[methodName] || []
|
|
@@ -146,7 +146,7 @@ export function expressX(options = {}) {
|
|
|
146
146
|
|
|
147
147
|
|
|
148
148
|
app.post(path, async (req, res) => {
|
|
149
|
-
if (
|
|
149
|
+
if (app.get('logger')) app.get('logger').log('verbose', "http request POST", req.url)
|
|
150
150
|
context.http.req = req
|
|
151
151
|
try {
|
|
152
152
|
const value = await service.__create(context, { data: req.body })
|
|
@@ -159,7 +159,7 @@ export function expressX(options = {}) {
|
|
|
159
159
|
})
|
|
160
160
|
|
|
161
161
|
app.get(path, async (req, res) => {
|
|
162
|
-
if (
|
|
162
|
+
if (app.get('logger')) app.get('logger').log('verbose', "http request GET", req.url)
|
|
163
163
|
context.http.req = req
|
|
164
164
|
const query = { ...req.query }
|
|
165
165
|
try {
|
|
@@ -195,7 +195,7 @@ export function expressX(options = {}) {
|
|
|
195
195
|
})
|
|
196
196
|
|
|
197
197
|
app.get(`${path}/:id`, async (req, res) => {
|
|
198
|
-
if (
|
|
198
|
+
if (app.get('logger')) app.get('logger').log('verbose', "http request GET", req.url)
|
|
199
199
|
context.http.req = req
|
|
200
200
|
try {
|
|
201
201
|
const value = await service.__findUnique(context, {
|
|
@@ -212,7 +212,7 @@ export function expressX(options = {}) {
|
|
|
212
212
|
})
|
|
213
213
|
|
|
214
214
|
app.patch(`${path}/:id`, async (req, res) => {
|
|
215
|
-
if (
|
|
215
|
+
if (app.get('logger')) app.get('logger').log('verbose', "http request PATCH", req.url)
|
|
216
216
|
context.http.req = req
|
|
217
217
|
try {
|
|
218
218
|
const value = await service.__update(context, {
|
|
@@ -230,7 +230,7 @@ export function expressX(options = {}) {
|
|
|
230
230
|
})
|
|
231
231
|
|
|
232
232
|
app.delete(`${path}/:id`, async (req, res) => {
|
|
233
|
-
if (
|
|
233
|
+
if (app.get('logger')) app.get('logger').log('verbose', "http request DELETE", req.url)
|
|
234
234
|
context.http.req = req
|
|
235
235
|
try {
|
|
236
236
|
const value = await service.__delete(context, {
|
|
@@ -246,7 +246,7 @@ export function expressX(options = {}) {
|
|
|
246
246
|
}
|
|
247
247
|
})
|
|
248
248
|
|
|
249
|
-
if (
|
|
249
|
+
if (app.get('logger')) app.get('logger').log('info', `added HTTP endpoints for service '${service.name}' at path '${path}'`)
|
|
250
250
|
}
|
|
251
251
|
|
|
252
252
|
/*
|
|
@@ -261,7 +261,7 @@ export function expressX(options = {}) {
|
|
|
261
261
|
const io = new Server(server)
|
|
262
262
|
|
|
263
263
|
io.on('connection', function(socket) {
|
|
264
|
-
if (
|
|
264
|
+
if (app.get('logger')) app.get('logger').log('verbose', 'Client connected to the WebSocket')
|
|
265
265
|
const connection = {
|
|
266
266
|
id: lastConnectionId++,
|
|
267
267
|
socket,
|
|
@@ -269,7 +269,7 @@ export function expressX(options = {}) {
|
|
|
269
269
|
}
|
|
270
270
|
// store connection in cache
|
|
271
271
|
connections[connection.id] = connection
|
|
272
|
-
if (
|
|
272
|
+
if (app.get('logger')) app.get('logger').log('verbose', 'active connections', Object.keys(connections))
|
|
273
273
|
|
|
274
274
|
// emit 'connection' event for app (expressjs extends EventEmitter)
|
|
275
275
|
app.emit('connection', connection)
|
|
@@ -278,7 +278,7 @@ export function expressX(options = {}) {
|
|
|
278
278
|
socket.emit('connected', connection.id)
|
|
279
279
|
|
|
280
280
|
socket.on('disconnect', () => {
|
|
281
|
-
if (
|
|
281
|
+
if (app.get('logger')) app.get('logger').log('verbose', 'Client disconnected', connection.id)
|
|
282
282
|
delete connections[connection.id]
|
|
283
283
|
})
|
|
284
284
|
|
|
@@ -288,7 +288,7 @@ export function expressX(options = {}) {
|
|
|
288
288
|
* Emit in return a 'client-response' message
|
|
289
289
|
*/
|
|
290
290
|
socket.on('client-request', async ({ uid, name, action, args }) => {
|
|
291
|
-
if (
|
|
291
|
+
if (app.get('logger')) app.get('logger').log('verbose', "client-request", uid, name, action, args)
|
|
292
292
|
if (name in services) {
|
|
293
293
|
const service = services[name]
|
|
294
294
|
try {
|
|
@@ -341,12 +341,12 @@ export function expressX(options = {}) {
|
|
|
341
341
|
const publishFunc = service.publishCallback
|
|
342
342
|
if (publishFunc) {
|
|
343
343
|
const channelNames = await publishFunc(result, app)
|
|
344
|
-
if (
|
|
344
|
+
if (app.get('logger')) app.get('logger').log('verbose', 'publish channels', service.name, action, channelNames)
|
|
345
345
|
for (const channelName of channelNames) {
|
|
346
|
-
if (
|
|
346
|
+
if (app.get('logger')) app.get('logger').log('verbose', 'service-event', service.name, action, channelName)
|
|
347
347
|
const connectionList = Object.values(connections).filter(cnx => cnx.channelNames.has(channelName))
|
|
348
348
|
for (const connection of connectionList) {
|
|
349
|
-
if (
|
|
349
|
+
if (app.get('logger')) app.get('logger').log('verbose', 'emit to', connection.id, service.name, action, result)
|
|
350
350
|
connection.socket.emit('service-event', {
|
|
351
351
|
name: service.name,
|
|
352
352
|
action,
|