sm-utility 2.4.28-beta.3 → 2.4.29

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sm-utility",
3
- "version": "2.4.28-beta.3",
3
+ "version": "2.4.29",
4
4
  "description": "reusable utility codes for sm projects",
5
5
  "main": "index.js",
6
6
  "types": "./index.d.ts",
@@ -9,8 +9,9 @@ const request_interceptor_1 = require("./interceptors/request.interceptor");
9
9
  const response_interceptor_1 = require("./interceptors/response.interceptor");
10
10
  const error_interceptor_1 = require("./interceptors/error.interceptor");
11
11
  function createApi(axiosConfig) {
12
+ const instanceSanitizeKeys = (axiosConfig === null || axiosConfig === void 0 ? void 0 : axiosConfig.sanitizeKeys) || [];
12
13
  const axiosApi = axios_1.default.create(axiosConfig);
13
- axiosApi.interceptors.request.use((0, request_interceptor_1.createRequestInterceptor)(), (0, error_interceptor_1.createErrorInterceptor)());
14
- axiosApi.interceptors.response.use((0, response_interceptor_1.createResponseInterceptor)(), (0, error_interceptor_1.createErrorInterceptor)());
14
+ axiosApi.interceptors.request.use((0, request_interceptor_1.createRequestInterceptor)(instanceSanitizeKeys), (0, error_interceptor_1.createErrorInterceptor)(instanceSanitizeKeys));
15
+ axiosApi.interceptors.response.use((0, response_interceptor_1.createResponseInterceptor)(instanceSanitizeKeys), (0, error_interceptor_1.createErrorInterceptor)(instanceSanitizeKeys));
15
16
  return axiosApi;
16
17
  }
@@ -1,2 +1,2 @@
1
1
  import { AxiosError } from "axios";
2
- export declare function createErrorInterceptor(): (error: AxiosError) => never;
2
+ export declare function createErrorInterceptor(instanceSanitizeKeys?: string[]): (error: AxiosError) => never;
@@ -3,12 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createErrorInterceptor = createErrorInterceptor;
4
4
  const loggers_1 = require("../loggers");
5
5
  const utils_1 = require("../utils");
