mock-config-server 2.4.0 → 3.0.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.
Files changed (78) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +502 -326
  3. package/dist/bin/validateMockServerConfig/helpers/index.d.ts +2 -0
  4. package/dist/bin/validateMockServerConfig/helpers/index.js +18 -0
  5. package/dist/bin/validateMockServerConfig/helpers/isCheckModeValid/index.d.ts +1 -0
  6. package/dist/bin/validateMockServerConfig/helpers/isCheckModeValid/index.js +17 -0
  7. package/dist/bin/validateMockServerConfig/helpers/isCheckModeValid/isCheckModeValid.d.ts +1 -0
  8. package/dist/bin/validateMockServerConfig/helpers/isCheckModeValid/isCheckModeValid.js +10 -0
  9. package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/index.d.ts +1 -0
  10. package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/index.js +17 -0
  11. package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/isDescriptorValueValid.d.ts +1 -0
  12. package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/isDescriptorValueValid.js +23 -0
  13. package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateRoutes/validateRoutes.js +44 -18
  14. package/dist/bin/validateMockServerConfig/validateRestConfig/validateRoutes/validateRoutes.js +38 -42
  15. package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.js +17 -6
  16. package/dist/src/core/graphql/createGraphQLRoutes/helpers/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.js +7 -2
  17. package/dist/src/core/middlewares/corsMiddleware/corsMiddleware.js +6 -5
  18. package/dist/src/core/middlewares/errorMiddleware/errorMiddleware.d.ts +2 -0
  19. package/dist/src/core/middlewares/errorMiddleware/errorMiddleware.js +18 -0
  20. package/dist/src/core/middlewares/index.d.ts +1 -0
  21. package/dist/src/core/middlewares/index.js +1 -0
  22. package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.d.ts +1 -6
  23. package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.js +1 -1
  24. package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.js +2 -2
  25. package/dist/src/core/rest/createRestRoutes/createRestRoutes.js +19 -3
  26. package/dist/src/core/rest/createRestRoutes/helpers/prepareRestRequestConfigs/prepareRestRequestConfigs.js +6 -1
  27. package/dist/src/server/createMockServer/createMockServer.js +2 -4
  28. package/dist/src/static/views/assets/icons/scheme-dark.svg +3 -3
  29. package/dist/src/static/views/assets/icons/scheme-light.svg +3 -3
  30. package/dist/src/static/views/assets/images/404.png +0 -0
  31. package/dist/src/static/views/assets/images/logo.png +0 -0
  32. package/dist/src/static/views/assets/styles/global.css +88 -88
  33. package/dist/src/static/views/components/header/index.css +55 -55
  34. package/dist/src/static/views/components/header/index.ejs +39 -39
  35. package/dist/src/static/views/features/scheme/dark.css +12 -12
  36. package/dist/src/static/views/features/scheme/index.ejs +3 -3
  37. package/dist/src/static/views/features/scheme/light.css +12 -12
  38. package/dist/src/static/views/features/tab/index.css +30 -30
  39. package/dist/src/static/views/features/tab/index.ejs +1 -1
  40. package/dist/src/static/views/pages/404/index.css +10 -10
  41. package/dist/src/static/views/pages/404/index.ejs +84 -84
  42. package/dist/src/utils/constants/checkModes.d.ts +9 -0
  43. package/dist/src/utils/constants/checkModes.js +31 -0
  44. package/dist/src/utils/constants/default.js +1 -1
  45. package/dist/src/utils/constants/index.d.ts +1 -0
  46. package/dist/src/utils/constants/index.js +1 -0
  47. package/dist/src/utils/helpers/asyncHandler.d.ts +2 -0
  48. package/dist/src/utils/helpers/asyncHandler.js +5 -0
  49. package/dist/src/utils/helpers/config/index.d.ts +1 -1
  50. package/dist/src/utils/helpers/config/index.js +1 -1
  51. package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.d.ts +2 -0
  52. package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.js +131 -0
  53. package/dist/src/utils/helpers/entities/convertToEntityDescriptor/convertToEntityDescriptor.d.ts +2 -0
  54. package/dist/src/utils/helpers/entities/convertToEntityDescriptor/convertToEntityDescriptor.js +8 -0
  55. package/dist/src/utils/helpers/entities/index.d.ts +2 -0
  56. package/dist/src/utils/helpers/entities/index.js +18 -0
  57. package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.d.ts +1 -0
  58. package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.js +6 -0
  59. package/dist/src/utils/helpers/index.d.ts +4 -0
  60. package/dist/src/utils/helpers/index.js +4 -0
  61. package/dist/src/utils/helpers/isPlainObject/isPlainObject.js +4 -1
  62. package/dist/src/utils/helpers/isPrimitive/isPrimitive.d.ts +2 -0
  63. package/dist/src/utils/helpers/isPrimitive/isPrimitive.js +5 -0
  64. package/dist/src/utils/helpers/isRegExp/isRegExp.d.ts +1 -0
  65. package/dist/src/utils/helpers/isRegExp/isRegExp.js +5 -0
  66. package/dist/src/utils/types/checkModes.d.ts +12 -0
  67. package/dist/src/utils/types/checkModes.js +2 -0
  68. package/dist/src/utils/types/graphql.d.ts +58 -18
  69. package/dist/src/utils/types/index.d.ts +1 -0
  70. package/dist/src/utils/types/index.js +1 -0
  71. package/dist/src/utils/types/interceptors.d.ts +4 -3
  72. package/dist/src/utils/types/rest.d.ts +61 -23
  73. package/dist/src/utils/types/server.d.ts +4 -3
  74. package/dist/src/utils/types/values.d.ts +1 -7
  75. package/package.json +113 -113
  76. package/dist/src/static/views/assets/images/success.png +0 -0
  77. package/dist/src/utils/helpers/config/isEntitiesEqual/isEntityValuesEqual.d.ts +0 -1
  78. package/dist/src/utils/helpers/config/isEntitiesEqual/isEntityValuesEqual.js +0 -29
