@jcbuisson/express-x 1.2.2 → 1.3.1

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",
3
- "version": "1.2.2",
3
+ "version": "1.3.1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
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.debug) console.log('connected', connectionId)
22
+ if (app.get('logger')) app.get('logger').log('info', '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.debug) console.log('client-response', uid, error, result)
28
+ if (app.get('logger')) app.get('logger').log('info', '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
@@ -11,7 +11,6 @@ export function expressX(options = {}) {
11
11
 
12
12
  const app = express()
13
13
 
14
- if (options.debug === undefined) options.debug = true
15
14
  if (options.ws === undefined) options.ws = { ws_prefix: "expressx" }
16
15
 
17
16
  const services = {}
@@ -38,7 +37,7 @@ export function expressX(options = {}) {
38
37
  service.prisma = prisma
39
38
  service.entity = prismaOptions.entity
40
39
 
41
- if (options.debug) console.log(`created service '${name}' over table '${prismaOptions.entity}'`)
40
+ if (app.get('logger')) app.get('logger').log('info', `created service '${name}' over table '${prismaOptions.entity}'`)
42
41
  return service
43
42
  }
44
43
 
@@ -69,7 +68,7 @@ export function expressX(options = {}) {
69
68
 
70
69
  // call method
71
70
  const result = await method(...context.args)
72
- // if (options.debug) console.log('result', result)
71
+ // if (app.get('logger')) app.get('logger').log('info', 'result', result)
73
72
 
74
73
  // call 'after' hooks
75
74
  const afterMethodHooks = service?.hooks?.after && service.hooks.after[methodName] || []
@@ -147,7 +146,7 @@ export function expressX(options = {}) {
147
146
 
148
147
 
149
148
  app.post(path, async (req, res) => {
150
- if (options.debug) console.log("http request POST", req.url)
149
+ if (app.get('logger')) app.get('logger').log('info', "http request POST", req.url)
151
150
  context.http.req = req
152
151
  try {
153
152
  const value = await service.__create(context, { data: req.body })
@@ -160,7 +159,7 @@ export function expressX(options = {}) {
160
159
  })
161
160
 
162
161
  app.get(path, async (req, res) => {
163
- if (options.debug) console.log("http request GET", req.url)
162
+ if (app.get('logger')) app.get('logger').log('info', "http request GET", req.url)
164
163
  context.http.req = req
165
164
  const query = { ...req.query }
166
165
  try {
@@ -196,7 +195,7 @@ export function expressX(options = {}) {
196
195
  })
197
196
 
198
197
  app.get(`${path}/:id`, async (req, res) => {
199
- if (options.debug) console.log("http request GET", req.url)
198
+ if (app.get('logger')) app.get('logger').log('info', "http request GET", req.url)
200
199
  context.http.req = req
201
200
  try {
202
201
  const value = await service.__findUnique(context, {
@@ -213,7 +212,7 @@ export function expressX(options = {}) {
213
212
  })
214
213
 
215
214
  app.patch(`${path}/:id`, async (req, res) => {
216
- if (options.debug) console.log("http request PATCH", req.url)
215
+ if (app.get('logger')) app.get('logger').log('info', "http request PATCH", req.url)
217
216
  context.http.req = req
218
217
  try {
219
218
  const value = await service.__update(context, {
@@ -231,7 +230,7 @@ export function expressX(options = {}) {
231
230
  })
232
231
 
233
232
  app.delete(`${path}/:id`, async (req, res) => {
234
- if (options.debug) console.log("http request DELETE", req.url)
233
+ if (app.get('logger')) app.get('logger').log('info', "http request DELETE", req.url)
235
234
  context.http.req = req
236
235
  try {
237
236
  const value = await service.__delete(context, {
@@ -247,7 +246,7 @@ export function expressX(options = {}) {
247
246
  }
248
247
  })
249
248
 
250
- if (options.debug) console.log(`added HTTP endpoints for service '${service.name}' at path '${path}'`)
249
+ if (app.get('logger')) app.get('logger').log('info', `added HTTP endpoints for service '${service.name}' at path '${path}'`)
251
250
  }
252
251
 
253
252
  /*
@@ -262,7 +261,7 @@ export function expressX(options = {}) {
262
261
  const io = new Server(server)
263
262
 
264
263
  io.on('connection', function(socket) {
265
- if (options.debug) console.log('Client connected to the WebSocket')
264
+ if (app.get('logger')) app.get('logger').log('info', 'Client connected to the WebSocket')
266
265
  const connection = {
267
266
  id: lastConnectionId++,
268
267
  socket,
@@ -270,7 +269,7 @@ export function expressX(options = {}) {
270
269
  }
271
270
  // store connection in cache
272
271
  connections[connection.id] = connection
273
- if (options.debug) console.log('active connections', Object.keys(connections))
272
+ if (app.get('logger')) app.get('logger').log('info', 'active connections', Object.keys(connections))
274
273
 
275
274
  // emit 'connection' event for app (expressjs extends EventEmitter)
276
275
  app.emit('connection', connection)
@@ -279,7 +278,7 @@ export function expressX(options = {}) {
279
278
  socket.emit('connected', connection.id)
280
279
 
281
280
  socket.on('disconnect', () => {
282
- if (options.debug) console.log('Client disconnected', connection.id)
281
+ if (app.get('logger')) app.get('logger').log('info', 'Client disconnected', connection.id)
283
282
  delete connections[connection.id]
284
283
  })
285
284
 
@@ -289,7 +288,7 @@ export function expressX(options = {}) {
289
288
  * Emit in return a 'client-response' message
290
289
  */
291
290
  socket.on('client-request', async ({ uid, name, action, args }) => {
292
- if (options.debug) console.log("client-request", uid, name, action, args)
291
+ if (app.get('logger')) app.get('logger').log('info', "client-request", uid, name, action, args)
293
292
  if (name in services) {
294
293
  const service = services[name]
295
294
  try {
@@ -342,12 +341,12 @@ export function expressX(options = {}) {
342
341
  const publishFunc = service.publishCallback
343
342
  if (publishFunc) {
344
343
  const channelNames = await publishFunc(result, app)
345
- if (options.debug) console.log('publish channels', service.name, action, channelNames)
344
+ if (app.get('logger')) app.get('logger').log('info', 'publish channels', service.name, action, channelNames)
346
345
  for (const channelName of channelNames) {
347
- if (options.debug) console.log('service-event', service.name, action, channelName)
346
+ if (app.get('logger')) app.get('logger').log('info', 'service-event', service.name, action, channelName)
348
347
  const connectionList = Object.values(connections).filter(cnx => cnx.channelNames.has(channelName))
349
348
  for (const connection of connectionList) {
350
- if (options.debug) console.log('emit to', connection.id, service.name, action, result)
349
+ if (app.get('logger')) app.get('logger').log('info', 'emit to', connection.id, service.name, action, result)
351
350
  connection.socket.emit('service-event', {
352
351
  name: service.name,
353
352
  action,