Skip to content

Commit

Permalink
feat: support extra value under the content-type + use async/await ov…
Browse files Browse the repository at this point in the history
…er done in testing
  • Loading branch information
3imed-jaberi committed Jun 24, 2023
1 parent b496e70 commit 0421a1d
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 90 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ jobs:
matrix:
node-version:
- 16
- 17
- 18
- 19
- 20
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@types/lodash.merge": "^4.6.7",
"@types/node": "^18.15.11",
"@types/supertest": "^2.0.12",
"@types/type-is": "^1.6.3",
"eslint-config-xo-lass": "^2.0.1",
"husky": "^8.0.3",
"jest": "^29.5.0",
Expand All @@ -64,7 +65,8 @@
},
"dependencies": {
"co-body": "^6.1.0",
"lodash.merge": "^4.6.2"
"lodash.merge": "^4.6.2",
"type-is": "^1.6.18"
},
"engines": {
"node": ">= 16"
Expand Down
13 changes: 9 additions & 4 deletions src/body-parser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import parser from 'co-body';
import type * as Koa from 'koa';
import type {BodyParserOptions, BodyType} from './body-parser.types';
import {getIsEnabledBodyAs, getMimeTypes} from './body-parser.utils';
import {getIsEnabledBodyAs, getMimeTypes, isTypes} from './body-parser.utils';

/**
* Global declaration for the added properties to the 'ctx.request'
Expand Down Expand Up @@ -42,8 +42,13 @@ export function bodyParserWrapper(opts: BodyParserOptions = {}) {
* Handler to parse the request coming data
*/
async function parseBody(ctx: Koa.Context) {
const shouldParseBodyAs = (type: BodyType) =>
Boolean(isEnabledBodyAs[type] && ctx.request.is(mimeTypes[type]));
const shouldParseBodyAs = (type: BodyType) => {
return Boolean(
isEnabledBodyAs[type] &&
isTypes(ctx.request.get('content-type'), mimeTypes[type]),
);
};

const bodyType =
detectJSON?.(ctx) || shouldParseBodyAs('json')
? 'json'
Expand All @@ -58,7 +63,7 @@ export function bodyParserWrapper(opts: BodyParserOptions = {}) {
const parserOptions = {
// force co-body return raw body
returnRawBody: true,
strict: shouldParseBodyAs('json') ? restOpts.jsonStrict : undefined,
strict: bodyType === 'json' ? restOpts.jsonStrict : undefined,
[`${bodyType}Types`]: mimeTypes[bodyType],
limit: restOpts[`${shouldParseBodyAs('xml') ? 'xml' : bodyType}Limit`],
};
Expand Down
16 changes: 16 additions & 0 deletions src/body-parser.utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import deepMerge from 'lodash.merge';
import typeis from 'type-is';
import {
type BodyParserOptions,
supportedBodyTypes,
Expand Down Expand Up @@ -78,3 +79,18 @@ export function getMimeTypes(

return mimeTypes;
}

/**
* Check if the incoming request contains the "Content-Type" header
* field, and it contains any of the give mime types. If there
* is no request body, null is returned. If there is no content type,
* false is returned. Otherwise, it returns the first type that matches.
*/
export function isTypes(contentTypeValue: string, types: string[]) {
if (typeof contentTypeValue === 'string') {
// trim extra semicolon
contentTypeValue = contentTypeValue.replace(/;$/, '');
}

return typeis.is(contentTypeValue, types);
}

0 comments on commit 0421a1d

Please sign in to comment.