mock-config-server 3.2.0 → 3.3.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 (89) hide show
  1. package/README.md +153 -3
  2. package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/isDescriptorValueValid.d.ts +2 -1
  3. package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/isDescriptorValueValid.js +30 -7
  4. package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateGraphqlConfig.js +9 -3
  5. package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateRoutes/validateRoutes.js +67 -18
  6. package/dist/bin/validateMockServerConfig/validateQueue/validateQueue.d.ts +1 -0
  7. package/dist/bin/validateMockServerConfig/validateQueue/validateQueue.js +29 -0
  8. package/dist/bin/validateMockServerConfig/validateRestConfig/validateRoutes/validateRoutes.js +67 -18
  9. package/dist/bin/validateMockServerConfig/validateSettings/validateSettings.d.ts +1 -0
  10. package/dist/bin/validateMockServerConfig/validateSettings/validateSettings.js +34 -0
  11. package/dist/src/core/database/createDatabaseRoutes/createDatabaseRoutes.test.ts +112 -0
  12. package/dist/src/core/database/createDatabaseRoutes/helpers/array/createNewId/createNewId.test.ts +13 -0
  13. package/dist/src/core/database/createDatabaseRoutes/helpers/array/findIndexById/findIndexById.test.ts +17 -0
  14. package/dist/src/core/database/createDatabaseRoutes/helpers/array/isIndex/isIndex.test.ts +30 -0
  15. package/dist/src/core/database/createDatabaseRoutes/helpers/createNestedDatabaseRoutes/createNestedDatabaseRoutes.js +45 -2
  16. package/dist/src/core/database/createDatabaseRoutes/helpers/createNestedDatabaseRoutes/createNestedDatabaseRoutes.test.ts +399 -0
  17. package/dist/src/core/database/createDatabaseRoutes/helpers/createShallowDatabaseRoutes/createShallowDatabaseRoutes.test.ts +118 -0
  18. package/dist/src/core/database/createDatabaseRoutes/helpers/filter/filter.d.ts +2 -1
  19. package/dist/src/core/database/createDatabaseRoutes/helpers/operators/operators.d.ts +3 -0
  20. package/dist/src/core/database/createDatabaseRoutes/helpers/operators/operators.js +30 -0
  21. package/dist/src/core/database/createDatabaseRoutes/helpers/pagination/pagination.d.ts +13 -0
  22. package/dist/src/core/database/createDatabaseRoutes/helpers/pagination/pagination.js +36 -0
  23. package/dist/src/core/database/createDatabaseRoutes/helpers/search/search.d.ts +3 -0
  24. package/dist/src/core/database/createDatabaseRoutes/helpers/search/search.js +31 -0
  25. package/dist/src/core/database/createDatabaseRoutes/helpers/sort/sort.d.ts +2 -0
  26. package/dist/src/core/database/createDatabaseRoutes/helpers/sort/sort.js +42 -0
  27. package/dist/src/core/database/createDatabaseRoutes/helpers/splitDatabaseByNesting/splitDatabaseByNesting.test.ts +25 -0
  28. package/dist/src/core/database/createDatabaseRoutes/storages/File/FileStorage.test.ts +156 -0
  29. package/dist/src/core/database/createDatabaseRoutes/storages/File/FileWriter.test.ts +48 -0
  30. package/dist/src/core/database/createDatabaseRoutes/storages/Memory/MemoryStorage.test.ts +96 -0
  31. package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.d.ts +7 -1
  32. package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.js +65 -24
  33. package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.test.ts +851 -0
  34. package/dist/src/core/graphql/createGraphQLRoutes/helpers/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.test.ts +116 -0
  35. package/dist/src/core/middlewares/cookieParseMiddleware/cookieParseMiddleware.test.ts +22 -0
  36. package/dist/src/core/middlewares/cookieParseMiddleware/helpers/parseCookie/parseCookie.test.ts +45 -0
  37. package/dist/src/core/middlewares/corsMiddleware/corsMiddleware.test.ts +152 -0
  38. package/dist/src/core/middlewares/corsMiddleware/helpers/getAllowedOrigins/getAllowedOrigins.test.ts +15 -0
  39. package/dist/src/core/middlewares/errorMiddleware/errorMiddleware.test.ts +29 -0
  40. package/dist/src/core/middlewares/noCorsMiddleware/noCorsMiddleware.test.ts +49 -0
  41. package/dist/src/core/middlewares/notFoundMiddleware/helpers/getGraphqlUrlSuggestions/getGraphqlUrlSuggestions.test.ts +27 -0
  42. package/dist/src/core/middlewares/notFoundMiddleware/helpers/getLevenshteinDistance/getLevenshteinDistance.test.ts +12 -0
  43. package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/getRestUrlSuggestions.test.ts +54 -0
  44. package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getActualRestUrlMeaningfulString/getActualRestUrlMeaningfulString.test.ts +12 -0
  45. package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getPatternRestUrlMeaningfulString/getPatternRestUrlMeaningfulString.test.ts +10 -0
  46. package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.js +1 -3
  47. package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.test.ts +285 -0
  48. package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.d.ts +7 -1
  49. package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.js +6 -2
  50. package/dist/src/core/rest/createRestRoutes/createRestRoutes.d.ts +7 -1
  51. package/dist/src/core/rest/createRestRoutes/createRestRoutes.js +55 -12
  52. package/dist/src/core/rest/createRestRoutes/createRestRoutes.test.ts +648 -0
  53. package/dist/src/core/rest/createRestRoutes/helpers/prepareRestRequestConfigs/prepareRestRequestConfigs.test.ts +154 -0
  54. package/dist/src/server/createDatabaseMockServer/createDatabaseMockServer.js +4 -1
  55. package/dist/src/server/createGraphQLMockServer/createGraphQLMockServer.js +11 -4
  56. package/dist/src/server/createMockServer/createMockServer.js +28 -9
  57. package/dist/src/server/createRestMockServer/createRestMockServer.js +11 -4
  58. package/dist/src/server/index.d.ts +3 -3
  59. package/dist/src/server/index.js +23 -23
  60. package/dist/src/static/views/features/scheme/index.js +31 -31
  61. package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.test.ts +1452 -0
  62. package/dist/src/utils/helpers/entities/convertToEntityDescriptor/convertToEntityDescriptor.test.ts +27 -0
  63. package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.d.ts +2 -1
  64. package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.test.ts +15 -0
  65. package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.d.ts +7 -2
  66. package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.js +6 -4
  67. package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.test.ts +140 -0
  68. package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.d.ts +1 -1
  69. package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.js +1 -1
  70. package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.test.ts +32 -0
  71. package/dist/src/utils/helpers/interceptors/callRequestInterceptor/callRequestInterceptors.test.ts +53 -0
  72. package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.js +19 -10
  73. package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.test.ts +262 -0
  74. package/dist/src/utils/helpers/isPlainObject/isPlainObject.test.ts +20 -0
  75. package/dist/src/utils/helpers/isPrimitive/isPrimitive.test.ts +26 -0
  76. package/dist/src/utils/helpers/isRegExp/isRegExp.test.ts +20 -0
  77. package/dist/src/utils/helpers/url/convertWin32PathToUnix/convertWin32PathToUnix.test.ts +21 -0
  78. package/dist/src/utils/helpers/url/getUrlParts/getUrlParts.test.ts +8 -0
  79. package/dist/src/utils/helpers/url/removeLeadingAndTrailingSlashes/removeLeadingAndTrailingSlashes.test.ts +10 -0
  80. package/dist/src/utils/helpers/url/urlJoin/urlJoin.test.ts +9 -0
  81. package/dist/src/utils/types/graphql.d.ts +60 -51
  82. package/dist/src/utils/types/index.d.ts +1 -0
  83. package/dist/src/utils/types/index.js +11 -0
  84. package/dist/src/utils/types/interceptors.d.ts +1 -1
  85. package/dist/src/utils/types/rest.d.ts +47 -40
  86. package/dist/src/utils/types/utils.d.ts +8 -0
  87. package/dist/src/utils/types/utils.js +1 -0
  88. package/dist/src/utils/types/values.d.ts +0 -1
  89. package/package.json +2 -3