@@ -0,0 +1,2 @@
1
+ export * from './isCheckModeValid';
2
+ export * from './isDescriptorValueValid';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./isCheckModeValid"), exports);
18
+ __exportStar(require("./isDescriptorValueValid"), exports);
@@ -0,0 +1 @@
1
+ export * from './isCheckModeValid';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./isCheckModeValid"), exports);
@@ -0,0 +1 @@
1
+ export declare const isCheckModeValid: (checkMode: unknown, entityName?: unknown) => boolean;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isCheckModeValid = void 0;
4
+ const constants_1 = require("../../../../src/utils/constants");
5
+ const isCheckModeValid = (checkMode, entityName) => {
6
+ if (entityName === 'body' || entityName === 'variables')
7
+ return constants_1.PLAIN_ENTITY_CHECK_MODES.includes(checkMode);
8
+ return constants_1.CHECK_MODES.includes(checkMode);
9
+ };
10
+ exports.isCheckModeValid = isCheckModeValid;
@@ -0,0 +1 @@
1
+ export * from './isDescriptorValueValid';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./isDescriptorValueValid"), exports);
@@ -0,0 +1 @@
1
+ export declare const isDescriptorValueValid: (checkMode: unknown, value: unknown, entityName?: unknown) => boolean;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isDescriptorValueValid = void 0;
4
+ const constants_1 = require("../../../../src/utils/constants");
5
+ const isDescriptorValueValid = (checkMode, value, entityName) => {
6
+ if (constants_1.CHECK_ACTUAL_VALUE_CHECK_MODES.includes(checkMode))
7
+ return typeof value === 'undefined';
8
+ if (constants_1.COMPARE_WITH_DESCRIPTOR_VALUE_CHECK_MODES.includes(checkMode)) {
9
+ if (entityName === 'body' || entityName === 'variables') {
10
+ return (typeof value === 'object' &&
11
+ value !== null &&
12
+ typeof value !== 'function' &&
13
+ !(value instanceof RegExp));
14
+ }
15
+ return typeof value === 'boolean' || typeof value === 'number' || typeof value === 'string';
16
+ }
17
+ if (checkMode === 'function')
18
+ return typeof value === 'function';
19
+ if (checkMode === 'regExp')
20
+ return value instanceof RegExp;
21
+ throw new Error('Invalid checkMode');
22
+ };
23
+ exports.isDescriptorValueValid = isDescriptorValueValid;
@@ -2,39 +2,65 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.validateRoutes = void 0;
4
4
  const helpers_1 = require("../../../../src/utils/helpers");
5
+ const helpers_2 = require("../../helpers");
5
6
  const validateInterceptors_1 = require("../../validateInterceptors/validateInterceptors");
6
7
  const ALLOWED_ENTITIES_BY_OPERATION_TYPE = {
7
8
  query: ['headers', 'cookies', 'query', 'variables'],
8
9
  mutation: ['headers', 'cookies', 'query', 'variables']
9
10
  };
