@cley_faye/boilerplate 3.7.0 → 3.7.1

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.
@@ -2,4 +2,8 @@ import { NextFunction, Request, Response } from "express";
2
2
  export declare type ExtraLoggingDataType = Record<string, unknown> | undefined;
3
3
  export declare type ExtraLoggingDataFunc = (req: Request) => Promise<ExtraLoggingDataType> | ExtraLoggingDataType;
4
4
  export declare type ExtraLoggingData = ExtraLoggingDataFunc | Record<string, unknown>;
5
- export declare const bodyLogger: (bodyProps?: string[] | undefined, extraLoggingData?: ExtraLoggingData | undefined) => (req: Request, res: Response, next: NextFunction) => void;
5
+ export interface ExtraLoggingOptions {
6
+ /** Do not expose the "hidden" body props */
7
+ noHiddenBodyProps?: boolean;
8
+ }
9
+ export declare const bodyLogger: (bodyProps?: string[] | undefined, extraLoggingData?: ExtraLoggingData | undefined, options?: ExtraLoggingOptions) => (req: Request, res: Response, next: NextFunction) => void;
@@ -38,12 +38,46 @@ const setLogInfoInReq = (req, body, extraData) => {
38
38
  castReq._cleyfayeLogging = { extraLoggingData: extraData };
39
39
  }
40
40
  };
41
- const bodyLogger = (bodyProps, extraLoggingData) => (req, res, next) => {
41
+ const getPropType = (value) => {
42
+ if (typeof value === "string")
43
+ return `string(${value.length})`;
44
+ if (typeof value === "number")
45
+ return "number";
46
+ if (typeof value === "boolean")
47
+ return "boolean";
48
+ if (typeof value === "undefined")
49
+ return "undefined";
50
+ if (Array.isArray(value))
51
+ return `Array(${value.length})`;
52
+ return "object";
53
+ };
54
+ const getHiddenBodyProps = (req, ignoreKeys = []) => {
55
+ if (typeof req.body !== "object")
56
+ return;
57
+ const body = req.body;
58
+ const res = {};
59
+ for (const key of Object.keys(body)) {
60
+ if (ignoreKeys.includes(key))
61
+ continue;
62
+ res[key] = getPropType(body[key]);
63
+ }
64
+ if (Object.keys(res).length === 0)
65
+ return;
66
+ return res;
67
+ };
68
+ const bodyLogger = (bodyProps, extraLoggingData, options = {}) => (req, res, next) => {
42
69
  const asyncFunc = async () => {
70
+ const allExtraData = {};
71
+ if (!options.noHiddenBodyProps) {
72
+ const redactedBody = getHiddenBodyProps(req, bodyProps);
73
+ if (redactedBody)
74
+ Object.assign(allExtraData, { redactedBody });
75
+ }
43
76
  const extraLogData = typeof extraLoggingData === "function"
44
77
  ? await extraLoggingData(req)
45
78
  : extraLoggingData;
46
- setLogInfoInReq(req, bodyProps, extraLogData);
79
+ Object.assign(allExtraData, extraLogData);
80
+ setLogInfoInReq(req, bodyProps, allExtraData);
47
81
  };
48
82
  asyncFunc()
49
83
  .then(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cley_faye/boilerplate",
3
- "version": "3.7.0",
3
+ "version": "3.7.1",
4
4
  "description": "Boilerplate code generator for generic projects",
5
5
  "repository": {
6
6
  "type": "git",