@middy/http-response-serializer 3.0.0-alpha.3 → 3.0.0-alpha.7
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 +23 -13
- package/index.js +2 -63
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,26 +1,36 @@
|
|
|
1
|
-
# Middy http-response-serializer middleware
|
|
2
|
-
|
|
3
|
-
<div align="center">
|
|
4
|
-
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/>
|
|
5
|
-
</div>
|
|
6
|
-
|
|
7
1
|
<div align="center">
|
|
2
|
+
<h1>Middy http-response-serializer middleware</h1>
|
|
3
|
+
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
|
|
8
4
|
<p><strong>HTTP response serializer middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
|
|
9
|
-
</div>
|
|
10
|
-
|
|
11
|
-
<div align="center">
|
|
12
5
|
<p>
|
|
13
|
-
<a href="
|
|
6
|
+
<a href="https://www.npmjs.com/package/@middy/http-response-serializer?activeTab=versions">
|
|
14
7
|
<img src="https://badge.fury.io/js/%40middy%2Fhttp-response-serializer.svg" alt="npm version" style="max-width:100%;">
|
|
15
8
|
</a>
|
|
9
|
+
<a href="https://packagephobia.com/result?p=@middy/http-response-serializer">
|
|
10
|
+
<img src="https://packagephobia.com/badge?p=@middy/http-response-serializer" alt="npm install size" style="max-width:100%;">
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://github.com/middyjs/middy/actions">
|
|
13
|
+
<img src="https://github.com/middyjs/middy/workflows/Tests/badge.svg" alt="GitHub Actions test status badge" style="max-width:100%;">
|
|
14
|
+
</a>
|
|
15
|
+
<br/>
|
|
16
|
+
<a href="https://standardjs.com/">
|
|
17
|
+
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
|
|
18
|
+
</a>
|
|
16
19
|
<a href="https://snyk.io/test/github/middyjs/middy">
|
|
17
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%;">
|
|
18
21
|
</a>
|
|
19
|
-
<a href="https://
|
|
20
|
-
<img src="https://img.shields.io/
|
|
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%;">
|
|
24
|
+
</a>
|
|
25
|
+
<a href="https://bestpractices.coreinfrastructure.org/projects/5280">
|
|
26
|
+
<img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
|
|
21
27
|
</a>
|
|
28
|
+
<br/>
|
|
22
29
|
<a href="https://gitter.im/middyjs/Lobby">
|
|
23
|
-
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter"
|
|
30
|
+
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
|
|
31
|
+
</a>
|
|
32
|
+
<a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
|
|
33
|
+
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
|
|
24
34
|
</a>
|
|
25
35
|
</p>
|
|
26
36
|
</div>
|
package/index.js
CHANGED
|
@@ -1,64 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
import Accept from '@hapi/accept'
|
|
1
|
+
import{normalizeHttpResponse}from'@middy/util';import Accept from'@hapi/accept';const defaults={serializers:[],defaultContentType:undefined};const httpResponseSerializerMiddleware=(opts={})=>{const{serializers,defaultContentType}={...defaults,...opts};const httpResponseSerializerMiddlewareAfter=async request=>{normalizeHttpResponse(request);if(request.response.headers['Content-Type'])return;let types;if(request.event.requiredContentType){types=[request.event.requiredContentType]}else{const acceptHeader=request.event.headers.Accept??request.event.headers.accept;types=[...(acceptHeader&&Accept.mediaTypes(acceptHeader))??[],request.event.preferredContentType,defaultContentType]}for(const type of types){let breakTypes;for(const s of serializers){if(!s.regex.test(type)){continue}request.response.headers['Content-Type']=type;const result=s.serializer(request.response);if(typeof result==='object'&&'body'in result){request.response=result}else{request.response.body=result}breakTypes=true;break}if(breakTypes)break}};const httpResponseSerializerMiddlewareOnError=async request=>{if(request.response===undefined)return;return httpResponseSerializerMiddlewareAfter(request)};return{after:httpResponseSerializerMiddlewareAfter,onError:httpResponseSerializerMiddlewareOnError}};export default httpResponseSerializerMiddleware
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
serializers: [],
|
|
6
|
-
defaultContentType: undefined
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const httpResponseSerializerMiddleware = (opts = {}) => {
|
|
10
|
-
const { serializers, defaultContentType } = { ...defaults, ...opts }
|
|
11
|
-
const httpResponseSerializerMiddlewareAfter = async (request) => {
|
|
12
|
-
normalizeHttpResponse(request)
|
|
13
|
-
|
|
14
|
-
// skip serialization when Content-Type is already set
|
|
15
|
-
if (request.response.headers['Content-Type']) return
|
|
16
|
-
|
|
17
|
-
// find accept value(s)
|
|
18
|
-
let types
|
|
19
|
-
|
|
20
|
-
if (request.event.requiredContentType) {
|
|
21
|
-
types = [request.event.requiredContentType]
|
|
22
|
-
} else {
|
|
23
|
-
const acceptHeader = request.event.headers.Accept ?? request.event.headers.accept
|
|
24
|
-
types = [
|
|
25
|
-
...((acceptHeader && Accept.mediaTypes(acceptHeader)) ?? []),
|
|
26
|
-
request.event.preferredContentType,
|
|
27
|
-
defaultContentType
|
|
28
|
-
]
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
for (const type of types) {
|
|
32
|
-
let breakTypes
|
|
33
|
-
for (const s of serializers) {
|
|
34
|
-
if (!s.regex.test(type)) {
|
|
35
|
-
continue
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
request.response.headers['Content-Type'] = type
|
|
39
|
-
const result = s.serializer(request.response)
|
|
40
|
-
if (typeof result === 'object' && 'body' in result) {
|
|
41
|
-
request.response = result
|
|
42
|
-
} else {
|
|
43
|
-
// otherwise only replace the body attribute
|
|
44
|
-
request.response.body = result
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
breakTypes = true
|
|
48
|
-
break
|
|
49
|
-
}
|
|
50
|
-
if (breakTypes) break
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const httpResponseSerializerMiddlewareOnError = async (request) => {
|
|
55
|
-
if (request.response === undefined) return
|
|
56
|
-
return httpResponseSerializerMiddlewareAfter(request)
|
|
57
|
-
}
|
|
58
|
-
return {
|
|
59
|
-
after: httpResponseSerializerMiddlewareAfter,
|
|
60
|
-
onError: httpResponseSerializerMiddlewareOnError
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export default httpResponseSerializerMiddleware
|
|
3
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-response-serializer",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.7",
|
|
4
4
|
"description": "The Http Serializer middleware lets you define serialization mechanisms based on the current content negotiation.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -52,10 +52,10 @@
|
|
|
52
52
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"@hapi/accept": "5.0.2",
|
|
55
|
-
"@middy/util": "^3.0.0-alpha.
|
|
55
|
+
"@middy/util": "^3.0.0-alpha.7"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@middy/core": "^3.0.0-alpha.
|
|
58
|
+
"@middy/core": "^3.0.0-alpha.7"
|
|
59
59
|
},
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "5cef39ebe49c201f97d71bb0680004de4b82cb91"
|
|
61
61
|
}
|