@jcbuisson/express-x 1.8.0 → 1.8.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.8.0",
3
+ "version": "1.8.2",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
@@ -32,11 +32,14 @@ export function protect(field) {
32
32
  }
33
33
 
34
34
  /*
35
+ * Does nothing for calls which are not client-side with websocket transport
35
36
  * Check if the 'expireAt' key in connection data is met
36
37
  * If it is met, throw an error (which will be sent back to the calling server or client) and reset connection data
37
38
  * If not, do nothing. If needed, an application-level hook may automatically extend the expiration data at each service call
38
39
  */
39
40
  export const isNotExpired = async (context) => {
41
+ if (context.caller !== 'client' || context.transport !== 'ws') return
42
+
40
43
  const expireAt = await getConnectionDataItem(context, 'expireAt')
41
44
  if (expireAt) {
42
45
  const expireAtDate = new Date(expireAt)
package/src/context.mjs CHANGED
@@ -1,12 +1,12 @@
1
1
 
2
2
  export async function getContextConnection(context) {
3
- const id = context.params.connectionId
3
+ const id = context.connectionId
4
4
  const connection = await context.app.prisma.Connection.findUnique({ where: { id }})
5
5
  return connection
6
6
  }
7
7
 
8
8
  export async function resetConnection(context) {
9
- const id = context.params.connectionId
9
+ const id = context.connectionId
10
10
  // by using updateMany, we cover the case where the connection `id` no longer exists
11
11
  await context.app.prisma.Connection.updateMany({
12
12
  where: { id },
@@ -18,14 +18,14 @@ export async function resetConnection(context) {
18
18
  }
19
19
 
20
20
  export async function getConnectionDataItem(context, key) {
21
- const id = context.params.connectionId
21
+ const id = context.connectionId
22
22
  const connection = await context.app.prisma.Connection.findUnique({ where: { id }})
23
23
  const data = JSON.parse(connection.data)
24
24
  return data[key]
25
25
  }
26
26
 
27
27
  export async function setConnectionDataItem(context, key, value) {
28
- const id = context.params.connectionId
28
+ const id = context.connectionId
29
29
  const connection = await context.app.prisma.Connection.findUnique({ where: { id }})
30
30
  const data = JSON.parse(connection.data)
31
31
  data[key] = value
@@ -38,7 +38,7 @@ export async function setConnectionDataItem(context, key, value) {
38
38
  }
39
39
 
40
40
  export async function removeConnectionDataItem(context, key) {
41
- const id = context.params.connectionId
41
+ const id = context.connectionId
42
42
  const connection = await context.app.prisma.Connection.findUnique({ where: { id }})
43
43
  const data = JSON.parse(connection.data)
44
44
  delete data[key]
@@ -51,7 +51,7 @@ export async function removeConnectionDataItem(context, key) {
51
51
  }
52
52
 
53
53
  export async function sendServiceEventToClient(context, name, action, result) {
54
- const id = context.params.connectionId
54
+ const id = context.connectionId
55
55
  const socket = context.app.cnx2Socket[id]
56
56
  socket.emit('service-event', {
57
57
  name,
package/src/server.mjs CHANGED
@@ -3,8 +3,6 @@ import http from 'http'
3
3
  import { Server } from 'socket.io'
4
4
  import express from 'express'
5
5
 
6
- import { getDMMF } from '@prisma/internals'
7
-
8
6
  /*
9
7
  * Enhance `app` express application with services and real-time features
10
8
  */
@@ -158,7 +156,11 @@ export function expressX(prisma, config) {
158
156
  // hooked version of method to be used server-side
159
157
  service[methodName] = (...args) => {
160
158
  const context = {
161
- caller: 'server'
159
+ app,
160
+ caller: 'server',
161
+ serviceName: service._name,
162
+ methodName,
163
+ args,
162
164
  }
163
165
  const hookedMethod = service['__' + methodName]
164
166
  return hookedMethod(context, ...args)
@@ -201,16 +203,16 @@ export function expressX(prisma, config) {
201
203
  */
202
204
  async function addHttpRest(path, service) {
203
205
  const context = {
204
- caller: 'client',
205
206
  app,
207
+ caller: 'client',
206
208
  transport: 'http',
207
- params: { name: service._name }
209
+ serviceName: service._name,
210
+
211
+ // params: { name: service._name }
208
212
  }
209
213
 
210
214
  // introspect schema and return a map: field name => prisma type
211
215
  function getTypesMap() {
212
- // const dmmf = await service.prisma._getDmmf()
213
- // const fieldDescriptions = dmmf.modelMap[service._name].fields
214
216
  const dmmf = service.prisma._runtimeDataModel
215
217
  const fieldDescriptions = dmmf.models[service._name].fields
216
218
  return fieldDescriptions.reduce((accu, descr) => {
@@ -222,7 +224,7 @@ export function expressX(prisma, config) {
222
224
 
223
225
  app.post(path, async (req, res) => {
224
226
  app.log('verbose', `http request POST ${req.url}`)
225
- context.params.req = req
227
+ context.req = req
226
228
  try {
227
229
  const value = await service.__create(context, { data: req.body })
228
230
  res.json(value)
@@ -234,7 +236,7 @@ export function expressX(prisma, config) {
234
236
 
235
237
  app.get(path, async (req, res) => {
236
238
  app.log('verbose', `http request GET ${req.url}`)
237
- context.params.req = req
239
+ context.req = req
238
240
  const query = { ...req.query }
239
241
  try {
240
242
  // the values in `req.query` are all strings, but Prisma need proper types
@@ -269,7 +271,7 @@ export function expressX(prisma, config) {
269
271
 
270
272
  app.get(`${path}/:id`, async (req, res) => {
271
273
  app.log('verbose', `http request GET ${req.url}`)
272
- context.params.req = req
274
+ context.req = req
273
275
  try {
274
276
  const value = await service.__findUnique(context, {
275
277
  where: {
@@ -285,7 +287,7 @@ export function expressX(prisma, config) {
285
287
 
286
288
  app.patch(`${path}/:id`, async (req, res) => {
287
289
  app.log('verbose', `http request PATCH ${req.url}`)
288
- context.params.req = req
290
+ context.req = req
289
291
  try {
290
292
  const value = await service.__update(context, {
291
293
  where: {
@@ -302,7 +304,7 @@ export function expressX(prisma, config) {
302
304
 
303
305
  app.delete(`${path}/:id`, async (req, res) => {
304
306
  app.log('verbose', `http request DELETE ${req.url}`)
305
- context.params.req = req
307
+ context.req = req
306
308
  try {
307
309
  const value = await service.__delete(context, {
308
310
  where: {
@@ -389,10 +391,15 @@ export function expressX(prisma, config) {
389
391
  const serviceMethod = service['__' + action]
390
392
  if (serviceMethod) {
391
393
  const context = {
392
- caller: 'client',
393
394
  app,
395
+ caller: 'client',
394
396
  transport: 'ws',
395
- params: { connectionId: connection.id, name, action, args },
397
+ connectionId: connection.id,
398
+ serviceName: name,
399
+ methodName: action,
400
+ args,
401
+
402
+ // params: { connectionId: connection.id, name, action, args },
396
403
  }
397
404
 
398
405
  try {