@middy/http-response-serializer 2.5.6 → 3.0.0-alpha.3

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2021 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
3
+ Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -61,7 +61,7 @@ The middleware is configured by defining some `serializers`.
61
61
  }
62
62
  ```
63
63
 
64
- The `default` (optional) option is used if the request and handler don't specify what type is wanted.
64
+ The `defaultContentType` (optional) option is used if the request and handler don't specify what type is wanted.
65
65
 
66
66
 
67
67
  ## Serializer Functions
@@ -84,7 +84,7 @@ The content type is determined in the following order:
84
84
  * `event.requiredContentType` -- allows the handler to override everything else
85
85
  * The `Accept` header via [accept](https://www.npmjs.com/package/accept)
86
86
  * `event.preferredContentType` -- allows the handler to override the default, but lets the request ask first
87
- * `default` middleware configuration
87
+ * `defaultContentType` middleware option
88
88
 
89
89
  All options allow for multiple types to be specified in your order of preference, and the first matching serializer will be executed.
90
90
 
@@ -120,7 +120,7 @@ handler
120
120
  serializer: ({ body }) => body
121
121
  }
122
122
  ],
123
- default: 'application/json'
123
+ defaultContentType: 'application/json'
124
124
  }))
125
125
 
126
126
  const event = {
@@ -147,7 +147,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
147
147
 
148
148
  ## License
149
149
 
150
- Licensed under [MIT License](LICENSE). Copyright (c) 2017-2021 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
150
+ Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
151
151
 
152
152
  <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
153
153
  <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
package/index.d.ts CHANGED
@@ -7,7 +7,7 @@ interface SerializerHandler {
7
7
 
8
8
  interface Options {
9
9
  serializers: SerializerHandler[]
10
- default?: string
10
+ defaultContentType?: string
11
11
  }
12
12
 
13
13
  declare function httpResponseSerializer (options?: Options): middy.MiddlewareObj
package/index.js CHANGED
@@ -1,42 +1,36 @@
1
- const { normalizeHttpResponse } = require('@middy/util')
2
- const Accept = require('@hapi/accept')
1
+ import { normalizeHttpResponse } from '@middy/util'
2
+ import Accept from '@hapi/accept'
3
3
 
4
4
  const defaults = {
5
5
  serializers: [],
6
- default: undefined
6
+ defaultContentType: undefined
7
7
  }
8
8
 
9
9
  const httpResponseSerializerMiddleware = (opts = {}) => {
10
- const options = { ...defaults, ...opts }
10
+ const { serializers, defaultContentType } = { ...defaults, ...opts }
11
11
  const httpResponseSerializerMiddlewareAfter = async (request) => {
12
- if (request.response === undefined) return
13
- request.response = normalizeHttpResponse(request.response)
14
- // skip serialization when content-type is already set
15
- if (
16
- request.response.headers['Content-Type'] ||
17
- request.response.headers['content-type']
18
- ) {
19
- return
20
- }
12
+ normalizeHttpResponse(request)
13
+
14
+ // skip serialization when Content-Type is already set
15
+ if (request.response.headers['Content-Type']) return
21
16
 
22
17
  // find accept value(s)
23
18
  let types
24
19
 
25
- if (request.event?.requiredContentType) {
20
+ if (request.event.requiredContentType) {
26
21
  types = [request.event.requiredContentType]
27
22
  } else {
28
- const acceptHeader =
29
- request.event?.headers?.accept ?? request.event?.headers?.Accept
23
+ const acceptHeader = request.event.headers.Accept ?? request.event.headers.accept
30
24
  types = [
31
25
  ...((acceptHeader && Accept.mediaTypes(acceptHeader)) ?? []),
32
26
  request.event.preferredContentType,
33
- options.default
27
+ defaultContentType
34
28
  ]
35
29
  }
36
30
 
37
31
  for (const type of types) {
38
32
  let breakTypes
39
- for (const s of options.serializers) {
33
+ for (const s of serializers) {
40
34
  if (!s.regex.test(type)) {
41
35
  continue
42
36
  }
@@ -56,11 +50,15 @@ const httpResponseSerializerMiddleware = (opts = {}) => {
56
50
  if (breakTypes) break
57
51
  }
58
52
  }
59
- const httpResponseSerializerMiddlewareOnError = httpResponseSerializerMiddlewareAfter
53
+
54
+ const httpResponseSerializerMiddlewareOnError = async (request) => {
55
+ if (request.response === undefined) return
56
+ return httpResponseSerializerMiddlewareAfter(request)
57
+ }
60
58
  return {
61
59
  after: httpResponseSerializerMiddlewareAfter,
62
60
  onError: httpResponseSerializerMiddlewareOnError
63
61
  }
64
62
  }
65
63
 
66
- module.exports = httpResponseSerializerMiddleware
64
+ export default httpResponseSerializerMiddleware
package/package.json CHANGED
@@ -1,23 +1,25 @@
1
1
  {
2
2
  "name": "@middy/http-response-serializer",
3
- "version": "2.5.6",
3
+ "version": "3.0.0-alpha.3",
4
4
  "description": "The Http Serializer middleware lets you define serialization mechanisms based on the current content negotiation.",
5
- "type": "commonjs",
5
+ "type": "module",
6
6
  "engines": {
7
- "node": ">=12"
7
+ "node": ">=14"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "main": "index.js",
13
+ "exports": "./index.js",
14
14
  "types": "index.d.ts",
15
15
  "files": [
16
+ "index.js",
16
17
  "index.d.ts"
17
18
  ],
18
19
  "scripts": {
19
20
  "test": "npm run test:unit",
20
- "test:unit": "ava"
21
+ "test:unit": "ava",
22
+ "test:benchmark": "node __benchmarks__/index.js"
21
23
  },
22
24
  "license": "MIT",
23
25
  "keywords": [
@@ -50,10 +52,10 @@
50
52
  "homepage": "https://github.com/middyjs/middy#readme",
51
53
  "dependencies": {
52
54
  "@hapi/accept": "5.0.2",
53
- "@middy/util": "^2.5.6"
55
+ "@middy/util": "^3.0.0-alpha.3"
54
56
  },
55
57
  "devDependencies": {
56
- "@middy/core": "^2.5.6"
58
+ "@middy/core": "^3.0.0-alpha.3"
57
59
  },
58
- "gitHead": "0c789f55b4adf691f977b0d9904d1a805bb3bb2b"
60
+ "gitHead": "1441158711580313765e6d156046ef0fade0d156"
59
61
  }