@jcbuisson/express-x-client 1.6.0 → 1.6.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,25 +1,15 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x-client",
3
- "version": "1.6.0",
3
+ "version": "1.6.2",
4
4
  "type": "module",
5
5
  "description": "Client library for ExpressX framework",
6
6
  "main": "src/index.mjs",
7
7
  "test": "node --test",
8
8
  "scripts": {
9
- "test": "node --test",
10
- "generate": "npx prisma generate",
11
- "migrate": "npx prisma migrate dev --name init"
12
9
  },
13
10
  "keywords": [],
14
11
  "author": "",
15
12
  "license": "ISC",
16
13
  "dependencies": {
17
- "@jcbuisson/express-x": "^1.4.0",
18
- "@prisma/client": "^4.16.1",
19
- "axios": "^1.4.0",
20
- "body-parser": "^1.20.2",
21
- "prisma": "^4.16.1",
22
- "socket.io-client": "^4.7.1",
23
- "uuid": "^9.0.0"
24
14
  }
25
15
  }
package/src/index.mjs CHANGED
@@ -7,6 +7,7 @@ export default function expressXClient(socket, options={}) {
7
7
  const action2service2handlers = {}
8
8
  const type2appHandlers = {}
9
9
  let onConnectionCallback = null
10
+ let onReconnectionCallback = null
10
11
  let onDisconnectionCallback = null
11
12
  let nodeCnxId
12
13
 
@@ -14,6 +15,10 @@ export default function expressXClient(socket, options={}) {
14
15
  onConnectionCallback = callback
15
16
  }
16
17
 
18
+ const setReconnectionCallback = (callback) => {
19
+ onReconnectionCallback = callback
20
+ }
21
+
17
22
  const setDisconnectionCallback = (callback) => {
18
23
  onDisconnectionCallback = callback
19
24
  }
@@ -59,9 +64,11 @@ export default function expressXClient(socket, options={}) {
59
64
  if (onConnectionCallback) onConnectionCallback(connectionId)
60
65
  })
61
66
 
62
- socket.on("cnx-transfer-ack", async (connectionId) => {
63
- if (options.debug) console.log('cnx-transfer-ack', connectionId)
64
- _setCnxId(connectionId)
67
+ socket.on("cnx-transfer-ack", async (connection) => {
68
+ if (options.debug) console.log('cnx-transfer-ack', connection)
69
+ _setCnxId(connection.id)
70
+ // call user-defined reconnection callback
71
+ if (onReconnectionCallback) onReconnectionCallback(connection)
65
72
  })
66
73
 
67
74
 
@@ -186,6 +193,7 @@ export default function expressXClient(socket, options={}) {
186
193
 
187
194
  return {
188
195
  setConnectionCallback,
196
+ setReconnectionCallback,
189
197
  setDisconnectionCallback,
190
198
  service,
191
199
  on,
@@ -1,130 +0,0 @@
1
-
2
- import bodyParser from 'body-parser'
3
- import axios from 'axios'
4
- import io from 'socket.io-client'
5
- import { PrismaClient } from '@prisma/client'
6
-
7
-
8
- import { describe, it, before, after } from 'node:test'
9
- import { strict as assert } from 'node:assert'
10
-
11
- import { expressX } from '@jcbuisson/express-x'
12
- import expressXClient from '../src/index.mjs'
13
-
14
- const prisma = new PrismaClient()
15
-
16
- // `app` is a regular express application, enhanced with services and real-time features
17
- const app = expressX(prisma)
18
-
19
-
20
- describe('HTTP/REST API', () => {
21
-
22
- let chris
23
-
24
- before(async () => {
25
- console.log("before")
26
- // add body parsers for http requests
27
- app.use(bodyParser.json())
28
- app.use(bodyParser.urlencoded({ extended: false }))
29
-
30
- app.createDatabaseService('User')
31
- app.createDatabaseService('Post')
32
-
33
- await app.service('User').deleteMany()
34
- await app.service('Post').deleteMany()
35
-
36
- // add http/rest endpoints
37
- app.addHttpRest('/api/user', app.service('User'))
38
- app.addHttpRest('/api/post', app.service('Post'))
39
-
40
- await new Promise((resolve) => {
41
- app.server.listen(8008, () => {
42
- console.log(`App listening at http://localhost:8008`)
43
- resolve()
44
- })
45
- })
46
- })
47
-
48
- after(() => {
49
- console.log("after")
50
- app.server.close()
51
- })
52
-
53
- it("can create a user", async () => {
54
- const res = await axios.post('http://localhost:8008/api/user', {
55
- name: "chris",
56
- email: 'chris@mail.fr'
57
- })
58
- assert(res?.data?.name === 'chris')
59
- })
60
-
61
- it("can find users", async () => {
62
- const res = await axios.get('http://localhost:8008/api/user')
63
- assert(res?.data?.length > 0)
64
- })
65
-
66
- it("can find a user by name", async () => {
67
- const res = await axios.get('http://localhost:8008/api/user?name=chris')
68
- assert(res?.data?.length > 0)
69
- chris = res.data[0]
70
- })
71
-
72
- it("can find a user by id", async () => {
73
- const res = await axios.get(`http://localhost:8008/api/user/${chris.id}`)
74
- assert(res?.data?.id === chris.id)
75
- })
76
-
77
- it("can patch a user", async () => {
78
- const res = await axios.patch(`http://localhost:8008/api/user/${chris.id}`, {
79
- name: "Christophe",
80
- })
81
- assert(res?.data?.name === "Christophe")
82
- })
83
-
84
- it("can delete a user", async () => {
85
- const res = await axios.delete(`http://localhost:8008/api/user/${chris.id}`)
86
- assert(res?.data?.name === "Christophe")
87
- })
88
-
89
- after(async () => {
90
- app.server.close()
91
- })
92
- })
93
-
94
-
95
- // test compatibility with client API
96
- describe('Client API', () => {
97
-
98
- let clientApp, socket
99
-
100
- before(async () => {
101
- await new Promise((resolve) => {
102
- app.server.listen(8008, () => {
103
- console.log(`App listening at http://localhost:8008`)
104
- resolve()
105
- })
106
- })
107
-
108
- socket = io('http://localhost:8008', { transports: ["websocket"] })
109
- clientApp = expressXClient(socket)
110
- })
111
-
112
- after(async () => {
113
- app.server.close()
114
- })
115
-
116
- it("can create a user", async () => {
117
- const user = await clientApp.service('User').create({
118
- data: {
119
- name: "chris",
120
- email: 'chris@mail.fr'
121
- },
122
- })
123
- assert(user.name === 'chris')
124
- })
125
-
126
- after(async () => {
127
- await socket.close()
128
- await app.server.close()
129
- })
130
- })