6
- function createErrorInterceptor() {
6
+ const sanitizer_1 = require("../sanitizer");
7
+ function createErrorInterceptor(instanceSanitizeKeys = []) {
7
8
  return (error) => {
8
9
  const { config, message, response } = error;
9
10
  const { method, meta } = config || {};
10
11
  const fullUrl = (0, utils_1.getFullUrlFromConfig)(config);
11
- (0, loggers_1.logError)(meta === null || meta === void 0 ? void 0 : meta.requestId, method, fullUrl, response === null || response === void 0 ? void 0 : response.data, response === null || response === void 0 ? void 0 : response.status, message);
12
+ const sanitizeKeys = (0, sanitizer_1.mergeSanitizeKeys)(instanceSanitizeKeys, config === null || config === void 0 ? void 0 : config.sanitizeKeys);
13
+ (0, loggers_1.logError)(meta === null || meta === void 0 ? void 0 : meta.requestId, method, fullUrl, (0, sanitizer_1.sanitizeValueByKeys)(response === null || response === void 0 ? void 0 : response.data, sanitizeKeys), response === null || response === void 0 ? void 0 : response.status, message);
12
14
  throw error;
13
15
  };
14
16
  }
@@ -1,2 +1,2 @@
1
1
  import { InternalAxiosRequestConfig } from "axios";
2
- export declare function createRequestInterceptor(): (request: InternalAxiosRequestConfig) => InternalAxiosRequestConfig<any>;
2
+ export declare function createRequestInterceptor(instanceSanitizeKeys?: string[]): (request: InternalAxiosRequestConfig) => InternalAxiosRequestConfig<any>;
@@ -4,15 +4,17 @@ exports.createRequestInterceptor = createRequestInterceptor;
4
4
  const uuid_1 = require("uuid");
5
5
  const utils_1 = require("../utils");
6
6
  const loggers_1 = require("../loggers");
7
- function createRequestInterceptor() {
7
+ const sanitizer_1 = require("../sanitizer");
8
+ function createRequestInterceptor(instanceSanitizeKeys = []) {
8
9
  return (request) => {
9
10
  if (request === null || request === void 0 ? void 0 : request.logRequest) {
10
11
  const { data, params, method } = request;
11
12
  const fullUrl = (0, utils_1.getFullUrlFromConfig)(request);
13
+ const sanitizeKeys = (0, sanitizer_1.mergeSanitizeKeys)(instanceSanitizeKeys, request.sanitizeKeys);
12
14
  request.meta = request.meta || {};
13
15
  request.meta.requestId = (0, uuid_1.v4)();
14
16
  request.meta.requestStartedAt = new Date().getTime();
15
- (0, loggers_1.logRequest)(request.meta.requestId, method, fullUrl, data, params);
17
+ (0, loggers_1.logRequest)(request.meta.requestId, method, fullUrl, (0, sanitizer_1.sanitizeValueByKeys)(data, sanitizeKeys), (0, sanitizer_1.sanitizeValueByKeys)(params, sanitizeKeys));
16
18
  }
17
19
  return request;
18
20
  };
@@ -1,2 +1,2 @@
1
1
  import { AxiosResponse } from "axios";
2
- export declare function createResponseInterceptor(): (response: AxiosResponse) => AxiosResponse<any, any, {}>;
2
+ export declare function createResponseInterceptor(instanceSanitizeKeys?: string[]): (response: AxiosResponse) => AxiosResponse<any, any, {}>;
@@ -3,7 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createResponseInterceptor = createResponseInterceptor;
4
4
  const loggers_1 = require("../loggers");
5
5
  const utils_1 = require("../utils");
6
- function createResponseInterceptor() {
6
+ const sanitizer_1 = require("../sanitizer");
7
+ function createResponseInterceptor(instanceSanitizeKeys = []) {
7
8
  return (response) => {
8
9
  var _a;
9
10
  if ((_a = response.config) === null || _a === void 0 ? void 0 : _a.logResponse) {
@@ -11,7 +12,8 @@ function createResponseInterceptor() {
11
12
  const { method, meta } = config;
12
13
  const fullUrl = (0, utils_1.getFullUrlFromConfig)(config);
13
14
  const responseTime = (0, utils_1.getResponseTimeFromConfig)(config);
14
- (0, loggers_1.logResponse)(meta === null || meta === void 0 ? void 0 : meta.requestId, method, fullUrl, data, status, responseTime);
15
+ const sanitizeKeys = (0, sanitizer_1.mergeSanitizeKeys)(instanceSanitizeKeys, config.sanitizeKeys);
16
+ (0, loggers_1.logResponse)(meta === null || meta === void 0 ? void 0 : meta.requestId, method, fullUrl, (0, sanitizer_1.sanitizeValueByKeys)(data, sanitizeKeys), status, responseTime);
15
17
  }
16
18
  return response;
17
19
  };
@@ -0,0 +1,2 @@
1
+ export declare function mergeSanitizeKeys(instanceKeys?: string[], requestKeys?: string[]): string[];
2
+ export declare function sanitizeValueByKeys<T>(value: T, sanitizeKeys?: string[]): T;
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mergeSanitizeKeys = mergeSanitizeKeys;
4
+ exports.sanitizeValueByKeys = sanitizeValueByKeys;
5
+ function isObjectLike(value) {
6
+ return typeof value === "object" && value !== null;
7
+ }
8
+ function maskString(value) {
9
+ if (!value.length) {
10
+ return value;
11
+ }
12
+ const visibleLength = Math.max(1, Math.ceil(value.length * 0.2));
13
+ const visiblePart = value.slice(0, visibleLength);
14
+ const maskedPart = "*".repeat(Math.max(0, value.length - visibleLength));
15
+ return visiblePart + maskedPart;
16
+ }
17
+ function maskSanitizedValue(value) {
18
+ if (value === null || value === undefined) {
19
+ return value;
20
+ }
21
+ if (Array.isArray(value)) {
22
+ return value.map((item) => maskSanitizedValue(item));
23
+ }
24
+ if (isObjectLike(value)) {
25
+ const maskedObject = {};
26
+ for (const [key, nestedValue] of Object.entries(value)) {
27
+ maskedObject[key] = maskSanitizedValue(nestedValue);
28
+ }
29
+ return maskedObject;
30
+ }
31
+ return maskString(String(value));
32
+ }
33
+ function mergeSanitizeKeys(instanceKeys = [], requestKeys = []) {
34
+ return Array.from(new Set([...instanceKeys, ...requestKeys]));
35
+ }
36
+ function sanitizeValueByKeys(value, sanitizeKeys = []) {
37
+ if (!sanitizeKeys.length || !isObjectLike(value)) {
38
+ return value;
39
+ }
40
+ const keySet = new Set(sanitizeKeys);
41
+ const visited = new WeakMap();
42
+ const sanitizeRecursive = (currentValue) => {
43
+ if (!isObjectLike(currentValue)) {
44
+ return currentValue;
45
+ }
46
+ if (visited.has(currentValue)) {
47
+ return visited.get(currentValue);
48
+ }
49
+ if (Array.isArray(currentValue)) {
50
+ const sanitizedArray = [];
51
+ visited.set(currentValue, sanitizedArray);
52
+ for (const item of currentValue) {
53
+ sanitizedArray.push(sanitizeRecursive(item));
54
+ }
55
+ return sanitizedArray;
56
+ }
57
+ const sanitizedObject = {};
58
+ visited.set(currentValue, sanitizedObject);
59
+ for (const [key, nestedValue] of Object.entries(currentValue)) {
60
+ sanitizedObject[key] = keySet.has(key)
61
+ ? maskSanitizedValue(nestedValue)
62
+ : sanitizeRecursive(nestedValue);
63
+ }
64
+ return sanitizedObject;
65
+ };
66
+ return sanitizeRecursive(value);
67
+ }
@@ -5,6 +5,7 @@ declare module 'axios' {
5
5
  meta?: Record<string, any>;
6
6
  logRequest?: boolean;
7
7
  logResponse?: boolean;
8
+ sanitizeKeys?: string[];
8
9
  }
9
10
  }
10
11
  export type CustomClientCreateApiParams = Omit<AxiosRequestConfig, 'meta'>;