@fastify/express 2.1.0 → 2.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/.github/workflows/{nodejs.yml → ci.yml} +1 -0
- package/.taprc +1 -0
- package/README.md +21 -0
- package/index.js +11 -3
- package/package.json +11 -8
- package/test/enhance-request.test.js +118 -0
- package/types/index.d.ts +32 -0
- package/types/index.test-d.ts +25 -0
- package/index.d.ts +0 -20
- package/test/types/index.ts +0 -12
package/.taprc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--no-coverage
|
package/README.md
CHANGED
|
@@ -193,6 +193,27 @@ async function subsystem (fastify, opts) {
|
|
|
193
193
|
}
|
|
194
194
|
```
|
|
195
195
|
|
|
196
|
+
### Wrap Express req in Proxy
|
|
197
|
+
|
|
198
|
+
It is possible to wrap the Express request object in a Proxy by passing `createProxyHandler` function to generate the Proxy handler. The function will receive the Fastify request object as the first parameter.
|
|
199
|
+
|
|
200
|
+
For example using Proxy to expose something from Fastify request into the Express request.
|
|
201
|
+
|
|
202
|
+
```js
|
|
203
|
+
fastify.decorateRequest('welcomeMessage', 'Hello World');
|
|
204
|
+
fastify.register(expressPlugin, {
|
|
205
|
+
createProxyHandler: fastifyRequest => ({
|
|
206
|
+
get (target, prop) {
|
|
207
|
+
if (prop === 'welcomeMessage') {
|
|
208
|
+
return fastifyRequest[prop]
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return target[prop]
|
|
212
|
+
}
|
|
213
|
+
})
|
|
214
|
+
})
|
|
215
|
+
```
|
|
216
|
+
|
|
196
217
|
## TypeScript support
|
|
197
218
|
|
|
198
219
|
To use this module with TypeScript, make sure to install `@types/express`.
|
package/index.js
CHANGED
|
@@ -4,9 +4,10 @@ const fp = require('fastify-plugin')
|
|
|
4
4
|
const Express = require('express')
|
|
5
5
|
const kMiddlewares = Symbol('fastify-express-middlewares')
|
|
6
6
|
|
|
7
|
-
function
|
|
7
|
+
function fastifyExpress (fastify, options, next) {
|
|
8
8
|
const {
|
|
9
|
-
expressHook = 'onRequest'
|
|
9
|
+
expressHook = 'onRequest',
|
|
10
|
+
createProxyHandler
|
|
10
11
|
} = options
|
|
11
12
|
|
|
12
13
|
fastify.decorate('use', use)
|
|
@@ -34,6 +35,11 @@ function expressPlugin (fastify, options, next) {
|
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
function enhanceRequest (req, reply, next) {
|
|
38
|
+
// Allow attaching custom Proxy handlers to Express req
|
|
39
|
+
if (typeof createProxyHandler === 'function') {
|
|
40
|
+
req.raw = new Proxy(req.raw, createProxyHandler(req))
|
|
41
|
+
}
|
|
42
|
+
|
|
37
43
|
req.raw.originalUrl = req.raw.url
|
|
38
44
|
req.raw.id = req.id
|
|
39
45
|
req.raw.hostname = req.hostname
|
|
@@ -87,7 +93,9 @@ function expressPlugin (fastify, options, next) {
|
|
|
87
93
|
next()
|
|
88
94
|
}
|
|
89
95
|
|
|
90
|
-
module.exports = fp(
|
|
96
|
+
module.exports = fp(fastifyExpress, {
|
|
91
97
|
fastify: '4.x',
|
|
92
98
|
name: '@fastify/express'
|
|
93
99
|
})
|
|
100
|
+
module.exports.default = fastifyExpress
|
|
101
|
+
module.exports.fastifyExpress = fastifyExpress
|
package/package.json
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/express",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "Express compatibility layer for Fastify",
|
|
5
5
|
"main": "index.js",
|
|
6
|
-
"types": "index.d.ts",
|
|
6
|
+
"types": "types/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"lint": "standard",
|
|
9
|
+
"test": "npm run test:unit && npm run test:typescript",
|
|
10
|
+
"test:unit": "tap",
|
|
11
|
+
"test:typescript": "tsd"
|
|
12
|
+
},
|
|
7
13
|
"repository": {
|
|
8
14
|
"type": "git",
|
|
9
15
|
"url": "git+https://github.com/fastify/fastify-express.git"
|
|
@@ -22,12 +28,12 @@
|
|
|
22
28
|
},
|
|
23
29
|
"homepage": "https://github.com/fastify/fastify-express#readme",
|
|
24
30
|
"devDependencies": {
|
|
31
|
+
"@fastify/formbody": "^7.0.1",
|
|
25
32
|
"@types/express": "^4.17.1",
|
|
26
33
|
"@types/node": "*",
|
|
27
34
|
"body-parser": "^1.20.0",
|
|
28
35
|
"cors": "^2.8.5",
|
|
29
36
|
"fastify": "^4.0.0-rc.3",
|
|
30
|
-
"@fastify/formbody": "^7.0.1",
|
|
31
37
|
"helmet": "^6.0.0",
|
|
32
38
|
"passport": "^0.6.0",
|
|
33
39
|
"passport-http-bearer": "^1.0.1",
|
|
@@ -35,7 +41,7 @@
|
|
|
35
41
|
"simple-get": "^4.0.0",
|
|
36
42
|
"standard": "^17.0.0",
|
|
37
43
|
"tap": "^16.0.0",
|
|
38
|
-
"tsd": "^0.
|
|
44
|
+
"tsd": "^0.25.0"
|
|
39
45
|
},
|
|
40
46
|
"dependencies": {
|
|
41
47
|
"express": "^4.17.1",
|
|
@@ -46,8 +52,5 @@
|
|
|
46
52
|
},
|
|
47
53
|
"publishConfig": {
|
|
48
54
|
"access": "public"
|
|
49
|
-
},
|
|
50
|
-
"scripts": {
|
|
51
|
-
"test": "standard && tap --no-coverage test/*.test.js && tsd"
|
|
52
55
|
}
|
|
53
|
-
}
|
|
56
|
+
}
|
|
@@ -67,3 +67,121 @@ test('trust proxy protocol', (t) => {
|
|
|
67
67
|
})
|
|
68
68
|
})
|
|
69
69
|
})
|
|
70
|
+
|
|
71
|
+
test('passing createProxyHandler sets up a Proxy with Express req', t => {
|
|
72
|
+
t.plan(8)
|
|
73
|
+
const testString = 'test proxy'
|
|
74
|
+
|
|
75
|
+
const fastify = Fastify()
|
|
76
|
+
fastify.register(expressPlugin, {
|
|
77
|
+
createProxyHandler: () => ({
|
|
78
|
+
set (target, prop, value) {
|
|
79
|
+
if (prop === 'customField') {
|
|
80
|
+
t.equal(value, testString)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return Reflect.set(target, prop, value)
|
|
84
|
+
},
|
|
85
|
+
get (target, prop) {
|
|
86
|
+
if (prop === 'customField') {
|
|
87
|
+
t.pass('get customField called')
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return target[prop]
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
.after(() => {
|
|
95
|
+
fastify.use(function (req, res, next) {
|
|
96
|
+
req.customField = testString
|
|
97
|
+
t.equal(req.customField, testString)
|
|
98
|
+
next()
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
fastify.get('/', function (request, reply) {
|
|
103
|
+
reply.send({ hello: 'world' })
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
fastify.listen({ port: 0 }, err => {
|
|
107
|
+
t.error(err)
|
|
108
|
+
|
|
109
|
+
t.teardown(fastify.server.close.bind(fastify.server))
|
|
110
|
+
|
|
111
|
+
sget({
|
|
112
|
+
method: 'GET',
|
|
113
|
+
url: 'http://localhost:' + fastify.server.address().port
|
|
114
|
+
}, (err, response, body) => {
|
|
115
|
+
t.error(err)
|
|
116
|
+
t.equal(response.statusCode, 200)
|
|
117
|
+
t.equal(response.headers['content-length'], '' + body.length)
|
|
118
|
+
t.same(JSON.parse(body), { hello: 'world' })
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
test('createProxyHandler has access to Fastify request object', t => {
|
|
124
|
+
t.plan(12)
|
|
125
|
+
const startTestString = 'original'
|
|
126
|
+
|
|
127
|
+
const fastify = Fastify()
|
|
128
|
+
fastify.decorateRequest('getAndSetFastify', startTestString)
|
|
129
|
+
fastify.decorateRequest('getOnlyFastify', startTestString)
|
|
130
|
+
|
|
131
|
+
fastify.register(expressPlugin, {
|
|
132
|
+
createProxyHandler: fastifyReq => ({
|
|
133
|
+
set (target, prop, value) {
|
|
134
|
+
if (prop === 'getAndSetFastify') {
|
|
135
|
+
t.pass('set to Fastify called')
|
|
136
|
+
return Reflect.set(fastifyReq, prop, value)
|
|
137
|
+
} else if (prop === 'getOnlyFastify') {
|
|
138
|
+
return true
|
|
139
|
+
} else {
|
|
140
|
+
return Reflect.set(target, prop, value)
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
get (target, prop) {
|
|
144
|
+
if (prop === 'getAndSetFastify' || prop === 'getOnlyFastify') {
|
|
145
|
+
// Return something from Fastify req
|
|
146
|
+
t.pass('get from Fastify called')
|
|
147
|
+
return fastifyReq[prop]
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return target[prop]
|
|
151
|
+
}
|
|
152
|
+
})
|
|
153
|
+
})
|
|
154
|
+
.after(() => {
|
|
155
|
+
fastify.use(function (req, res, next) {
|
|
156
|
+
t.equal(req.getAndSetFastify, startTestString)
|
|
157
|
+
t.equal(req.getOnlyFastify, startTestString)
|
|
158
|
+
req.getAndSetFastify = 'updated'
|
|
159
|
+
req.getOnlyFastify = 'updated'
|
|
160
|
+
next()
|
|
161
|
+
})
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
fastify.get('/', function (request, reply) {
|
|
165
|
+
// getOnlyFastify should change and getOnlyFastify should not
|
|
166
|
+
t.equal(request.getAndSetFastify, 'updated')
|
|
167
|
+
t.equal(request.getOnlyFastify, startTestString)
|
|
168
|
+
|
|
169
|
+
reply.send({ hello: 'world' })
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
fastify.listen({ port: 0 }, err => {
|
|
173
|
+
t.error(err)
|
|
174
|
+
|
|
175
|
+
t.teardown(fastify.server.close.bind(fastify.server))
|
|
176
|
+
|
|
177
|
+
sget({
|
|
178
|
+
method: 'GET',
|
|
179
|
+
url: 'http://localhost:' + fastify.server.address().port
|
|
180
|
+
}, (err, response, body) => {
|
|
181
|
+
t.error(err)
|
|
182
|
+
t.equal(response.statusCode, 200)
|
|
183
|
+
t.equal(response.headers['content-length'], '' + body.length)
|
|
184
|
+
t.same(JSON.parse(body), { hello: 'world' })
|
|
185
|
+
})
|
|
186
|
+
})
|
|
187
|
+
})
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Application, Request } from 'express'
|
|
2
|
+
import { FastifyPluginCallback, FastifyRequest } from 'fastify'
|
|
3
|
+
|
|
4
|
+
declare module 'fastify' {
|
|
5
|
+
interface FastifyInstance {
|
|
6
|
+
/**
|
|
7
|
+
* Express middleware function
|
|
8
|
+
*/
|
|
9
|
+
use: Application['use']
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Express application instance
|
|
13
|
+
*/
|
|
14
|
+
express: Application
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type FastifyExpress = FastifyPluginCallback<fastifyExpress.FastifyExpressOptions>
|
|
19
|
+
|
|
20
|
+
declare namespace fastifyExpress {
|
|
21
|
+
|
|
22
|
+
export interface FastifyExpressOptions {
|
|
23
|
+
expressHook?: string;
|
|
24
|
+
createProxyHandler?: (fastifyReq: FastifyRequest) => ProxyHandler<Request>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const fastifyExpress: FastifyExpress
|
|
28
|
+
export { fastifyExpress as default }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
declare function fastifyExpress(...params: Parameters<FastifyExpress>): ReturnType<FastifyExpress>
|
|
32
|
+
export = fastifyExpress
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import Fastify, { FastifyRequest } from 'fastify'
|
|
2
|
+
import fastifyExpress from '..'
|
|
3
|
+
import { expectType } from "tsd"
|
|
4
|
+
import { Application } from 'express'
|
|
5
|
+
|
|
6
|
+
const app = Fastify()
|
|
7
|
+
|
|
8
|
+
app.register(fastifyExpress)
|
|
9
|
+
app.register(fastifyExpress, {
|
|
10
|
+
expressHook: 'onRequest',
|
|
11
|
+
createProxyHandler: (fastifyReq) => ({
|
|
12
|
+
set(target, prop, value) {
|
|
13
|
+
expectType<FastifyRequest>(fastifyReq)
|
|
14
|
+
return Reflect.set(target, prop, value)
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
expectType<Application>(app.express)
|
|
20
|
+
|
|
21
|
+
app.express.disable('x-powered-by')
|
|
22
|
+
|
|
23
|
+
app.use('/world', (_req, res) => {
|
|
24
|
+
res.sendStatus(200)
|
|
25
|
+
})
|
package/index.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Application } from 'express'
|
|
2
|
-
import { FastifyPluginCallback } from 'fastify'
|
|
3
|
-
|
|
4
|
-
declare module 'fastify' {
|
|
5
|
-
interface FastifyInstance {
|
|
6
|
-
/**
|
|
7
|
-
* Express middleware function
|
|
8
|
-
*/
|
|
9
|
-
use: Application['use']
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Express application instance
|
|
13
|
-
*/
|
|
14
|
-
express: Application
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
declare const fastifyExpress: FastifyPluginCallback
|
|
19
|
-
|
|
20
|
-
export default fastifyExpress
|
package/test/types/index.ts
DELETED