@@ -7,12 +7,16 @@ exports.createGraphQLRoutes = void 0;
7
7
  var _flat = require("flat");
8
8
  var _helpers = require("../../../utils/helpers");
9
9
  var _helpers2 = require("./helpers");
10
- const createGraphQLRoutes = (router, graphqlConfig, serverResponseInterceptors) => {
10
+ const createGraphQLRoutes = ({
11
+ router,
12
+ graphqlConfig,
13
+ serverResponseInterceptor
14
+ }) => {
11
15
  const preparedGraphQLRequestConfig = (0, _helpers2.prepareGraphQLRequestConfigs)(graphqlConfig.configs);
12
16
  const graphqlMiddleware = async (request, response, next) => {
13
- var _matchedRequestConfig, _matchedRouteConfig$e, _matchedRouteConfig$i, _matchedRequestConfig2, _graphqlConfig$interc;
17
+ var _matchedRequestConfig, _matchedRouteConfig$s, _matchedRouteConfig$e, _matchedRouteConfig$i, _matchedRequestConfig2, _graphqlConfig$interc;
14
18
  const graphQLInput = (0, _helpers.getGraphQLInput)(request);
15
- if (!graphQLInput || !graphQLInput.query) {
19
+ if (!graphQLInput.query) {
16
20
  return response.status(400).json({
17
21
  message: 'Query is missing, you must pass a valid GraphQL query'
18
22
  });
@@ -23,16 +27,16 @@ const createGraphQLRoutes = (router, graphqlConfig, serverResponseInterceptors)
23
27
  message: 'Query is invalid, you must use a valid GraphQL query'
24
28
  });
25
29
  }
26
- if (!query.operationName || !query.operationType) {
27
- return response.status(400).json({
28
- message: `You should to specify operationName and operationType for ${request.method}:${request.baseUrl}${request.path}`
29
- });
30
- }
31
30
  const matchedRequestConfig = preparedGraphQLRequestConfig.find(requestConfig => {
32
- if (requestConfig.operationName instanceof RegExp) {
33
- return new RegExp(requestConfig.operationName).test(query.operationName) && requestConfig.operationType === query.operationType;
31
+ var _graphQLInput$query;
32
+ if (requestConfig.operationType !== query.operationType) return false;
33
+ if ('query' in requestConfig && requestConfig.query.replace(/\s+/gi, ' ') !== ((_graphQLInput$query = graphQLInput.query) === null || _graphQLInput$query === void 0 ? void 0 : _graphQLInput$query.replace(/\s+/gi, ' '))) return false;
34
+ if ('operationName' in requestConfig) {
35
+ if (!query.operationName) return false;
36
+ if (requestConfig.operationName instanceof RegExp) return new RegExp(requestConfig.operationName).test(query.operationName);
37
+ return requestConfig.operationName === query.operationName;
34
38
  }
35
- return requestConfig.operationName === query.operationName && requestConfig.operationType === query.operationType;
39
+ return true;
36
40
  });
37
41
  if (!matchedRequestConfig) {
38
42
  return next();
@@ -49,25 +53,29 @@ const createGraphQLRoutes = (router, graphqlConfig, serverResponseInterceptors)
49
53
  }) => {
50
54
  if (!entities) return true;
51
55
  const entries = Object.entries(entities);
52
- return entries.every(([entityName, valueOrDescriptor]) => {
56
+ return entries.every(([entityName, entityDescriptorOrValue]) => {
53
57
  const {
54
58
  checkMode,
55
- value: descriptorValue
56
- } = (0, _helpers.convertToEntityDescriptor)(valueOrDescriptor);
59
+ value: entityDescriptorValue
60
+ } = (0, _helpers.convertToEntityDescriptor)(entityDescriptorOrValue);
57
61
 
58
62
  // ✅ important: check whole variables as plain value strictly if descriptor used for variables
59
- const isVariablesPlain = entityName === 'variables' && (0, _helpers.isEntityDescriptor)(valueOrDescriptor);
60
- if (isVariablesPlain) {
61
- // important: getGraphQLInput returns empty object if variables not sent or invalid, so count {} as undefined
62
- return (0, _helpers.resolveEntityValues)(checkMode, Object.keys(graphQLInput.variables).length ? graphQLInput.variables : undefined, descriptorValue);
63
+ const isEntityVariablesByTopLevelDescriptor = entityName === 'variables' && (0, _helpers.isEntityDescriptor)(entityDescriptorOrValue);
64
+ if (isEntityVariablesByTopLevelDescriptor) {
65
+ return (0, _helpers.resolveEntityValues)(checkMode, graphQLInput.variables, entityDescriptorValue);
63
66
  }
64
- const mappedEntityDescriptors = Object.entries(valueOrDescriptor);
65
- return mappedEntityDescriptors.every(([entityKey, mappedEntityDescriptor]) => {
67
+ const isEntityVariablesByTopLevelArray = entityName === 'variables' && Array.isArray(entityDescriptorOrValue);
68
+ if (isEntityVariablesByTopLevelArray) {
69
+ return entityDescriptorOrValue.some(entityDescriptorOrValueElement => (0, _helpers.resolveEntityValues)(checkMode, graphQLInput.variables, entityDescriptorOrValueElement));
70
+ }
71
+ const recordOrArrayEntries = Object.entries(entityDescriptorOrValue);
72
+ return recordOrArrayEntries.every(([entityKey, entityValue]) => {
66
73
  const {
67
74
  checkMode,
68
75
  value: descriptorValue
69
- } = (0, _helpers.convertToEntityDescriptor)(mappedEntityDescriptor);
76
+ } = (0, _helpers.convertToEntityDescriptor)(entityValue);
70
77
  const flattenEntity = (0, _flat.flatten)(entityName === 'variables' ? graphQLInput.variables : request[entityName]);
78
+
71
79
  // ✅ important: transform header keys to lower case because browsers send headers in lowercase
72
80
  return (0, _helpers.resolveEntityValues)(checkMode, flattenEntity[entityName === 'headers' ? entityKey.toLowerCase() : entityKey], descriptorValue);
73
81
  });
@@ -76,16 +84,49 @@ const createGraphQLRoutes = (router, graphqlConfig, serverResponseInterceptors)
76
84
  if (!matchedRouteConfig) {
77
85
  return next();
78
86
  }
79
- const matchedRouteConfigData = typeof matchedRouteConfig.data === 'function' ? await matchedRouteConfig.data(request, (_matchedRouteConfig$e = matchedRouteConfig.entities) !== null && _matchedRouteConfig$e !== void 0 ? _matchedRouteConfig$e : {}) : matchedRouteConfig.data;
87
+ let matchedRouteConfigData = null;
88
+ if ((_matchedRouteConfig$s = matchedRouteConfig.settings) !== null && _matchedRouteConfig$s !== void 0 && _matchedRouteConfig$s.polling && 'queue' in matchedRouteConfig) {
89
+ var _shallowMatchedRouteC;
90
+ if (!matchedRouteConfig.queue.length) return next();
91
+ const shallowMatchedRouteConfig = matchedRouteConfig;
92
+ let index = (_shallowMatchedRouteC = shallowMatchedRouteConfig.__pollingIndex) !== null && _shallowMatchedRouteC !== void 0 ? _shallowMatchedRouteC : 0;
93
+ const {
94
+ time,
95
+ data
96
+ } = matchedRouteConfig.queue[index];
97
+ const updateIndex = () => {
98
+ if (matchedRouteConfig.queue.length - 1 === index) {
99
+ index = 0;
100
+ } else {
101
+ index += 1;
102
+ }
103
+ shallowMatchedRouteConfig.__pollingIndex = index;
104
+ };
105
+ if (time && !shallowMatchedRouteConfig.__timeoutInProgress) {
106
+ shallowMatchedRouteConfig.__timeoutInProgress = true;
107
+ setTimeout(() => {
108
+ shallowMatchedRouteConfig.__timeoutInProgress = false;
109
+ updateIndex();
110
+ }, time);
111
+ }
112
+ if (!time && !shallowMatchedRouteConfig.__timeoutInProgress) {
113
+ updateIndex();
114
+ }
115
+ matchedRouteConfigData = data;
116
+ }
117
+ if ('data' in matchedRouteConfig) {
118
+ matchedRouteConfigData = matchedRouteConfig.data;
119
+ }
120
+ const resolvedData = typeof matchedRouteConfigData === 'function' ? await matchedRouteConfigData(request, (_matchedRouteConfig$e = matchedRouteConfig.entities) !== null && _matchedRouteConfig$e !== void 0 ? _matchedRouteConfig$e : {}) : matchedRouteConfigData;
80
121
  const data = await (0, _helpers.callResponseInterceptors)({
81
- data: matchedRouteConfigData,
122
+ data: resolvedData,
82
123
  request,
83
124
  response,
84
125
  interceptors: {
85
126
  routeInterceptor: (_matchedRouteConfig$i = matchedRouteConfig.interceptors) === null || _matchedRouteConfig$i === void 0 ? void 0 : _matchedRouteConfig$i.response,
86
127
  requestInterceptor: (_matchedRequestConfig2 = matchedRequestConfig.interceptors) === null || _matchedRequestConfig2 === void 0 ? void 0 : _matchedRequestConfig2.response,
87
128
  apiInterceptor: (_graphqlConfig$interc = graphqlConfig.interceptors) === null || _graphqlConfig$interc === void 0 ? void 0 : _graphqlConfig$interc.response,
88
- serverInterceptor: serverResponseInterceptors
129
+ serverInterceptor: serverResponseInterceptor
89
130
  }
90
131
  });
91
132