@fastify/express 2.2.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/README.md +21 -0
- package/index.js +7 -1
- package/package.json +2 -2
- package/test/enhance-request.test.js +118 -0
- package/types/index.d.ts +3 -2
- package/types/index.test-d.ts +9 -3
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
|
@@ -6,7 +6,8 @@ const kMiddlewares = Symbol('fastify-express-middlewares')
|
|
|
6
6
|
|
|
7
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 fastifyExpress (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
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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
6
|
"types": "types/index.d.ts",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"simple-get": "^4.0.0",
|
|
42
42
|
"standard": "^17.0.0",
|
|
43
43
|
"tap": "^16.0.0",
|
|
44
|
-
"tsd": "^0.
|
|
44
|
+
"tsd": "^0.25.0"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"express": "^4.17.1",
|
|
@@ -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
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Application } from 'express'
|
|
2
|
-
import { FastifyPluginCallback } from 'fastify'
|
|
1
|
+
import { Application, Request } from 'express'
|
|
2
|
+
import { FastifyPluginCallback, FastifyRequest } from 'fastify'
|
|
3
3
|
|
|
4
4
|
declare module 'fastify' {
|
|
5
5
|
interface FastifyInstance {
|
|
@@ -21,6 +21,7 @@ declare namespace fastifyExpress {
|
|
|
21
21
|
|
|
22
22
|
export interface FastifyExpressOptions {
|
|
23
23
|
expressHook?: string;
|
|
24
|
+
createProxyHandler?: (fastifyReq: FastifyRequest) => ProxyHandler<Request>
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
export const fastifyExpress: FastifyExpress
|
package/types/index.test-d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import Fastify from 'fastify'
|
|
1
|
+
import Fastify, { FastifyRequest } from 'fastify'
|
|
2
2
|
import fastifyExpress from '..'
|
|
3
3
|
import { expectType } from "tsd"
|
|
4
4
|
import { Application } from 'express'
|
|
@@ -7,7 +7,13 @@ const app = Fastify()
|
|
|
7
7
|
|
|
8
8
|
app.register(fastifyExpress)
|
|
9
9
|
app.register(fastifyExpress, {
|
|
10
|
-
expressHook: 'onRequest'
|
|
10
|
+
expressHook: 'onRequest',
|
|
11
|
+
createProxyHandler: (fastifyReq) => ({
|
|
12
|
+
set(target, prop, value) {
|
|
13
|
+
expectType<FastifyRequest>(fastifyReq)
|
|
14
|
+
return Reflect.set(target, prop, value)
|
|
15
|
+
}
|
|
16
|
+
})
|
|
11
17
|
})
|
|
12
18
|
|
|
13
19
|
expectType<Application>(app.express)
|
|
@@ -16,4 +22,4 @@ app.express.disable('x-powered-by')
|
|
|
16
22
|
|
|
17
23
|
app.use('/world', (_req, res) => {
|
|
18
24
|
res.sendStatus(200)
|
|
19
|
-
})
|
|
25
|
+
})
|