Skip to content

Commit 0c2e10b

Browse files
Add proxyPayloads option (#87)
1 parent ac003b2 commit 0c2e10b

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ Rewrite the prefix to the specified string. Default: `''`.
9494

9595
A `preHandler` to be applied on all routes. Useful for performing actions before the proxy is executed (e.g. check for authentication).
9696

97+
### proxyPayloads
98+
99+
When this option is `false`, you will be able to access the body but it will also disable direct pass through of the payload. As a result, it is left up to the implementation to properly parse and proxy the payload correctly.
100+
101+
For example, if you are expecting a payload of type `application/xml`, then you would have to add a parser for it like so:
102+
103+
```javascript
104+
fastify.addContentTypeParser('application/xml', (req, done) => {
105+
const parsedBody = parsingCode(req)
106+
done(null, parsedBody)
107+
})
108+
```
109+
97110
### config
98111

99112
An object accessible within the `preHandler` via `reply.context.config`.

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ declare const fastifyHttpProxy: Plugin<
2323
prefix?: string;
2424
rewritePrefix?: string;
2525
http2?: boolean;
26+
proxyPayloads?: boolean;
2627
preHandler?: (
2728
request: FastifyRequest<
2829
HttpRequest,

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ module.exports = async function (fastify, opts) {
2222

2323
fastify.register(From, fromOpts)
2424

25-
fastify.addContentTypeParser('application/json', bodyParser)
26-
fastify.addContentTypeParser('*', bodyParser)
25+
if (opts.proxyPayloads !== false) {
26+
fastify.addContentTypeParser('application/json', bodyParser)
27+
fastify.addContentTypeParser('*', bodyParser)
28+
}
2729

2830
function rewriteHeaders (headers) {
2931
const location = headers.location

test/test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,30 @@ async function run () {
111111
t.deepEqual(resultRoot.body, { something: 'posted' })
112112
})
113113

114+
test('skip proxying the incoming payload', async t => {
115+
const server = Fastify()
116+
server.register(proxy, {
117+
upstream: `http://localhost:${origin.server.address().port}`,
118+
proxyPayloads: false,
119+
preHandler (request, reply, next) {
120+
t.deepEqual(request.body, { hello: 'world' })
121+
next()
122+
}
123+
})
124+
125+
await server.listen(0)
126+
t.tearDown(server.close.bind(server))
127+
128+
await got(
129+
`http://localhost:${server.server.address().port}/this-has-data`,
130+
{
131+
method: 'POST',
132+
json: { hello: 'world' },
133+
responseType: 'json'
134+
}
135+
)
136+
})
137+
114138
test('preHandler', async t => {
115139
const server = Fastify()
116140
server.register(proxy, {

0 commit comments

Comments
 (0)