Skip to content

Commit

Permalink
test: coverage 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
3imed-jaberi committed Apr 15, 2023
1 parent 4de8fe0 commit 73d432b
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 233 deletions.
33 changes: 17 additions & 16 deletions src/body-parser.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@ import {
type BodyType,
} from './body-parser.types';

export class UnsupportedBodyTypeError extends Error {
constructor(wrongType: string) {
super();
this.name = 'UnsupportedBodyTypeError';
this.message =
`Invalid enabled type '${wrongType}'.` +
` make sure to pass an array contains ` +
`supported types ([${supportedBodyTypes}]).`;
}
}

/**
* Utility which help us to check if the body type enabled
*/
export function getIsEnabledBodyAs(enableTypes: BodyType[]) {
for (const enabledType of enableTypes) {
if (!supportedBodyTypes.includes(enabledType)) {
throw new Error(
`Invalid enabled type '${enabledType}'.` +
` make sure to pass an array contains ` +
`supported types ([${supportedBodyTypes}]).`,
);
throw new UnsupportedBodyTypeError(enabledType);
}
}

Expand All @@ -37,17 +44,11 @@ export function getMimeTypes(
extendTypes: NonNullable<BodyParserOptions['extendTypes']>,
) {
for (const extendedType of Object.keys(extendTypes) as BodyType[]) {
if (!supportedBodyTypes.includes(extendedType)) {
throw new Error(
`Invalid extend type '${extendedType}'.` +
` make sure to pass supported types ([${supportedBodyTypes}]).`,
);
}

if (!Array.isArray(extendTypes[extendedType])) {
throw new TypeError(
'Invalid extend type value. make sure to pass an array of strings.',
);
if (
!supportedBodyTypes.includes(extendedType) ||
!Array.isArray(extendTypes[extendedType])
) {
throw new UnsupportedBodyTypeError(extendedType);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export {
/* istanbul ignore next */
bodyParserWrapper as bodyParser,
bodyParserWrapper as default,
} from './body-parser';

0 comments on commit 73d432b

Please sign in to comment.