hapijs / hapijs/hapi

Add ability to create a custom body parser

Open
#4,223 26 comments 2 reactions 0 assignees View on GitHub
feature
Dominant language
JavaScript
Stars
14.8k
Forks
1.4k
Avg merge
22d 3h
Merged PRs (30d)
1

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*: 14
* *module version*: 20.0.3
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...): hapi
* *any other relevant information*: n.a.

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

I want to create a plugin that enables transparent `yaml` parsing of request payload. By transparent I mean that I would create an equivalence between `yaml` and `json` and transparently parse yaml and allow it to be used with routes expecting JSON.

My current workaround consists of monkey patching `Subtext.parse` to do something like:

```js
const origParse = Subtext.parse
Subtext.parse = async function (req, tap, options) {
const { payload, mime } = await origParse.call(this, req, tap, {
...options,
allow: options.allow?.some(isJsonMime)
? [ ...options.allow, 'text/vnd.yaml', 'text/yaml', 'text/x-yaml', 'application/x-yaml' ]
: options.allow,
});

if (options.parse === true && isYamlMime(mime)) {
// Expose Yaml as plain JSON
return { payload: parseYaml(payload), mime: 'application/json' };
}

return { payload, mime };
}

```

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

nope

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.