@jcbuisson/express-x 1.8.1 → 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 +1 -1
- package/src/common-hooks.mjs +3 -0
- package/src/context.mjs +6 -6
- package/src/server.mjs +21 -12
package/package.json
CHANGED
package/src/common-hooks.mjs
CHANGED
|
@@ -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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
@@ -156,7 +156,11 @@ export function expressX(prisma, config) {
|
|
|
156
156
|
// hooked version of method to be used server-side
|
|
157
157
|
service[methodName] = (...args) => {
|
|
158
158
|
const context = {
|
|
159
|
-
|
|
159
|
+
app,
|
|
160
|
+
caller: 'server',
|
|
161
|
+
serviceName: service._name,
|
|
162
|
+
methodName,
|
|
163
|
+
args,
|
|
160
164
|
}
|
|
161
165
|
const hookedMethod = service['__' + methodName]
|
|
162
166
|
return hookedMethod(context, ...args)
|
|
@@ -199,16 +203,16 @@ export function expressX(prisma, config) {
|
|
|
199
203
|
*/
|
|
200
204
|
async function addHttpRest(path, service) {
|
|
201
205
|
const context = {
|
|
202
|
-
caller: 'client',
|
|
203
206
|
app,
|
|
207
|
+
caller: 'client',
|
|
204
208
|
transport: 'http',
|
|
205
|
-
|
|
209
|
+
serviceName: service._name,
|
|
210
|
+
|
|
211
|
+
// params: { name: service._name }
|
|
206
212
|
}
|
|
207
213
|
|
|
208
214
|
// introspect schema and return a map: field name => prisma type
|
|
209
215
|
function getTypesMap() {
|
|
210
|
-
// const dmmf = await service.prisma._getDmmf()
|
|
211
|
-
// const fieldDescriptions = dmmf.modelMap[service._name].fields
|
|
212
216
|
const dmmf = service.prisma._runtimeDataModel
|
|
213
217
|
const fieldDescriptions = dmmf.models[service._name].fields
|
|
214
218
|
return fieldDescriptions.reduce((accu, descr) => {
|
|
@@ -220,7 +224,7 @@ export function expressX(prisma, config) {
|
|
|
220
224
|
|
|
221
225
|
app.post(path, async (req, res) => {
|
|
222
226
|
app.log('verbose', `http request POST ${req.url}`)
|
|
223
|
-
context.
|
|
227
|
+
context.req = req
|
|
224
228
|
try {
|
|
225
229
|
const value = await service.__create(context, { data: req.body })
|
|
226
230
|
res.json(value)
|
|
@@ -232,7 +236,7 @@ export function expressX(prisma, config) {
|
|
|
232
236
|
|
|
233
237
|
app.get(path, async (req, res) => {
|
|
234
238
|
app.log('verbose', `http request GET ${req.url}`)
|
|
235
|
-
context.
|
|
239
|
+
context.req = req
|
|
236
240
|
const query = { ...req.query }
|
|
237
241
|
try {
|
|
238
242
|
// the values in `req.query` are all strings, but Prisma need proper types
|
|
@@ -267,7 +271,7 @@ export function expressX(prisma, config) {
|
|
|
267
271
|
|
|
268
272
|
app.get(`${path}/:id`, async (req, res) => {
|
|
269
273
|
app.log('verbose', `http request GET ${req.url}`)
|
|
270
|
-
context.
|
|
274
|
+
context.req = req
|
|
271
275
|
try {
|
|
272
276
|
const value = await service.__findUnique(context, {
|
|
273
277
|
where: {
|
|
@@ -283,7 +287,7 @@ export function expressX(prisma, config) {
|
|
|
283
287
|
|
|
284
288
|
app.patch(`${path}/:id`, async (req, res) => {
|
|
285
289
|
app.log('verbose', `http request PATCH ${req.url}`)
|
|
286
|
-
context.
|
|
290
|
+
context.req = req
|
|
287
291
|
try {
|
|
288
292
|
const value = await service.__update(context, {
|
|
289
293
|
where: {
|
|
@@ -300,7 +304,7 @@ export function expressX(prisma, config) {
|
|
|
300
304
|
|
|
301
305
|
app.delete(`${path}/:id`, async (req, res) => {
|
|
302
306
|
app.log('verbose', `http request DELETE ${req.url}`)
|
|
303
|
-
context.
|
|
307
|
+
context.req = req
|
|
304
308
|
try {
|
|
305
309
|
const value = await service.__delete(context, {
|
|
306
310
|
where: {
|
|
@@ -387,10 +391,15 @@ export function expressX(prisma, config) {
|
|
|
387
391
|
const serviceMethod = service['__' + action]
|
|
388
392
|
if (serviceMethod) {
|
|
389
393
|
const context = {
|
|
390
|
-
caller: 'client',
|
|
391
394
|
app,
|
|
395
|
+
caller: 'client',
|
|
392
396
|
transport: 'ws',
|
|
393
|
-
|
|
397
|
+
connectionId: connection.id,
|
|
398
|
+
serviceName: name,
|
|
399
|
+
methodName: action,
|
|
400
|
+
args,
|
|
401
|
+
|
|
402
|
+
// params: { connectionId: connection.id, name, action, args },
|
|
394
403
|
}
|
|
395
404
|
|
|
396
405
|
try {
|