fastify-raw-body
Advanced tools
Comparing version 4.2.2 to 4.3.0
{ | ||
"name": "fastify-raw-body", | ||
"version": "4.2.2", | ||
"version": "4.3.0", | ||
"description": "Request raw body", | ||
@@ -40,3 +40,3 @@ "main": "plugin.js", | ||
"tap": "^16.2.0", | ||
"tsd": "^0.28.0" | ||
"tsd": "^0.29.0" | ||
}, | ||
@@ -43,0 +43,0 @@ "dependencies": { |
@@ -22,2 +22,3 @@ import { FastifyPluginCallback } from 'fastify' | ||
routes?: string[] | ||
jsonContentTypes?: string[] | ||
} | ||
@@ -24,0 +25,0 @@ |
@@ -15,3 +15,3 @@ 'use strict' | ||
const { field, encoding, global, runFirst, routes } = Object.assign({ | ||
const { field, encoding, global, runFirst, routes, jsonContentTypes } = Object.assign({ | ||
field: 'rawBody', | ||
@@ -21,7 +21,8 @@ encoding: 'utf8', | ||
runFirst: false, | ||
routes: [] | ||
routes: [], | ||
jsonContentTypes: ['application/json'] | ||
}, opts) | ||
if (encoding === false) { | ||
fastify.addContentTypeParser('application/json', | ||
fastify.addContentTypeParser(jsonContentTypes, | ||
{ parseAs: 'buffer' }, | ||
@@ -28,0 +29,0 @@ almostDefaultJsonParser) |
@@ -39,3 +39,4 @@ # fastify-raw-body | ||
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 | ||
routes: [], // array of routes, **`global`** will be ignored, wildcard routes not supported | ||
jsonContentTypes: [], // array of content-types to handle as JSON. **Default ['application/json']** | ||
}) | ||
@@ -42,0 +43,0 @@ |
@@ -417,2 +417,25 @@ 'use strict' | ||
t.test('raw body alternative content-type (application/ld+json)', async t => { | ||
const app = Fastify() | ||
const payload = { hello: 'world' } | ||
await app.register(rawBody, { encoding: false, jsonContentTypes: ['application/ld+json'] }) | ||
app.post('/', (req, reply) => { | ||
t.same(req.body, { hello: 'world' }) | ||
t.type(req.rawBody, Buffer) | ||
reply.send(req.rawBody) | ||
}) | ||
const res = await app.inject({ | ||
method: 'POST', | ||
url: '/', | ||
headers: { 'content-type': 'application/ld+json' }, | ||
payload | ||
}) | ||
t.equal(res.statusCode, 200) | ||
t.equal(res.payload, JSON.stringify(payload)) | ||
}) | ||
t.test('body limit', async t => { | ||
@@ -419,0 +442,0 @@ const app = Fastify({ bodyLimit: 5 }) |
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
25920
725
76