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.
- package/README.md +2 -0
- package/dist/bin/bin.js +3 -6
- package/dist/bin/build.js +52 -50
- package/dist/bin/cli.js +50 -43
- package/dist/bin/helpers/createTemplate.js +29 -21
- package/dist/bin/helpers/getMostSpecificPathFromError.js +34 -30
- package/dist/bin/helpers/getValidationMessageFromPath.js +11 -8
- package/dist/bin/helpers/index.js +21 -79
- package/dist/bin/helpers/isOnlyRequestedDataResolvingPropertyExists.js +13 -6
- package/dist/bin/helpers/resolveConfigFile.js +22 -19
- package/dist/bin/helpers/resolveConfigFilePath.js +19 -12
- package/dist/bin/helpers/resolveExportsFromSourceCode.js +13 -10
- package/dist/bin/init.js +114 -97
- package/dist/bin/run.js +42 -43
- package/dist/bin/validateMockServerConfig/baseUrlSchema/baseUrlSchema.js +9 -5
- package/dist/bin/validateMockServerConfig/corsSchema/corsSchema.d.ts +2 -2
- package/dist/bin/validateMockServerConfig/corsSchema/corsSchema.js +32 -14
- package/dist/bin/validateMockServerConfig/databaseConfigSchema/databaseConfigSchema.js +19 -9
- package/dist/bin/validateMockServerConfig/graphqlConfigSchema/graphqlConfigSchema.d.ts +82 -72
- package/dist/bin/validateMockServerConfig/graphqlConfigSchema/graphqlConfigSchema.js +38 -22
- package/dist/bin/validateMockServerConfig/graphqlConfigSchema/routeConfigSchema/routeConfigSchema.d.ts +25 -21
- package/dist/bin/validateMockServerConfig/graphqlConfigSchema/routeConfigSchema/routeConfigSchema.js +36 -28
- package/dist/bin/validateMockServerConfig/interceptorsSchema/interceptorsSchema.js +12 -8
- package/dist/bin/validateMockServerConfig/portSchema/portSchema.js +9 -5
- package/dist/bin/validateMockServerConfig/queueSchema/queueSchema.d.ts +2 -2
- package/dist/bin/validateMockServerConfig/queueSchema/queueSchema.js +18 -9
- package/dist/bin/validateMockServerConfig/restConfigSchema/restConfigSchema.d.ts +270 -240
- package/dist/bin/validateMockServerConfig/restConfigSchema/restConfigSchema.js +33 -19
- package/dist/bin/validateMockServerConfig/restConfigSchema/routeConfigSchema/routeConfigSchema.d.ts +33 -28
- package/dist/bin/validateMockServerConfig/restConfigSchema/routeConfigSchema/routeConfigSchema.js +60 -48
- package/dist/bin/validateMockServerConfig/settingsSchema/settingsSchema.js +13 -9
- package/dist/bin/validateMockServerConfig/staticPathSchema/staticPathSchema.js +19 -9
- package/dist/bin/validateMockServerConfig/utils/checkModeSchema/checkModeSchema.js +27 -9
- package/dist/bin/validateMockServerConfig/utils/entitiesSchema/entitiesSchema.d.ts +4 -8
- package/dist/bin/validateMockServerConfig/utils/entitiesSchema/entitiesSchema.js +117 -60
- package/dist/bin/validateMockServerConfig/utils/index.js +20 -68
- package/dist/bin/validateMockServerConfig/utils/jsonSchema/jsonSchema.js +30 -10
- package/dist/bin/validateMockServerConfig/utils/plainObjectSchema/plainObjectSchema.js +9 -8
- package/dist/bin/validateMockServerConfig/utils/requiredPropertiesSchema/requiredPropertiesSchema.d.ts +1 -1
- package/dist/bin/validateMockServerConfig/utils/requiredPropertiesSchema/requiredPropertiesSchema.js +10 -7
- package/dist/bin/validateMockServerConfig/utils/sharedSchema/sharedSchema.js +18 -6
- package/dist/bin/validateMockServerConfig/validateApiMockServerConfig.js +43 -40
- package/dist/bin/validateMockServerConfig/validateMockServerConfig.js +38 -35
- package/dist/index.js +15 -13
- package/dist/src/core/database/createDatabaseRoutes/createDatabaseRoutes.js +25 -28
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/createNewId/createNewId.js +14 -11
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/findIndexById/findIndexById.js +8 -5
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/index.js +17 -35
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/isIndex/isIndex.js +8 -5
- package/dist/src/core/database/createDatabaseRoutes/helpers/createNestedDatabaseRoutes/createNestedDatabaseRoutes.js +159 -143
- package/dist/src/core/database/createDatabaseRoutes/helpers/createRewrittenDatabaseRoutes/createRewrittenDatabaseRoutes.js +15 -8
- package/dist/src/core/database/createDatabaseRoutes/helpers/createShallowDatabaseRoutes/createShallowDatabaseRoutes.js +90 -94
- package/dist/src/core/database/createDatabaseRoutes/helpers/filter/filter.js +49 -46
- package/dist/src/core/database/createDatabaseRoutes/helpers/index.js +19 -57
- package/dist/src/core/database/createDatabaseRoutes/helpers/pagination/pagination.js +32 -31
- package/dist/src/core/database/createDatabaseRoutes/helpers/search/search.js +34 -24
- package/dist/src/core/database/createDatabaseRoutes/helpers/sort/sort.js +37 -33
- package/dist/src/core/database/createDatabaseRoutes/helpers/splitDatabaseByNesting/splitDatabaseByNesting.js +26 -25
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileStorage.js +65 -50
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileWriter.js +55 -48
- package/dist/src/core/database/createDatabaseRoutes/storages/Memory/MemoryStorage.js +51 -41
- package/dist/src/core/database/createDatabaseRoutes/storages/index.js +16 -24
- package/dist/src/core/database/index.js +15 -13
- package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.js +131 -140
- package/dist/src/core/graphql/createGraphQLRoutes/helpers/index.js +15 -13
- package/dist/src/core/graphql/createGraphQLRoutes/helpers/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.js +36 -41
- package/dist/src/core/graphql/index.js +15 -13
- package/dist/src/core/middlewares/cookieParseMiddleware/cookieParseMiddleware.js +16 -13
- package/dist/src/core/middlewares/cookieParseMiddleware/helpers/index.js +15 -13
- package/dist/src/core/middlewares/cookieParseMiddleware/helpers/parseCookie/parseCookie.js +18 -15
- package/dist/src/core/middlewares/corsMiddleware/corsMiddleware.js +50 -47
- package/dist/src/core/middlewares/corsMiddleware/helpers/getAllowedOrigins/getAllowedOrigins.js +17 -12
- package/dist/src/core/middlewares/corsMiddleware/helpers/index.js +15 -13
- package/dist/src/core/middlewares/destroyerMiddleware/destroyerMiddleware.js +23 -20
- package/dist/src/core/middlewares/errorMiddleware/errorMiddleware.js +23 -17
- package/dist/src/core/middlewares/index.js +23 -91
- package/dist/src/core/middlewares/noCorsMiddleware/noCorsMiddleware.js +22 -19
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getGraphqlUrlSuggestions/getGraphqlUrlSuggestions.js +21 -23
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getLevenshteinDistance/getLevenshteinDistance.js +80 -80
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/getRestUrlSuggestions.js +34 -34
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getActualRestUrlMeaningfulString/getActualRestUrlMeaningfulString.js +8 -5
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getPatternRestUrlMeaningfulString/getPatternRestUrlMeaningfulString.js +8 -5
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/index.js +16 -24
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/index.js +17 -35
- package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.js +58 -60
- package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.js +16 -17
- package/dist/src/core/middlewares/staticMiddleware/staticMiddleware.js +32 -25
- package/dist/src/core/rest/createRestRoutes/createRestRoutes.js +125 -130
- package/dist/src/core/rest/createRestRoutes/helpers/index.js +15 -13
- package/dist/src/core/rest/createRestRoutes/helpers/prepareRestRequestConfigs/prepareRestRequestConfigs.js +37 -43
- package/dist/src/core/rest/index.js +15 -13
- package/dist/src/index.js +16 -24
- package/dist/src/server/createDatabaseMockServer/createDatabaseMockServer.js +56 -53
- package/dist/src/server/createGraphQLMockServer/createGraphQLMockServer.js +64 -62
- package/dist/src/server/createMockServer/createMockServer.js +94 -91
- package/dist/src/server/createRestMockServer/createRestMockServer.js +64 -62
- package/dist/src/server/index.js +23 -91
- package/dist/src/server/startDatabaseMockServer/startDatabaseMockServer.js +25 -19
- package/dist/src/server/startGraphQLMockServer/startGraphQLMockServer.js +25 -19
- package/dist/src/server/startMockServer/startMockServer.js +25 -19
- package/dist/src/server/startRestMockServer/startRestMockServer.js +25 -19
- package/dist/src/utils/constants/appPath.js +8 -4
- package/dist/src/utils/constants/checkModes.js +74 -11
- package/dist/src/utils/constants/default.js +18 -14
- package/dist/src/utils/constants/index.js +17 -35
- package/dist/src/utils/helpers/asyncHandler.js +8 -5
- package/dist/src/utils/helpers/config/index.js +15 -13
- package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.js +103 -110
- package/dist/src/utils/helpers/entities/convertToEntityDescriptor/convertToEntityDescriptor.js +12 -9
- package/dist/src/utils/helpers/entities/index.js +16 -24
- package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.js +9 -6
- package/dist/src/utils/helpers/files/index.js +15 -13
- package/dist/src/utils/helpers/files/isFilePathValid/isFilePathValid.js +22 -15
- package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.js +25 -29
- package/dist/src/utils/helpers/graphql/index.js +17 -35
- package/dist/src/utils/helpers/graphql/parseGraphQLRequest/parseGraphQLRequest.js +13 -10
- package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.js +23 -19
- package/dist/src/utils/helpers/index.js +26 -124
- package/dist/src/utils/helpers/interceptors/callRequestInterceptor/callRequestInterceptor.js +21 -21
- package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.js +62 -64
- package/dist/src/utils/helpers/interceptors/helpers/setDelay.js +10 -7
- package/dist/src/utils/helpers/interceptors/index.js +16 -24
- package/dist/src/utils/helpers/isPlainObject/isPlainObject.js +8 -5
- package/dist/src/utils/helpers/isPrimitive/isPrimitive.js +8 -5
- package/dist/src/utils/helpers/isRegExp/isRegExp.js +8 -5
- package/dist/src/utils/helpers/sleep.js +9 -6
- package/dist/src/utils/helpers/tests/createTmpDir.js +16 -9
- package/dist/src/utils/helpers/tests/index.js +15 -13
- package/dist/src/utils/helpers/url/convertWin32PathToUnix/convertWin32PathToUnix.js +8 -5
- package/dist/src/utils/helpers/url/getUrlParts/getUrlParts.js +9 -6
- package/dist/src/utils/helpers/url/index.js +18 -46
- package/dist/src/utils/helpers/url/removeLeadingAndTrailingSlashes/removeLeadingAndTrailingSlashes.js +8 -5
- package/dist/src/utils/helpers/url/urlJoin/urlJoin.js +18 -11
- package/dist/src/utils/types/checkModes.js +4 -1
- package/dist/src/utils/types/database.js +4 -1
- package/dist/src/utils/types/entities.js +4 -1
- package/dist/src/utils/types/graphql.d.ts +1 -1
- package/dist/src/utils/types/graphql.js +4 -1
- package/dist/src/utils/types/index.js +24 -102
- package/dist/src/utils/types/interceptors.js +4 -1
- package/dist/src/utils/types/rest.d.ts +1 -1
- package/dist/src/utils/types/rest.js +4 -1
- package/dist/src/utils/types/server.js +4 -1
- package/dist/src/utils/types/utils.js +4 -1
- package/dist/src/utils/types/values.js +4 -1
- package/package.json +26 -29
- package/dist/bin/templates/.eslintrc.js +0 -14
- package/dist/bin/templates/tsconfig.json +0 -11
|
@@ -1,50 +1,60 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
2
|
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
|
|
3
|
+
value: true
|
|
5
4
|
});
|
|
6
|
-
exports
|
|
7
|
-
|
|
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
|
-
|
|
10
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
3
|
+
value: true
|
|
5
4
|
});
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
3
|
+
value: true
|
|
5
4
|
});
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
3
|
+
value: true
|
|
5
4
|
});
|
|
6
|
-
exports
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
|
68
|
-
if (
|
|
69
|
-
|
|
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
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
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
|
-
|
|
3
|
+
value: true
|
|
5
4
|
});
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
3
|
+
value: true
|
|
5
4
|
});
|
|
6
|
-
exports
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
3
|
+
value: true
|
|
5
4
|
});
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
+
}
|