10
- const validateHeadersOrCookiesOrQuery = (headersOrCookiesOrQuery, entity) => {
11
- const isHeadersOrCookiesOrQueryObject = (0, helpers_1.isPlainObject)(headersOrCookiesOrQuery);
12
- if (isHeadersOrCookiesOrQueryObject) {
13
- Object.entries(headersOrCookiesOrQuery).forEach(([headerOrCookieOrQueryKey, headerOrCookieOrQueryValue]) => {
14
- if (typeof headerOrCookieOrQueryValue !== 'string') {
15
- throw new Error(`${entity}.${headerOrCookieOrQueryKey}`);
11
+ const validateEntity = (entity, entityName) => {
12
+ const { checkMode: topLevelCheckMode, value: topLevelValue } = (0, helpers_1.convertToEntityDescriptor)(entity);
13
+ const isVariables = entityName === 'variables';
14
+ const isTopLevelDescriptor = (0, helpers_1.isEntityDescriptor)(entity);
15
+ if (isTopLevelDescriptor && isVariables) {
16
+ if (!(0, helpers_2.isCheckModeValid)(topLevelCheckMode, 'variables')) {
17
+ throw new Error('variables.checkMode');
18
+ }
19
+ if (!(0, helpers_2.isDescriptorValueValid)(topLevelCheckMode, topLevelValue, 'variables')) {
20
+ const errorMessage = 'variables.value';
21
+ throw new Error(errorMessage);
22
+ }
23
+ }
24
+ const isEntityObject = (0, helpers_1.isPlainObject)(entity) && !(entity instanceof RegExp);
25
+ const isEntityArray = Array.isArray(entity) && isVariables;
26
+ if (isEntityObject || isEntityArray) {
27
+ Object.entries(topLevelValue).forEach(([key, valueOrDescriptor]) => {
28
+ const { checkMode, value } = (0, helpers_1.convertToEntityDescriptor)(valueOrDescriptor);
29
+ if (!(0, helpers_2.isCheckModeValid)(checkMode)) {
30
+ throw new Error(`${entityName}.${key}.checkMode`);
31
+ }
32
+ const isDescriptor = (0, helpers_1.isEntityDescriptor)(valueOrDescriptor);
33
+ const errorMessage = `${entityName}.${key}${isDescriptor ? '.value' : ''}`;
34
+ const isValueArray = Array.isArray(value);
35
+ if (isValueArray && !isVariables) {
36
+ value.forEach((element, index) => {
37
+ if (!(0, helpers_2.isDescriptorValueValid)(checkMode, element)) {
38
+ throw new Error(`${errorMessage}[${index}]`);
39
+ }
40
+ });
41
+ return;
42
+ }
43
+ if (!(0, helpers_2.isDescriptorValueValid)(checkMode, value)) {
44
+ throw new Error(errorMessage);
16
45
  }
17
46
  });
18
47
  return;
19
48
  }
20
- throw new Error(entity);
49
+ throw new Error(entityName);
21
50
  };
22
51
  const validateEntities = (entities, operationType) => {
23
52
  const isEntitiesObject = (0, helpers_1.isPlainObject)(entities);
24
53
  if (isEntitiesObject) {
25
- Object.keys(entities).forEach((entity) => {
26
- const isEntityAllowed = ALLOWED_ENTITIES_BY_OPERATION_TYPE[operationType].includes(entity);
54
+ Object.keys(entities).forEach((entityName) => {
55
+ const isEntityAllowed = ALLOWED_ENTITIES_BY_OPERATION_TYPE[operationType].includes(entityName);
27
56
  if (!isEntityAllowed) {
28
- throw new Error(`entities.${entity}`);
57
+ throw new Error(`entities.${entityName}`);
29
58
  }
30
- if (entity === 'headers' || entity === 'query' || entity === 'cookies') {
31
- try {
32
- const headersOrCookiesOrQuery = entities[entity];
33
- validateHeadersOrCookiesOrQuery(headersOrCookiesOrQuery, entity);
34
- }
35
- catch (error) {
36
- throw new Error(`entities.${error.message}`);
37
- }
59
+ try {
60
+ validateEntity(entities[entityName], entityName);
61
+ }
62
+ catch (error) {
63
+ throw new Error(`entities.${error.message}`);
38
64
  }
39
65
  });
40
66
  return;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.validateRoutes = void 0;
4
4
  const helpers_1 = require("../../../../src/utils/helpers");
5
+ const helpers_2 = require("../../helpers");
5
6
  const validateInterceptors_1 = require("../../validateInterceptors/validateInterceptors");
6
7
  const ALLOWED_ENTITIES_BY_METHOD = {
7
8
  get: ['headers', 'cookies', 'query', 'params'],
@@ -11,64 +12,59 @@ const ALLOWED_ENTITIES_BY_METHOD = {
11
12
  patch: ['headers', 'cookies', 'query', 'params', 'body'],
12
13
  options: ['headers', 'cookies', 'query', 'params']
13
14
  };
14
- const validateHeadersOrCookiesOrParams = (headersOrCookiesOrParams, entity) => {
15
- const isHeadersOrCookiesOrParamsObject = (0, helpers_1.isPlainObject)(headersOrCookiesOrParams);
16
- if (isHeadersOrCookiesOrParamsObject) {
17
- Object.entries(headersOrCookiesOrParams).forEach(([headerOrCookieOrParamKey, headerOrCookieOrParamValue]) => {
18
- if (typeof headerOrCookieOrParamValue !== 'string') {
19
- throw new Error(`${entity}.${headerOrCookieOrParamKey}`);
20
- }
21
- });
22
- return;
15
+ const validateEntity = (entity, entityName) => {
16
+ const { checkMode: topLevelCheckMode, value: topLevelValue } = (0, helpers_1.convertToEntityDescriptor)(entity);
17
+ const isBody = entityName === 'body';
18
+ const isTopLevelDescriptor = (0, helpers_1.isEntityDescriptor)(entity);
19
+ if (isTopLevelDescriptor && isBody) {
20
+ if (!(0, helpers_2.isCheckModeValid)(topLevelCheckMode, 'body')) {
21
+ throw new Error('body.checkMode');
22
+ }
23
+ if (!(0, helpers_2.isDescriptorValueValid)(topLevelCheckMode, topLevelValue, 'body')) {
24
+ const errorMessage = 'body.value';
25
+ throw new Error(errorMessage);
26
+ }
23
27
  }
24
- throw new Error(entity);
25
- };
26
- const validateQuery = (query, entity) => {
27
- const isQueryObject = (0, helpers_1.isPlainObject)(query);
28
- if (isQueryObject) {
29
- Object.entries(query).forEach(([queryKey, queryValue]) => {
30
- const isQueryValueArray = Array.isArray(queryValue);
31
- if (isQueryValueArray) {
32
- queryValue.forEach((queryValueElement, index) => {
33
- if (typeof queryValueElement !== 'string') {
34
- throw new Error(`${entity}.${queryKey}[${index}]`);
28
+ const isEntityObject = (0, helpers_1.isPlainObject)(entity) && !(entity instanceof RegExp);
29
+ const isEntityArray = Array.isArray(entity) && isBody;
30
+ if (isEntityObject || isEntityArray) {
31
+ Object.entries(topLevelValue).forEach(([key, valueOrDescriptor]) => {
32
+ const { checkMode, value } = (0, helpers_1.convertToEntityDescriptor)(valueOrDescriptor);
33
+ if (!(0, helpers_2.isCheckModeValid)(checkMode)) {
34
+ throw new Error(`${entityName}.${key}.checkMode`);
35
+ }
36
+ const isDescriptor = (0, helpers_1.isEntityDescriptor)(valueOrDescriptor);
37
+ const errorMessage = `${entityName}.${key}${isDescriptor ? '.value' : ''}`;
38
+ const isValueArray = Array.isArray(value);
39
+ if (isValueArray && !isBody) {
40
+ value.forEach((element, index) => {
41
+ if (!(0, helpers_2.isDescriptorValueValid)(checkMode, element)) {
42
+ throw new Error(`${errorMessage}[${index}]`);
35
43
  }
36
44
  });
37
45
  return;
38
46
  }
39
- if (typeof queryValue !== 'string') {
40
- throw new Error(`${entity}.${queryKey}`);
47
+ if (!(0, helpers_2.isDescriptorValueValid)(checkMode, value)) {
48
+ throw new Error(errorMessage);
41
49
  }
42
50
  });
43
51
  return;
44
52
  }
45
- throw new Error(entity);
53
+ throw new Error(entityName);
46
54
  };
47
55
  const validateEntities = (entities, method) => {
48
56
  const isEntitiesObject = (0, helpers_1.isPlainObject)(entities);
49
57
  if (isEntitiesObject) {
50
- Object.keys(entities).forEach((entity) => {
51
- const isEntityAllowed = ALLOWED_ENTITIES_BY_METHOD[method].includes(entity);
58
+ Object.keys(entities).forEach((entityName) => {
59
+ const isEntityAllowed = ALLOWED_ENTITIES_BY_METHOD[method].includes(entityName);
52
60
  if (!isEntityAllowed) {
53
- throw new Error(`entities.${entity}`);
61
+ throw new Error(`entities.${entityName}`);
54
62
  }
55
- if (entity === 'headers' || entity === 'params' || entity === 'cookies') {
56
- try {
57
- const headersOrCookiesOrParams = entities[entity];
58
- validateHeadersOrCookiesOrParams(headersOrCookiesOrParams, entity);
59
- }
60
- catch (error) {
61
- throw new Error(`entities.${error.message}`);
62
- }
63
+ try {
64
+ validateEntity(entities[entityName], entityName);
63
65
  }
64
- if (entity === 'query') {
65
- try {
66
- const query = entities[entity];
67
- validateQuery(query, entity);
68
- }
69
- catch (error) {
70
- throw new Error(`entities.${error.message}`);
71
- }
66
+ catch (error) {
67
+ throw new Error(`entities.${error.message}`);
72
68
  }
73
69
  });
74
70
  return;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createGraphQLRoutes = void 0;
4
+ const flat_1 = require("flat");
4
5
  const helpers_1 = require("../../../utils/helpers");
5
6
  const helpers_2 = require("./helpers");
6
7
  const createGraphQLRoutes = (router, graphqlConfig, serverResponseInterceptors) => {
@@ -41,11 +42,21 @@ const createGraphQLRoutes = (router, graphqlConfig, serverResponseInterceptors)
41
42
  const matchedRouteConfig = matchedRequestConfig.routes.find(({ entities }) => {
42
43
  if (!entities)
43
44
  return true;
44
- return Object.entries(entities).every(([entity, entityValue]) => {
45
- if (entity === 'variables') {
46
- return (0, helpers_1.isEntityValuesEqual)(entityValue, graphQLInput.variables);
45
+ const entries = Object.entries(entities);
46
+ return entries.every(([entityName, valueOrDescriptor]) => {
47
+ const { checkMode, value: descriptorValue } = (0, helpers_1.convertToEntityDescriptor)(valueOrDescriptor);
48
+ // ✅ important: check whole variables as plain value strictly if descriptor used for variables
49
+ const isVariablesPlain = entityName === 'variables' && (0, helpers_1.isEntityDescriptor)(valueOrDescriptor);
50
+ if (isVariablesPlain) {
51
+ // ✅ important: getGraphQLInput returns empty object if variables not sent or invalid, so count {} as undefined
52
+ return (0, helpers_1.resolveEntityValues)(checkMode, Object.keys(graphQLInput.variables).length ? graphQLInput.variables : undefined, descriptorValue);
47
53
  }
48
- return (0, helpers_1.isEntityValuesEqual)(entityValue, request[entity]);
54
+ const mappedEntityDescriptors = Object.entries(valueOrDescriptor);
55
+ return mappedEntityDescriptors.every(([entityKey, mappedEntityDescriptor]) => {
56
+ const { checkMode, value: descriptorValue } = (0, helpers_1.convertToEntityDescriptor)(mappedEntityDescriptor);
57
+ const flattenEntity = (0, flat_1.flatten)(entityName === 'variables' ? graphQLInput.variables : request[entityName]);
58
+ return (0, helpers_1.resolveEntityValues)(checkMode, flattenEntity[entityKey], descriptorValue);
59
+ });
49
60
  });
50
61
  });
51
62
  if (!matchedRouteConfig) {
@@ -71,8 +82,8 @@ const createGraphQLRoutes = (router, graphqlConfig, serverResponseInterceptors)
71
82
  response.set('Cache-control', 'max-age=0, must-revalidate');
72
83
  return response.status(response.statusCode).json(data);
73
84
  };
74
- router.route('/').get(graphqlMiddleware);
75
- router.route('/').post(graphqlMiddleware);
85
+ router.route('/').get((0, helpers_1.asyncHandler)(graphqlMiddleware));
86
+ router.route('/').post((0, helpers_1.asyncHandler)(graphqlMiddleware));
76
87
  return router;
77
88
  };
78
89
  exports.createGraphQLRoutes = createGraphQLRoutes;
@@ -14,8 +14,13 @@ const calculateRouteConfigWeight = (graphQLRouteConfig) => {
14
14
  routeConfigWeight += Object.keys(cookies).length;
15
15
  if (query)
16
16
  routeConfigWeight += Object.keys(query).length;
17
- if (variables)
18
- routeConfigWeight += (0, helpers_1.isPlainObject)(variables) ? Object.keys(variables).length : 1;
17
+ if (variables) {
18
+ if ((0, helpers_1.isPlainObject)(variables) && variables.checkMode) {
19
+ routeConfigWeight += (0, helpers_1.isPlainObject)(variables.value) ? Object.keys(variables.value).length : 1;
20
+ return routeConfigWeight;
21
+ }
22
+ routeConfigWeight += Object.keys(variables).length;
23
+ }
19
24
  return routeConfigWeight;
20
25
  };
21
26
  const prepareGraphQLRequestConfigs = (requestConfigs) => {
@@ -2,19 +2,20 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.corsMiddleware = void 0;
4
4
  const constants_1 = require("../../../utils/constants");
5
- const helpers_1 = require("./helpers");
5
+ const helpers_1 = require("../../../utils/helpers");
6
+ const helpers_2 = require("./helpers");
6
7
  const corsMiddleware = (server, cors) => {
7
- server.use(async (request, response, next) => {
8
+ server.use((0, helpers_1.asyncHandler)(async (request, response, next) => {
8
9
  if (Array.isArray(cors.origin) && !cors.origin.length) {
9
10
  return next();
10
11
  }
11
12
  let allowedOrigins = [];
12
13
  if (typeof cors.origin === 'function') {
13
14
  const origins = await cors.origin(request);
14
- allowedOrigins = (0, helpers_1.getAllowedOrigins)(origins);
15
+ allowedOrigins = (0, helpers_2.getAllowedOrigins)(origins);
15
16
  }
16
17
  else {
17
- allowedOrigins = (0, helpers_1.getAllowedOrigins)(cors.origin);
18
+ allowedOrigins = (0, helpers_2.getAllowedOrigins)(cors.origin);
18
19
  }
19
20
  const { origin } = request.headers;
20
21
  if (!allowedOrigins?.length || !origin) {
@@ -39,6 +40,6 @@ const corsMiddleware = (server, cors) => {
39
40
  }
40
41
  }
41
42
  return next();
42
- });
43
+ }));
43
44
  };
44
45
  exports.corsMiddleware = corsMiddleware;
@@ -0,0 +1,2 @@
1
+ import type { Express } from 'express';
2
+ export declare const errorMiddleware: (server: Express) => void;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.errorMiddleware = void 0;
7
+ const ansi_colors_1 = __importDefault(require("ansi-colors"));
8
+ const errorMiddleware = (server) => {
9
+ server.use(((error, request, response, next) => {
10
+ console.error(ansi_colors_1.default.bgRed(`\nError on ${request.method} ${request.url} request\n`));
11
+ const message = `Message: ${error.message ?? 'Internal server error'}\n\n${error.stack}`;
12
+ response.status(error.status || 500).send(message);
13
+ // ✅ important:
14
+ // call next function for trigger default express error handling behavior
15
+ next(error);
16
+ }));
17
+ };
18
+ exports.errorMiddleware = errorMiddleware;
@@ -5,3 +5,4 @@ export * from './notFoundMiddleware/notFoundMiddleware';
5
5
  export * from './requestInterceptorMiddleware/requestInterceptorMiddleware';
6
6
  export * from './destroyerMiddleware/destroyerMiddleware';
7
7
  export * from './cookieParseMiddleware/cookieParseMiddleware';
8
+ export * from './errorMiddleware/errorMiddleware';
@@ -21,3 +21,4 @@ __exportStar(require("./notFoundMiddleware/notFoundMiddleware"), exports);
21
21
  __exportStar(require("./requestInterceptorMiddleware/requestInterceptorMiddleware"), exports);
22
22
  __exportStar(require("./destroyerMiddleware/destroyerMiddleware"), exports);
23
23
  __exportStar(require("./cookieParseMiddleware/cookieParseMiddleware"), exports);
24
+ __exportStar(require("./errorMiddleware/errorMiddleware"), exports);
@@ -1,8 +1,3 @@
1
1
  import type { Express } from 'express';
2
2
  import type { MockServerConfig } from '../../../utils/types';
3
- interface NotFoundMiddlewareParams {
4
- server: Express;
5
- mockServerConfig: Pick<MockServerConfig, 'baseUrl' | 'rest' | 'graphql'>;
6
- }
7
- export declare const notFoundMiddleware: ({ server, mockServerConfig }: NotFoundMiddlewareParams) => void;
8
- export {};
3
+ export declare const notFoundMiddleware: (server: Express, mockServerConfig: Pick<MockServerConfig, 'baseUrl' | 'rest' | 'graphql'>) => void;
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.notFoundMiddleware = void 0;
4
4
  const helpers_1 = require("../../../utils/helpers");
5
5
  const helpers_2 = require("./helpers");
6
- const notFoundMiddleware = ({ server, mockServerConfig }) => {
6
+ const notFoundMiddleware = (server, mockServerConfig) => {
7
7
  const { baseUrl: serverBaseUrl, rest, graphql } = mockServerConfig;
8
8
  const restRequestConfigs = rest?.configs
9
9
  .filter(({ path }) => !(path instanceof RegExp))
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.requestInterceptorMiddleware = void 0;
4
4
  const helpers_1 = require("../../../utils/helpers");
5
5
  const requestInterceptorMiddleware = (server, interceptor) => {
6
- server.use(async (request, _response, next) => {
6
+ server.use((0, helpers_1.asyncHandler)(async (request, _response, next) => {
7
7
  await (0, helpers_1.callRequestInterceptor)({ request, interceptor });
8
8
  return next();
9
- });
9
+ }));
10
10
  };
11
11
  exports.requestInterceptorMiddleware = requestInterceptorMiddleware;
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createRestRoutes = void 0;
4
+ const flat_1 = require("flat");
4
5
  const helpers_1 = require("../../../utils/helpers");
5
6
  const helpers_2 = require("./helpers");
6
7
  const createRestRoutes = (router, restConfig, serverResponseInterceptors) => {
7
8
  (0, helpers_2.prepareRestRequestConfigs)(restConfig.configs).forEach((requestConfig) => {
8
- router.route(requestConfig.path)[requestConfig.method](async (request, response, next) => {
9
+ router.route(requestConfig.path)[requestConfig.method]((0, helpers_1.asyncHandler)(async (request, response, next) => {
9
10
  const requestInterceptor = requestConfig.interceptors?.request;
10
11
  if (requestInterceptor) {
11
12
  await (0, helpers_1.callRequestInterceptor)({ request, interceptor: requestInterceptor });
@@ -13,7 +14,22 @@ const createRestRoutes = (router, restConfig, serverResponseInterceptors) => {
13
14
  const matchedRouteConfig = requestConfig.routes.find(({ entities }) => {
14
15
  if (!entities)
15
16
  return true;
16
- return Object.entries(entities).every(([entity, entityValue]) => (0, helpers_1.isEntityValuesEqual)(entityValue, request[entity]));
17
+ const entries = Object.entries(entities);
18
+ return entries.every(([entityName, valueOrDescriptor]) => {
19
+ const { checkMode, value: descriptorValue } = (0, helpers_1.convertToEntityDescriptor)(valueOrDescriptor);
20
+ // ✅ important: check whole body as plain value strictly if descriptor used for body
21
+ const isBodyPlain = entityName === 'body' && (0, helpers_1.isEntityDescriptor)(valueOrDescriptor);
22
+ if (isBodyPlain) {
23
+ // ✅ important: bodyParser sets body to empty object if body not sent or invalid, so count {} as undefined
24
+ return (0, helpers_1.resolveEntityValues)(checkMode, Object.keys(request.body).length ? request.body : undefined, descriptorValue);
25
+ }
26
+ const mappedEntityDescriptors = Object.entries(valueOrDescriptor);
27
+ return mappedEntityDescriptors.every(([entityKey, mappedEntityDescriptor]) => {
28
+ const { checkMode, value: descriptorValue } = (0, helpers_1.convertToEntityDescriptor)(mappedEntityDescriptor);
29
+ const flattenEntity = (0, flat_1.flatten)(request[entityName]);
30
+ return (0, helpers_1.resolveEntityValues)(checkMode, flattenEntity[entityKey], descriptorValue);
31
+ });
32
+ });
17
33
  });
18
34
  if (!matchedRouteConfig) {
19
35
  return next();
@@ -37,7 +53,7 @@ const createRestRoutes = (router, restConfig, serverResponseInterceptors) => {
37
53
  // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
38
54
  response.set('Cache-control', 'max-age=0, must-revalidate');
39
55
  return response.status(response.statusCode).json(data);
40
- });
56
+ }));
41
57
  });
42
58
  return router;
43
59
  };
@@ -16,8 +16,13 @@ const calculateRouteConfigWeight = (restRouteConfig) => {
16
16
  routeConfigWeight += Object.keys(query).length;
17
17
  if (params)
18
18
  routeConfigWeight += Object.keys(params).length;
19
- if (body)
19
+ if (body) {
20
+ if ((0, helpers_1.isPlainObject)(body) && body.checkMode) {
21
+ routeConfigWeight += (0, helpers_1.isPlainObject)(body.value) ? Object.keys(body.value).length : 1;
22
+ return routeConfigWeight;
23
+ }
20
24
  routeConfigWeight += (0, helpers_1.isPlainObject)(body) ? Object.keys(body).length : 1;
25
+ }
21
26
  return routeConfigWeight;
22
27
  };
23
28
  const prepareRestRequestConfigs = (requestConfigs) => {
@@ -51,10 +51,8 @@ const createMockServer = (mockServerConfig) => {
51
51
  const graphqlBaseUrl = (0, helpers_1.urlJoin)(baseUrl, graphql.baseUrl ?? '/');
52
52
  server.use(graphqlBaseUrl, routerWithGraphQLRoutes);
53
53
  }
54
- (0, middlewares_1.notFoundMiddleware)({
55
- server,
56
- mockServerConfig
57
- });
54
+ (0, middlewares_1.notFoundMiddleware)(server, mockServerConfig);
55
+ (0, middlewares_1.errorMiddleware)(server);
58
56
  return server;
59
57
  };
60
58
  exports.createMockServer = createMockServer;
@@ -1,3 +1,3 @@
1
- <svg width="22" height="22" viewBox="0 0 22 22" fill="none" xmlns="http://www.w3.org/2000/svg">
2
- <path d="M11 8C12.65 8 14 9.35 14 11C14 12.65 12.65 14 11 14C9.35 14 8 12.65 8 11C8 9.35 9.35 8 11 8ZM11 6C8.24 6 6 8.24 6 11C6 13.76 8.24 16 11 16C13.76 16 16 13.76 16 11C16 8.24 13.76 6 11 6ZM1 12H3C3.55 12 4 11.55 4 11C4 10.45 3.55 10 3 10H1C0.45 10 0 10.45 0 11C0 11.55 0.45 12 1 12ZM19 12H21C21.55 12 22 11.55 22 11C22 10.45 21.55 10 21 10H19C18.45 10 18 10.45 18 11C18 11.55 18.45 12 19 12ZM10 1V3C10 3.55 10.45 4 11 4C11.55 4 12 3.55 12 3V1C12 0.45 11.55 0 11 0C10.45 0 10 0.45 10 1ZM10 19V21C10 21.55 10.45 22 11 22C11.55 22 12 21.55 12 21V19C12 18.45 11.55 18 11 18C10.45 18 10 18.45 10 19ZM4.99 3.58C4.6 3.19 3.96 3.19 3.58 3.58C3.19 3.97 3.19 4.61 3.58 4.99L4.64 6.05C5.03 6.44 5.67 6.44 6.05 6.05C6.43 5.66 6.44 5.02 6.05 4.64L4.99 3.58ZM17.36 15.95C16.97 15.56 16.33 15.56 15.95 15.95C15.56 16.34 15.56 16.98 15.95 17.36L17.01 18.42C17.4 18.81 18.04 18.81 18.42 18.42C18.81 18.03 18.81 17.39 18.42 17.01L17.36 15.95ZM18.42 4.99C18.81 4.6 18.81 3.96 18.42 3.58C18.03 3.19 17.39 3.19 17.01 3.58L15.95 4.64C15.56 5.03 15.56 5.67 15.95 6.05C16.34 6.43 16.98 6.44 17.36 6.05L18.42 4.99ZM6.05 17.36C6.44 16.97 6.44 16.33 6.05 15.95C5.66 15.56 5.02 15.56 4.64 15.95L3.58 17.01C3.19 17.4 3.19 18.04 3.58 18.42C3.97 18.8 4.61 18.81 4.99 18.42L6.05 17.36Z" fill="white"/>
3
- </svg>
1
+ <svg width="22" height="22" viewBox="0 0 22 22" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <path d="M11 8C12.65 8 14 9.35 14 11C14 12.65 12.65 14 11 14C9.35 14 8 12.65 8 11C8 9.35 9.35 8 11 8ZM11 6C8.24 6 6 8.24 6 11C6 13.76 8.24 16 11 16C13.76 16 16 13.76 16 11C16 8.24 13.76 6 11 6ZM1 12H3C3.55 12 4 11.55 4 11C4 10.45 3.55 10 3 10H1C0.45 10 0 10.45 0 11C0 11.55 0.45 12 1 12ZM19 12H21C21.55 12 22 11.55 22 11C22 10.45 21.55 10 21 10H19C18.45 10 18 10.45 18 11C18 11.55 18.45 12 19 12ZM10 1V3C10 3.55 10.45 4 11 4C11.55 4 12 3.55 12 3V1C12 0.45 11.55 0 11 0C10.45 0 10 0.45 10 1ZM10 19V21C10 21.55 10.45 22 11 22C11.55 22 12 21.55 12 21V19C12 18.45 11.55 18 11 18C10.45 18 10 18.45 10 19ZM4.99 3.58C4.6 3.19 3.96 3.19 3.58 3.58C3.19 3.97 3.19 4.61 3.58 4.99L4.64 6.05C5.03 6.44 5.67 6.44 6.05 6.05C6.43 5.66 6.44 5.02 6.05 4.64L4.99 3.58ZM17.36 15.95C16.97 15.56 16.33 15.56 15.95 15.95C15.56 16.34 15.56 16.98 15.95 17.36L17.01 18.42C17.4 18.81 18.04 18.81 18.42 18.42C18.81 18.03 18.81 17.39 18.42 17.01L17.36 15.95ZM18.42 4.99C18.81 4.6 18.81 3.96 18.42 3.58C18.03 3.19 17.39 3.19 17.01 3.58L15.95 4.64C15.56 5.03 15.56 5.67 15.95 6.05C16.34 6.43 16.98 6.44 17.36 6.05L18.42 4.99ZM6.05 17.36C6.44 16.97 6.44 16.33 6.05 15.95C5.66 15.56 5.02 15.56 4.64 15.95L3.58 17.01C3.19 17.4 3.19 18.04 3.58 18.42C3.97 18.8 4.61 18.81 4.99 18.42L6.05 17.36Z" fill="white"/>
3
+ </svg>
@@ -1,3 +1,3 @@
1
- <svg width="18" height="18" fill="none" xmlns="http://www.w3.org/2000/svg">
2
- <path fill="#343434" fill-opacity="0.9" d="M6.36997 2.51C6.18997 3.15 6.09997 3.82 6.09997 4.5C6.09997 8.58 9.41997 11.9 13.5 11.9C14.18 11.9 14.85 11.81 15.49 11.63C14.45 14.19 11.93 16 8.99997 16C5.13997 16 1.99997 12.86 1.99997 9C1.99997 6.07 3.80997 3.55 6.36997 2.51ZM8.99997 0C4.02997 0 -3.05176e-05 4.03 -3.05176e-05 9C-3.05176e-05 13.97 4.02997 18 8.99997 18C13.97 18 18 13.97 18 9C18 8.54 17.96 8.08 17.9 7.64C16.92 9.01 15.32 9.9 13.5 9.9C10.52 9.9 8.09997 7.48 8.09997 4.5C8.09997 2.69 8.98997 1.08 10.36 0.0999999C9.91997 0.0399999 9.45997 0 8.99997 0Z"/>
3
- </svg>
1
+ <svg width="18" height="18" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <path fill="#343434" fill-opacity="0.9" d="M6.36997 2.51C6.18997 3.15 6.09997 3.82 6.09997 4.5C6.09997 8.58 9.41997 11.9 13.5 11.9C14.18 11.9 14.85 11.81 15.49 11.63C14.45 14.19 11.93 16 8.99997 16C5.13997 16 1.99997 12.86 1.99997 9C1.99997 6.07 3.80997 3.55 6.36997 2.51ZM8.99997 0C4.02997 0 -3.05176e-05 4.03 -3.05176e-05 9C-3.05176e-05 13.97 4.02997 18 8.99997 18C13.97 18 18 13.97 18 9C18 8.54 17.96 8.08 17.9 7.64C16.92 9.01 15.32 9.9 13.5 9.9C10.52 9.9 8.09997 7.48 8.09997 4.5C8.09997 2.69 8.98997 1.08 10.36 0.0999999C9.91997 0.0399999 9.45997 0 8.99997 0Z"/>
3
+ </svg>