fastify-raw-body
Advanced tools
Comparing version 3.1.0 to 3.2.0
{ | ||
"name": "fastify-raw-body", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Request raw body", | ||
@@ -33,3 +33,3 @@ "main": "plugin.js", | ||
"standard": "^14.3.4", | ||
"tap": "^14.10.7", | ||
"tap": "^15.0.1", | ||
"tsd": "^0.14.0" | ||
@@ -36,0 +36,0 @@ }, |
@@ -14,2 +14,3 @@ import { FastifyPluginCallback } from 'fastify' | ||
runFirst?: boolean | ||
routes?: string[] | ||
} | ||
@@ -16,0 +17,0 @@ |
@@ -15,7 +15,8 @@ 'use strict' | ||
const { field, encoding, global, runFirst } = Object.assign({ | ||
const { field, encoding, global, runFirst, routes } = Object.assign({ | ||
field: 'rawBody', | ||
encoding: 'utf8', | ||
global: true, | ||
runFirst: false | ||
runFirst: false, | ||
routes: [] | ||
}, opts) | ||
@@ -32,3 +33,7 @@ | ||
if ((global && !wantSkip) || (routeOptions.config && routeOptions.config.rawBody === true)) { | ||
if ( | ||
(global && !wantSkip && !routes.length) || | ||
(routeOptions.config && routeOptions.config.rawBody === true) || | ||
routes.includes(routeOptions.path) | ||
) { | ||
if (!routeOptions.preParsing) { | ||
@@ -35,0 +40,0 @@ routeOptions.preParsing = [preparsingRawBody] |
@@ -37,3 +37,4 @@ # fastify-raw-body | ||
encoding: 'utf8', // set it to false to set rawBody as a Buffer **Default utf8** | ||
runFirst: true // get the body before any preParsing hook change/uncompress it. **Default false** | ||
runFirst: true, // get the body before any preParsing hook change/uncompress it. **Default false** | ||
routes: [] // array of routes, **`global`** will be ignored, wildcard routes not supported | ||
}) | ||
@@ -40,0 +41,0 @@ |
@@ -54,3 +54,3 @@ 'use strict' | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, shouldBe) | ||
t.equal(res.payload, shouldBe) | ||
}) | ||
@@ -68,3 +68,3 @@ }) | ||
t.ok(err) | ||
t.like(err.message, /twice/) | ||
t.match(err.message, /twice/) | ||
}) | ||
@@ -115,3 +115,3 @@ }) | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, 'raw=undefined') | ||
t.equal(res.payload, 'raw=undefined') | ||
}) | ||
@@ -126,3 +126,3 @@ | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, shouldBe) | ||
t.equal(res.payload, shouldBe) | ||
}) | ||
@@ -157,3 +157,3 @@ }) | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, 'raw=undefined') | ||
t.equal(res.payload, 'raw=undefined') | ||
}) | ||
@@ -168,3 +168,3 @@ | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, shouldBe) | ||
t.equal(res.payload, shouldBe) | ||
}) | ||
@@ -181,3 +181,3 @@ }) | ||
app.addHook('preParsing', function (req, reply, payload, done) { | ||
t.equals(order++, 0) | ||
t.equal(order++, 0) | ||
const change = new Readable() | ||
@@ -193,4 +193,4 @@ change.receivedEncodedLength = parseInt(req.headers['content-length'], 10) | ||
app.post('/', (req, reply) => { | ||
t.deepEquals(req.body, { hello: 'another world' }) | ||
t.equals(JSON.stringify(req.body), req.rawBody) | ||
t.same(req.body, { hello: 'another world' }) | ||
t.equal(JSON.stringify(req.body), req.rawBody) | ||
reply.send(req.rawBody) | ||
@@ -201,3 +201,3 @@ }) | ||
preParsing: function (req, reply, payload, done) { | ||
t.equals(order++, 1) | ||
t.equal(order++, 1) | ||
t.notOk(req.rawBody) | ||
@@ -211,4 +211,4 @@ const change = new Readable() | ||
}, (req, reply) => { | ||
t.deepEquals(req.body, { hello: 'last world' }) | ||
t.equals(JSON.stringify(req.body), req.rawBody) | ||
t.same(req.body, { hello: 'last world' }) | ||
t.equal(JSON.stringify(req.body), req.rawBody) | ||
reply.send(req.rawBody) | ||
@@ -224,3 +224,3 @@ }) | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, JSON.stringify({ hello: 'another world' })) | ||
t.equal(res.payload, JSON.stringify({ hello: 'another world' })) | ||
@@ -235,3 +235,3 @@ order = 0 // reset the global order | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, JSON.stringify({ hello: 'last world' })) | ||
t.equal(res.payload, JSON.stringify({ hello: 'last world' })) | ||
}) | ||
@@ -261,3 +261,3 @@ }) | ||
app.post('/', (req, reply) => { | ||
t.deepEquals(req.body, { HELLO: 'WORLD' }) | ||
t.same(req.body, { HELLO: 'WORLD' }) | ||
reply.send(req.rawBody) | ||
@@ -278,3 +278,3 @@ }) | ||
}, (req, reply) => { | ||
t.deepEquals(req.body, { HELLO: 'WORLD' }) | ||
t.same(req.body, { HELLO: 'WORLD' }) | ||
reply.send(req.rawBody) | ||
@@ -290,3 +290,3 @@ }) | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, JSON.stringify(payload)) | ||
t.equal(res.payload, JSON.stringify(payload)) | ||
@@ -300,3 +300,3 @@ app.inject({ | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, JSON.stringify(payload)) | ||
t.equal(res.payload, JSON.stringify(payload)) | ||
}) | ||
@@ -324,4 +324,4 @@ }) | ||
}, (req, reply) => { | ||
t.deepEquals(req.body, { hello: 'last world' }) | ||
t.equals(JSON.stringify(req.body), req.rawBody) | ||
t.same(req.body, { hello: 'last world' }) | ||
t.equal(JSON.stringify(req.body), req.rawBody) | ||
reply.send(req.rawBody) | ||
@@ -337,3 +337,3 @@ }) | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, JSON.stringify({ hello: 'last world' })) | ||
t.equal(res.payload, JSON.stringify({ hello: 'last world' })) | ||
}) | ||
@@ -362,4 +362,4 @@ }) | ||
}, (req, reply) => { | ||
t.deepEquals(req.body, { HELLO: 'WORLD' }) | ||
t.equals(req.rawBody, JSON.stringify(payload)) | ||
t.same(req.body, { HELLO: 'WORLD' }) | ||
t.equal(req.rawBody, JSON.stringify(payload)) | ||
reply.send(req.rawBody) | ||
@@ -375,3 +375,3 @@ }) | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, JSON.stringify(payload)) | ||
t.equal(res.payload, JSON.stringify(payload)) | ||
}) | ||
@@ -389,3 +389,3 @@ }) | ||
app.post('/', (req, reply) => { | ||
t.deepEquals(req.body, { hello: 'world' }) | ||
t.same(req.body, { hello: 'world' }) | ||
t.type(req.rawRawRaw, Buffer) | ||
@@ -402,3 +402,3 @@ reply.send(req.rawRawRaw) | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, JSON.stringify(payload)) | ||
t.equal(res.payload, JSON.stringify(payload)) | ||
}) | ||
@@ -416,3 +416,3 @@ }) | ||
app.post('/', (req, reply) => { | ||
t.deepEquals(req.body, { hello: 'world' }) | ||
t.same(req.body, { hello: 'world' }) | ||
t.type(req.rawBody, Buffer) | ||
@@ -429,3 +429,3 @@ reply.send(req.rawBody) | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, JSON.stringify(payload)) | ||
t.equal(res.payload, JSON.stringify(payload)) | ||
}) | ||
@@ -497,1 +497,63 @@ }) | ||
}) | ||
t.test('defined routes', (t) => { | ||
t.plan(12) | ||
const app = Fastify() | ||
const payload = { hello: 'world' } | ||
const shouldBe = JSON.stringify(payload) | ||
app.register(rawBody, { routes: ['/test', '/webhook/123'], global: false }) | ||
app.post('/test', (req, reply) => { | ||
t.ok(req.rawBody) | ||
reply.send(req.rawBody) | ||
}) | ||
app.post('/webhook/123', (req, reply) => { | ||
t.ok(req.rawBody) | ||
reply.send(req.rawBody) | ||
}) | ||
app.post('/notmapped', (req, reply) => { | ||
t.notOk(req.rawBody) | ||
reply.send(`raw=${req.rawBody}`) | ||
}) | ||
app.inject( | ||
{ | ||
method: 'POST', | ||
url: '/test', | ||
payload | ||
}, | ||
(err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.equal(res.payload, shouldBe) | ||
} | ||
) | ||
app.inject( | ||
{ | ||
method: 'POST', | ||
url: '/webhook/123', | ||
payload | ||
}, | ||
(err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.equal(res.payload, shouldBe) | ||
} | ||
) | ||
app.inject( | ||
{ | ||
method: 'POST', | ||
url: '/notmapped', | ||
payload | ||
}, | ||
(err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.equal(res.payload, 'raw=undefined') | ||
} | ||
) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20770
11
575
69