Skip to content

Commit

Permalink
feat: support for multiple content types (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
perillamint authored Nov 16, 2023
1 parent 7c08116 commit d7c15db
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ await app.register(import('fastify-raw-body'), {
global: false, // add the rawBody to every request. **Default true**
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**
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']**
})

app.post('/', {
Expand Down
1 change: 1 addition & 0 deletions plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ declare namespace fastifyRawBody {
encoding?: string | null | false
runFirst?: boolean
routes?: string[]
jsonContentTypes?: string[]
}

export const fastifyRawBody: FastifyRawBody;
Expand Down
7 changes: 4 additions & 3 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ function rawBody (fastify, opts, next) {
return
}

const { field, encoding, global, runFirst, routes } = Object.assign({
const { field, encoding, global, runFirst, routes, jsonContentTypes } = Object.assign({
field: 'rawBody',
encoding: 'utf8',
global: true,
runFirst: false,
routes: []
routes: [],
jsonContentTypes: ['application/json']
}, opts)

if (encoding === false) {
fastify.addContentTypeParser('application/json',
fastify.addContentTypeParser(jsonContentTypes,
{ parseAs: 'buffer' },
almostDefaultJsonParser)
}
Expand Down
23 changes: 23 additions & 0 deletions test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,29 @@ t.test('raw body buffer', async t => {
t.equal(res.payload, JSON.stringify(payload))
})

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 => {
const app = Fastify({ bodyLimit: 5 })

Expand Down

0 comments on commit d7c15db

Please sign in to comment.