mock-config-server 3.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/bin.d.ts +2 -0
- package/dist/bin/bin.js +8 -0
- package/dist/bin/build.d.ts +2 -0
- package/dist/bin/build.js +49 -0
- package/dist/bin/cli.d.ts +1 -0
- package/dist/bin/cli.js +48 -0
- package/dist/bin/helpers/index.d.ts +3 -0
- package/dist/bin/helpers/index.js +19 -0
- package/dist/bin/helpers/resolveConfigFile.d.ts +2 -0
- package/dist/bin/helpers/resolveConfigFile.js +20 -0
- package/dist/bin/helpers/resolveConfigFilePath.d.ts +1 -0
- package/dist/bin/helpers/resolveConfigFilePath.js +39 -0
- package/dist/bin/helpers/resolveExportsFromSourceCode.d.ts +1 -0
- package/dist/bin/helpers/resolveExportsFromSourceCode.js +11 -0
- package/dist/bin/resolveConfigFilePath/resolveConfigFilePath.d.ts +1 -0
- package/dist/bin/resolveConfigFilePath/resolveConfigFilePath.js +39 -0
- package/dist/bin/run.d.ts +6 -0
- package/dist/bin/run.js +17 -0
- package/dist/bin/validateMockServerConfig/helpers/index.d.ts +2 -0
- package/dist/bin/validateMockServerConfig/helpers/index.js +18 -0
- package/dist/bin/validateMockServerConfig/helpers/isCheckModeValid/index.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/helpers/isCheckModeValid/index.js +17 -0
- package/dist/bin/validateMockServerConfig/helpers/isCheckModeValid/isCheckModeValid.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/helpers/isCheckModeValid/isCheckModeValid.js +10 -0
- package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/index.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/index.js +17 -0
- package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/isDescriptorValueValid.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/isDescriptorValueValid.js +23 -0
- package/dist/bin/validateMockServerConfig/validateBaseUrl/validateBaseUrl.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validateBaseUrl/validateBaseUrl.js +12 -0
- package/dist/bin/validateMockServerConfig/validateCors/validateCors.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validateCors/validateCors.js +82 -0
- package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateGraphqlConfig.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateGraphqlConfig.js +48 -0
- package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateRoutes/validateRoutes.d.ts +2 -0
- package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateRoutes/validateRoutes.js +97 -0
- package/dist/bin/validateMockServerConfig/validateInterceptors/validateInterceptors.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validateInterceptors/validateInterceptors.js +21 -0
- package/dist/bin/validateMockServerConfig/validateMockServerConfig.d.ts +2 -0
- package/dist/bin/validateMockServerConfig/validateMockServerConfig.js +30 -0
- package/dist/bin/validateMockServerConfig/validatePort/validatePort.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validatePort/validatePort.js +9 -0
- package/dist/bin/validateMockServerConfig/validateRestConfig/validateRestConfig.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validateRestConfig/validateRestConfig.js +52 -0
- package/dist/bin/validateMockServerConfig/validateRestConfig/validateRoutes/validateRoutes.d.ts +2 -0
- package/dist/bin/validateMockServerConfig/validateRestConfig/validateRoutes/validateRoutes.js +101 -0
- package/dist/bin/validateMockServerConfig/validateStaticPath/validateStaticPath.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validateStaticPath/validateStaticPath.js +44 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +17 -0
- package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.d.ts +3 -0
- package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.js +89 -0
- package/dist/src/core/graphql/createGraphQLRoutes/helpers/index.d.ts +1 -0
- package/dist/src/core/graphql/createGraphQLRoutes/helpers/index.js +17 -0
- package/dist/src/core/graphql/createGraphQLRoutes/helpers/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.d.ts +2 -0
- package/dist/src/core/graphql/createGraphQLRoutes/helpers/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.js +35 -0
- package/dist/src/core/graphql/index.d.ts +1 -0
- package/dist/src/core/graphql/index.js +17 -0
- package/dist/src/core/middlewares/cookieParseMiddleware/cookieParseMiddleware.d.ts +2 -0
- package/dist/src/core/middlewares/cookieParseMiddleware/cookieParseMiddleware.js +16 -0
- package/dist/src/core/middlewares/cookieParseMiddleware/helpers/index.d.ts +1 -0
- package/dist/src/core/middlewares/cookieParseMiddleware/helpers/index.js +17 -0
- package/dist/src/core/middlewares/cookieParseMiddleware/helpers/parseCookie/parseCookie.d.ts +3 -0
- package/dist/src/core/middlewares/cookieParseMiddleware/helpers/parseCookie/parseCookie.js +17 -0
- package/dist/src/core/middlewares/corsMiddleware/corsMiddleware.d.ts +3 -0
- package/dist/src/core/middlewares/corsMiddleware/corsMiddleware.js +45 -0
- package/dist/src/core/middlewares/corsMiddleware/helpers/getAllowedOrigins/getAllowedOrigins.d.ts +2 -0
- package/dist/src/core/middlewares/corsMiddleware/helpers/getAllowedOrigins/getAllowedOrigins.js +13 -0
- package/dist/src/core/middlewares/corsMiddleware/helpers/index.d.ts +1 -0
- package/dist/src/core/middlewares/corsMiddleware/helpers/index.js +17 -0
- package/dist/src/core/middlewares/destroyerMiddleware/destroyerMiddleware.d.ts +7 -0
- package/dist/src/core/middlewares/destroyerMiddleware/destroyerMiddleware.js +23 -0
- package/dist/src/core/middlewares/errorMiddleware/errorMiddleware.d.ts +2 -0
- package/dist/src/core/middlewares/errorMiddleware/errorMiddleware.js +18 -0
- package/dist/src/core/middlewares/index.d.ts +8 -0
- package/dist/src/core/middlewares/index.js +24 -0
- package/dist/src/core/middlewares/noCorsMiddleware/noCorsMiddleware.d.ts +2 -0
- package/dist/src/core/middlewares/noCorsMiddleware/noCorsMiddleware.js +24 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getGraphqlUrlSuggestions/getGraphqlUrlSuggestions.d.ts +11 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getGraphqlUrlSuggestions/getGraphqlUrlSuggestions.js +19 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getLevenshteinDistance/getLevenshteinDistance.d.ts +1 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getLevenshteinDistance/getLevenshteinDistance.js +80 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/getRestUrlSuggestions.d.ts +11 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/getRestUrlSuggestions.js +34 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getActualRestUrlMeaningfulString/getActualRestUrlMeaningfulString.d.ts +1 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getActualRestUrlMeaningfulString/getActualRestUrlMeaningfulString.js +7 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getPatternRestUrlMeaningfulString/getPatternRestUrlMeaningfulString.d.ts +1 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getPatternRestUrlMeaningfulString/getPatternRestUrlMeaningfulString.js +5 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/index.d.ts +2 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/index.js +18 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/index.d.ts +3 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/index.js +19 -0
- package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.d.ts +3 -0
- package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.js +53 -0
- package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.d.ts +3 -0
- package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.js +11 -0
- package/dist/src/core/middlewares/staticMiddleware/staticMiddleware.d.ts +3 -0
- package/dist/src/core/middlewares/staticMiddleware/staticMiddleware.js +30 -0
- package/dist/src/core/rest/createRestRoutes/createRestRoutes.d.ts +3 -0
- package/dist/src/core/rest/createRestRoutes/createRestRoutes.js +60 -0
- package/dist/src/core/rest/createRestRoutes/helpers/index.d.ts +1 -0
- package/dist/src/core/rest/createRestRoutes/helpers/index.js +17 -0
- package/dist/src/core/rest/createRestRoutes/helpers/prepareRestRequestConfigs/prepareRestRequestConfigs.d.ts +2 -0
- package/dist/src/core/rest/createRestRoutes/helpers/prepareRestRequestConfigs/prepareRestRequestConfigs.js +37 -0
- package/dist/src/core/rest/index.d.ts +1 -0
- package/dist/src/core/rest/index.js +17 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.js +19 -0
- package/dist/src/server/createMockServer/createMockServer.d.ts +3 -0
- package/dist/src/server/createMockServer/createMockServer.js +58 -0
- package/dist/src/server/index.d.ts +2 -0
- package/dist/src/server/index.js +18 -0
- package/dist/src/server/startMockServer/startMockServer.d.ts +5 -0
- package/dist/src/server/startMockServer/startMockServer.js +20 -0
- package/dist/src/static/views/assets/icons/scheme-dark.svg +3 -0
- package/dist/src/static/views/assets/icons/scheme-light.svg +3 -0
- package/dist/src/static/views/assets/images/404.png +0 -0
- package/dist/src/static/views/assets/images/logo.png +0 -0
- package/dist/src/static/views/assets/styles/global.css +88 -0
- package/dist/src/static/views/components/header/index.css +55 -0
- package/dist/src/static/views/components/header/index.ejs +40 -0
- package/dist/src/static/views/components/header/index.js +1 -0
- package/dist/src/static/views/features/scheme/dark.css +12 -0
- package/dist/src/static/views/features/scheme/index.ejs +3 -0
- package/dist/src/static/views/features/scheme/index.js +31 -0
- package/dist/src/static/views/features/scheme/light.css +12 -0
- package/dist/src/static/views/features/tab/index.css +30 -0
- package/dist/src/static/views/features/tab/index.ejs +2 -0
- package/dist/src/static/views/features/tab/index.js +12 -0
- package/dist/src/static/views/pages/404/index.css +10 -0
- package/dist/src/static/views/pages/404/index.ejs +85 -0
- package/dist/src/utils/constants/appPath.d.ts +1 -0
- package/dist/src/utils/constants/appPath.js +4 -0
- package/dist/src/utils/constants/checkModes.d.ts +9 -0
- package/dist/src/utils/constants/checkModes.js +31 -0
- package/dist/src/utils/constants/default.d.ts +11 -0
- package/dist/src/utils/constants/default.js +14 -0
- package/dist/src/utils/constants/index.d.ts +3 -0
- package/dist/src/utils/constants/index.js +19 -0
- package/dist/src/utils/helpers/asyncHandler.d.ts +2 -0
- package/dist/src/utils/helpers/asyncHandler.js +5 -0
- package/dist/src/utils/helpers/config/index.d.ts +1 -0
- package/dist/src/utils/helpers/config/index.js +17 -0
- package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.d.ts +2 -0
- package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.js +131 -0
- package/dist/src/utils/helpers/entities/convertToEntityDescriptor/convertToEntityDescriptor.d.ts +2 -0
- package/dist/src/utils/helpers/entities/convertToEntityDescriptor/convertToEntityDescriptor.js +8 -0
- package/dist/src/utils/helpers/entities/index.d.ts +2 -0
- package/dist/src/utils/helpers/entities/index.js +18 -0
- package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.d.ts +1 -0
- package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.js +6 -0
- package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.d.ts +3 -0
- package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.js +21 -0
- package/dist/src/utils/helpers/graphql/index.d.ts +3 -0
- package/dist/src/utils/helpers/graphql/index.js +19 -0
- package/dist/src/utils/helpers/graphql/parseGraphQLRequest/parseGraphQLRequest.d.ts +3 -0
- package/dist/src/utils/helpers/graphql/parseGraphQLRequest/parseGraphQLRequest.js +12 -0
- package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.d.ts +7 -0
- package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.js +21 -0
- package/dist/src/utils/helpers/index.d.ts +10 -0
- package/dist/src/utils/helpers/index.js +26 -0
- package/dist/src/utils/helpers/interceptors/callRequestInterceptor/callRequestInterceptor.d.ts +8 -0
- package/dist/src/utils/helpers/interceptors/callRequestInterceptor/callRequestInterceptor.js +19 -0
- package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.d.ts +15 -0
- package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.js +52 -0
- package/dist/src/utils/helpers/interceptors/helpers/setDelay.d.ts +1 -0
- package/dist/src/utils/helpers/interceptors/helpers/setDelay.js +8 -0
- package/dist/src/utils/helpers/interceptors/index.d.ts +2 -0
- package/dist/src/utils/helpers/interceptors/index.js +18 -0
- package/dist/src/utils/helpers/isPlainObject/isPlainObject.d.ts +1 -0
- package/dist/src/utils/helpers/isPlainObject/isPlainObject.js +8 -0
- package/dist/src/utils/helpers/isPrimitive/isPrimitive.d.ts +2 -0
- package/dist/src/utils/helpers/isPrimitive/isPrimitive.js +5 -0
- package/dist/src/utils/helpers/isRegExp/isRegExp.d.ts +1 -0
- package/dist/src/utils/helpers/isRegExp/isRegExp.js +5 -0
- package/dist/src/utils/helpers/sleep.d.ts +1 -0
- package/dist/src/utils/helpers/sleep.js +7 -0
- package/dist/src/utils/helpers/url/convertWin32PathToUnix/convertWin32PathToUnix.d.ts +1 -0
- package/dist/src/utils/helpers/url/convertWin32PathToUnix/convertWin32PathToUnix.js +8 -0
- package/dist/src/utils/helpers/url/getUrlParts/getUrlParts.d.ts +1 -0
- package/dist/src/utils/helpers/url/getUrlParts/getUrlParts.js +6 -0
- package/dist/src/utils/helpers/url/index.d.ts +4 -0
- package/dist/src/utils/helpers/url/index.js +20 -0
- package/dist/src/utils/helpers/url/removeLeadingAndTrailingSlashes/removeLeadingAndTrailingSlashes.d.ts +1 -0
- package/dist/src/utils/helpers/url/removeLeadingAndTrailingSlashes/removeLeadingAndTrailingSlashes.js +5 -0
- package/dist/src/utils/helpers/url/urlJoin/urlJoin.d.ts +1 -0
- package/dist/src/utils/helpers/url/urlJoin/urlJoin.js +14 -0
- package/dist/src/utils/types/checkModes.d.ts +12 -0
- package/dist/src/utils/types/checkModes.js +2 -0
- package/dist/src/utils/types/graphql.d.ts +74 -0
- package/dist/src/utils/types/graphql.js +2 -0
- package/dist/src/utils/types/index.d.ts +6 -0
- package/dist/src/utils/types/index.js +22 -0
- package/dist/src/utils/types/interceptors.d.ts +31 -0
- package/dist/src/utils/types/interceptors.js +2 -0
- package/dist/src/utils/types/rest.d.ts +77 -0
- package/dist/src/utils/types/rest.js +2 -0
- package/dist/src/utils/types/server.d.ts +48 -0
- package/dist/src/utils/types/server.js +2 -0
- package/dist/src/utils/types/values.d.ts +4 -0
- package/dist/src/utils/types/values.js +2 -0
- package/package.json +2 -2
package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateRoutes/validateRoutes.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateRoutes = void 0;
|
|
4
|
+
const helpers_1 = require("../../../../src/utils/helpers");
|
|
5
|
+
const helpers_2 = require("../../helpers");
|
|
6
|
+
const validateInterceptors_1 = require("../../validateInterceptors/validateInterceptors");
|
|
7
|
+
const ALLOWED_ENTITIES_BY_OPERATION_TYPE = {
|
|
8
|
+
query: ['headers', 'cookies', 'query', 'variables'],
|
|
9
|
+
mutation: ['headers', 'cookies', 'query', 'variables']
|
|
10
|
+
};
|
|
11
|
+
const validateEntity = (entity, entityName) => {
|
|
12
|
+
const { checkMode: topLevelCheckMode, value: topLevelValue } = (0, helpers_1.convertToEntityDescriptor)(entity);
|
|
13
|
+
const isVariables = entityName === 'variables';
|
|
14
|
+
const isTopLevelDescriptor = (0, helpers_1.isEntityDescriptor)(entity);
|
|
15
|
+
if (isTopLevelDescriptor && isVariables) {
|
|
16
|
+
if (!(0, helpers_2.isCheckModeValid)(topLevelCheckMode, 'variables')) {
|
|
17
|
+
throw new Error('variables.checkMode');
|
|
18
|
+
}
|
|
19
|
+
if (!(0, helpers_2.isDescriptorValueValid)(topLevelCheckMode, topLevelValue, 'variables')) {
|
|
20
|
+
const errorMessage = 'variables.value';
|
|
21
|
+
throw new Error(errorMessage);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const isEntityObject = (0, helpers_1.isPlainObject)(entity) && !(entity instanceof RegExp);
|
|
25
|
+
const isEntityArray = Array.isArray(entity) && isVariables;
|
|
26
|
+
if (isEntityObject || isEntityArray) {
|
|
27
|
+
Object.entries(topLevelValue).forEach(([key, valueOrDescriptor]) => {
|
|
28
|
+
const { checkMode, value } = (0, helpers_1.convertToEntityDescriptor)(valueOrDescriptor);
|
|
29
|
+
if (!(0, helpers_2.isCheckModeValid)(checkMode)) {
|
|
30
|
+
throw new Error(`${entityName}.${key}.checkMode`);
|
|
31
|
+
}
|
|
32
|
+
const isDescriptor = (0, helpers_1.isEntityDescriptor)(valueOrDescriptor);
|
|
33
|
+
const errorMessage = `${entityName}.${key}${isDescriptor ? '.value' : ''}`;
|
|
34
|
+
const isValueArray = Array.isArray(value);
|
|
35
|
+
if (isValueArray && !isVariables) {
|
|
36
|
+
value.forEach((element, index) => {
|
|
37
|
+
if (!(0, helpers_2.isDescriptorValueValid)(checkMode, element)) {
|
|
38
|
+
throw new Error(`${errorMessage}[${index}]`);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (!(0, helpers_2.isDescriptorValueValid)(checkMode, value)) {
|
|
44
|
+
throw new Error(errorMessage);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
throw new Error(entityName);
|
|
50
|
+
};
|
|
51
|
+
const validateEntities = (entities, operationType) => {
|
|
52
|
+
const isEntitiesObject = (0, helpers_1.isPlainObject)(entities);
|
|
53
|
+
if (isEntitiesObject) {
|
|
54
|
+
Object.keys(entities).forEach((entityName) => {
|
|
55
|
+
const isEntityAllowed = ALLOWED_ENTITIES_BY_OPERATION_TYPE[operationType].includes(entityName);
|
|
56
|
+
if (!isEntityAllowed) {
|
|
57
|
+
throw new Error(`entities.${entityName}`);
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
validateEntity(entities[entityName], entityName);
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
throw new Error(`entities.${error.message}`);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (typeof entities !== 'undefined') {
|
|
69
|
+
throw new Error('entities');
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
const validateRoutes = (routes, operationType) => {
|
|
73
|
+
const isRoutesArray = Array.isArray(routes);
|
|
74
|
+
if (isRoutesArray) {
|
|
75
|
+
routes.forEach((route, index) => {
|
|
76
|
+
const isRouteObject = (0, helpers_1.isPlainObject)(route);
|
|
77
|
+
if (isRouteObject) {
|
|
78
|
+
const isRouteHasDataProperty = 'data' in route;
|
|
79
|
+
if (!isRouteHasDataProperty) {
|
|
80
|
+
throw new Error(`routes[${index}]`);
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
validateEntities(route.entities, operationType);
|
|
84
|
+
(0, validateInterceptors_1.validateInterceptors)(route.interceptors);
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
throw new Error(`routes[${index}].${error.message}`);
|
|
88
|
+
}
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
throw new Error(`routes[${index}]`);
|
|
92
|
+
});
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
throw new Error('routes');
|
|
96
|
+
};
|
|
97
|
+
exports.validateRoutes = validateRoutes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const validateInterceptors: (interceptors: unknown) => void;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateInterceptors = void 0;
|
|
4
|
+
const helpers_1 = require("../../../src/utils/helpers");
|
|
5
|
+
const validateInterceptors = (interceptors) => {
|
|
6
|
+
const isInterceptorsObject = (0, helpers_1.isPlainObject)(interceptors);
|
|
7
|
+
if (isInterceptorsObject) {
|
|
8
|
+
const { request, response } = interceptors;
|
|
9
|
+
if (typeof request !== 'function' && typeof request !== 'undefined') {
|
|
10
|
+
throw new Error('interceptors.request');
|
|
11
|
+
}
|
|
12
|
+
if (typeof response !== 'function' && typeof response !== 'undefined') {
|
|
13
|
+
throw new Error('interceptors.response');
|
|
14
|
+
}
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (typeof interceptors !== 'undefined') {
|
|
18
|
+
throw new Error('interceptors');
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
exports.validateInterceptors = validateInterceptors;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateMockServerConfig = void 0;
|
|
4
|
+
const validateBaseUrl_1 = require("./validateBaseUrl/validateBaseUrl");
|
|
5
|
+
const validateCors_1 = require("./validateCors/validateCors");
|
|
6
|
+
const validateGraphqlConfig_1 = require("./validateGraphqlConfig/validateGraphqlConfig");
|
|
7
|
+
const validateInterceptors_1 = require("./validateInterceptors/validateInterceptors");
|
|
8
|
+
const validatePort_1 = require("./validatePort/validatePort");
|
|
9
|
+
const validateRestConfig_1 = require("./validateRestConfig/validateRestConfig");
|
|
10
|
+
const validateStaticPath_1 = require("./validateStaticPath/validateStaticPath");
|
|
11
|
+
const validateMockServerConfig = (mockServerConfig) => {
|
|
12
|
+
if (!mockServerConfig.rest && !mockServerConfig.graphql) {
|
|
13
|
+
throw new Error('configuration should contain at least one of these configs: rest | graphql; see our doc (https://www.npmjs.com/package/mock-config-server) for more information');
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
if (mockServerConfig.rest)
|
|
17
|
+
(0, validateRestConfig_1.validateRestConfig)(mockServerConfig.rest);
|
|
18
|
+
if (mockServerConfig.graphql)
|
|
19
|
+
(0, validateGraphqlConfig_1.validateGraphqlConfig)(mockServerConfig.graphql);
|
|
20
|
+
(0, validateBaseUrl_1.validateBaseUrl)(mockServerConfig.baseUrl);
|
|
21
|
+
(0, validatePort_1.validatePort)(mockServerConfig.port);
|
|
22
|
+
(0, validateStaticPath_1.validateStaticPath)(mockServerConfig.staticPath);
|
|
23
|
+
(0, validateInterceptors_1.validateInterceptors)(mockServerConfig.interceptors);
|
|
24
|
+
(0, validateCors_1.validateCors)(mockServerConfig.cors);
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
throw new Error(`Validation Error: configuration.${error.message} does not match the API schema. Click here to see correct type: https://github.com/siberiacancode/mock-config-server`);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
exports.validateMockServerConfig = validateMockServerConfig;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const validatePort: (port: unknown) => void;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validatePort = void 0;
|
|
4
|
+
const validatePort = (port) => {
|
|
5
|
+
if (typeof port !== 'number' && typeof port !== 'undefined') {
|
|
6
|
+
throw new Error('port');
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
exports.validatePort = validatePort;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const validateRestConfig: (restConfig: unknown) => void;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateRestConfig = void 0;
|
|
4
|
+
const helpers_1 = require("../../../src/utils/helpers");
|
|
5
|
+
const validateBaseUrl_1 = require("../validateBaseUrl/validateBaseUrl");
|
|
6
|
+
const validateInterceptors_1 = require("../validateInterceptors/validateInterceptors");
|
|
7
|
+
const validateRoutes_1 = require("./validateRoutes/validateRoutes");
|
|
8
|
+
const validateConfigs = (configs) => {
|
|
9
|
+
const isConfigsArray = Array.isArray(configs);
|
|
10
|
+
if (isConfigsArray) {
|
|
11
|
+
configs.forEach((config, index) => {
|
|
12
|
+
const { path, method } = config;
|
|
13
|
+
const isPathStringWithLeadingSlash = typeof path === 'string' && path.startsWith('/');
|
|
14
|
+
if (!isPathStringWithLeadingSlash && !(path instanceof RegExp)) {
|
|
15
|
+
throw new Error(`configs[${index}].path`);
|
|
16
|
+
}
|
|
17
|
+
// ✅ important:
|
|
18
|
+
// compare without 'toLowerCase' because Express methods names is case-sensitive
|
|
19
|
+
const allowedMethods = ['get', 'post', 'delete', 'put', 'patch', 'options'];
|
|
20
|
+
const isMethodAllowed = typeof method === 'string' && allowedMethods.includes(method);
|
|
21
|
+
if (!isMethodAllowed) {
|
|
22
|
+
throw new Error(`configs[${index}].method`);
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
(0, validateRoutes_1.validateRoutes)(config.routes, method);
|
|
26
|
+
(0, validateInterceptors_1.validateInterceptors)(config.interceptors);
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
throw new Error(`configs[${index}].${error.message}`);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
throw new Error('configs');
|
|
35
|
+
};
|
|
36
|
+
const validateRestConfig = (restConfig) => {
|
|
37
|
+
const isRestConfigObject = (0, helpers_1.isPlainObject)(restConfig);
|
|
38
|
+
if (isRestConfigObject) {
|
|
39
|
+
try {
|
|
40
|
+
(0, validateBaseUrl_1.validateBaseUrl)(restConfig.baseUrl);
|
|
41
|
+
validateConfigs(restConfig.configs);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
throw new Error(`rest.${error.message}`);
|
|
45
|
+
}
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (typeof restConfig !== 'undefined') {
|
|
49
|
+
throw new Error('rest');
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
exports.validateRestConfig = validateRestConfig;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateRoutes = void 0;
|
|
4
|
+
const helpers_1 = require("../../../../src/utils/helpers");
|
|
5
|
+
const helpers_2 = require("../../helpers");
|
|
6
|
+
const validateInterceptors_1 = require("../../validateInterceptors/validateInterceptors");
|
|
7
|
+
const ALLOWED_ENTITIES_BY_METHOD = {
|
|
8
|
+
get: ['headers', 'cookies', 'query', 'params'],
|
|
9
|
+
delete: ['headers', 'cookies', 'query', 'params'],
|
|
10
|
+
post: ['headers', 'cookies', 'query', 'params', 'body'],
|
|
11
|
+
put: ['headers', 'cookies', 'query', 'params', 'body'],
|
|
12
|
+
patch: ['headers', 'cookies', 'query', 'params', 'body'],
|
|
13
|
+
options: ['headers', 'cookies', 'query', 'params']
|
|
14
|
+
};
|
|
15
|
+
const validateEntity = (entity, entityName) => {
|
|
16
|
+
const { checkMode: topLevelCheckMode, value: topLevelValue } = (0, helpers_1.convertToEntityDescriptor)(entity);
|
|
17
|
+
const isBody = entityName === 'body';
|
|
18
|
+
const isTopLevelDescriptor = (0, helpers_1.isEntityDescriptor)(entity);
|
|
19
|
+
if (isTopLevelDescriptor && isBody) {
|
|
20
|
+
if (!(0, helpers_2.isCheckModeValid)(topLevelCheckMode, 'body')) {
|
|
21
|
+
throw new Error('body.checkMode');
|
|
22
|
+
}
|
|
23
|
+
if (!(0, helpers_2.isDescriptorValueValid)(topLevelCheckMode, topLevelValue, 'body')) {
|
|
24
|
+
const errorMessage = 'body.value';
|
|
25
|
+
throw new Error(errorMessage);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const isEntityObject = (0, helpers_1.isPlainObject)(entity) && !(entity instanceof RegExp);
|
|
29
|
+
const isEntityArray = Array.isArray(entity) && isBody;
|
|
30
|
+
if (isEntityObject || isEntityArray) {
|
|
31
|
+
Object.entries(topLevelValue).forEach(([key, valueOrDescriptor]) => {
|
|
32
|
+
const { checkMode, value } = (0, helpers_1.convertToEntityDescriptor)(valueOrDescriptor);
|
|
33
|
+
if (!(0, helpers_2.isCheckModeValid)(checkMode)) {
|
|
34
|
+
throw new Error(`${entityName}.${key}.checkMode`);
|
|
35
|
+
}
|
|
36
|
+
const isDescriptor = (0, helpers_1.isEntityDescriptor)(valueOrDescriptor);
|
|
37
|
+
const errorMessage = `${entityName}.${key}${isDescriptor ? '.value' : ''}`;
|
|
38
|
+
const isValueArray = Array.isArray(value);
|
|
39
|
+
if (isValueArray && !isBody) {
|
|
40
|
+
value.forEach((element, index) => {
|
|
41
|
+
if (!(0, helpers_2.isDescriptorValueValid)(checkMode, element)) {
|
|
42
|
+
throw new Error(`${errorMessage}[${index}]`);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (!(0, helpers_2.isDescriptorValueValid)(checkMode, value)) {
|
|
48
|
+
throw new Error(errorMessage);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
throw new Error(entityName);
|
|
54
|
+
};
|
|
55
|
+
const validateEntities = (entities, method) => {
|
|
56
|
+
const isEntitiesObject = (0, helpers_1.isPlainObject)(entities);
|
|
57
|
+
if (isEntitiesObject) {
|
|
58
|
+
Object.keys(entities).forEach((entityName) => {
|
|
59
|
+
const isEntityAllowed = ALLOWED_ENTITIES_BY_METHOD[method].includes(entityName);
|
|
60
|
+
if (!isEntityAllowed) {
|
|
61
|
+
throw new Error(`entities.${entityName}`);
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
validateEntity(entities[entityName], entityName);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
throw new Error(`entities.${error.message}`);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (typeof entities !== 'undefined') {
|
|
73
|
+
throw new Error('entities');
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const validateRoutes = (routes, method) => {
|
|
77
|
+
const isRoutesArray = Array.isArray(routes);
|
|
78
|
+
if (isRoutesArray) {
|
|
79
|
+
routes.forEach((route, index) => {
|
|
80
|
+
const isRouteObject = (0, helpers_1.isPlainObject)(route);
|
|
81
|
+
if (isRouteObject) {
|
|
82
|
+
const isRouteHasDataProperty = 'data' in route;
|
|
83
|
+
if (!isRouteHasDataProperty) {
|
|
84
|
+
throw new Error(`routes[${index}]`);
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
validateEntities(route.entities, method);
|
|
88
|
+
(0, validateInterceptors_1.validateInterceptors)(route.interceptors);
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
throw new Error(`routes[${index}].${error.message}`);
|
|
92
|
+
}
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
throw new Error(`routes[${index}]`);
|
|
96
|
+
});
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
throw new Error('routes');
|
|
100
|
+
};
|
|
101
|
+
exports.validateRoutes = validateRoutes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const validateStaticPath: (staticPath: unknown) => void;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateStaticPath = void 0;
|
|
4
|
+
const helpers_1 = require("../../../src/utils/helpers");
|
|
5
|
+
const validateStaticPath = (staticPath) => {
|
|
6
|
+
const isStaticPathArray = Array.isArray(staticPath);
|
|
7
|
+
if (isStaticPathArray) {
|
|
8
|
+
staticPath.forEach((staticPathElement, index) => {
|
|
9
|
+
const isStaticPathElementObject = (0, helpers_1.isPlainObject)(staticPathElement);
|
|
10
|
+
if (isStaticPathElementObject) {
|
|
11
|
+
const { prefix, path } = staticPathElement;
|
|
12
|
+
if (typeof prefix !== 'string' || !prefix.startsWith('/')) {
|
|
13
|
+
throw new Error(`staticPath[${index}].prefix`);
|
|
14
|
+
}
|
|
15
|
+
if (typeof path !== 'string' || !path.startsWith('/')) {
|
|
16
|
+
throw new Error(`staticPath[${index}].path`);
|
|
17
|
+
}
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
if (typeof staticPathElement !== 'string' || !staticPathElement.startsWith('/')) {
|
|
21
|
+
throw new Error(`staticPath[${index}]`);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const isStaticPathObject = (0, helpers_1.isPlainObject)(staticPath);
|
|
27
|
+
if (isStaticPathObject) {
|
|
28
|
+
const { prefix, path } = staticPath;
|
|
29
|
+
if (typeof prefix !== 'string' || !prefix.startsWith('/')) {
|
|
30
|
+
throw new Error('staticPath.prefix');
|
|
31
|
+
}
|
|
32
|
+
if (typeof path !== 'string' || !path.startsWith('/')) {
|
|
33
|
+
throw new Error('staticPath.path');
|
|
34
|
+
}
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (typeof staticPath !== 'string' && typeof staticPath !== 'undefined') {
|
|
38
|
+
throw new Error('staticPath');
|
|
39
|
+
}
|
|
40
|
+
if (typeof staticPath === 'string' && !staticPath.startsWith('/')) {
|
|
41
|
+
throw new Error('staticPath');
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
exports.validateStaticPath = validateStaticPath;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./src"), exports);
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createGraphQLRoutes = void 0;
|
|
4
|
+
const flat_1 = require("flat");
|
|
5
|
+
const helpers_1 = require("../../../utils/helpers");
|
|
6
|
+
const helpers_2 = require("./helpers");
|
|
7
|
+
const createGraphQLRoutes = (router, graphqlConfig, serverResponseInterceptors) => {
|
|
8
|
+
const preparedGraphQLRequestConfig = (0, helpers_2.prepareGraphQLRequestConfigs)(graphqlConfig.configs);
|
|
9
|
+
const graphqlMiddleware = async (request, response, next) => {
|
|
10
|
+
const graphQLInput = (0, helpers_1.getGraphQLInput)(request);
|
|
11
|
+
if (!graphQLInput || !graphQLInput.query) {
|
|
12
|
+
return response
|
|
13
|
+
.status(400)
|
|
14
|
+
.json({ message: 'Query is missing, you must pass a valid GraphQL query' });
|
|
15
|
+
}
|
|
16
|
+
const query = (0, helpers_1.parseQuery)(graphQLInput.query);
|
|
17
|
+
if (!query) {
|
|
18
|
+
return response
|
|
19
|
+
.status(400)
|
|
20
|
+
.json({ message: 'Query is invalid, you must use a valid GraphQL query' });
|
|
21
|
+
}
|
|
22
|
+
if (!query.operationName || !query.operationType) {
|
|
23
|
+
return response.status(400).json({
|
|
24
|
+
message: `You should to specify operationName and operationType for ${request.method}:${request.baseUrl}${request.path}`
|
|
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
|
+
}
|
|
78
|
+
});
|
|
79
|
+
// ✅ important:
|
|
80
|
+
// set 'Cache-Control' header for explicit browsers response revalidate
|
|
81
|
+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
|
|
82
|
+
response.set('Cache-control', 'max-age=0, must-revalidate');
|
|
83
|
+
return response.status(response.statusCode).json(data);
|
|
84
|
+
};
|
|
85
|
+
router.route('/').get((0, helpers_1.asyncHandler)(graphqlMiddleware));
|
|
86
|
+
router.route('/').post((0, helpers_1.asyncHandler)(graphqlMiddleware));
|
|
87
|
+
return router;
|
|
88
|
+
};
|
|
89
|
+
exports.createGraphQLRoutes = createGraphQLRoutes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs"), exports);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.prepareGraphQLRequestConfigs = void 0;
|
|
4
|
+
const helpers_1 = require("../../../../../utils/helpers");
|
|
5
|
+
const calculateRouteConfigWeight = (graphQLRouteConfig) => {
|
|
6
|
+
const { entities } = graphQLRouteConfig;
|
|
7
|
+
if (!entities)
|
|
8
|
+
return 0;
|
|
9
|
+
let routeConfigWeight = 0;
|
|
10
|
+
const { headers, cookies, query, variables } = entities;
|
|
11
|
+
if (headers)
|
|
12
|
+
routeConfigWeight += Object.keys(headers).length;
|
|
13
|
+
if (cookies)
|
|
14
|
+
routeConfigWeight += Object.keys(cookies).length;
|
|
15
|
+
if (query)
|
|
16
|
+
routeConfigWeight += Object.keys(query).length;
|
|
17
|
+
if (variables) {
|
|
18
|
+
if ((0, helpers_1.isPlainObject)(variables) && variables.checkMode) {
|
|
19
|
+
routeConfigWeight += (0, helpers_1.isPlainObject)(variables.value) ? Object.keys(variables.value).length : 1;
|
|
20
|
+
return routeConfigWeight;
|
|
21
|
+
}
|
|
22
|
+
routeConfigWeight += Object.keys(variables).length;
|
|
23
|
+
}
|
|
24
|
+
return routeConfigWeight;
|
|
25
|
+
};
|
|
26
|
+
const prepareGraphQLRequestConfigs = (requestConfigs) => {
|
|
27
|
+
requestConfigs.forEach((requestConfig) => {
|
|
28
|
+
requestConfig.routes.sort((first, second) =>
|
|
29
|
+
// ✅ important:
|
|
30
|
+
// Lift more specific configs for correct working of routes
|
|
31
|
+
calculateRouteConfigWeight(second) - calculateRouteConfigWeight(first));
|
|
32
|
+
});
|
|
33
|
+
return requestConfigs;
|
|
34
|
+
};
|
|
35
|
+
exports.prepareGraphQLRequestConfigs = prepareGraphQLRequestConfigs;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './createGraphQLRoutes/createGraphQLRoutes';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./createGraphQLRoutes/createGraphQLRoutes"), exports);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cookieParseMiddleware = void 0;
|
|
4
|
+
const helpers_1 = require("./helpers");
|
|
5
|
+
const cookieParseMiddleware = (server) => {
|
|
6
|
+
server.use((request, _response, next) => {
|
|
7
|
+
if (request.headers.cookie) {
|
|
8
|
+
request.cookies = (0, helpers_1.parseCookie)(request.headers.cookie);
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
request.cookies = {};
|
|
12
|
+
}
|
|
13
|
+
return next();
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
exports.cookieParseMiddleware = cookieParseMiddleware;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './parseCookie/parseCookie';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./parseCookie/parseCookie"), exports);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseCookie = void 0;
|
|
4
|
+
const parseCookie = (cookieHeader) => {
|
|
5
|
+
if (!cookieHeader)
|
|
6
|
+
return {};
|
|
7
|
+
const cookies = {};
|
|
8
|
+
const cookiePairs = cookieHeader.split(';');
|
|
9
|
+
cookiePairs.forEach((cookie) => {
|
|
10
|
+
const [name, value] = cookie.trim().split('=');
|
|
11
|
+
if (!name)
|
|
12
|
+
return;
|
|
13
|
+
cookies[name.trim()] = value?.trim() ?? '';
|
|
14
|
+
});
|
|
15
|
+
return cookies;
|
|
16
|
+
};
|
|
17
|
+
exports.parseCookie = parseCookie;
|