hapijs / hapijs/subtext

Increase separation of concerns and configurability of parsing

Open
#90 5 comments 0 reactions 0 assignees View on GitHub
feature
Dominant language
JavaScript
Stars
24
Forks
27
PR merge metrics
No merged PRs in 30d

Description

#### Support plan

* *is this issue currently blocking your project?* (yes/no): no
* *is this issue affecting a production system?* (yes/no): no

#### Context

* *node version*: na.
* *module version*: `7.0.3`
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...): hapi
* *any other relevant information*:

#### What problem are you trying to solve?

I want to be able to add [custom parsing](https://github.com/hapijs/hapi/issues/4223) capabilities to my Hapi server.

While investigating about how this could be achieved, I came across the [hard coded list of parsers](https://github.com/hapijs/subtext/blob/c59fcd3e085dea3ee021bd0ad0d55cfbf16b26e3/lib/index.js#L175-L216) in this module.

It would be very convenient to be able to be able to pass the list of parsers as an option. IMHO the default list of parsers should not even be coded in this module at all (though I understand this would be a big breaking change).

#### Do you have a new or modified API suggestion to solve the problem?

Be able to provide a custom list of parsers in the options. The default parsers would look like:
```js
[
// Binary

{
mime: 'application/octet-stream',
parse: (payload) => (payload.length ? payload : null),
},

// Text

{
mime: /^text\/.+$/,
parse: (payload) => payload.toString('utf8'),
},

// JSON

{
mime: /^application\/(?:.+\+)?json$/,
parse: (payload, mime, options) => {
if (!payload.length) {
return null
}

try {
return Bourne.parse(payload.toString('utf8'), { protoAction: options.protoAction })
} catch (err) {
const error = Boom.badRequest('Invalid request payload JSON format', err)
error.raw = payload
throw error
}
},
},

// Form-encoded

{
mime: 'application/x-www-form-urlencoded',
parse: (payload, mime, options) => {
const parse = options.querystring || Querystring.parse
return payload.length ? parse(payload.toString('utf8')) : {}
},
},
]
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.