mock-config-server 3.4.1 → 3.6.0

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 (148) hide show
  1. package/README.md +2 -0
  2. package/dist/bin/bin.js +3 -6
  3. package/dist/bin/build.js +52 -50
  4. package/dist/bin/cli.js +50 -43
  5. package/dist/bin/helpers/createTemplate.js +29 -21
  6. package/dist/bin/helpers/getMostSpecificPathFromError.js +34 -30
  7. package/dist/bin/helpers/getValidationMessageFromPath.js +11 -8
  8. package/dist/bin/helpers/index.js +21 -79
  9. package/dist/bin/helpers/isOnlyRequestedDataResolvingPropertyExists.js +13 -6
  10. package/dist/bin/helpers/resolveConfigFile.js +22 -19
  11. package/dist/bin/helpers/resolveConfigFilePath.js +19 -12
  12. package/dist/bin/helpers/resolveExportsFromSourceCode.js +13 -10
  13. package/dist/bin/init.js +114 -97
  14. package/dist/bin/run.js +42 -43
  15. package/dist/bin/validateMockServerConfig/baseUrlSchema/baseUrlSchema.js +9 -5
  16. package/dist/bin/validateMockServerConfig/corsSchema/corsSchema.d.ts +2 -2
  17. package/dist/bin/validateMockServerConfig/corsSchema/corsSchema.js +32 -14
  18. package/dist/bin/validateMockServerConfig/databaseConfigSchema/databaseConfigSchema.js +19 -9
  19. package/dist/bin/validateMockServerConfig/graphqlConfigSchema/graphqlConfigSchema.d.ts +82 -72
  20. package/dist/bin/validateMockServerConfig/graphqlConfigSchema/graphqlConfigSchema.js +38 -22
  21. package/dist/bin/validateMockServerConfig/graphqlConfigSchema/routeConfigSchema/routeConfigSchema.d.ts +25 -21
  22. package/dist/bin/validateMockServerConfig/graphqlConfigSchema/routeConfigSchema/routeConfigSchema.js +36 -28
  23. package/dist/bin/validateMockServerConfig/interceptorsSchema/interceptorsSchema.js +12 -8
  24. package/dist/bin/validateMockServerConfig/portSchema/portSchema.js +9 -5
  25. package/dist/bin/validateMockServerConfig/queueSchema/queueSchema.d.ts +2 -2
  26. package/dist/bin/validateMockServerConfig/queueSchema/queueSchema.js +18 -9
  27. package/dist/bin/validateMockServerConfig/restConfigSchema/restConfigSchema.d.ts +270 -240
  28. package/dist/bin/validateMockServerConfig/restConfigSchema/restConfigSchema.js +33 -19
  29. package/dist/bin/validateMockServerConfig/restConfigSchema/routeConfigSchema/routeConfigSchema.d.ts +33 -28
  30. package/dist/bin/validateMockServerConfig/restConfigSchema/routeConfigSchema/routeConfigSchema.js +60 -48
  31. package/dist/bin/validateMockServerConfig/settingsSchema/settingsSchema.js +13 -9
  32. package/dist/bin/validateMockServerConfig/staticPathSchema/staticPathSchema.js +19 -9
  33. package/dist/bin/validateMockServerConfig/utils/checkModeSchema/checkModeSchema.js +27 -9
  34. package/dist/bin/validateMockServerConfig/utils/entitiesSchema/entitiesSchema.d.ts +4 -8
  35. package/dist/bin/validateMockServerConfig/utils/entitiesSchema/entitiesSchema.js +117 -60
  36. package/dist/bin/validateMockServerConfig/utils/index.js +20 -68
  37. package/dist/bin/validateMockServerConfig/utils/jsonSchema/jsonSchema.js +30 -10
  38. package/dist/bin/validateMockServerConfig/utils/plainObjectSchema/plainObjectSchema.js +9 -8
  39. package/dist/bin/validateMockServerConfig/utils/requiredPropertiesSchema/requiredPropertiesSchema.d.ts +1 -1
  40. package/dist/bin/validateMockServerConfig/utils/requiredPropertiesSchema/requiredPropertiesSchema.js +10 -7
  41. package/dist/bin/validateMockServerConfig/utils/sharedSchema/sharedSchema.js +18 -6
  42. package/dist/bin/validateMockServerConfig/validateApiMockServerConfig.js +43 -40
  43. package/dist/bin/validateMockServerConfig/validateMockServerConfig.js +38 -35
  44. package/dist/index.js +15 -13
  45. package/dist/src/core/database/createDatabaseRoutes/createDatabaseRoutes.js +25 -28
  46. package/dist/src/core/database/createDatabaseRoutes/helpers/array/createNewId/createNewId.js +14 -11
  47. package/dist/src/core/database/createDatabaseRoutes/helpers/array/findIndexById/findIndexById.js +8 -5
  48. package/dist/src/core/database/createDatabaseRoutes/helpers/array/index.js +17 -35
  49. package/dist/src/core/database/createDatabaseRoutes/helpers/array/isIndex/isIndex.js +8 -5
  50. package/dist/src/core/database/createDatabaseRoutes/helpers/createNestedDatabaseRoutes/createNestedDatabaseRoutes.js +159 -143
  51. package/dist/src/core/database/createDatabaseRoutes/helpers/createRewrittenDatabaseRoutes/createRewrittenDatabaseRoutes.js +15 -8
  52. package/dist/src/core/database/createDatabaseRoutes/helpers/createShallowDatabaseRoutes/createShallowDatabaseRoutes.js +90 -94
  53. package/dist/src/core/database/createDatabaseRoutes/helpers/filter/filter.js +49 -46
  54. package/dist/src/core/database/createDatabaseRoutes/helpers/index.js +19 -57
  55. package/dist/src/core/database/createDatabaseRoutes/helpers/pagination/pagination.js +32 -31
  56. package/dist/src/core/database/createDatabaseRoutes/helpers/search/search.js +34 -24
  57. package/dist/src/core/database/createDatabaseRoutes/helpers/sort/sort.js +37 -33
  58. package/dist/src/core/database/createDatabaseRoutes/helpers/splitDatabaseByNesting/splitDatabaseByNesting.js +26 -25
  59. package/dist/src/core/database/createDatabaseRoutes/storages/File/FileStorage.js +65 -50
  60. package/dist/src/core/database/createDatabaseRoutes/storages/File/FileWriter.js +55 -48
  61. package/dist/src/core/database/createDatabaseRoutes/storages/Memory/MemoryStorage.js +51 -41
  62. package/dist/src/core/database/createDatabaseRoutes/storages/index.js +16 -24
  63. package/dist/src/core/database/index.js +15 -13
  64. package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.js +131 -140
  65. package/dist/src/core/graphql/createGraphQLRoutes/helpers/index.js +15 -13
  66. package/dist/src/core/graphql/createGraphQLRoutes/helpers/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.js +36 -41
  67. package/dist/src/core/graphql/index.js +15 -13
  68. package/dist/src/core/middlewares/cookieParseMiddleware/cookieParseMiddleware.js +16 -13
  69. package/dist/src/core/middlewares/cookieParseMiddleware/helpers/index.js +15 -13
  70. package/dist/src/core/middlewares/cookieParseMiddleware/helpers/parseCookie/parseCookie.js +18 -15
  71. package/dist/src/core/middlewares/corsMiddleware/corsMiddleware.js +50 -47
  72. package/dist/src/core/middlewares/corsMiddleware/helpers/getAllowedOrigins/getAllowedOrigins.js +17 -12
  73. package/dist/src/core/middlewares/corsMiddleware/helpers/index.js +15 -13
  74. package/dist/src/core/middlewares/destroyerMiddleware/destroyerMiddleware.js +23 -20
  75. package/dist/src/core/middlewares/errorMiddleware/errorMiddleware.js +23 -17
  76. package/dist/src/core/middlewares/index.js +23 -91
  77. package/dist/src/core/middlewares/noCorsMiddleware/noCorsMiddleware.js +22 -19
  78. package/dist/src/core/middlewares/notFoundMiddleware/helpers/getGraphqlUrlSuggestions/getGraphqlUrlSuggestions.js +21 -23
  79. package/dist/src/core/middlewares/notFoundMiddleware/helpers/getLevenshteinDistance/getLevenshteinDistance.js +80 -80
  80. package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/getRestUrlSuggestions.js +34 -34
  81. package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getActualRestUrlMeaningfulString/getActualRestUrlMeaningfulString.js +8 -5
  82. package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getPatternRestUrlMeaningfulString/getPatternRestUrlMeaningfulString.js +8 -5
  83. package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/index.js +16 -24
  84. package/dist/src/core/middlewares/notFoundMiddleware/helpers/index.js +17 -35
  85. package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.js +58 -60
  86. package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.js +16 -17
  87. package/dist/src/core/middlewares/staticMiddleware/staticMiddleware.js +32 -25
  88. package/dist/src/core/rest/createRestRoutes/createRestRoutes.js +125 -130
  89. package/dist/src/core/rest/createRestRoutes/helpers/index.js +15 -13
  90. package/dist/src/core/rest/createRestRoutes/helpers/prepareRestRequestConfigs/prepareRestRequestConfigs.js +37 -43
  91. package/dist/src/core/rest/index.js +15 -13
  92. package/dist/src/index.js +16 -24
  93. package/dist/src/server/createDatabaseMockServer/createDatabaseMockServer.js +56 -53
  94. package/dist/src/server/createGraphQLMockServer/createGraphQLMockServer.js +64 -62
  95. package/dist/src/server/createMockServer/createMockServer.js +94 -91
  96. package/dist/src/server/createRestMockServer/createRestMockServer.js +64 -62
  97. package/dist/src/server/index.js +23 -91
  98. package/dist/src/server/startDatabaseMockServer/startDatabaseMockServer.js +25 -19
  99. package/dist/src/server/startGraphQLMockServer/startGraphQLMockServer.js +25 -19
  100. package/dist/src/server/startMockServer/startMockServer.js +25 -19
  101. package/dist/src/server/startRestMockServer/startRestMockServer.js +25 -19
  102. package/dist/src/utils/constants/appPath.js +8 -4
  103. package/dist/src/utils/constants/checkModes.js +74 -11
  104. package/dist/src/utils/constants/default.js +18 -14
  105. package/dist/src/utils/constants/index.js +17 -35
  106. package/dist/src/utils/helpers/asyncHandler.js +8 -5
  107. package/dist/src/utils/helpers/config/index.js +15 -13
  108. package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.js +103 -110
  109. package/dist/src/utils/helpers/entities/convertToEntityDescriptor/convertToEntityDescriptor.js +12 -9
  110. package/dist/src/utils/helpers/entities/index.js +16 -24
  111. package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.js +9 -6
  112. package/dist/src/utils/helpers/files/index.js +15 -13
  113. package/dist/src/utils/helpers/files/isFilePathValid/isFilePathValid.js +22 -15
  114. package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.js +25 -29
  115. package/dist/src/utils/helpers/graphql/index.js +17 -35
  116. package/dist/src/utils/helpers/graphql/parseGraphQLRequest/parseGraphQLRequest.js +13 -10
  117. package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.js +23 -19
  118. package/dist/src/utils/helpers/index.js +26 -124
  119. package/dist/src/utils/helpers/interceptors/callRequestInterceptor/callRequestInterceptor.js +21 -21
  120. package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.js +62 -64
  121. package/dist/src/utils/helpers/interceptors/helpers/setDelay.js +10 -7
  122. package/dist/src/utils/helpers/interceptors/index.js +16 -24
  123. package/dist/src/utils/helpers/isPlainObject/isPlainObject.js +8 -5
  124. package/dist/src/utils/helpers/isPrimitive/isPrimitive.js +8 -5
  125. package/dist/src/utils/helpers/isRegExp/isRegExp.js +8 -5
  126. package/dist/src/utils/helpers/sleep.js +9 -6
  127. package/dist/src/utils/helpers/tests/createTmpDir.js +16 -9
  128. package/dist/src/utils/helpers/tests/index.js +15 -13
  129. package/dist/src/utils/helpers/url/convertWin32PathToUnix/convertWin32PathToUnix.js +8 -5
  130. package/dist/src/utils/helpers/url/getUrlParts/getUrlParts.js +9 -6
  131. package/dist/src/utils/helpers/url/index.js +18 -46
  132. package/dist/src/utils/helpers/url/removeLeadingAndTrailingSlashes/removeLeadingAndTrailingSlashes.js +8 -5
  133. package/dist/src/utils/helpers/url/urlJoin/urlJoin.js +18 -11
  134. package/dist/src/utils/types/checkModes.js +4 -1
  135. package/dist/src/utils/types/database.js +4 -1
  136. package/dist/src/utils/types/entities.js +4 -1
  137. package/dist/src/utils/types/graphql.d.ts +1 -1
  138. package/dist/src/utils/types/graphql.js +4 -1
  139. package/dist/src/utils/types/index.js +24 -102
  140. package/dist/src/utils/types/interceptors.js +4 -1
  141. package/dist/src/utils/types/rest.d.ts +1 -1
  142. package/dist/src/utils/types/rest.js +4 -1
  143. package/dist/src/utils/types/server.js +4 -1
  144. package/dist/src/utils/types/utils.js +4 -1
  145. package/dist/src/utils/types/values.js +4 -1
  146. package/package.json +26 -29
  147. package/dist/bin/templates/.eslintrc.js +0 -14
  148. package/dist/bin/templates/tsconfig.json +0 -11
