@middy/http-partial-response 3.0.0-alpha.0 → 3.0.0-alpha.4

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.
Files changed (2) hide show
  1. package/index.js +34 -0
  2. package/package.json +7 -5
package/index.js ADDED
@@ -0,0 +1,34 @@
1
+ import mask from 'json-mask';
2
+ import { normalizeHttpResponse, jsonSafeParse } from '@middy/util';
3
+ const defaults = {
4
+ filteringKeyName: 'fields'
5
+ };
6
+
7
+ const httpPartialResponseMiddleware = (opts = {}) => {
8
+ const options = { ...defaults,
9
+ ...opts
10
+ };
11
+ const {
12
+ filteringKeyName
13
+ } = options;
14
+
15
+ const httpPartialResponseMiddlewareAfter = async request => {
16
+ var _request$event, _request$event$queryS;
17
+
18
+ const fields = (_request$event = request.event) === null || _request$event === void 0 ? void 0 : (_request$event$queryS = _request$event.queryStringParameters) === null || _request$event$queryS === void 0 ? void 0 : _request$event$queryS[filteringKeyName];
19
+ if (!fields) return;
20
+ normalizeHttpResponse(request);
21
+ const body = request.response.body;
22
+ const bodyIsString = typeof body === 'string';
23
+ const parsedBody = jsonSafeParse(body);
24
+ if (typeof parsedBody !== 'object') return;
25
+ const filteredBody = mask(parsedBody, fields);
26
+ request.response.body = bodyIsString ? JSON.stringify(filteredBody) : filteredBody;
27
+ };
28
+
29
+ return {
30
+ after: httpPartialResponseMiddlewareAfter
31
+ };
32
+ };
33
+
34
+ export default httpPartialResponseMiddleware;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-partial-response",
3
- "version": "3.0.0-alpha.0",
3
+ "version": "3.0.0-alpha.4",
4
4
  "description": "Http partial response middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -13,11 +13,13 @@
13
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": [
@@ -47,11 +49,11 @@
47
49
  },
48
50
  "homepage": "https://github.com/middyjs/middy#readme",
49
51
  "dependencies": {
50
- "@middy/util": "^3.0.0-alpha.0",
52
+ "@middy/util": "^3.0.0-alpha.4",
51
53
  "json-mask": "1.0.4"
52
54
  },
53
- "gitHead": "c533f62841c8a39d061d7b94f30ba178f002c8db",
55
+ "gitHead": "d4bea7f4e21f6a9bbb1f6f6908361169598b9e53",
54
56
  "devDependencies": {
55
- "@middy/core": "^3.0.0-alpha.0"
57
+ "@middy/core": "^3.0.0-alpha.4"
56
58
  }
57
59
  }