@jcbuisson/express-x 1.7.13 → 1.8.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/server.mjs +25 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.7.13",
3
+ "version": "1.8.1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
package/src/server.mjs CHANGED
@@ -88,7 +88,7 @@ export function expressX(prisma, config) {
88
88
  * create a service `name` with given `methods`
89
89
  */
90
90
  function createService(name, methods) {
91
- const service = { name }
91
+ const service = { _name: name }
92
92
 
93
93
  for (const methodName in methods) {
94
94
  const method = methods[methodName]
@@ -105,8 +105,8 @@ export function expressX(prisma, config) {
105
105
 
106
106
  // call 'before' hooks, possibly modifying `context`
107
107
  const beforeAppHooks = appHooks?.before || []
108
- const beforeMethodHooks = service?.hooks?.before && service.hooks.before[methodName] || []
109
- const beforeAllHooks = service?.hooks?.before?.all || []
108
+ const beforeMethodHooks = service?._hooks?.before && service._hooks.before[methodName] || []
109
+ const beforeAllHooks = service?._hooks?.before?.all || []
110
110
  for (const hook of [...beforeAppHooks, ...beforeMethodHooks, ...beforeAllHooks]) {
111
111
  await hook(context)
112
112
  }
@@ -117,8 +117,8 @@ export function expressX(prisma, config) {
117
117
  context.result = result
118
118
 
119
119
  // call 'after' hooks, possibly modifying `context`
120
- const afterMethodHooks = service?.hooks?.after && service.hooks.after[methodName] || []
121
- const afterAllHooks = service?.hooks?.after?.all || []
120
+ const afterMethodHooks = service?._hooks?.after && service._hooks.after[methodName] || []
121
+ const afterAllHooks = service?._hooks?.after?.all || []
122
122
  const afterAppHooks = appHooks?.after || []
123
123
  for (const hook of [...afterMethodHooks, ...afterAllHooks, ...afterAppHooks]) {
124
124
  await hook(context)
@@ -127,10 +127,10 @@ export function expressX(prisma, config) {
127
127
  // publish event (websocket transport)
128
128
  if (config.WS_TRANSPORT && service.publishFunction) {
129
129
  const channelNames = await service.publishFunction(context)
130
- app.log('verbose', `publish channels ${service.name} ${methodName} ${channelNames}`)
130
+ app.log('verbose', `publish channels ${service._name} ${methodName} ${channelNames}`)
131
131
  const connections = await app.prisma.Connection.findMany({})
132
132
  for (const channelName of channelNames) {
133
- app.log('verbose', `service-event ${service.name} ${methodName} ${channelName}`)
133
+ app.log('verbose', `service-event ${service._name} ${methodName} ${channelName}`)
134
134
  const connectionList = connections.filter(connection => {
135
135
  const channelNames = JSON.parse(connection.channelNames)
136
136
  return channelNames.includes(channelName)
@@ -138,11 +138,11 @@ export function expressX(prisma, config) {
138
138
 
139
139
  for (const connection of connectionList) {
140
140
  const trimmedResult = result ? JSON.stringify(result).slice(0, 300) : ''
141
- app.log('verbose', `emit to ${connection.id} ${service.name} ${methodName} ${trimmedResult}`)
141
+ app.log('verbose', `emit to ${connection.id} ${service._name} ${methodName} ${trimmedResult}`)
142
142
  const socket = getSocket(connection.id)
143
143
  // emit service event
144
144
  socket && socket.emit('service-event', {
145
- name: service.name,
145
+ name: service._name,
146
146
  action: methodName,
147
147
  result,
148
148
  })
@@ -153,11 +153,14 @@ export function expressX(prisma, config) {
153
153
  return context.result
154
154
  }
155
155
 
156
- // TODO: NOT CLEAR AND PROBABLY USELESS
157
- // hooked version of method: `create`, etc., to be called from backend with no context
158
- service[methodName] = method
159
- // un-hooked version of method: `_create`, etc., to be called from backend with no context
160
- service['_' + methodName] = method
156
+ // hooked version of method to be used server-side
157
+ service[methodName] = (...args) => {
158
+ const context = {
159
+ caller: 'server'
160
+ }
161
+ const hookedMethod = service['__' + methodName]
162
+ return hookedMethod(context, ...args)
163
+ }
161
164
  }
162
165
 
163
166
  // attach pub/sub publish callback
@@ -167,7 +170,7 @@ export function expressX(prisma, config) {
167
170
 
168
171
  // attach hooks
169
172
  service.hooks = (hooks) => {
170
- service.hooks = hooks
173
+ service._hooks = hooks
171
174
  }
172
175
 
173
176
  // cache service in `services`
@@ -196,23 +199,25 @@ export function expressX(prisma, config) {
196
199
  */
197
200
  async function addHttpRest(path, service) {
198
201
  const context = {
202
+ caller: 'client',
199
203
  app,
200
204
  transport: 'http',
201
- params: { name: service.name }
205
+ params: { name: service._name }
202
206
  }
203
207
 
204
208
  // introspect schema and return a map: field name => prisma type
205
209
  function getTypesMap() {
206
210
  // const dmmf = await service.prisma._getDmmf()
207
- // const fieldDescriptions = dmmf.modelMap[service.name].fields
211
+ // const fieldDescriptions = dmmf.modelMap[service._name].fields
208
212
  const dmmf = service.prisma._runtimeDataModel
209
- const fieldDescriptions = dmmf.models[service.name].fields
213
+ const fieldDescriptions = dmmf.models[service._name].fields
210
214
  return fieldDescriptions.reduce((accu, descr) => {
211
215
  accu[descr.name] = descr.type
212
216
  return accu
213
217
  }, {})
214
218
  }
215
219
 
220
+
216
221
  app.post(path, async (req, res) => {
217
222
  app.log('verbose', `http request POST ${req.url}`)
218
223
  context.params.req = req
@@ -309,7 +314,7 @@ export function expressX(prisma, config) {
309
314
  }
310
315
  })
311
316
 
312
- app.log('info', `added HTTP endpoints for service '${service.name}' at path '${path}'`)
317
+ app.log('info', `added HTTP endpoints for service '${service._name}' at path '${path}'`)
313
318
  }
314
319
 
315
320
  /*
@@ -382,6 +387,7 @@ export function expressX(prisma, config) {
382
387
  const serviceMethod = service['__' + action]
383
388
  if (serviceMethod) {
384
389
  const context = {
390
+ caller: 'client',
385
391
  app,
386
392
  transport: 'ws',
387
393
  params: { connectionId: connection.id, name, action, args },