@@ -1,50 +1,60 @@
1
1
  "use strict";
2
-
3
2
  Object.defineProperty(exports, "__esModule", {
4
- value: true
3
+ value: true
5
4
  });
6
- exports.MemoryStorage = void 0;
7
- var _helpers = require("../../helpers");
5
+ Object.defineProperty(exports, "MemoryStorage", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return MemoryStorage;
9
+ }
10
+ });
11
+ const _helpers = require("../../helpers");
8
12
  class MemoryStorage {
9
- constructor(initialData) {
10
- this.data = initialData;
11
- }
12
- read(key) {
13
- if (!key) return this.data;
14
- const keys = Array.isArray(key) ? key : [key];
15
- let readable = this.data;
16
- for (const currentKey of keys) {
17
- readable = readable[currentKey];
13
+ data;
14
+ constructor(initialData){
15
+ this.data = initialData;
18
16
  }
19
- return readable;
20
- }
21
- write(key, value) {
22
- const keys = Array.isArray(key) ? key : [key];
23
- let writable = this.data;
24
- let index = 0;
25
- // important:
26
- // stop iterate one element before end of keys for get access to writable object property
27
- while (index < keys.length - 1) {
28
- writable = writable[keys[index]];
29
- index += 1;
17
+ read(key) {
18
+ if (!key) return this.data;
19
+ const keys = Array.isArray(key) ? key : [
20
+ key
21
+ ];
22
+ let readable = this.data;
23
+ for (const currentKey of keys){
24
+ readable = readable[currentKey];
25
+ }
26
+ return readable;
30
27
  }
31
- writable[keys[index]] = value;
32
- }
33
- delete(key) {
34
- const keys = Array.isArray(key) ? key : [key];
35
- let deletable = this.data;
36
- let index = 0;
37
- // ✅ important:
38
- // stop iterate one element before end of key for get access to deletable object property
39
- while (index < keys.length - 1) {
40
- deletable = deletable[keys[index]];
41
- index += 1;
28
+ write(key, value) {
29
+ const keys = Array.isArray(key) ? key : [
30
+ key
31
+ ];
32
+ let writable = this.data;
33
+ let index = 0;
34
+ // ✅ important:
35
+ // stop iterate one element before end of keys for get access to writable object property
36
+ while(index < keys.length - 1){
37
+ writable = writable[keys[index]];
38
+ index += 1;
39
+ }
40
+ writable[keys[index]] = value;
42
41
  }
43
- if (Array.isArray(deletable) && (0, _helpers.isIndex)(keys[index])) {
44
- deletable.splice(keys[index], 1);
45
- return;
42
+ delete(key) {
43
+ const keys = Array.isArray(key) ? key : [
44
+ key
45
+ ];
46
+ let deletable = this.data;
47
+ let index = 0;
48
+ // ✅ important:
49
+ // stop iterate one element before end of key for get access to deletable object property
50
+ while(index < keys.length - 1){
51
+ deletable = deletable[keys[index]];
52
+ index += 1;
53
+ }
54
+ if (Array.isArray(deletable) && (0, _helpers.isIndex)(keys[index])) {
55
+ deletable.splice(keys[index], 1);
56
+ return;
57
+ }
58
+ delete deletable[keys[index]];
46
59
  }
47
- delete deletable[keys[index]];
48
- }
49
60
  }
50
- exports.MemoryStorage = MemoryStorage;
@@ -1,27 +1,19 @@
1
1
  "use strict";
2
-
3
2
  Object.defineProperty(exports, "__esModule", {
4
- value: true
3
+ value: true
5
4
  });
6
- var _FileStorage = require("./File/FileStorage");
7
- Object.keys(_FileStorage).forEach(function (key) {
8
- if (key === "default" || key === "__esModule") return;
9
- if (key in exports && exports[key] === _FileStorage[key]) return;
10
- Object.defineProperty(exports, key, {
11
- enumerable: true,
12
- get: function () {
13
- return _FileStorage[key];
14
- }
15
- });
16
- });
17
- var _MemoryStorage = require("./Memory/MemoryStorage");
18
- Object.keys(_MemoryStorage).forEach(function (key) {
19
- if (key === "default" || key === "__esModule") return;
20
- if (key in exports && exports[key] === _MemoryStorage[key]) return;
21
- Object.defineProperty(exports, key, {
22
- enumerable: true,
23
- get: function () {
24
- return _MemoryStorage[key];
25
- }
26
- });
27
- });
5
+ _export_star(require("./File/FileStorage"), exports);
6
+ _export_star(require("./Memory/MemoryStorage"), exports);
7
+ function _export_star(from, to) {
8
+ Object.keys(from).forEach(function(k) {
9
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
10
+ Object.defineProperty(to, k, {
11
+ enumerable: true,
12
+ get: function() {
13
+ return from[k];
14
+ }
15
+ });
16
+ }
17
+ });
18
+ return from;
19
+ }
@@ -1,16 +1,18 @@
1
1
  "use strict";
2
-
3
2
  Object.defineProperty(exports, "__esModule", {
4
- value: true
3
+ value: true
5
4
  });
6
- var _createDatabaseRoutes = require("./createDatabaseRoutes/createDatabaseRoutes");
7
- Object.keys(_createDatabaseRoutes).forEach(function (key) {
8
- if (key === "default" || key === "__esModule") return;
9
- if (key in exports && exports[key] === _createDatabaseRoutes[key]) return;
10
- Object.defineProperty(exports, key, {
11
- enumerable: true,
12
- get: function () {
13
- return _createDatabaseRoutes[key];
14
- }
15
- });
16
- });
5
+ _export_star(require("./createDatabaseRoutes/createDatabaseRoutes"), exports);
6
+ function _export_star(from, to) {
7
+ Object.keys(from).forEach(function(k) {
8
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
9
+ Object.defineProperty(to, k, {
10
+ enumerable: true,
11
+ get: function() {
12
+ return from[k];
13
+ }
14
+ });
15
+ }
16
+ });
17
+ return from;
18
+ }
@@ -1,149 +1,140 @@
1
1
  "use strict";
2
-
3
2
  Object.defineProperty(exports, "__esModule", {
4
- value: true
3
+ value: true
5
4
  });
6
- exports.createGraphQLRoutes = void 0;
7
- var _flat = require("flat");
8
- var _helpers = require("../../../utils/helpers");
9
- var _helpers2 = require("./helpers");
10
- const createGraphQLRoutes = ({
11
- router,
12
- graphqlConfig,
13
- serverResponseInterceptor
14
- }) => {
15
- const preparedGraphQLRequestConfig = (0, _helpers2.prepareGraphQLRequestConfigs)(graphqlConfig.configs);
16
- const graphqlMiddleware = async (request, response, next) => {
17
- var _matchedRequestConfig, _matchedRouteConfig$s, _matchedRouteConfig$e, _matchedRouteConfig$s2, _matchedRouteConfig$i, _matchedRequestConfig2, _graphqlConfig$interc, _matchedRouteConfig$s3;
18
- const graphQLInput = (0, _helpers.getGraphQLInput)(request);
19
- if (!graphQLInput.query) {
20
- return response.status(400).json({
21
- message: 'Query is missing, you must pass a valid GraphQL query'
22
- });
5
+ Object.defineProperty(exports, "createGraphQLRoutes", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return createGraphQLRoutes;
23
9
  }
24
- const query = (0, _helpers.parseQuery)(graphQLInput.query);
25
- if (!query) {
26
- return response.status(400).json({
27
- message: 'Query is invalid, you must use a valid GraphQL query'
28
- });
29
- }
30
- const matchedRequestConfig = preparedGraphQLRequestConfig.find(requestConfig => {
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;
38
- }
39
- return true;
40
- });
41
- if (!matchedRequestConfig) {
42
- return next();
43
- }
44
- const requestInterceptor = (_matchedRequestConfig = matchedRequestConfig.interceptors) === null || _matchedRequestConfig === void 0 ? void 0 : _matchedRequestConfig.request;
45
- if (requestInterceptor) {
46
- await (0, _helpers.callRequestInterceptor)({
47
- request,
48
- interceptor: requestInterceptor
49
- });
50
- }
51
- const matchedRouteConfig = matchedRequestConfig.routes.find(({
52
- entities
53
- }) => {
54
- if (!entities) return true;
55
- const entries = Object.entries(entities);
56
- return entries.every(([entityName, entityDescriptorOrValue]) => {
57
- const {
58
- checkMode,
59
- value: entityDescriptorValue
60
- } = (0, _helpers.convertToEntityDescriptor)(entityDescriptorOrValue);
61
-
62
- // ✅ important: check whole variables as plain value strictly if descriptor used for variables
63
- const isEntityVariablesByTopLevelDescriptor = entityName === 'variables' && (0, _helpers.isEntityDescriptor)(entityDescriptorOrValue);
64
- if (isEntityVariablesByTopLevelDescriptor) {
65
- return (0, _helpers.resolveEntityValues)(checkMode, graphQLInput.variables, entityDescriptorValue);
10
+ });
11
+ const _flat = require("flat");
12
+ const _helpers = require("../../../utils/helpers");
13
+ const _helpers1 = require("./helpers");
14
+ const createGraphQLRoutes = ({ router, graphqlConfig, serverResponseInterceptor })=>{
15
+ const preparedGraphQLRequestConfig = (0, _helpers1.prepareGraphQLRequestConfigs)(graphqlConfig.configs);
16
+ const graphqlMiddleware = async (request, response, next)=>{
17
+ var _matchedRequestConfig_interceptors, _matchedRouteConfig_interceptors, _matchedRouteConfig_settings, _matchedRouteConfig_settings1, _matchedRouteConfig_interceptors1, _matchedRequestConfig_interceptors1, _graphqlConfig_interceptors, _matchedRouteConfig_settings2;
18
+ const graphQLInput = (0, _helpers.getGraphQLInput)(request);
19
+ if (!graphQLInput.query) {
20
+ return response.status(400).json({
21
+ message: 'Query is missing, you must pass a valid GraphQL query'
22
+ });
66
23
  }
67
- const isEntityVariablesByTopLevelArray = entityName === 'variables' && Array.isArray(entityDescriptorOrValue);
68
- if (isEntityVariablesByTopLevelArray) {
69
- return entityDescriptorOrValue.some(entityDescriptorOrValueElement => (0, _helpers.resolveEntityValues)(checkMode, graphQLInput.variables, entityDescriptorOrValueElement));
24
+ const query = (0, _helpers.parseQuery)(graphQLInput.query);
25
+ if (!query) {
26
+ return response.status(400).json({
27
+ message: 'Query is invalid, you must use a valid GraphQL query'
28
+ });
70
29
  }
71
- const recordOrArrayEntries = Object.entries(entityDescriptorOrValue);
72
- return recordOrArrayEntries.every(([entityKey, entityValue]) => {
73
- const {
74
- checkMode,
75
- value: descriptorValue
76
- } = (0, _helpers.convertToEntityDescriptor)(entityValue);
77
- const flattenEntity = (0, _flat.flatten)(entityName === 'variables' ? graphQLInput.variables : request[entityName]);
78
-
79
- // ✅ important: transform header keys to lower case because browsers send headers in lowercase
80
- return (0, _helpers.resolveEntityValues)(checkMode, flattenEntity[entityName === 'headers' ? entityKey.toLowerCase() : entityKey], descriptorValue);
30
+ const matchedRequestConfig = preparedGraphQLRequestConfig.find((requestConfig)=>{
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;
38
+ }
39
+ return true;
81
40
  });
82
- });
83
- });
84
- if (!matchedRouteConfig) {
85
- return next();
86
- }
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;
41
+ if (!matchedRequestConfig) {
42
+ return next();
102
43
  }
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;
121
- if ((_matchedRouteConfig$s2 = matchedRouteConfig.settings) !== null && _matchedRouteConfig$s2 !== void 0 && _matchedRouteConfig$s2.status) {
122
- response.statusCode = matchedRouteConfig.settings.status;
123
- }
124
-
125
- // important:
126
- // set 'Cache-Control' header for explicit browsers response revalidate: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
127
- // this code should place before response interceptors for giving opportunity to rewrite 'Cache-Control' header
128
- if (matchedRequestConfig.operationType === 'query') response.set('Cache-control', 'no-cache');
129
- const data = await (0, _helpers.callResponseInterceptors)({
130
- data: resolvedData,
131
- request,
132
- response,
133
- interceptors: {
134
- routeInterceptor: (_matchedRouteConfig$i = matchedRouteConfig.interceptors) === null || _matchedRouteConfig$i === void 0 ? void 0 : _matchedRouteConfig$i.response,
135
- requestInterceptor: (_matchedRequestConfig2 = matchedRequestConfig.interceptors) === null || _matchedRequestConfig2 === void 0 ? void 0 : _matchedRequestConfig2.response,
136
- apiInterceptor: (_graphqlConfig$interc = graphqlConfig.interceptors) === null || _graphqlConfig$interc === void 0 ? void 0 : _graphqlConfig$interc.response,
137
- serverInterceptor: serverResponseInterceptor
138
- }
139
- });
140
- if ((_matchedRouteConfig$s3 = matchedRouteConfig.settings) !== null && _matchedRouteConfig$s3 !== void 0 && _matchedRouteConfig$s3.delay) {
141
- await (0, _helpers.sleep)(matchedRouteConfig.settings.delay);
142
- }
143
- return response.json(data);
144
- };
145
- router.route('/').get((0, _helpers.asyncHandler)(graphqlMiddleware));
146
- router.route('/').post((0, _helpers.asyncHandler)(graphqlMiddleware));
147
- return router;
44
+ if ((_matchedRequestConfig_interceptors = matchedRequestConfig.interceptors) === null || _matchedRequestConfig_interceptors === void 0 ? void 0 : _matchedRequestConfig_interceptors.request) {
45
+ await (0, _helpers.callRequestInterceptor)({
46
+ request,
47
+ interceptor: matchedRequestConfig.interceptors.request
48
+ });
49
+ }
50
+ const matchedRouteConfig = matchedRequestConfig.routes.find(({ entities })=>{
51
+ if (!entities) return true;
52
+ const entries = Object.entries(entities);
53
+ return entries.every(([entityName, entityDescriptorOrValue])=>{
54
+ const { checkMode, value: entityDescriptorValue } = (0, _helpers.convertToEntityDescriptor)(entityDescriptorOrValue);
55
+ // ✅ important: check whole variables as plain value strictly if descriptor used for variables
56
+ const isEntityVariablesByTopLevelDescriptor = entityName === 'variables' && (0, _helpers.isEntityDescriptor)(entityDescriptorOrValue);
57
+ if (isEntityVariablesByTopLevelDescriptor) {
58
+ return (0, _helpers.resolveEntityValues)(checkMode, graphQLInput.variables, entityDescriptorValue);
59
+ }
60
+ const isEntityVariablesByTopLevelArray = entityName === 'variables' && Array.isArray(entityDescriptorOrValue);
61
+ if (isEntityVariablesByTopLevelArray) {
62
+ return entityDescriptorOrValue.some((entityDescriptorOrValueElement)=>(0, _helpers.resolveEntityValues)(checkMode, graphQLInput.variables, entityDescriptorOrValueElement));
63
+ }
64
+ const recordOrArrayEntries = Object.entries(entityDescriptorOrValue);
65
+ return recordOrArrayEntries.every(([entityKey, entityValue])=>{
66
+ const { checkMode, value: descriptorValue } = (0, _helpers.convertToEntityDescriptor)(entityValue);
67
+ const flattenEntity = (0, _flat.flatten)(entityName === 'variables' ? graphQLInput.variables : request[entityName]);
68
+ // important: transform header keys to lower case because browsers send headers in lowercase
69
+ return (0, _helpers.resolveEntityValues)(checkMode, flattenEntity[entityName === 'headers' ? entityKey.toLowerCase() : entityKey], descriptorValue);
70
+ });
71
+ });
72
+ });
73
+ if (!matchedRouteConfig) {
74
+ return next();
75
+ }
76
+ if ((_matchedRouteConfig_interceptors = matchedRouteConfig.interceptors) === null || _matchedRouteConfig_interceptors === void 0 ? void 0 : _matchedRouteConfig_interceptors.request) {
77
+ await (0, _helpers.callRequestInterceptor)({
78
+ request,
79
+ interceptor: matchedRouteConfig.interceptors.request
80
+ });
81
+ }
82
+ let matchedRouteConfigData = null;
83
+ if (((_matchedRouteConfig_settings = matchedRouteConfig.settings) === null || _matchedRouteConfig_settings === void 0 ? void 0 : _matchedRouteConfig_settings.polling) && 'queue' in matchedRouteConfig) {
84
+ if (!matchedRouteConfig.queue.length) return next();
85
+ const shallowMatchedRouteConfig = matchedRouteConfig;
86
+ var _shallowMatchedRouteConfig___pollingIndex;
87
+ let index = (_shallowMatchedRouteConfig___pollingIndex = shallowMatchedRouteConfig.__pollingIndex) !== null && _shallowMatchedRouteConfig___pollingIndex !== void 0 ? _shallowMatchedRouteConfig___pollingIndex : 0;
88
+ const { time, data } = matchedRouteConfig.queue[index];
89
+ const updateIndex = ()=>{
90
+ if (matchedRouteConfig.queue.length - 1 === index) {
91
+ index = 0;
92
+ } else {
93
+ index += 1;
94
+ }
95
+ shallowMatchedRouteConfig.__pollingIndex = index;
96
+ };
97
+ if (time && !shallowMatchedRouteConfig.__timeoutInProgress) {
98
+ shallowMatchedRouteConfig.__timeoutInProgress = true;
99
+ setTimeout(()=>{
100
+ shallowMatchedRouteConfig.__timeoutInProgress = false;
101
+ updateIndex();
102
+ }, time);
103
+ }
104
+ if (!time && !shallowMatchedRouteConfig.__timeoutInProgress) {
105
+ updateIndex();
106
+ }
107
+ matchedRouteConfigData = data;
108
+ }
109
+ if ('data' in matchedRouteConfig) {
110
+ matchedRouteConfigData = matchedRouteConfig.data;
111
+ }
112
+ var _matchedRouteConfig_entities;
113
+ const resolvedData = typeof matchedRouteConfigData === 'function' ? await matchedRouteConfigData(request, (_matchedRouteConfig_entities = matchedRouteConfig.entities) !== null && _matchedRouteConfig_entities !== void 0 ? _matchedRouteConfig_entities : {}) : matchedRouteConfigData;
114
+ if ((_matchedRouteConfig_settings1 = matchedRouteConfig.settings) === null || _matchedRouteConfig_settings1 === void 0 ? void 0 : _matchedRouteConfig_settings1.status) {
115
+ response.statusCode = matchedRouteConfig.settings.status;
116
+ }
117
+ // ✅ important:
118
+ // set 'Cache-Control' header for explicit browsers response revalidate: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
119
+ // this code should place before response interceptors for giving opportunity to rewrite 'Cache-Control' header
120
+ if (matchedRequestConfig.operationType === 'query') response.set('Cache-control', 'no-cache');
121
+ const data = await (0, _helpers.callResponseInterceptors)({
122
+ data: resolvedData,
123
+ request,
124
+ response,
125
+ interceptors: {
126
+ routeInterceptor: (_matchedRouteConfig_interceptors1 = matchedRouteConfig.interceptors) === null || _matchedRouteConfig_interceptors1 === void 0 ? void 0 : _matchedRouteConfig_interceptors1.response,
127
+ requestInterceptor: (_matchedRequestConfig_interceptors1 = matchedRequestConfig.interceptors) === null || _matchedRequestConfig_interceptors1 === void 0 ? void 0 : _matchedRequestConfig_interceptors1.response,
128
+ apiInterceptor: (_graphqlConfig_interceptors = graphqlConfig.interceptors) === null || _graphqlConfig_interceptors === void 0 ? void 0 : _graphqlConfig_interceptors.response,
129
+ serverInterceptor: serverResponseInterceptor
130
+ }
131
+ });
132
+ if ((_matchedRouteConfig_settings2 = matchedRouteConfig.settings) === null || _matchedRouteConfig_settings2 === void 0 ? void 0 : _matchedRouteConfig_settings2.delay) {
133
+ await (0, _helpers.sleep)(matchedRouteConfig.settings.delay);
134
+ }
135
+ return response.json(data);
136
+ };
137
+ router.route('/').get((0, _helpers.asyncHandler)(graphqlMiddleware));
138
+ router.route('/').post((0, _helpers.asyncHandler)(graphqlMiddleware));
139
+ return router;
148
140
  };
149
- exports.createGraphQLRoutes = createGraphQLRoutes;
@@ -1,16 +1,18 @@
1
1
  "use strict";
2
-
3
2
  Object.defineProperty(exports, "__esModule", {
4
- value: true
3
+ value: true
5
4
  });
6
- var _prepareGraphQLRequestConfigs = require("./prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs");
7
- Object.keys(_prepareGraphQLRequestConfigs).forEach(function (key) {
8
- if (key === "default" || key === "__esModule") return;
9
- if (key in exports && exports[key] === _prepareGraphQLRequestConfigs[key]) return;
10
- Object.defineProperty(exports, key, {
11
- enumerable: true,
12
- get: function () {
13
- return _prepareGraphQLRequestConfigs[key];
14
- }
15
- });
16
- });
5
+ _export_star(require("./prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs"), exports);
6
+ function _export_star(from, to) {
7
+ Object.keys(from).forEach(function(k) {
8
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
9
+ Object.defineProperty(to, k, {
10
+ enumerable: true,
11
+ get: function() {
12
+ return from[k];
13
+ }
14
+ });
15
+ }
16
+ });
17
+ return from;
18
+ }
@@ -1,47 +1,42 @@
1
1
  "use strict";
2
-
3
2
  Object.defineProperty(exports, "__esModule", {
4
- value: true
3
+ value: true
5
4
  });
6
- exports.prepareGraphQLRequestConfigs = void 0;
7
- var _helpers = require("../../../../../utils/helpers");
8
- const calculateRouteConfigWeight = graphQLRouteConfig => {
9
- const {
10
- entities
11
- } = graphQLRouteConfig;
12
- if (!entities) return 0;
13
- let routeConfigWeight = 0;
14
- const {
15
- headers,
16
- cookies,
17
- query,
18
- variables
19
- } = entities;
20
- if (headers) routeConfigWeight += Object.keys(headers).length;
21
- if (cookies) routeConfigWeight += Object.keys(cookies).length;
22
- if (query) routeConfigWeight += Object.keys(query).length;
23
- if (variables) {
24
- if ((0, _helpers.isPlainObject)(variables) && variables.checkMode) {
25
- // ✅ important:
26
- // check that actual value check modes does not have `value` for compare
27
- if (variables.checkMode === 'exists' || variables.checkMode === 'notExists') {
28
- routeConfigWeight += 1;
29
- return routeConfigWeight;
30
- }
31
- routeConfigWeight += (0, _helpers.isPlainObject)(variables.value) ? Object.keys(variables.value).length : 1;
32
- return routeConfigWeight;
5
+ Object.defineProperty(exports, "prepareGraphQLRequestConfigs", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return prepareGraphQLRequestConfigs;
33
9
  }
34
- routeConfigWeight += (0, _helpers.isPlainObject)(variables) ? Object.keys(variables).length : 1;
35
- }
36
- return routeConfigWeight;
10
+ });
11
+ const _helpers = require("../../../../../utils/helpers");
12
+ const calculateRouteConfigWeight = (graphQLRouteConfig)=>{
13
+ const { entities } = graphQLRouteConfig;
14
+ if (!entities) return 0;
15
+ let routeConfigWeight = 0;
16
+ const { headers, cookies, query, variables } = entities;
17
+ if (headers) routeConfigWeight += Object.keys(headers).length;
18
+ if (cookies) routeConfigWeight += Object.keys(cookies).length;
19
+ if (query) routeConfigWeight += Object.keys(query).length;
20
+ if (variables) {
21
+ if ((0, _helpers.isPlainObject)(variables) && variables.checkMode) {
22
+ // ✅ important:
23
+ // check that actual value check modes does not have `value` for compare
24
+ if (variables.checkMode === 'exists' || variables.checkMode === 'notExists') {
25
+ routeConfigWeight += 1;
26
+ return routeConfigWeight;
27
+ }
28
+ routeConfigWeight += (0, _helpers.isPlainObject)(variables.value) ? Object.keys(variables.value).length : 1;
29
+ return routeConfigWeight;
30
+ }
31
+ routeConfigWeight += (0, _helpers.isPlainObject)(variables) ? Object.keys(variables).length : 1;
32
+ }
33
+ return routeConfigWeight;
37
34
  };
38
- const prepareGraphQLRequestConfigs = requestConfigs => {
39
- requestConfigs.forEach(requestConfig => {
40
- requestConfig.routes.sort((first, second) =>
41
- // important:
42
- // Lift more specific configs for correct working of routes
43
- calculateRouteConfigWeight(second) - calculateRouteConfigWeight(first));
44
- });
45
- return requestConfigs;
35
+ const prepareGraphQLRequestConfigs = (requestConfigs)=>{
36
+ requestConfigs.forEach((requestConfig)=>{
37
+ requestConfig.routes.sort((first, second)=>// ✅ important:
38
+ // Lift more specific configs for correct working of routes
39
+ calculateRouteConfigWeight(second) - calculateRouteConfigWeight(first));
40
+ });
41
+ return requestConfigs;
46
42
  };
47
- exports.prepareGraphQLRequestConfigs = prepareGraphQLRequestConfigs;
@@ -1,16 +1,18 @@
1
1
  "use strict";
2
-
3
2
  Object.defineProperty(exports, "__esModule", {
4
- value: true
3
+ value: true
5
4
  });
6
- var _createGraphQLRoutes = require("./createGraphQLRoutes/createGraphQLRoutes");
7
- Object.keys(_createGraphQLRoutes).forEach(function (key) {
8
- if (key === "default" || key === "__esModule") return;
9
- if (key in exports && exports[key] === _createGraphQLRoutes[key]) return;
10
- Object.defineProperty(exports, key, {
11
- enumerable: true,
12
- get: function () {
13
- return _createGraphQLRoutes[key];
14
- }
15
- });
16
- });
5
+ _export_star(require("./createGraphQLRoutes/createGraphQLRoutes"), exports);
6
+ function _export_star(from, to) {
7
+ Object.keys(from).forEach(function(k) {
8
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
9
+ Object.defineProperty(to, k, {
10
+ enumerable: true,
11
+ get: function() {
12
+ return from[k];
13
+ }
14
+ });
15
+ }
16
+ });
17
+ return from;
18
+ }