@jcbuisson/express-x 1.2.1 → 1.3.0
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/prisma/dev.db +0 -0
- package/src/client.mjs +2 -2
- package/src/server.mjs +16 -17
package/package.json
CHANGED
package/prisma/dev.db
CHANGED
|
Binary file
|
package/src/client.mjs
CHANGED
|
@@ -19,13 +19,13 @@ export function expressXClient(socket, options={}) {
|
|
|
19
19
|
|
|
20
20
|
// on connection
|
|
21
21
|
socket.on("connected", async (connectionId) => {
|
|
22
|
-
if (options.
|
|
22
|
+
if (options.logger) options.logger.log('info', 'connected', connectionId)
|
|
23
23
|
if (onConnectionCallback) onConnectionCallback(connectionId)
|
|
24
24
|
})
|
|
25
25
|
|
|
26
26
|
// on receiving response from service request
|
|
27
27
|
socket.on('client-response', ({ uid, error, result }) => {
|
|
28
|
-
if (options.
|
|
28
|
+
if (options.logger) options.logger.log('info', 'client-response', uid, error, result)
|
|
29
29
|
if (!waitingPromisesByUid[uid]) return // may not exist because a timeout removed it
|
|
30
30
|
const [resolve, reject] = waitingPromisesByUid[uid]
|
|
31
31
|
if (error) {
|
package/src/server.mjs
CHANGED
|
@@ -7,11 +7,10 @@ import { PrismaClient } from '@prisma/client'
|
|
|
7
7
|
/*
|
|
8
8
|
* Enhance `app` express application with services and real-time features
|
|
9
9
|
*/
|
|
10
|
-
export function expressX(options={
|
|
10
|
+
export function expressX(options = {}) {
|
|
11
11
|
|
|
12
12
|
const app = express()
|
|
13
13
|
|
|
14
|
-
if (options.debug === undefined) options.debug = true
|
|
15
14
|
if (options.ws === undefined) options.ws = { ws_prefix: "expressx" }
|
|
16
15
|
|
|
17
16
|
const services = {}
|
|
@@ -38,7 +37,7 @@ export function expressX(options={ debug: false }) {
|
|
|
38
37
|
service.prisma = prisma
|
|
39
38
|
service.entity = prismaOptions.entity
|
|
40
39
|
|
|
41
|
-
if (options.
|
|
40
|
+
if (options.logger) options.logger.log('info', `created service '${name}' over table '${prismaOptions.entity}'`)
|
|
42
41
|
return service
|
|
43
42
|
}
|
|
44
43
|
|
|
@@ -69,7 +68,7 @@ export function expressX(options={ debug: false }) {
|
|
|
69
68
|
|
|
70
69
|
// call method
|
|
71
70
|
const result = await method(...context.args)
|
|
72
|
-
// if (options.
|
|
71
|
+
// if (options.logger) options.logger.log('info', 'result', result)
|
|
73
72
|
|
|
74
73
|
// call 'after' hooks
|
|
75
74
|
const afterMethodHooks = service?.hooks?.after && service.hooks.after[methodName] || []
|
|
@@ -147,7 +146,7 @@ export function expressX(options={ debug: false }) {
|
|
|
147
146
|
|
|
148
147
|
|
|
149
148
|
app.post(path, async (req, res) => {
|
|
150
|
-
if (options.
|
|
149
|
+
if (options.logger) options.logger.log('info', "http request POST", req.url)
|
|
151
150
|
context.http.req = req
|
|
152
151
|
try {
|
|
153
152
|
const value = await service.__create(context, { data: req.body })
|
|
@@ -160,7 +159,7 @@ export function expressX(options={ debug: false }) {
|
|
|
160
159
|
})
|
|
161
160
|
|
|
162
161
|
app.get(path, async (req, res) => {
|
|
163
|
-
if (options.
|
|
162
|
+
if (options.logger) options.logger.log('info', "http request GET", req.url)
|
|
164
163
|
context.http.req = req
|
|
165
164
|
const query = { ...req.query }
|
|
166
165
|
try {
|
|
@@ -196,7 +195,7 @@ export function expressX(options={ debug: false }) {
|
|
|
196
195
|
})
|
|
197
196
|
|
|
198
197
|
app.get(`${path}/:id`, async (req, res) => {
|
|
199
|
-
if (options.
|
|
198
|
+
if (options.logger) options.logger.log('info', "http request GET", req.url)
|
|
200
199
|
context.http.req = req
|
|
201
200
|
try {
|
|
202
201
|
const value = await service.__findUnique(context, {
|
|
@@ -213,7 +212,7 @@ export function expressX(options={ debug: false }) {
|
|
|
213
212
|
})
|
|
214
213
|
|
|
215
214
|
app.patch(`${path}/:id`, async (req, res) => {
|
|
216
|
-
if (options.
|
|
215
|
+
if (options.logger) options.logger.log('info', "http request PATCH", req.url)
|
|
217
216
|
context.http.req = req
|
|
218
217
|
try {
|
|
219
218
|
const value = await service.__update(context, {
|
|
@@ -231,7 +230,7 @@ export function expressX(options={ debug: false }) {
|
|
|
231
230
|
})
|
|
232
231
|
|
|
233
232
|
app.delete(`${path}/:id`, async (req, res) => {
|
|
234
|
-
if (options.
|
|
233
|
+
if (options.logger) options.logger.log('info', "http request DELETE", req.url)
|
|
235
234
|
context.http.req = req
|
|
236
235
|
try {
|
|
237
236
|
const value = await service.__delete(context, {
|
|
@@ -247,7 +246,7 @@ export function expressX(options={ debug: false }) {
|
|
|
247
246
|
}
|
|
248
247
|
})
|
|
249
248
|
|
|
250
|
-
if (options.
|
|
249
|
+
if (options.logger) options.logger.log('info', `added HTTP endpoints for service '${service.name}' at path '${path}'`)
|
|
251
250
|
}
|
|
252
251
|
|
|
253
252
|
/*
|
|
@@ -262,7 +261,7 @@ export function expressX(options={ debug: false }) {
|
|
|
262
261
|
const io = new Server(server)
|
|
263
262
|
|
|
264
263
|
io.on('connection', function(socket) {
|
|
265
|
-
if (options.
|
|
264
|
+
if (options.logger) options.logger.log('info', 'Client connected to the WebSocket')
|
|
266
265
|
const connection = {
|
|
267
266
|
id: lastConnectionId++,
|
|
268
267
|
socket,
|
|
@@ -270,7 +269,7 @@ export function expressX(options={ debug: false }) {
|
|
|
270
269
|
}
|
|
271
270
|
// store connection in cache
|
|
272
271
|
connections[connection.id] = connection
|
|
273
|
-
if (options.
|
|
272
|
+
if (options.logger) options.logger.log('info', 'active connections', Object.keys(connections))
|
|
274
273
|
|
|
275
274
|
// emit 'connection' event for app (expressjs extends EventEmitter)
|
|
276
275
|
app.emit('connection', connection)
|
|
@@ -279,7 +278,7 @@ export function expressX(options={ debug: false }) {
|
|
|
279
278
|
socket.emit('connected', connection.id)
|
|
280
279
|
|
|
281
280
|
socket.on('disconnect', () => {
|
|
282
|
-
if (options.
|
|
281
|
+
if (options.logger) options.logger.log('info', 'Client disconnected', connection.id)
|
|
283
282
|
delete connections[connection.id]
|
|
284
283
|
})
|
|
285
284
|
|
|
@@ -289,7 +288,7 @@ export function expressX(options={ debug: false }) {
|
|
|
289
288
|
* Emit in return a 'client-response' message
|
|
290
289
|
*/
|
|
291
290
|
socket.on('client-request', async ({ uid, name, action, args }) => {
|
|
292
|
-
if (options.
|
|
291
|
+
if (options.logger) options.logger.log('info', "client-request", uid, name, action, args)
|
|
293
292
|
if (name in services) {
|
|
294
293
|
const service = services[name]
|
|
295
294
|
try {
|
|
@@ -342,12 +341,12 @@ export function expressX(options={ debug: false }) {
|
|
|
342
341
|
const publishFunc = service.publishCallback
|
|
343
342
|
if (publishFunc) {
|
|
344
343
|
const channelNames = await publishFunc(result, app)
|
|
345
|
-
if (options.
|
|
344
|
+
if (options.logger) options.logger.log('info', 'publish channels', service.name, action, channelNames)
|
|
346
345
|
for (const channelName of channelNames) {
|
|
347
|
-
if (options.
|
|
346
|
+
if (options.logger) options.logger.log('info', 'service-event', service.name, action, channelName)
|
|
348
347
|
const connectionList = Object.values(connections).filter(cnx => cnx.channelNames.has(channelName))
|
|
349
348
|
for (const connection of connectionList) {
|
|
350
|
-
if (options.
|
|
349
|
+
if (options.logger) options.logger.log('info', 'emit to', connection.id, service.name, action, result)
|
|
351
350
|
connection.socket.emit('service-event', {
|
|
352
351
|
name: service.name,
|
|
353
352
|
action,
|