@jcbuisson/express-x 1.0.14 → 1.0.15

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.mjs +9 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
package/src/index.mjs CHANGED
@@ -81,8 +81,8 @@ function expressX(app, options) {
81
81
  service['__' + methodName] = async (context, ...args) => {
82
82
  context.args = args
83
83
 
84
- // if a hook or the method throws an error, it will be caught by `socket.on('client-request'`
85
- // and the client will get a rejected promise
84
+ // if a hook or the method throws an error, it will be caught by `socket.on('client-request'` (ws)
85
+ // or by express (http) and the client will get a rejected promise
86
86
 
87
87
  // call 'before' hooks, modifying `context.args`
88
88
  const beforeMethodHooks = service?.hooks?.before && service.hooks.before[methodName] || []
@@ -143,41 +143,35 @@ function expressX(app, options) {
143
143
  function addHttpRestRoutes(path, service) {
144
144
  const context = {
145
145
  app,
146
- transport: 'http',
147
- name: service.name,
146
+ http: { name: service.name }
148
147
  }
149
148
 
150
149
  app.post(path, async (req, res) => {
151
- context.action = 'create'
152
- context.args = [req.body]
150
+ context.http.req = req
153
151
  const value = await service.__create(context, req.body)
154
152
  res.json(value)
155
153
  })
156
154
 
157
155
  app.get(path, async (req, res) => {
158
- context.action = 'find'
159
- context.args = [req.body]
156
+ context.http.req = req
160
157
  const values = await service.__find(context, req.body)
161
158
  res.json(values)
162
159
  })
163
160
 
164
161
  app.get(`${path}/:id`, async (req, res) => {
165
- context.action = 'get'
166
- context.args = [req.params.id]
162
+ context.http.req = req
167
163
  const value = await service.__get(context, parseInt(req.params.id))
168
164
  res.json(value)
169
165
  })
170
166
 
171
167
  app.patch(`${path}/:id`, async (req, res) => {
172
- context.action = 'patch'
173
- context.args = [req.params.id, req.body]
168
+ context.http.req = req
174
169
  const value = await service.__patch(context, parseInt(req.params.id), req.body)
175
170
  res.json(value)
176
171
  })
177
172
 
178
173
  app.delete(`${path}/:id`, async (req, res) => {
179
- context.action = 'remove'
180
- context.args = [req.params.id]
174
+ context.http.req = req
181
175
  const value = await service.__remove(context, parseInt(req.params.id))
182
176
  res.json(value)
183
177
  })
@@ -227,11 +221,7 @@ function expressX(app, options) {
227
221
  if (serviceMethod) {
228
222
  const context = {
229
223
  app,
230
- transport: 'ws',
231
- connection,
232
- name,
233
- action,
234
- args,
224
+ ws: { connection, name, action, args },
235
225
  }
236
226
  const result = await serviceMethod(context, ...args)
237
227