mock-config-server 3.0.1 → 3.1.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.
- package/LICENSE +21 -21
- package/README.md +614 -502
- package/dist/bin/bin.js +8 -3
- package/dist/bin/build.js +51 -45
- package/dist/bin/cli.js +37 -44
- package/dist/bin/helpers/index.js +36 -17
- package/dist/bin/helpers/resolveConfigFile.js +20 -17
- package/dist/bin/helpers/resolveConfigFilePath.js +12 -35
- package/dist/bin/helpers/resolveExportsFromSourceCode.js +11 -8
- package/dist/bin/resolveConfigFilePath/resolveConfigFilePath.js +12 -35
- package/dist/bin/run.js +17 -12
- package/dist/bin/validateMockServerConfig/helpers/index.js +25 -16
- package/dist/bin/validateMockServerConfig/helpers/isCheckModeValid/index.js +14 -15
- package/dist/bin/validateMockServerConfig/helpers/isCheckModeValid/isCheckModeValid.js +8 -6
- package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/index.js +14 -15
- package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/isDescriptorValueValid.js +15 -18
- package/dist/bin/validateMockServerConfig/validateBaseUrl/validateBaseUrl.js +12 -9
- package/dist/bin/validateMockServerConfig/validateCors/validateCors.js +75 -73
- package/dist/bin/validateMockServerConfig/validateDatabaseConfig/validateDatabaseConfig.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validateDatabaseConfig/validateDatabaseConfig.js +45 -0
- package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateGraphqlConfig.js +47 -43
- package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateRoutes/validateRoutes.js +90 -83
- package/dist/bin/validateMockServerConfig/validateInterceptors/validateInterceptors.js +22 -16
- package/dist/bin/validateMockServerConfig/validateMockServerConfig.js +29 -27
- package/dist/bin/validateMockServerConfig/validatePort/validatePort.js +9 -6
- package/dist/bin/validateMockServerConfig/validateRestConfig/validateRestConfig.js +52 -47
- package/dist/bin/validateMockServerConfig/validateRestConfig/validateRoutes/validateRoutes.js +94 -87
- package/dist/bin/validateMockServerConfig/validateStaticPath/validateStaticPath.js +43 -34
- package/dist/index.js +14 -15
- package/dist/src/core/database/createDatabaseRoutes/createDatabaseRoutes.d.ts +3 -0
- package/dist/src/core/database/createDatabaseRoutes/createDatabaseRoutes.js +33 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/createNewId/createNewId.d.ts +4 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/createNewId/createNewId.js +16 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/findIndexById/findIndexById.d.ts +4 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/findIndexById/findIndexById.js +8 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/index.d.ts +3 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/index.js +38 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/isIndex/isIndex.d.ts +1 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/isIndex/isIndex.js +8 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/createNestedDatabaseRoutes/createNestedDatabaseRoutes.d.ts +4 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/createNestedDatabaseRoutes/createNestedDatabaseRoutes.js +88 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/createRewrittenDatabaseRoutes/createRewrittenDatabaseRoutes.d.ts +2 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/createRewrittenDatabaseRoutes/createRewrittenDatabaseRoutes.js +12 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/createShallowDatabaseRoutes/createShallowDatabaseRoutes.d.ts +4 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/createShallowDatabaseRoutes/createShallowDatabaseRoutes.js +38 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/index.d.ts +5 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/index.js +60 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/splitDatabaseByNesting/splitDatabaseByNesting.d.ts +5 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/splitDatabaseByNesting/splitDatabaseByNesting.js +30 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileStorage.d.ts +11 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileStorage.js +64 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileWriter.d.ts +11 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileWriter.js +58 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/Memory/MemoryStorage.d.ts +10 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/Memory/MemoryStorage.js +54 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/index.d.ts +2 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/index.js +27 -0
- package/dist/src/core/database/index.d.ts +1 -0
- package/dist/src/core/database/index.js +16 -0
- package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.js +96 -83
- package/dist/src/core/graphql/createGraphQLRoutes/helpers/index.js +14 -15
- package/dist/src/core/graphql/createGraphQLRoutes/helpers/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.js +36 -30
- package/dist/src/core/graphql/index.js +14 -15
- package/dist/src/core/middlewares/cookieParseMiddleware/cookieParseMiddleware.js +15 -13
- package/dist/src/core/middlewares/cookieParseMiddleware/helpers/index.js +14 -15
- package/dist/src/core/middlewares/cookieParseMiddleware/helpers/parseCookie/parseCookie.js +16 -14
- package/dist/src/core/middlewares/corsMiddleware/corsMiddleware.js +48 -41
- package/dist/src/core/middlewares/corsMiddleware/helpers/getAllowedOrigins/getAllowedOrigins.js +13 -10
- package/dist/src/core/middlewares/corsMiddleware/helpers/index.js +14 -15
- package/dist/src/core/middlewares/destroyerMiddleware/destroyerMiddleware.js +21 -18
- package/dist/src/core/middlewares/errorMiddleware/errorMiddleware.js +18 -15
- package/dist/src/core/middlewares/index.js +92 -23
- package/dist/src/core/middlewares/noCorsMiddleware/noCorsMiddleware.js +21 -21
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getGraphqlUrlSuggestions/getGraphqlUrlSuggestions.js +24 -16
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getLevenshteinDistance/getLevenshteinDistance.js +78 -74
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/getRestUrlSuggestions.js +36 -31
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getActualRestUrlMeaningfulString/getActualRestUrlMeaningfulString.js +6 -5
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getPatternRestUrlMeaningfulString/getPatternRestUrlMeaningfulString.js +6 -3
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/index.js +25 -16
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/index.js +36 -17
- package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.js +63 -48
- package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.js +13 -7
- package/dist/src/core/middlewares/staticMiddleware/staticMiddleware.js +26 -25
- package/dist/src/core/rest/createRestRoutes/createRestRoutes.js +71 -56
- package/dist/src/core/rest/createRestRoutes/helpers/index.js +14 -15
- package/dist/src/core/rest/createRestRoutes/helpers/prepareRestRequestConfigs/prepareRestRequestConfigs.js +38 -32
- package/dist/src/core/rest/index.js +14 -15
- package/dist/src/index.js +36 -17
- package/dist/src/server/createMockServer/createMockServer.js +74 -53
- package/dist/src/server/index.js +25 -16
- package/dist/src/server/startMockServer/startMockServer.js +20 -17
- package/dist/src/static/views/assets/icons/scheme-dark.svg +3 -3
- package/dist/src/static/views/assets/icons/scheme-light.svg +3 -3
- package/dist/src/static/views/assets/styles/global.css +88 -88
- package/dist/src/static/views/components/header/index.css +55 -55
- package/dist/src/static/views/components/header/index.ejs +39 -39
- package/dist/src/static/views/components/header/index.js +1 -1
- package/dist/src/static/views/features/scheme/dark.css +12 -12
- package/dist/src/static/views/features/scheme/index.ejs +3 -3
- package/dist/src/static/views/features/scheme/index.js +31 -31
- package/dist/src/static/views/features/scheme/light.css +12 -12
- package/dist/src/static/views/features/tab/index.css +30 -30
- package/dist/src/static/views/features/tab/index.ejs +1 -1
- package/dist/src/static/views/features/tab/index.js +12 -12
- package/dist/src/static/views/pages/404/index.css +10 -10
- package/dist/src/static/views/pages/404/index.ejs +84 -84
- package/dist/src/utils/constants/appPath.js +6 -2
- package/dist/src/utils/constants/checkModes.js +21 -30
- package/dist/src/utils/constants/default.js +15 -11
- package/dist/src/utils/constants/index.js +36 -17
- package/dist/src/utils/helpers/asyncHandler.js +6 -3
- package/dist/src/utils/helpers/config/index.js +14 -15
- package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.js +109 -122
- package/dist/src/utils/helpers/entities/convertToEntityDescriptor/convertToEntityDescriptor.js +10 -6
- package/dist/src/utils/helpers/entities/index.js +25 -16
- package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.js +7 -4
- package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.js +28 -18
- package/dist/src/utils/helpers/graphql/index.js +36 -17
- package/dist/src/utils/helpers/graphql/parseGraphQLRequest/parseGraphQLRequest.js +11 -9
- package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.js +20 -17
- package/dist/src/utils/helpers/index.js +114 -25
- package/dist/src/utils/helpers/interceptors/callRequestInterceptor/callRequestInterceptor.js +22 -16
- package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.js +56 -48
- package/dist/src/utils/helpers/interceptors/helpers/setDelay.js +8 -5
- package/dist/src/utils/helpers/interceptors/index.js +25 -16
- package/dist/src/utils/helpers/isPlainObject/isPlainObject.js +6 -6
- package/dist/src/utils/helpers/isPrimitive/isPrimitive.js +6 -3
- package/dist/src/utils/helpers/isRegExp/isRegExp.js +6 -3
- package/dist/src/utils/helpers/sleep.js +7 -4
- package/dist/src/utils/helpers/url/convertWin32PathToUnix/convertWin32PathToUnix.js +6 -6
- package/dist/src/utils/helpers/url/getUrlParts/getUrlParts.js +7 -4
- package/dist/src/utils/helpers/url/index.js +47 -18
- package/dist/src/utils/helpers/url/removeLeadingAndTrailingSlashes/removeLeadingAndTrailingSlashes.js +6 -3
- package/dist/src/utils/helpers/url/urlJoin/urlJoin.js +11 -10
- package/dist/src/utils/types/checkModes.js +1 -2
- package/dist/src/utils/types/database.d.ts +6 -0
- package/dist/src/utils/types/database.js +1 -0
- package/dist/src/utils/types/graphql.js +1 -2
- package/dist/src/utils/types/index.d.ts +1 -0
- package/dist/src/utils/types/index.js +80 -20
- package/dist/src/utils/types/interceptors.js +1 -2
- package/dist/src/utils/types/rest.js +1 -2
- package/dist/src/utils/types/server.d.ts +5 -0
- package/dist/src/utils/types/server.js +1 -2
- package/dist/src/utils/types/values.js +1 -2
- package/package.json +130 -113
|
@@ -1,89 +1,102 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
3
6
|
exports.createGraphQLRoutes = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
+
var _flat = require("flat");
|
|
8
|
+
var _helpers = require("../../../utils/helpers");
|
|
9
|
+
var _helpers2 = require("./helpers");
|
|
7
10
|
const createGraphQLRoutes = (router, graphqlConfig, serverResponseInterceptors) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
const preparedGraphQLRequestConfig = (0, _helpers2.prepareGraphQLRequestConfigs)(graphqlConfig.configs);
|
|
12
|
+
const graphqlMiddleware = async (request, response, next) => {
|
|
13
|
+
var _matchedRequestConfig, _matchedRouteConfig$e, _matchedRouteConfig$i, _matchedRequestConfig2, _graphqlConfig$interc;
|
|
14
|
+
const graphQLInput = (0, _helpers.getGraphQLInput)(request);
|
|
15
|
+
if (!graphQLInput || !graphQLInput.query) {
|
|
16
|
+
return response.status(400).json({
|
|
17
|
+
message: 'Query is missing, you must pass a valid GraphQL query'
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
const query = (0, _helpers.parseQuery)(graphQLInput.query);
|
|
21
|
+
if (!query) {
|
|
22
|
+
return response.status(400).json({
|
|
23
|
+
message: 'Query is invalid, you must use a valid GraphQL query'
|
|
24
|
+
});
|
|
25
|
+
}
|
|
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
|
+
const matchedRequestConfig = preparedGraphQLRequestConfig.find(requestConfig => {
|
|
32
|
+
if (requestConfig.operationName instanceof RegExp) {
|
|
33
|
+
return new RegExp(requestConfig.operationName).test(query.operationName) && requestConfig.operationType === query.operationType;
|
|
34
|
+
}
|
|
35
|
+
return requestConfig.operationName === query.operationName && requestConfig.operationType === query.operationType;
|
|
36
|
+
});
|
|
37
|
+
if (!matchedRequestConfig) {
|
|
38
|
+
return next();
|
|
39
|
+
}
|
|
40
|
+
const requestInterceptor = (_matchedRequestConfig = matchedRequestConfig.interceptors) === null || _matchedRequestConfig === void 0 ? void 0 : _matchedRequestConfig.request;
|
|
41
|
+
if (requestInterceptor) {
|
|
42
|
+
await (0, _helpers.callRequestInterceptor)({
|
|
43
|
+
request,
|
|
44
|
+
interceptor: requestInterceptor
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
const matchedRouteConfig = matchedRequestConfig.routes.find(({
|
|
48
|
+
entities
|
|
49
|
+
}) => {
|
|
50
|
+
if (!entities) return true;
|
|
51
|
+
const entries = Object.entries(entities);
|
|
52
|
+
return entries.every(([entityName, valueOrDescriptor]) => {
|
|
53
|
+
const {
|
|
54
|
+
checkMode,
|
|
55
|
+
value: descriptorValue
|
|
56
|
+
} = (0, _helpers.convertToEntityDescriptor)(valueOrDescriptor);
|
|
57
|
+
|
|
58
|
+
// ✅ 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);
|
|
15
63
|
}
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
const matchedRequestConfig = preparedGraphQLRequestConfig.find((requestConfig) => {
|
|
28
|
-
if (requestConfig.operationName instanceof RegExp) {
|
|
29
|
-
return (new RegExp(requestConfig.operationName).test(query.operationName) &&
|
|
30
|
-
requestConfig.operationType === query.operationType);
|
|
31
|
-
}
|
|
32
|
-
return (requestConfig.operationName === query.operationName &&
|
|
33
|
-
requestConfig.operationType === query.operationType);
|
|
34
|
-
});
|
|
35
|
-
if (!matchedRequestConfig) {
|
|
36
|
-
return next();
|
|
37
|
-
}
|
|
38
|
-
const requestInterceptor = matchedRequestConfig.interceptors?.request;
|
|
39
|
-
if (requestInterceptor) {
|
|
40
|
-
await (0, helpers_1.callRequestInterceptor)({ request, interceptor: requestInterceptor });
|
|
41
|
-
}
|
|
42
|
-
const matchedRouteConfig = matchedRequestConfig.routes.find(({ entities }) => {
|
|
43
|
-
if (!entities)
|
|
44
|
-
return true;
|
|
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);
|
|
53
|
-
}
|
|
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
|
-
});
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
if (!matchedRouteConfig) {
|
|
63
|
-
return next();
|
|
64
|
-
}
|
|
65
|
-
const matchedRouteConfigData = typeof matchedRouteConfig.data === 'function'
|
|
66
|
-
? await matchedRouteConfig.data(request, matchedRouteConfig.entities ?? {})
|
|
67
|
-
: matchedRouteConfig.data;
|
|
68
|
-
const data = await (0, helpers_1.callResponseInterceptors)({
|
|
69
|
-
data: matchedRouteConfigData,
|
|
70
|
-
request,
|
|
71
|
-
response,
|
|
72
|
-
interceptors: {
|
|
73
|
-
routeInterceptor: matchedRouteConfig.interceptors?.response,
|
|
74
|
-
requestInterceptor: matchedRequestConfig.interceptors?.response,
|
|
75
|
-
apiInterceptor: graphqlConfig.interceptors?.response,
|
|
76
|
-
serverInterceptor: serverResponseInterceptors
|
|
77
|
-
}
|
|
64
|
+
const mappedEntityDescriptors = Object.entries(valueOrDescriptor);
|
|
65
|
+
return mappedEntityDescriptors.every(([entityKey, mappedEntityDescriptor]) => {
|
|
66
|
+
const {
|
|
67
|
+
checkMode,
|
|
68
|
+
value: descriptorValue
|
|
69
|
+
} = (0, _helpers.convertToEntityDescriptor)(mappedEntityDescriptor);
|
|
70
|
+
const flattenEntity = (0, _flat.flatten)(entityName === 'variables' ? graphQLInput.variables : request[entityName]);
|
|
71
|
+
// ✅ important: transform header keys to lower case because browsers send headers in lowercase
|
|
72
|
+
return (0, _helpers.resolveEntityValues)(checkMode, flattenEntity[entityName === 'headers' ? entityKey.toLowerCase() : entityKey], descriptorValue);
|
|
78
73
|
});
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
if (!matchedRouteConfig) {
|
|
77
|
+
return next();
|
|
78
|
+
}
|
|
79
|
+
const matchedRouteConfigData = typeof matchedRouteConfig.data === 'function' ? await matchedRouteConfig.data(request, (_matchedRouteConfig$e = matchedRouteConfig.entities) !== null && _matchedRouteConfig$e !== void 0 ? _matchedRouteConfig$e : {}) : matchedRouteConfig.data;
|
|
80
|
+
const data = await (0, _helpers.callResponseInterceptors)({
|
|
81
|
+
data: matchedRouteConfigData,
|
|
82
|
+
request,
|
|
83
|
+
response,
|
|
84
|
+
interceptors: {
|
|
85
|
+
routeInterceptor: (_matchedRouteConfig$i = matchedRouteConfig.interceptors) === null || _matchedRouteConfig$i === void 0 ? void 0 : _matchedRouteConfig$i.response,
|
|
86
|
+
requestInterceptor: (_matchedRequestConfig2 = matchedRequestConfig.interceptors) === null || _matchedRequestConfig2 === void 0 ? void 0 : _matchedRequestConfig2.response,
|
|
87
|
+
apiInterceptor: (_graphqlConfig$interc = graphqlConfig.interceptors) === null || _graphqlConfig$interc === void 0 ? void 0 : _graphqlConfig$interc.response,
|
|
88
|
+
serverInterceptor: serverResponseInterceptors
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// ✅ important:
|
|
93
|
+
// set 'Cache-Control' header for explicit browsers response revalidate
|
|
94
|
+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
|
|
95
|
+
response.set('Cache-control', 'max-age=0, must-revalidate');
|
|
96
|
+
return response.status(response.statusCode).json(data);
|
|
97
|
+
};
|
|
98
|
+
router.route('/').get((0, _helpers.asyncHandler)(graphqlMiddleware));
|
|
99
|
+
router.route('/').post((0, _helpers.asyncHandler)(graphqlMiddleware));
|
|
100
|
+
return router;
|
|
88
101
|
};
|
|
89
|
-
exports.createGraphQLRoutes = createGraphQLRoutes;
|
|
102
|
+
exports.createGraphQLRoutes = createGraphQLRoutes;
|
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
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];
|
|
7
14
|
}
|
|
8
|
-
|
|
9
|
-
})
|
|
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("./prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs"), exports);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -1,35 +1,41 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
3
6
|
exports.prepareGraphQLRequestConfigs = void 0;
|
|
4
|
-
|
|
5
|
-
const calculateRouteConfigWeight =
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
+
routeConfigWeight += (0, _helpers.isPlainObject)(variables.value) ? Object.keys(variables.value).length : 1;
|
|
26
|
+
return routeConfigWeight;
|
|
23
27
|
}
|
|
24
|
-
|
|
28
|
+
routeConfigWeight += Object.keys(variables).length;
|
|
29
|
+
}
|
|
30
|
+
return routeConfigWeight;
|
|
25
31
|
};
|
|
26
|
-
const prepareGraphQLRequestConfigs =
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
const prepareGraphQLRequestConfigs = requestConfigs => {
|
|
33
|
+
requestConfigs.forEach(requestConfig => {
|
|
34
|
+
requestConfig.routes.sort((first, second) =>
|
|
35
|
+
// ✅ important:
|
|
36
|
+
// Lift more specific configs for correct working of routes
|
|
37
|
+
calculateRouteConfigWeight(second) - calculateRouteConfigWeight(first));
|
|
38
|
+
});
|
|
39
|
+
return requestConfigs;
|
|
34
40
|
};
|
|
35
|
-
exports.prepareGraphQLRequestConfigs = prepareGraphQLRequestConfigs;
|
|
41
|
+
exports.prepareGraphQLRequestConfigs = prepareGraphQLRequestConfigs;
|
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
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];
|
|
7
14
|
}
|
|
8
|
-
|
|
9
|
-
})
|
|
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("./createGraphQLRoutes/createGraphQLRoutes"), exports);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
3
6
|
exports.cookieParseMiddleware = void 0;
|
|
4
|
-
|
|
5
|
-
const cookieParseMiddleware =
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
});
|
|
7
|
+
var _helpers = require("./helpers");
|
|
8
|
+
const cookieParseMiddleware = server => {
|
|
9
|
+
server.use((request, _response, next) => {
|
|
10
|
+
if (request.headers.cookie) {
|
|
11
|
+
request.cookies = (0, _helpers.parseCookie)(request.headers.cookie);
|
|
12
|
+
} else {
|
|
13
|
+
request.cookies = {};
|
|
14
|
+
}
|
|
15
|
+
return next();
|
|
16
|
+
});
|
|
15
17
|
};
|
|
16
|
-
exports.cookieParseMiddleware = cookieParseMiddleware;
|
|
18
|
+
exports.cookieParseMiddleware = cookieParseMiddleware;
|
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _parseCookie = require("./parseCookie/parseCookie");
|
|
7
|
+
Object.keys(_parseCookie).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _parseCookie[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _parseCookie[key];
|
|
7
14
|
}
|
|
8
|
-
|
|
9
|
-
})
|
|
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("./parseCookie/parseCookie"), exports);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
3
6
|
exports.parseCookie = void 0;
|
|
4
|
-
const parseCookie =
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return cookies;
|
|
7
|
+
const parseCookie = cookieHeader => {
|
|
8
|
+
if (!cookieHeader) return {};
|
|
9
|
+
const cookies = {};
|
|
10
|
+
const cookiePairs = cookieHeader.split(';');
|
|
11
|
+
cookiePairs.forEach(cookie => {
|
|
12
|
+
var _value$trim;
|
|
13
|
+
const [name, value] = cookie.trim().split('=');
|
|
14
|
+
if (!name) return;
|
|
15
|
+
cookies[name.trim()] = (_value$trim = value === null || value === void 0 ? void 0 : value.trim()) !== null && _value$trim !== void 0 ? _value$trim : '';
|
|
16
|
+
});
|
|
17
|
+
return cookies;
|
|
16
18
|
};
|
|
17
|
-
exports.parseCookie = parseCookie;
|
|
19
|
+
exports.parseCookie = parseCookie;
|
|
@@ -1,45 +1,52 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
3
6
|
exports.corsMiddleware = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
+
var _constants = require("../../../utils/constants");
|
|
8
|
+
var _helpers = require("../../../utils/helpers");
|
|
9
|
+
var _helpers2 = require("./helpers");
|
|
7
10
|
const corsMiddleware = (server, cors) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
11
|
+
server.use((0, _helpers.asyncHandler)(async (request, response, next) => {
|
|
12
|
+
var _allowedOrigins;
|
|
13
|
+
if (Array.isArray(cors.origin) && !cors.origin.length) {
|
|
14
|
+
return next();
|
|
15
|
+
}
|
|
16
|
+
let allowedOrigins = [];
|
|
17
|
+
if (typeof cors.origin === 'function') {
|
|
18
|
+
const origins = await cors.origin(request);
|
|
19
|
+
allowedOrigins = (0, _helpers2.getAllowedOrigins)(origins);
|
|
20
|
+
} else {
|
|
21
|
+
allowedOrigins = (0, _helpers2.getAllowedOrigins)(cors.origin);
|
|
22
|
+
}
|
|
23
|
+
const {
|
|
24
|
+
origin
|
|
25
|
+
} = request.headers;
|
|
26
|
+
if (!((_allowedOrigins = allowedOrigins) !== null && _allowedOrigins !== void 0 && _allowedOrigins.length) || !origin) {
|
|
27
|
+
return next();
|
|
28
|
+
}
|
|
29
|
+
const isRequestOriginAllowed = allowedOrigins.some(allowedOrigin => {
|
|
30
|
+
if (allowedOrigin instanceof RegExp) {
|
|
31
|
+
return new RegExp(allowedOrigin).test(origin);
|
|
32
|
+
}
|
|
33
|
+
return allowedOrigin === origin;
|
|
34
|
+
});
|
|
35
|
+
if (isRequestOriginAllowed) {
|
|
36
|
+
var _cors$credentials, _cors$exposedHeaders;
|
|
37
|
+
response.setHeader('Access-Control-Allow-Origin', origin);
|
|
38
|
+
response.setHeader('Access-Control-Allow-Credentials', `${(_cors$credentials = cors.credentials) !== null && _cors$credentials !== void 0 ? _cors$credentials : _constants.DEFAULT.CORS.CREDENTIALS}`);
|
|
39
|
+
response.setHeader('Access-Control-Expose-Headers', (_cors$exposedHeaders = cors.exposedHeaders) !== null && _cors$exposedHeaders !== void 0 ? _cors$exposedHeaders : _constants.DEFAULT.CORS.EXPOSED_HEADERS);
|
|
40
|
+
if (request.method === 'OPTIONS') {
|
|
41
|
+
var _cors$methods, _cors$allowedHeaders, _cors$maxAge;
|
|
42
|
+
response.setHeader('Access-Control-Allow-Methods', (_cors$methods = cors.methods) !== null && _cors$methods !== void 0 ? _cors$methods : _constants.DEFAULT.CORS.METHODS);
|
|
43
|
+
response.setHeader('Access-Control-Allow-Headers', (_cors$allowedHeaders = cors.allowedHeaders) !== null && _cors$allowedHeaders !== void 0 ? _cors$allowedHeaders : _constants.DEFAULT.CORS.ALLOWED_HEADERS);
|
|
44
|
+
response.setHeader('Access-Control-Max-Age', (_cors$maxAge = cors.maxAge) !== null && _cors$maxAge !== void 0 ? _cors$maxAge : _constants.DEFAULT.CORS.MAX_AGE);
|
|
45
|
+
response.sendStatus(204);
|
|
46
|
+
return response.end();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return next();
|
|
50
|
+
}));
|
|
44
51
|
};
|
|
45
|
-
exports.corsMiddleware = corsMiddleware;
|
|
52
|
+
exports.corsMiddleware = corsMiddleware;
|
package/dist/src/core/middlewares/corsMiddleware/helpers/getAllowedOrigins/getAllowedOrigins.js
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
3
6
|
exports.getAllowedOrigins = void 0;
|
|
4
|
-
const getAllowedOrigins =
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
const getAllowedOrigins = origin => {
|
|
8
|
+
if (Array.isArray(origin)) {
|
|
9
|
+
return origin;
|
|
10
|
+
}
|
|
11
|
+
if (typeof origin === 'string' || origin instanceof RegExp) {
|
|
12
|
+
return [origin];
|
|
13
|
+
}
|
|
14
|
+
throw new Error('Invalid cors origin format');
|
|
12
15
|
};
|
|
13
|
-
exports.getAllowedOrigins = getAllowedOrigins;
|
|
16
|
+
exports.getAllowedOrigins = getAllowedOrigins;
|
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _getAllowedOrigins = require("./getAllowedOrigins/getAllowedOrigins");
|
|
7
|
+
Object.keys(_getAllowedOrigins).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _getAllowedOrigins[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _getAllowedOrigins[key];
|
|
7
14
|
}
|
|
8
|
-
|
|
9
|
-
})
|
|
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("./getAllowedOrigins/getAllowedOrigins"), exports);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -1,23 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
3
6
|
exports.destroyerMiddleware = void 0;
|
|
4
|
-
const destroyerMiddleware =
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
const destroyerMiddleware = server => {
|
|
8
|
+
const serverWithDestroyer = server;
|
|
9
|
+
const connections = {};
|
|
10
|
+
serverWithDestroyer.on('connection', connection => {
|
|
11
|
+
const key = `${connection.remoteAddress}:${connection.remotePort}`;
|
|
12
|
+
connections[key] = connection;
|
|
13
|
+
connection.on('close', () => {
|
|
14
|
+
delete connections[key];
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
serverWithDestroyer.destroy = callback => {
|
|
18
|
+
serverWithDestroyer.close(callback);
|
|
19
|
+
Object.values(connections).forEach(connection => {
|
|
20
|
+
connection.destroy();
|
|
13
21
|
});
|
|
14
|
-
serverWithDestroyer.destroy = (callback) => {
|
|
15
|
-
serverWithDestroyer.close(callback);
|
|
16
|
-
Object.values(connections).forEach((connection) => {
|
|
17
|
-
connection.destroy();
|
|
18
|
-
});
|
|
19
|
-
return serverWithDestroyer;
|
|
20
|
-
};
|
|
21
22
|
return serverWithDestroyer;
|
|
23
|
+
};
|
|
24
|
+
return serverWithDestroyer;
|
|
22
25
|
};
|
|
23
|
-
exports.destroyerMiddleware = destroyerMiddleware;
|
|
26
|
+
exports.destroyerMiddleware = destroyerMiddleware;
|