@middy/http-response-serializer 2.5.4 → 3.0.0-alpha.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/LICENSE +1 -1
- package/README.md +4 -4
- package/index.d.ts +1 -1
- package/package.json +7 -8
- package/index.js +0 -74
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017-
|
|
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 `
|
|
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
|
-
* `
|
|
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
|
-
|
|
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-
|
|
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
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-response-serializer",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-alpha.0",
|
|
4
4
|
"description": "The Http Serializer middleware lets you define serialization mechanisms based on the current content negotiation.",
|
|
5
|
-
"type": "
|
|
5
|
+
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=14"
|
|
8
8
|
},
|
|
9
9
|
"engineStrict": true,
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"
|
|
13
|
+
"exports": "./index.js",
|
|
14
14
|
"types": "index.d.ts",
|
|
15
15
|
"files": [
|
|
16
16
|
"index.d.ts"
|
|
@@ -50,11 +50,10 @@
|
|
|
50
50
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"@hapi/accept": "5.0.2",
|
|
53
|
-
"@middy/util": "^
|
|
53
|
+
"@middy/util": "^3.0.0-alpha.0"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@middy/core": "^
|
|
57
|
-
"http-errors": "^1.8.0"
|
|
56
|
+
"@middy/core": "^3.0.0-alpha.0"
|
|
58
57
|
},
|
|
59
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "c533f62841c8a39d061d7b94f30ba178f002c8db"
|
|
60
59
|
}
|
package/index.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const {
|
|
4
|
-
normalizeHttpResponse
|
|
5
|
-
} = require('@middy/util');
|
|
6
|
-
|
|
7
|
-
const Accept = require('@hapi/accept');
|
|
8
|
-
|
|
9
|
-
const defaults = {
|
|
10
|
-
serializers: [],
|
|
11
|
-
default: undefined
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const httpResponseSerializerMiddleware = (opts = {}) => {
|
|
15
|
-
const options = { ...defaults,
|
|
16
|
-
...opts
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const httpResponseSerializerMiddlewareAfter = async request => {
|
|
20
|
-
var _request$event;
|
|
21
|
-
|
|
22
|
-
if (request.response === undefined) return;
|
|
23
|
-
request.response = normalizeHttpResponse(request.response); // skip serialization when content-type is already set
|
|
24
|
-
|
|
25
|
-
if (request.response.headers['Content-Type'] || request.response.headers['content-type']) {
|
|
26
|
-
return;
|
|
27
|
-
} // find accept value(s)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
let types;
|
|
31
|
-
|
|
32
|
-
if ((_request$event = request.event) !== null && _request$event !== void 0 && _request$event.requiredContentType) {
|
|
33
|
-
types = [request.event.requiredContentType];
|
|
34
|
-
} else {
|
|
35
|
-
var _request$event$header, _request$event2, _request$event2$heade, _request$event3, _request$event3$heade, _ref;
|
|
36
|
-
|
|
37
|
-
const acceptHeader = (_request$event$header = (_request$event2 = request.event) === null || _request$event2 === void 0 ? void 0 : (_request$event2$heade = _request$event2.headers) === null || _request$event2$heade === void 0 ? void 0 : _request$event2$heade.accept) !== null && _request$event$header !== void 0 ? _request$event$header : (_request$event3 = request.event) === null || _request$event3 === void 0 ? void 0 : (_request$event3$heade = _request$event3.headers) === null || _request$event3$heade === void 0 ? void 0 : _request$event3$heade.Accept;
|
|
38
|
-
types = [...((_ref = acceptHeader && Accept.mediaTypes(acceptHeader)) !== null && _ref !== void 0 ? _ref : []), request.event.preferredContentType, options.default];
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
for (const type of types) {
|
|
42
|
-
let breakTypes;
|
|
43
|
-
|
|
44
|
-
for (const s of options.serializers) {
|
|
45
|
-
if (!s.regex.test(type)) {
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
request.response.headers['Content-Type'] = type;
|
|
50
|
-
const result = s.serializer(request.response);
|
|
51
|
-
|
|
52
|
-
if (typeof result === 'object' && 'body' in result) {
|
|
53
|
-
request.response = result;
|
|
54
|
-
} else {
|
|
55
|
-
// otherwise only replace the body attribute
|
|
56
|
-
request.response.body = result;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
breakTypes = true;
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (breakTypes) break;
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const httpResponseSerializerMiddlewareOnError = httpResponseSerializerMiddlewareAfter;
|
|
68
|
-
return {
|
|
69
|
-
after: httpResponseSerializerMiddlewareAfter,
|
|
70
|
-
onError: httpResponseSerializerMiddlewareOnError
|
|
71
|
-
};
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
module.exports = httpResponseSerializerMiddleware;
|