@middy/http-response-serializer 6.1.6 → 6.2.0
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/index.d.ts +7 -7
- package/index.js +56 -56
- package/package.json +71 -74
package/index.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import middy from
|
|
1
|
+
import type middy from "@middy/core";
|
|
2
2
|
|
|
3
3
|
interface SerializerHandler {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
regex: RegExp;
|
|
5
|
+
serializer: (response: any) => string | { body: any; [key: string]: any };
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
interface Options {
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
serializers: SerializerHandler[];
|
|
10
|
+
defaultContentType?: string;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
declare function httpResponseSerializer
|
|
13
|
+
declare function httpResponseSerializer(options?: Options): middy.MiddlewareObj;
|
|
14
14
|
|
|
15
|
-
export default httpResponseSerializer
|
|
15
|
+
export default httpResponseSerializer;
|
package/index.js
CHANGED
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
import { normalizeHttpResponse } from
|
|
1
|
+
import { normalizeHttpResponse } from "@middy/util";
|
|
2
2
|
|
|
3
3
|
const defaults = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
4
|
+
serializers: [],
|
|
5
|
+
defaultContentType: undefined,
|
|
6
|
+
};
|
|
7
7
|
|
|
8
8
|
const httpResponseSerializerMiddleware = (opts = {}) => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export default httpResponseSerializerMiddleware
|
|
9
|
+
const { serializers, defaultContentType } = { ...defaults, ...opts };
|
|
10
|
+
const httpResponseSerializerMiddlewareAfter = async (request) => {
|
|
11
|
+
normalizeHttpResponse(request);
|
|
12
|
+
|
|
13
|
+
// skip serialization when Content-Type or content-type is already set
|
|
14
|
+
if (
|
|
15
|
+
request.response.headers["Content-Type"] ??
|
|
16
|
+
request.response.headers["content-type"]
|
|
17
|
+
) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// find accept value(s)
|
|
22
|
+
const types = [
|
|
23
|
+
...(request.context.preferredMediaTypes ?? []), // from @middy/http-content-negotiation
|
|
24
|
+
defaultContentType,
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
for (const type of types) {
|
|
28
|
+
let breakTypes;
|
|
29
|
+
for (const s of serializers) {
|
|
30
|
+
if (!s.regex.test(type)) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
request.response.headers["Content-Type"] = type;
|
|
35
|
+
const result = s.serializer(request.response);
|
|
36
|
+
if (typeof result === "object" && "body" in result) {
|
|
37
|
+
request.response = result;
|
|
38
|
+
} else {
|
|
39
|
+
// otherwise only replace the body attribute
|
|
40
|
+
request.response.body = result;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
breakTypes = true;
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
if (breakTypes) break;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const httpResponseSerializerMiddlewareOnError = async (request) => {
|
|
51
|
+
if (request.response === undefined) return;
|
|
52
|
+
await httpResponseSerializerMiddlewareAfter(request);
|
|
53
|
+
};
|
|
54
|
+
return {
|
|
55
|
+
after: httpResponseSerializerMiddlewareAfter,
|
|
56
|
+
onError: httpResponseSerializerMiddlewareOnError,
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export default httpResponseSerializerMiddleware;
|
package/package.json
CHANGED
|
@@ -1,76 +1,73 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
"@middy/core": "6.1.6"
|
|
74
|
-
},
|
|
75
|
-
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
2
|
+
"name": "@middy/http-response-serializer",
|
|
3
|
+
"version": "6.2.0",
|
|
4
|
+
"description": "The Http Serializer middleware lets you define serialization mechanisms based on the current content negotiation.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"engineStrict": true,
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"module": "./index.js",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"default": "./index.js"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"types": "index.d.ts",
|
|
26
|
+
"files": ["index.js", "index.d.ts"],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "npm run test:unit && npm run test:fuzz",
|
|
29
|
+
"test:unit": "node --test",
|
|
30
|
+
"test:fuzz": "node --test index.fuzz.js",
|
|
31
|
+
"test:perf": "node --test index.perf.js"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"Lambda",
|
|
36
|
+
"Middleware",
|
|
37
|
+
"Serverless",
|
|
38
|
+
"Framework",
|
|
39
|
+
"AWS",
|
|
40
|
+
"AWS Lambda",
|
|
41
|
+
"Middy",
|
|
42
|
+
"HTTP",
|
|
43
|
+
"API",
|
|
44
|
+
"Header",
|
|
45
|
+
"Headers",
|
|
46
|
+
"Helmet",
|
|
47
|
+
"Security"
|
|
48
|
+
],
|
|
49
|
+
"author": {
|
|
50
|
+
"name": "Middy contributors",
|
|
51
|
+
"url": "https://github.com/middyjs/middy/graphs/contributors"
|
|
52
|
+
},
|
|
53
|
+
"repository": {
|
|
54
|
+
"type": "git",
|
|
55
|
+
"url": "git+https://github.com/middyjs/middy.git",
|
|
56
|
+
"directory": "packages/http-response-serializer"
|
|
57
|
+
},
|
|
58
|
+
"bugs": {
|
|
59
|
+
"url": "https://github.com/middyjs/middy/issues"
|
|
60
|
+
},
|
|
61
|
+
"homepage": "https://middy.js.org",
|
|
62
|
+
"funding": {
|
|
63
|
+
"type": "github",
|
|
64
|
+
"url": "https://github.com/sponsors/willfarrell"
|
|
65
|
+
},
|
|
66
|
+
"dependencies": {
|
|
67
|
+
"@middy/util": "6.2.0"
|
|
68
|
+
},
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"@middy/core": "6.2.0"
|
|
71
|
+
},
|
|
72
|
+
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
76
73
|
}
|