@jcbuisson/express-x 1.6.17 → 1.7.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.6.17",
3
+ "version": "1.7.1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
@@ -1,9 +1,6 @@
1
1
 
2
2
  import bcrypt from 'bcryptjs'
3
3
 
4
- import { getConnectionDataItem } from './context.mjs'
5
- import { MyCustomError } from './server.mjs'
6
-
7
4
 
8
5
  // hash password of user record
9
6
  export const hashPassword = (passwordField) => async (context) => {
@@ -25,25 +22,3 @@ export function protect(field) {
25
22
  return (context)
26
23
  }
27
24
  }
28
-
29
-
30
- export async function isAuthenticated(context) {
31
- // extract sessionId from connection data
32
- const sessionId = await getConnectionDataItem(context, 'sessionId')
33
- if (!sessionId) throw new MyCustomError("not authenticated", 'not-authenticated')
34
- }
35
-
36
- export const isNotExpired = async (context) => {
37
- const expireAt = await getConnectionDataItem(context, 'expireAt')
38
- if (expireAt) {
39
- const expireAtDate = new Date(expireAt)
40
- const now = new Date()
41
- if (now > expireAtDate) {
42
- // expiration date is met: clear connection data & throw exception
43
- await resetConnection(context)
44
- throw new MyCustomError("session expired", 'session-expired')
45
- }
46
- } else {
47
- throw new MyCustomError("session expired", 'session-expired')
48
- }
49
- }
package/src/index.mjs CHANGED
@@ -1,10 +1,10 @@
1
1
 
2
- import { expressX, MyCustomError } from './server.mjs'
2
+ import { expressX } from './server.mjs'
3
3
  import { hashPassword, protect, isAuthenticated, isNotExpired } from './common-hooks.mjs'
4
4
  import { getContextConnection, resetConnection, getConnectionDataItem, setConnectionDataItem, removeConnectionDataItem, sendServiceEventToClient } from './context.mjs'
5
5
 
6
6
  export {
7
- expressX, MyCustomError,
7
+ expressX,
8
8
 
9
9
  getContextConnection,
10
10
  resetConnection,
package/src/server.mjs CHANGED
@@ -3,14 +3,6 @@ import http from 'http'
3
3
  import { Server } from "socket.io"
4
4
  import express from 'express'
5
5
 
6
- export class MyCustomError extends Error {
7
- constructor(message, code) {
8
- super(message);
9
- this.name = 'MyCustomError'
10
- this.code = code
11
- }
12
- }
13
-
14
6
  /*
15
7
  * Enhance `app` express application with services and real-time features
16
8
  */
@@ -182,7 +174,7 @@ export function expressX(prisma, options = {}) {
182
174
  function service(name) {
183
175
  // get service from `services` cache
184
176
  if (name in services) return services[name]
185
- throw Error(`there is no service named '${name}'`)
177
+ app.log('error', `there is no service named '${name}'`, 'missing-service')
186
178
  }
187
179
 
188
180
  function configure(callback) {
@@ -389,6 +381,7 @@ export function expressX(prisma, options = {}) {
389
381
 
390
382
  try {
391
383
  const result = await serviceMethod(context, ...args)
384
+
392
385
  const trimmedResult = result ? JSON.stringify(result).slice(0, 300) : ''
393
386
  app.log('verbose', `client-response ${connection.id} ${uid} ${trimmedResult}`)
394
387
  socket.emit('client-response', {
@@ -396,30 +389,43 @@ export function expressX(prisma, options = {}) {
396
389
  result,
397
390
  })
398
391
  } catch(err) {
399
- app.log('error', err.toString())
392
+ console.log('!!!!!!error', err.code, err)
400
393
  app.log('verbose', err.stack)
401
394
  socket.emit('client-response', {
402
395
  uid,
403
- error: new MyCustomError(err.message, err.code),
396
+ error: {
397
+ code: err.code || 'unknown-error',
398
+ message: err.stack,
399
+ }
404
400
  })
405
401
  }
406
402
  } else {
407
403
  socket.emit('client-response', {
408
404
  uid,
409
- error: new MyCustomError(`there is no method named '${action}' for service '${name}'`, 'missing-method'),
405
+ error: {
406
+ code: 'missing-method',
407
+ message: `there is no method named '${action}' for service '${name}'`,
408
+ }
410
409
  })
411
410
  }
412
411
  } catch(error) {
412
+ console.log('err', err)
413
413
  app.log('verbose', error.stack)
414
414
  socket.emit('client-response', {
415
415
  uid,
416
- error: new MyCustomError("unknown error", 'unknown-error'),
417
- })
416
+ error: {
417
+ code: err.code || 'unknown-error',
418
+ message: error.stack,
419
+ }
420
+ })
418
421
  }
419
422
  } else {
420
423
  socket.emit('client-response', {
421
424
  uid,
422
- error: new MyCustomError(`there is no service named '${name}'`, 'missing-service'),
425
+ error: {
426
+ code: 'missing-service',
427
+ message: `there is no service named '${name}'`,
428
+ }
423
429
  })
424
430
  }
425
431
  })
@@ -137,3 +137,33 @@ describe('HTTP/REST API', () => {
137
137
  app.server.close()
138
138
  })
139
139
  })
140
+
141
+
142
+ describe('Error codes', () => {
143
+
144
+ before(async () => {
145
+ app.createDatabaseService('User')
146
+ app.createDatabaseService('Post')
147
+
148
+ await app.service('User').deleteMany()
149
+ await app.service('Post').deleteMany()
150
+ })
151
+
152
+ it("can detect 'missing-service' error", async () => {
153
+ try {
154
+ await app.service('users').findMany({})
155
+ } catch(err) {
156
+ assert(err.code === 'missing-service')
157
+ }
158
+ })
159
+
160
+ // it("can detect 'missing-method' error", async () => {
161
+ // try {
162
+ // await app.service('User').dummyMethod({})
163
+ // } catch(err) {
164
+ // console.log('err', err)
165
+ // assert(err.code === 'missing-method')
166
+ // }
167
+ // })
168
+
169
+ })