@jcbuisson/express-x 1.1.3 → 1.1.4
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 +3 -2
- package/prisma/dev.db +0 -0
- package/src/index.mjs +5 -1
- package/test/index.test.js +47 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jcbuisson/express-x",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.mjs",
|
|
@@ -15,9 +15,10 @@
|
|
|
15
15
|
"test": "mocha --require esm 'test/**/*.test.js'",
|
|
16
16
|
"generate": "npx prisma generate",
|
|
17
17
|
"migrate": "npx prisma migrate dev --name init"
|
|
18
|
-
},
|
|
18
|
+
},
|
|
19
19
|
"keywords": [],
|
|
20
20
|
"dependencies": {
|
|
21
|
+
"@jcbuisson/express-x-client": "^1.0.10",
|
|
21
22
|
"@prisma/client": "^4.10.1",
|
|
22
23
|
"axios": "^1.4.0",
|
|
23
24
|
"config": "^3.3.9",
|
package/prisma/dev.db
CHANGED
|
Binary file
|
package/src/index.mjs
CHANGED
|
@@ -144,6 +144,7 @@ function expressX(app, options={}) {
|
|
|
144
144
|
|
|
145
145
|
|
|
146
146
|
app.post(path, async (req, res) => {
|
|
147
|
+
if (options.debug) console.log("http request POST", req)
|
|
147
148
|
context.http.req = req
|
|
148
149
|
try {
|
|
149
150
|
const value = await service.__create(context, { data: req.body })
|
|
@@ -156,6 +157,7 @@ function expressX(app, options={}) {
|
|
|
156
157
|
})
|
|
157
158
|
|
|
158
159
|
app.get(path, async (req, res) => {
|
|
160
|
+
if (options.debug) console.log("http request GET", req)
|
|
159
161
|
context.http.req = req
|
|
160
162
|
const query = { ...req.query }
|
|
161
163
|
try {
|
|
@@ -191,6 +193,7 @@ function expressX(app, options={}) {
|
|
|
191
193
|
})
|
|
192
194
|
|
|
193
195
|
app.get(`${path}/:id`, async (req, res) => {
|
|
196
|
+
if (options.debug) console.log("http request GET", req)
|
|
194
197
|
context.http.req = req
|
|
195
198
|
try {
|
|
196
199
|
const value = await service.__findUnique(context, {
|
|
@@ -207,6 +210,7 @@ function expressX(app, options={}) {
|
|
|
207
210
|
})
|
|
208
211
|
|
|
209
212
|
app.patch(`${path}/:id`, async (req, res) => {
|
|
213
|
+
if (options.debug) console.log("http request PATCH", req)
|
|
210
214
|
context.http.req = req
|
|
211
215
|
try {
|
|
212
216
|
const value = await service.__update(context, {
|
|
@@ -224,6 +228,7 @@ function expressX(app, options={}) {
|
|
|
224
228
|
})
|
|
225
229
|
|
|
226
230
|
app.delete(`${path}/:id`, async (req, res) => {
|
|
231
|
+
if (options.debug) console.log("http request DELETE", req)
|
|
227
232
|
context.http.req = req
|
|
228
233
|
try {
|
|
229
234
|
const value = await service.__delete(context, {
|
|
@@ -331,7 +336,6 @@ function expressX(app, options={}) {
|
|
|
331
336
|
|
|
332
337
|
// publish event on associated channels
|
|
333
338
|
async function publish(service, action, result) {
|
|
334
|
-
console.log('PUB!')
|
|
335
339
|
const publishFunc = service.publishCallback
|
|
336
340
|
if (publishFunc) {
|
|
337
341
|
const channelNames = await publishFunc(result, app)
|
package/test/index.test.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
|
|
2
|
-
import expressX from '../src/index.mjs'
|
|
3
2
|
import express from 'express'
|
|
4
3
|
import bodyParser from 'body-parser'
|
|
5
4
|
import axios from 'axios'
|
|
5
|
+
import io from 'socket.io-client'
|
|
6
|
+
import expressxClient from '@jcbuisson/express-x-client'
|
|
7
|
+
|
|
6
8
|
|
|
7
9
|
import { expect, assert } from 'chai'
|
|
8
10
|
|
|
11
|
+
import expressX from '../src/index.mjs'
|
|
12
|
+
|
|
9
13
|
|
|
10
14
|
// `app` is a regular express application, enhanced with express-x features
|
|
11
15
|
const app = expressX(express(), { debug: false })
|
|
@@ -15,7 +19,7 @@ app.createDatabaseService('Post')
|
|
|
15
19
|
|
|
16
20
|
|
|
17
21
|
|
|
18
|
-
describe('ExpressX API', () => {
|
|
22
|
+
describe('ExpressX API (no running server)', () => {
|
|
19
23
|
|
|
20
24
|
it("can delete all users", async () => {
|
|
21
25
|
const res = await app.service('User').deleteMany()
|
|
@@ -56,17 +60,19 @@ describe('ExpressX API', () => {
|
|
|
56
60
|
|
|
57
61
|
describe('HTTP/REST API', () => {
|
|
58
62
|
|
|
59
|
-
|
|
60
|
-
app.use(bodyParser.json())
|
|
61
|
-
app.use(bodyParser.urlencoded({ extended: false }))
|
|
63
|
+
let chris
|
|
62
64
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
65
|
+
before(() => {
|
|
66
|
+
// add body parsers for http requests
|
|
67
|
+
app.use(bodyParser.json())
|
|
68
|
+
app.use(bodyParser.urlencoded({ extended: false }))
|
|
66
69
|
|
|
67
|
-
|
|
70
|
+
// add http/rest endpoints
|
|
71
|
+
app.addHttpRest('/api/user', app.service('User'))
|
|
72
|
+
app.addHttpRest('/api/post', app.service('Post'))
|
|
68
73
|
|
|
69
|
-
|
|
74
|
+
app.server.listen(8008, () => console.log(`App listening at http://localhost:8008`))
|
|
75
|
+
})
|
|
70
76
|
|
|
71
77
|
it("can create a user", async () => {
|
|
72
78
|
const res = await axios.post('http://localhost:8008/api/user', {
|
|
@@ -104,7 +110,36 @@ describe('HTTP/REST API', () => {
|
|
|
104
110
|
assert(res?.data?.name === "Christophe")
|
|
105
111
|
})
|
|
106
112
|
|
|
107
|
-
|
|
108
|
-
app.server.close()
|
|
113
|
+
after(async () => {
|
|
114
|
+
await app.server.close()
|
|
115
|
+
})
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
// test compatibility with `express-x-client`
|
|
120
|
+
describe('Client API', () => {
|
|
121
|
+
|
|
122
|
+
let clientApp, socket
|
|
123
|
+
|
|
124
|
+
before(() => {
|
|
125
|
+
app.server.listen(8008, () => console.log(`App listening at http://localhost:8008`))
|
|
126
|
+
|
|
127
|
+
socket = io('http://localhost:8008', { transports: ["websocket"] })
|
|
128
|
+
clientApp = expressxClient(socket)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it("can create a user", async () => {
|
|
132
|
+
const user = await clientApp.service('User').create({
|
|
133
|
+
data: {
|
|
134
|
+
name: "chris",
|
|
135
|
+
email: 'chris@mail.fr'
|
|
136
|
+
},
|
|
137
|
+
})
|
|
138
|
+
assert(user.name === 'chris')
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
after(async () => {
|
|
142
|
+
await socket.close()
|
|
143
|
+
await app.server.close()
|
|
109
144
|
})
|
|
110
145
|
})
|