Skip to content

Add ability to create a custom body parser #4223

Description

@matthieusieben

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:

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew functionality or improvement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions