Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(middleware-body): extended available API via customizable parsers #102

merged 6 commits into from Feb 6, 2019


1 participant
Copy link

commented Feb 4, 2019

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[x] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

  • Parsers can work only upon application/json and application/x-www-form-urlencoded content type headers
  • The API doesn't allow to pass customized parsers

What is the new behavior?

  • The middleware does nothing if request content types is not matched. It creates the possibility for chaining multiple body parsers.
  • The type option is used to determine what media type the middleware will parse. Defaults to */*.
  • The parser option is used to set custom parser. It will parse all incoming request bodies that match to type option. Defaults to defaultParser.
  • Developers can create their own body parsers by conforming to RequestBodyParser interface.

Example usage:

  1. Default parser:

By default parses application/json, application/x-www-form-urlencoded, application/octet-stream and text/plain content-types

  1. JSON parser:
  parser: jsonParser,
  type: ['*/json', 'application/vnd.api+json'],

Parses req.body to JSON object.

  1. UrlEncoded parser:
  parser: urlEncodedParser,
  type: ['*/x-www-form-urlencoded'],

Parses URL encoded req.body to JSON object.

  1. Text parser:
  parser: textParser,
  type: ['text/*'],

Parses req.body to string.

  1. Raw parser:
  parser: rawParser,
  type: ['application/octet-stream'],

Parses req.body to Buffer.

Does this PR introduce a breaking change?

[ ] Yes
[x] No

@JozefFlakus JozefFlakus added this to the 2.0.0-rc.1 milestone Feb 4, 2019

@JozefFlakus JozefFlakus self-assigned this Feb 4, 2019

@JozefFlakus JozefFlakus added this to In progress in @marblejs/next via automation Feb 4, 2019


This comment has been minimized.

Copy link

commented Feb 4, 2019

Codecov Report

Merging #102 into next will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@         Coverage Diff         @@
##           next   #102   +/-   ##
  Coverage   100%   100%           
  Files        77     85    +8     
  Lines       962   1012   +50     
  Branches     85     89    +4     
+ Hits        962   1012   +50
Impacted Files Coverage Δ
...ges/middleware-body/src/parsers/url.body.parser.ts 100% <100%> (ø) 100% <100%> (ø)
packages/middleware-body/src/body.util.ts 100% <100%> (ø)
...middleware-body/src/parsers/default.body.parser.ts 100% <100%> (ø)
...ore/src/+internal/observable/fromReadableStream.ts 100% <100%> (ø)
...ges/middleware-body/src/parsers/raw.body.parser.ts 100% <100%> (ø)
packages/core/src/+internal/index.ts 100% <100%> (ø) ⬆️
packages/middleware-body/src/index.ts 100% <100%> (ø) ⬆️
packages/middleware-body/src/parsers/index.ts 100% <100%> (ø) 100% <100%> (ø)
... and 12 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 935e362...afc8632. Read the comment docs.

@JozefFlakus JozefFlakus added the WIP label Feb 4, 2019

@JozefFlakus JozefFlakus merged commit fe755de into next Feb 6, 2019

2 checks passed

Travis CI - Pull Request Build Passed
codecov/patch 100% of diff hit (target 100%)

@marblejs/next automation moved this from In progress to Done Feb 6, 2019

@JozefFlakus JozefFlakus deleted the fix/next/bodyParser branch Feb 6, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.