@middy/http-json-body-parser 5.0.0-alpha.0 → 5.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -19,8 +19,9 @@
19
19
  <a href="https://snyk.io/test/github/middyjs/middy">
20
20
  <img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
21
21
  </a>
22
- <a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
23
- <img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
22
+ <a href="https://github.com/middyjs/middy/actions/workflows/sast.yml">
23
+ <img src="https://github.com/middyjs/middy/actions/workflows/sast.yml/badge.svg
24
+ ?branch=main&event=push" alt="CodeQL" style="max-width:100%;">
24
25
  </a>
25
26
  <a href="https://bestpractices.coreinfrastructure.org/projects/5280">
26
27
  <img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
package/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import middy from '@middy/core'
2
- import { APIGatewayEvent } from 'aws-lambda'
2
+ import { APIGatewayEvent, APIGatewayProxyEventV2 } from 'aws-lambda'
3
3
  import { JsonValue } from 'type-fest'
4
4
 
5
5
  interface Options {
@@ -7,13 +7,15 @@ interface Options {
7
7
  disableContentTypeError?: boolean
8
8
  }
9
9
 
10
- export type Event = Omit<APIGatewayEvent, 'body'> & {
10
+ type VersionedApiGatewayEvent = APIGatewayEvent | APIGatewayProxyEventV2
11
+
12
+ export type Event<APIGatewayEventType extends VersionedApiGatewayEvent = VersionedApiGatewayEvent> = Omit<APIGatewayEventType, 'body'> & {
11
13
  /**
12
14
  * The body of the HTTP request.
13
15
  */
14
16
  body: JsonValue
15
17
  }
16
18
 
17
- declare function jsonBodyParser (options?: Options): middy.MiddlewareObj<Event>
19
+ declare function jsonBodyParser<APIGatewayEventType extends VersionedApiGatewayEvent = VersionedApiGatewayEvent> (options?: Options): middy.MiddlewareObj<Event<APIGatewayEventType>>
18
20
 
19
21
  export default jsonBodyParser
package/index.js CHANGED
@@ -2,7 +2,7 @@ import { createError } from '@middy/util';
2
2
  const mimePattern = /^application\/(.+\+)?json($|;.+)/;
3
3
  const defaults = {
4
4
  reviver: undefined,
5
- disableContentTypeError: true
5
+ disableContentTypeError: false
6
6
  };
7
7
  const httpJsonBodyParserMiddleware = (opts = {})=>{
8
8
  const options = {
@@ -10,22 +10,29 @@ const httpJsonBodyParserMiddleware = (opts = {})=>{
10
10
  ...opts
11
11
  };
12
12
  const httpJsonBodyParserMiddlewareBefore = async (request)=>{
13
- const { headers , body } = request.event;
14
- const contentType = headers['Content-Type'] ?? headers['content-type'];
13
+ const { headers, body } = request.event;
14
+ const contentType = headers?.['Content-Type'] ?? headers?.['content-type'];
15
15
  if (!mimePattern.test(contentType)) {
16
16
  if (options.disableContentTypeError) {
17
17
  return;
18
18
  }
19
19
  throw createError(415, 'Unsupported Media Type', {
20
- cause: contentType
20
+ cause: {
21
+ package: '@middy/http-json-body-parser',
22
+ data: contentType
23
+ }
21
24
  });
22
25
  }
23
26
  try {
24
27
  const data = request.event.isBase64Encoded ? Buffer.from(body, 'base64').toString() : body;
25
28
  request.event.body = JSON.parse(data, options.reviver);
26
- } catch (cause) {
29
+ } catch (err) {
30
+ // UnprocessableEntity
27
31
  throw createError(415, 'Invalid or malformed JSON was provided', {
28
- cause
32
+ cause: {
33
+ package: '@middy/http-json-body-parser',
34
+ data: err
35
+ }
29
36
  });
30
37
  }
31
38
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-json-body-parser",
3
- "version": "5.0.0-alpha.0",
3
+ "version": "5.0.0-alpha.2",
4
4
  "description": "Http JSON body parser middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -10,24 +10,18 @@
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "main": "./index.cjs",
14
13
  "module": "./index.js",
15
14
  "exports": {
16
15
  ".": {
17
16
  "import": {
18
17
  "types": "./index.d.ts",
19
18
  "default": "./index.js"
20
- },
21
- "require": {
22
- "types": "./index.d.ts",
23
- "default": "./index.cjs"
24
19
  }
25
20
  }
26
21
  },
27
22
  "types": "index.d.ts",
28
23
  "files": [
29
24
  "index.js",
30
- "index.cjs",
31
25
  "index.d.ts"
32
26
  ],
33
27
  "scripts": {
@@ -68,12 +62,12 @@
68
62
  "url": "https://github.com/sponsors/willfarrell"
69
63
  },
70
64
  "dependencies": {
71
- "@middy/util": "5.0.0-alpha.0"
65
+ "@middy/util": "5.0.0-alpha.2"
72
66
  },
73
67
  "devDependencies": {
74
- "@middy/core": "5.0.0-alpha.0",
68
+ "@middy/core": "5.0.0-alpha.2",
75
69
  "@types/aws-lambda": "^8.10.101",
76
- "type-fest": "^3.0.0"
70
+ "type-fest": "^4.0.0"
77
71
  },
78
- "gitHead": "08c35e3dba9efdad0b86666ce206ce302cc65d07"
72
+ "gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
79
73
  }
package/index.cjs DELETED
@@ -1,45 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(module, "exports", {
6
- enumerable: true,
7
- get: ()=>_default
8
- });
9
- const _util = require("@middy/util");
10
- const mimePattern = /^application\/(.+\+)?json($|;.+)/;
11
- const defaults = {
12
- reviver: undefined,
13
- disableContentTypeError: true
14
- };
15
- const httpJsonBodyParserMiddleware = (opts = {})=>{
16
- const options = {
17
- ...defaults,
18
- ...opts
19
- };
20
- const httpJsonBodyParserMiddlewareBefore = async (request)=>{
21
- const { headers , body } = request.event;
22
- const contentType = headers['Content-Type'] ?? headers['content-type'];
23
- if (!mimePattern.test(contentType)) {
24
- if (options.disableContentTypeError) {
25
- return;
26
- }
27
- throw (0, _util.createError)(415, 'Unsupported Media Type', {
28
- cause: contentType
29
- });
30
- }
31
- try {
32
- const data = request.event.isBase64Encoded ? Buffer.from(body, 'base64').toString() : body;
33
- request.event.body = JSON.parse(data, options.reviver);
34
- } catch (cause) {
35
- throw (0, _util.createError)(415, 'Invalid or malformed JSON was provided', {
36
- cause
37
- });
38
- }
39
- };
40
- return {
41
- before: httpJsonBodyParserMiddlewareBefore
42
- };
43
- };
44
- const _default = httpJsonBodyParserMiddleware;
45
-