-
-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathindex.js
More file actions
36 lines (28 loc) · 1 KB
/
index.js
File metadata and controls
36 lines (28 loc) · 1 KB
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
// Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors.
// SPDX-License-Identifier: MIT
import { jsonSafeParse, normalizeHttpResponse } from "@middy/util";
import mask from "json-mask";
const defaults = {
filteringKeyName: "fields",
};
const httpPartialResponseMiddleware = (opts = {}) => {
const options = { ...defaults, ...opts };
const { filteringKeyName } = options;
const httpPartialResponseMiddlewareAfter = (request) => {
const fields = request.event?.queryStringParameters?.[filteringKeyName];
if (!fields) return;
normalizeHttpResponse(request);
const body = request.response.body;
const bodyIsString = typeof body === "string";
const parsedBody = jsonSafeParse(body);
if (typeof parsedBody !== "object") return;
const filteredBody = mask(parsedBody, fields);
request.response.body = bodyIsString
? JSON.stringify(filteredBody)
: filteredBody;
};
return {
after: httpPartialResponseMiddlewareAfter,
};
};
export default httpPartialResponseMiddleware;