mock-config-server 4.0.3 → 5.0.0-beta.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 +229 -237
- package/dist/bin/build.d.ts +3 -3
- package/dist/bin/build.js +1 -11
- package/dist/bin/helpers/createTemplate.js +6 -6
- package/dist/bin/helpers/resolveConfigFile.d.ts +2 -2
- package/dist/bin/helpers/resolveConfigFile.js +2 -3
- package/dist/bin/helpers/resolveConfigFilePath.js +4 -4
- package/dist/bin/init.d.ts +2 -2
- package/dist/bin/init.js +7 -7
- package/dist/bin/run.d.ts +3 -3
- package/dist/bin/run.js +17 -24
- package/dist/bin/templates/js/full/mock-requests/graphql/index.js +2 -2
- package/dist/bin/templates/js/full/mock-requests/graphql/user.js +35 -35
- package/dist/bin/templates/js/full/mock-requests/graphql/users.js +12 -12
- package/dist/bin/templates/js/full/mock-requests/rest/index.js +2 -2
- package/dist/bin/templates/js/full/mock-requests/rest/user.js +35 -35
- package/dist/bin/templates/js/full/mock-requests/rest/users.js +12 -12
- package/dist/bin/templates/js/full/mock-server.config.js +19 -19
- package/dist/bin/templates/js/graphql/mock-requests/index.js +2 -2
- package/dist/bin/templates/js/graphql/mock-requests/user.js +35 -35
- package/dist/bin/templates/js/graphql/mock-requests/users.js +12 -12
- package/dist/bin/templates/js/graphql/mock-server.config.js +14 -14
- package/dist/bin/templates/js/rest/mock-requests/index.js +2 -2
- package/dist/bin/templates/js/rest/mock-requests/user.js +35 -35
- package/dist/bin/templates/js/rest/mock-requests/users.js +12 -12
- package/dist/bin/templates/js/rest/mock-server.config.js +14 -14
- package/dist/bin/templates/ts/full/mock-requests/graphql/index.ts +2 -2
- package/dist/bin/templates/ts/full/mock-requests/graphql/user.ts +37 -37
- package/dist/bin/templates/ts/full/mock-requests/graphql/users.ts +14 -14
- package/dist/bin/templates/ts/full/mock-requests/rest/index.ts +2 -2
- package/dist/bin/templates/ts/full/mock-requests/rest/user.ts +37 -37
- package/dist/bin/templates/ts/full/mock-requests/rest/users.ts +14 -14
- package/dist/bin/templates/ts/full/mock-server.config.ts +21 -21
- package/dist/bin/templates/ts/graphql/mock-requests/index.ts +2 -2
- package/dist/bin/templates/ts/graphql/mock-requests/user.ts +37 -37
- package/dist/bin/templates/ts/graphql/mock-requests/users.ts +14 -14
- package/dist/bin/templates/ts/graphql/mock-server.config.ts +16 -16
- package/dist/bin/templates/ts/rest/mock-requests/index.ts +2 -2
- package/dist/bin/templates/ts/rest/mock-requests/user.ts +37 -37
- package/dist/bin/templates/ts/rest/mock-requests/users.ts +14 -14
- package/dist/bin/templates/ts/rest/mock-server.config.ts +16 -16
- package/dist/src/core/database/createDatabaseRoutes/helpers/search/search.js +3 -3
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileStorage.js +4 -4
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileWriter.js +2 -2
- package/dist/src/core/functions/graphql/graphql.d.ts +54 -0
- package/dist/src/core/functions/graphql/graphql.js +126 -0
- package/dist/src/core/functions/graphql/helpers/createQueueHandler/createQueueHandler.d.ts +5 -0
- package/dist/src/core/functions/graphql/helpers/createQueueHandler/createQueueHandler.js +35 -0
- package/dist/src/core/functions/graphql/helpers/index.d.ts +1 -0
- package/dist/src/core/{rest/createRestRoutes → functions/graphql}/helpers/index.js +1 -1
- package/dist/src/core/functions/index.d.ts +3 -0
- package/dist/src/core/functions/index.js +20 -0
- package/dist/src/core/functions/mock.d.ts +2 -0
- package/dist/src/core/functions/mock.js +11 -0
- package/dist/src/core/functions/rest/helpers/createFileHandler/createFileHandler.d.ts +2 -0
- package/dist/src/core/functions/rest/helpers/createFileHandler/createFileHandler.js +29 -0
- package/dist/src/core/functions/rest/helpers/createQueueHandler/createQueueHandler.d.ts +5 -0
- package/dist/src/core/functions/rest/helpers/createQueueHandler/createQueueHandler.js +35 -0
- package/dist/src/core/functions/rest/helpers/formatSsePayload/formatSsePayload.d.ts +5 -0
- package/dist/src/core/functions/rest/helpers/formatSsePayload/formatSsePayload.js +50 -0
- package/dist/src/core/functions/rest/helpers/index.d.ts +3 -0
- package/dist/src/core/functions/rest/helpers/index.js +20 -0
- package/dist/src/core/functions/rest/index.d.ts +1 -0
- package/dist/src/core/{graphql/createGraphQLRoutes/helpers → functions/rest}/index.js +1 -1
- package/dist/src/core/functions/rest/rest.d.ts +102 -0
- package/dist/src/core/functions/rest/rest.js +165 -0
- package/dist/src/core/graphql/createGraphQLRoute/createGraphQLRoute.d.ts +8 -0
- package/dist/src/core/graphql/createGraphQLRoute/createGraphQLRoute.js +158 -0
- package/dist/src/core/graphql/createGraphQLRoute/helpers/calculateGraphQLRouteConfigWeight/calculateGraphQLRouteConfigWeight.d.ts +2 -0
- package/dist/src/core/graphql/{createGraphQLRoutes/helpers/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.js → createGraphQLRoute/helpers/calculateGraphQLRouteConfigWeight/calculateGraphQLRouteConfigWeight.js} +3 -11
- package/dist/src/core/graphql/createGraphQLRoute/helpers/index.d.ts +3 -0
- package/dist/src/core/graphql/createGraphQLRoute/helpers/index.js +20 -0
- package/dist/src/core/graphql/createGraphQLRoute/helpers/matchGraphQLRequestArtifacts/matchGraphQLRequestArtifacts.d.ts +12 -0
- package/dist/src/core/graphql/createGraphQLRoute/helpers/matchGraphQLRequestArtifacts/matchGraphQLRequestArtifacts.js +28 -0
- package/dist/src/core/graphql/createGraphQLRoute/helpers/prepareGraphQLRequestArtifacts/prepareGraphQLRequestArtifacts.d.ts +2 -0
- package/dist/src/core/graphql/createGraphQLRoute/helpers/prepareGraphQLRequestArtifacts/prepareGraphQLRequestArtifacts.js +11 -0
- package/dist/src/core/graphql/index.d.ts +2 -1
- package/dist/src/core/graphql/index.js +2 -1
- package/dist/src/core/middlewares/contextMiddleware/contextMiddleware.d.ts +6 -4
- package/dist/src/core/rest/createRestRoute/createRestRoute.d.ts +8 -0
- package/dist/src/core/rest/createRestRoute/createRestRoute.js +190 -0
- package/dist/src/core/rest/createRestRoute/helpers/calculateRestRouteConfigWeight/calculateRestRouteConfigWeight.d.ts +2 -0
- package/dist/src/core/rest/createRestRoute/helpers/calculateRestRouteConfigWeight/calculateRestRouteConfigWeight.js +35 -0
- package/dist/src/core/rest/createRestRoute/helpers/index.d.ts +3 -0
- package/dist/src/core/rest/createRestRoute/helpers/index.js +20 -0
- package/dist/src/core/rest/createRestRoute/helpers/matchRestRequestArtifacts/matchRestRequestArtifacts.d.ts +11 -0
- package/dist/src/core/rest/createRestRoute/helpers/matchRestRequestArtifacts/matchRestRequestArtifacts.js +31 -0
- package/dist/src/core/rest/createRestRoute/helpers/prepareRestRequestArtifacts/prepareRestRequestArtifacts.d.ts +2 -0
- package/dist/src/core/rest/createRestRoute/helpers/prepareRestRequestArtifacts/prepareRestRequestArtifacts.js +37 -0
- package/dist/src/core/rest/index.d.ts +2 -1
- package/dist/src/core/rest/index.js +2 -1
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/server/createMockServer/createMockServer.d.ts +1 -1
- package/dist/src/server/createMockServer/createMockServer.js +76 -45
- package/dist/src/server/index.d.ts +0 -8
- package/dist/src/server/index.js +0 -8
- package/dist/src/server/startMockServer/startMockServer.d.ts +2 -2
- package/dist/src/server/startMockServer/startMockServer.js +4 -3
- package/dist/src/utils/constants/checkModes.js +7 -7
- package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.js +1 -1
- package/dist/src/utils/helpers/files/isFileDescriptor/isFileDescriptor.js +2 -2
- package/dist/src/utils/helpers/files/isFilePathValid/isFilePathValid.js +3 -3
- package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.d.ts +3 -3
- package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.js +2 -2
- package/dist/src/utils/helpers/tests/createTmpDir.js +4 -4
- package/dist/src/utils/helpers/url/index.d.ts +1 -2
- package/dist/src/utils/helpers/url/index.js +1 -2
- package/dist/src/utils/helpers/url/normalizeUrl/normalizeUrl.d.ts +1 -0
- package/dist/src/utils/helpers/url/normalizeUrl/normalizeUrl.js +15 -0
- package/dist/src/utils/helpers/url/urlJoin/urlJoin.js +4 -4
- package/dist/src/utils/types/graphql.d.ts +44 -18
- package/dist/src/utils/types/rest.d.ts +44 -27
- package/dist/src/utils/types/server.d.ts +7 -24
- package/dist/src/utils/types/values.d.ts +6 -1
- package/dist/src/utils/validate/graphqlConfigSchema/graphqlConfigSchema.d.ts +41 -383
- package/dist/src/utils/validate/graphqlConfigSchema/graphqlConfigSchema.js +3 -3
- package/dist/src/utils/validate/graphqlConfigSchema/routeConfigSchema/routeConfigSchema.d.ts +3 -66
- package/dist/src/utils/validate/graphqlConfigSchema/routeConfigSchema/routeConfigSchema.js +7 -22
- package/dist/src/utils/validate/index.d.ts +0 -2
- package/dist/src/utils/validate/index.js +0 -2
- package/dist/src/utils/validate/queueSchema/queueSchema.d.ts +2 -11
- package/dist/src/utils/validate/queueSchema/queueSchema.js +7 -13
- package/dist/src/utils/validate/restConfigSchema/restConfigSchema.d.ts +101 -1309
- package/dist/src/utils/validate/restConfigSchema/restConfigSchema.js +3 -3
- package/dist/src/utils/validate/restConfigSchema/routeConfigSchema/routeConfigSchema.d.ts +3 -82
- package/dist/src/utils/validate/restConfigSchema/routeConfigSchema/routeConfigSchema.js +6 -28
- package/dist/src/utils/validate/settingsSchema/settingsSchema.d.ts +0 -3
- package/dist/src/utils/validate/settingsSchema/settingsSchema.js +0 -1
- package/dist/src/utils/validate/utils/checkModeSchema/checkModeSchema.js +6 -6
- package/dist/src/utils/validate/utils/entitiesSchema/entitiesSchema.js +4 -4
- package/dist/src/utils/validate/utils/jsonSchema/jsonSchema.js +3 -3
- package/dist/src/utils/validate/utils/requiredPropertiesSchema/requiredPropertiesSchema.js +1 -1
- package/dist/src/utils/validate/utils/sharedSchema/sharedSchema.js +3 -3
- package/dist/src/utils/validate/validateMockServerConfig.js +18 -8
- package/package.json +17 -40
- package/LICENSE +0 -21
- package/dist/bin/runFlatConfig.d.ts +0 -5
- package/dist/bin/runFlatConfig.js +0 -36
- package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.d.ts +0 -9
- package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.js +0 -160
- package/dist/src/core/graphql/createGraphQLRoutes/helpers/index.d.ts +0 -1
- package/dist/src/core/graphql/createGraphQLRoutes/helpers/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.d.ts +0 -2
- package/dist/src/core/rest/createRestRoutes/createRestRoutes.d.ts +0 -9
- package/dist/src/core/rest/createRestRoutes/createRestRoutes.js +0 -184
- package/dist/src/core/rest/createRestRoutes/helpers/index.d.ts +0 -1
- package/dist/src/core/rest/createRestRoutes/helpers/prepareRestRequestConfigs/prepareRestRequestConfigs.d.ts +0 -2
- package/dist/src/core/rest/createRestRoutes/helpers/prepareRestRequestConfigs/prepareRestRequestConfigs.js +0 -67
- package/dist/src/server/createDatabaseMockServer/createDatabaseMockServer.d.ts +0 -3
- package/dist/src/server/createDatabaseMockServer/createDatabaseMockServer.js +0 -62
- package/dist/src/server/createFlatMockServer/createFlatMockServer.d.ts +0 -3
- package/dist/src/server/createFlatMockServer/createFlatMockServer.js +0 -128
- package/dist/src/server/createGraphQLMockServer/createGraphQLMockServer.d.ts +0 -3
- package/dist/src/server/createGraphQLMockServer/createGraphQLMockServer.js +0 -67
- package/dist/src/server/createRestMockServer/createRestMockServer.d.ts +0 -3
- package/dist/src/server/createRestMockServer/createRestMockServer.js +0 -67
- package/dist/src/server/startDatabaseMockServer/startDatabaseMockServer.d.ts +0 -4
- package/dist/src/server/startDatabaseMockServer/startDatabaseMockServer.js +0 -29
- package/dist/src/server/startFlatMockServer/startFlatMockServer.d.ts +0 -4
- package/dist/src/server/startFlatMockServer/startFlatMockServer.js +0 -30
- package/dist/src/server/startGraphQLMockServer/startGraphQLMockServer.d.ts +0 -4
- package/dist/src/server/startGraphQLMockServer/startGraphQLMockServer.js +0 -29
- package/dist/src/server/startRestMockServer/startRestMockServer.d.ts +0 -4
- package/dist/src/server/startRestMockServer/startRestMockServer.js +0 -29
- package/dist/src/utils/helpers/url/getUrlParts/getUrlParts.d.ts +0 -1
- package/dist/src/utils/helpers/url/getUrlParts/getUrlParts.js +0 -12
- package/dist/src/utils/helpers/url/removeLeadingAndTrailingSlashes/removeLeadingAndTrailingSlashes.d.ts +0 -1
- package/dist/src/utils/helpers/url/removeLeadingAndTrailingSlashes/removeLeadingAndTrailingSlashes.js +0 -11
- package/dist/src/utils/validate/isOnlyRequestedDataResolvingPropertyExists.d.ts +0 -4
- package/dist/src/utils/validate/isOnlyRequestedDataResolvingPropertyExists.js +0 -16
- package/dist/src/utils/validate/validateApiMockServerConfig.d.ts +0 -2
- package/dist/src/utils/validate/validateApiMockServerConfig.js +0 -48
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "createGraphQLRoutes", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return createGraphQLRoutes;
|
|
9
|
-
}
|
|
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
|
-
});
|
|
23
|
-
}
|
|
24
|
-
const query = (0, _helpers.parseQuery)(graphQLInput.query);
|
|
25
|
-
if (!query) {
|
|
26
|
-
return response.status(400).json({
|
|
27
|
-
message: 'Query is invalid, you must use a valid GraphQL query'
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
const matchedRequestConfig = preparedGraphQLRequestConfig.find((requestConfig)=>{
|
|
31
|
-
var _graphQLInput_query;
|
|
32
|
-
if (requestConfig.operationType !== query.operationType) return false;
|
|
33
|
-
if ('query' in requestConfig && requestConfig.query.replace(/\s+/g, '') !== ((_graphQLInput_query = graphQLInput.query) === null || _graphQLInput_query === void 0 ? void 0 : _graphQLInput_query.replace(/\s+/g, ''))) return false;
|
|
34
|
-
if ('operationName' in requestConfig) {
|
|
35
|
-
if (!query.operationName) return false;
|
|
36
|
-
return requestConfig.operationName instanceof RegExp ? new RegExp(requestConfig.operationName).test(query.operationName) : requestConfig.operationName === query.operationName;
|
|
37
|
-
}
|
|
38
|
-
return true;
|
|
39
|
-
});
|
|
40
|
-
if (!matchedRequestConfig) {
|
|
41
|
-
return next();
|
|
42
|
-
}
|
|
43
|
-
if ((_matchedRequestConfig_interceptors = matchedRequestConfig.interceptors) === null || _matchedRequestConfig_interceptors === void 0 ? void 0 : _matchedRequestConfig_interceptors.request) {
|
|
44
|
-
await (0, _helpers.callRequestInterceptor)({
|
|
45
|
-
request,
|
|
46
|
-
interceptor: matchedRequestConfig.interceptors.request
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
const matchedRouteConfig = matchedRequestConfig.routes.find(({ entities })=>{
|
|
50
|
-
if (!entities) return true;
|
|
51
|
-
const entityEntries = Object.entries(entities);
|
|
52
|
-
return entityEntries.every(([entityName, entityDescriptorOrValue])=>{
|
|
53
|
-
// ✅ important:
|
|
54
|
-
// check whole variables as plain value strictly if descriptor used for variables
|
|
55
|
-
const isEntityVariablesByTopLevelDescriptor = entityName === 'variables' && (0, _helpers.isEntityDescriptor)(entityDescriptorOrValue);
|
|
56
|
-
if (isEntityVariablesByTopLevelDescriptor) {
|
|
57
|
-
const variablesDescriptor = entityDescriptorOrValue;
|
|
58
|
-
if (variablesDescriptor.checkMode === 'exists' || variablesDescriptor.checkMode === 'notExists') {
|
|
59
|
-
return (0, _helpers.resolveEntityValues)({
|
|
60
|
-
actualValue: graphQLInput.variables,
|
|
61
|
-
checkMode: variablesDescriptor.checkMode
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
var _variablesDescriptor_oneOf;
|
|
65
|
-
return (0, _helpers.resolveEntityValues)({
|
|
66
|
-
actualValue: graphQLInput.variables,
|
|
67
|
-
descriptorValue: variablesDescriptor.value,
|
|
68
|
-
checkMode: variablesDescriptor.checkMode,
|
|
69
|
-
oneOf: (_variablesDescriptor_oneOf = variablesDescriptor.oneOf) !== null && _variablesDescriptor_oneOf !== void 0 ? _variablesDescriptor_oneOf : false
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
const actualEntity = (0, _flat.flatten)(entityName === 'variables' ? graphQLInput.variables : request[entityName]);
|
|
73
|
-
const entityValueEntries = Object.entries(entityDescriptorOrValue);
|
|
74
|
-
return entityValueEntries.every(([entityPropertyKey, entityPropertyDescriptorOrValue])=>{
|
|
75
|
-
const entityPropertyDescriptor = (0, _helpers.convertToEntityDescriptor)(entityPropertyDescriptorOrValue);
|
|
76
|
-
// ✅ important: transform header keys to lower case because browsers send headers in lowercase
|
|
77
|
-
const actualPropertyKey = entityName === 'headers' ? entityPropertyKey.toLowerCase() : entityPropertyKey;
|
|
78
|
-
const actualPropertyValue = actualEntity[actualPropertyKey];
|
|
79
|
-
if (entityPropertyDescriptor.checkMode === 'exists' || entityPropertyDescriptor.checkMode === 'notExists') {
|
|
80
|
-
return (0, _helpers.resolveEntityValues)({
|
|
81
|
-
actualValue: actualPropertyValue,
|
|
82
|
-
checkMode: entityPropertyDescriptor.checkMode
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
var _entityPropertyDescriptor_oneOf;
|
|
86
|
-
return (0, _helpers.resolveEntityValues)({
|
|
87
|
-
actualValue: actualPropertyValue,
|
|
88
|
-
descriptorValue: entityPropertyDescriptor.value,
|
|
89
|
-
checkMode: entityPropertyDescriptor.checkMode,
|
|
90
|
-
oneOf: (_entityPropertyDescriptor_oneOf = entityPropertyDescriptor.oneOf) !== null && _entityPropertyDescriptor_oneOf !== void 0 ? _entityPropertyDescriptor_oneOf : false
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
if (!matchedRouteConfig) return next();
|
|
96
|
-
if ((_matchedRouteConfig_interceptors = matchedRouteConfig.interceptors) === null || _matchedRouteConfig_interceptors === void 0 ? void 0 : _matchedRouteConfig_interceptors.request) {
|
|
97
|
-
await (0, _helpers.callRequestInterceptor)({
|
|
98
|
-
request,
|
|
99
|
-
interceptor: matchedRouteConfig.interceptors.request
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
let matchedRouteConfigData = null;
|
|
103
|
-
if (((_matchedRouteConfig_settings = matchedRouteConfig.settings) === null || _matchedRouteConfig_settings === void 0 ? void 0 : _matchedRouteConfig_settings.polling) && 'queue' in matchedRouteConfig) {
|
|
104
|
-
if (!matchedRouteConfig.queue.length) return next();
|
|
105
|
-
const shallowMatchedRouteConfig = matchedRouteConfig;
|
|
106
|
-
var _shallowMatchedRouteConfig___pollingIndex;
|
|
107
|
-
let index = (_shallowMatchedRouteConfig___pollingIndex = shallowMatchedRouteConfig.__pollingIndex) !== null && _shallowMatchedRouteConfig___pollingIndex !== void 0 ? _shallowMatchedRouteConfig___pollingIndex : 0;
|
|
108
|
-
const { time, data } = matchedRouteConfig.queue[index];
|
|
109
|
-
const updateIndex = ()=>{
|
|
110
|
-
if (matchedRouteConfig.queue.length - 1 === index) {
|
|
111
|
-
index = 0;
|
|
112
|
-
} else {
|
|
113
|
-
index += 1;
|
|
114
|
-
}
|
|
115
|
-
shallowMatchedRouteConfig.__pollingIndex = index;
|
|
116
|
-
};
|
|
117
|
-
if (time && !shallowMatchedRouteConfig.__timeoutInProgress) {
|
|
118
|
-
shallowMatchedRouteConfig.__timeoutInProgress = true;
|
|
119
|
-
setTimeout(()=>{
|
|
120
|
-
shallowMatchedRouteConfig.__timeoutInProgress = false;
|
|
121
|
-
updateIndex();
|
|
122
|
-
}, time);
|
|
123
|
-
}
|
|
124
|
-
if (!time && !shallowMatchedRouteConfig.__timeoutInProgress) {
|
|
125
|
-
updateIndex();
|
|
126
|
-
}
|
|
127
|
-
matchedRouteConfigData = data;
|
|
128
|
-
}
|
|
129
|
-
if ('data' in matchedRouteConfig) {
|
|
130
|
-
matchedRouteConfigData = matchedRouteConfig.data;
|
|
131
|
-
}
|
|
132
|
-
var _matchedRouteConfig_entities;
|
|
133
|
-
const resolvedData = typeof matchedRouteConfigData === 'function' ? await matchedRouteConfigData(request, (_matchedRouteConfig_entities = matchedRouteConfig.entities) !== null && _matchedRouteConfig_entities !== void 0 ? _matchedRouteConfig_entities : {}) : matchedRouteConfigData;
|
|
134
|
-
if ((_matchedRouteConfig_settings1 = matchedRouteConfig.settings) === null || _matchedRouteConfig_settings1 === void 0 ? void 0 : _matchedRouteConfig_settings1.status) {
|
|
135
|
-
response.statusCode = matchedRouteConfig.settings.status;
|
|
136
|
-
}
|
|
137
|
-
// ✅ important:
|
|
138
|
-
// set 'Cache-Control' header for explicit browsers response revalidate: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
|
|
139
|
-
// this code should place before response interceptors for giving opportunity to rewrite 'Cache-Control' header
|
|
140
|
-
if (matchedRequestConfig.operationType === 'query') response.set('Cache-control', 'no-cache');
|
|
141
|
-
const data = await (0, _helpers.callResponseInterceptors)({
|
|
142
|
-
data: resolvedData,
|
|
143
|
-
request,
|
|
144
|
-
response,
|
|
145
|
-
interceptors: {
|
|
146
|
-
routeInterceptor: (_matchedRouteConfig_interceptors1 = matchedRouteConfig.interceptors) === null || _matchedRouteConfig_interceptors1 === void 0 ? void 0 : _matchedRouteConfig_interceptors1.response,
|
|
147
|
-
requestInterceptor: (_matchedRequestConfig_interceptors1 = matchedRequestConfig.interceptors) === null || _matchedRequestConfig_interceptors1 === void 0 ? void 0 : _matchedRequestConfig_interceptors1.response,
|
|
148
|
-
apiInterceptor: (_graphqlConfig_interceptors = graphqlConfig.interceptors) === null || _graphqlConfig_interceptors === void 0 ? void 0 : _graphqlConfig_interceptors.response,
|
|
149
|
-
serverInterceptor: serverResponseInterceptor
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
if ((_matchedRouteConfig_settings2 = matchedRouteConfig.settings) === null || _matchedRouteConfig_settings2 === void 0 ? void 0 : _matchedRouteConfig_settings2.delay) {
|
|
153
|
-
await (0, _helpers.sleep)(matchedRouteConfig.settings.delay);
|
|
154
|
-
}
|
|
155
|
-
return response.json(data);
|
|
156
|
-
};
|
|
157
|
-
router.route('/').get((0, _helpers.asyncHandler)(graphqlMiddleware));
|
|
158
|
-
router.route('/').post((0, _helpers.asyncHandler)(graphqlMiddleware));
|
|
159
|
-
return router;
|
|
160
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs';
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { IRouter } from 'express';
|
|
2
|
-
import type { Interceptors, RestConfig } from '../../../utils/types';
|
|
3
|
-
interface CreateRestRoutesParams {
|
|
4
|
-
restConfig: RestConfig;
|
|
5
|
-
router: IRouter;
|
|
6
|
-
serverResponseInterceptor?: Interceptors<'rest'>['response'];
|
|
7
|
-
}
|
|
8
|
-
export declare const createRestRoutes: ({ router, restConfig, serverResponseInterceptor }: CreateRestRoutesParams) => IRouter;
|
|
9
|
-
export {};
|
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "createRestRoutes", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return createRestRoutes;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _flat = require("flat");
|
|
12
|
-
const _nodefs = /*#__PURE__*/ _interop_require_default(require("node:fs"));
|
|
13
|
-
const _nodepath = /*#__PURE__*/ _interop_require_default(require("node:path"));
|
|
14
|
-
const _helpers = require("../../../utils/helpers");
|
|
15
|
-
const _helpers1 = require("./helpers");
|
|
16
|
-
function _interop_require_default(obj) {
|
|
17
|
-
return obj && obj.__esModule ? obj : {
|
|
18
|
-
default: obj
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
const createRestRoutes = ({ router, restConfig, serverResponseInterceptor })=>{
|
|
22
|
-
(0, _helpers1.prepareRestRequestConfigs)(restConfig.configs).forEach((requestConfig)=>{
|
|
23
|
-
router.route(requestConfig.path)[requestConfig.method]((0, _helpers.asyncHandler)(async (request, response, next)=>{
|
|
24
|
-
var _requestConfig_interceptors, _matchedRouteConfig_interceptors, _matchedRouteConfig_settings, _matchedRouteConfig_settings1, _matchedRouteConfig_interceptors1, _requestConfig_interceptors1, _restConfig_interceptors, _matchedRouteConfig_settings2;
|
|
25
|
-
if ((_requestConfig_interceptors = requestConfig.interceptors) === null || _requestConfig_interceptors === void 0 ? void 0 : _requestConfig_interceptors.request) {
|
|
26
|
-
await (0, _helpers.callRequestInterceptor)({
|
|
27
|
-
request,
|
|
28
|
-
interceptor: requestConfig.interceptors.request
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
const matchedRouteConfig = requestConfig.routes.find(({ entities })=>{
|
|
32
|
-
if (!entities) return true;
|
|
33
|
-
const entityEntries = Object.entries(entities);
|
|
34
|
-
return entityEntries.every(([entityName, entityDescriptorOrValue])=>{
|
|
35
|
-
// ✅ important:
|
|
36
|
-
// check whole body as plain value strictly if descriptor used for body
|
|
37
|
-
const isEntityBodyByTopLevelDescriptor = entityName === 'body' && (0, _helpers.isEntityDescriptor)(entityDescriptorOrValue);
|
|
38
|
-
if (isEntityBodyByTopLevelDescriptor) {
|
|
39
|
-
const bodyDescriptor = entityDescriptorOrValue;
|
|
40
|
-
if (bodyDescriptor.checkMode === 'exists' || bodyDescriptor.checkMode === 'notExists') {
|
|
41
|
-
return (0, _helpers.resolveEntityValues)({
|
|
42
|
-
actualValue: request.body,
|
|
43
|
-
checkMode: bodyDescriptor.checkMode
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
var _bodyDescriptor_oneOf;
|
|
47
|
-
return (0, _helpers.resolveEntityValues)({
|
|
48
|
-
actualValue: request.body,
|
|
49
|
-
descriptorValue: bodyDescriptor.value,
|
|
50
|
-
checkMode: bodyDescriptor.checkMode,
|
|
51
|
-
oneOf: (_bodyDescriptor_oneOf = bodyDescriptor.oneOf) !== null && _bodyDescriptor_oneOf !== void 0 ? _bodyDescriptor_oneOf : false
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
const isEntityBodyByTopLevelArray = entityName === 'body' && Array.isArray(entityDescriptorOrValue);
|
|
55
|
-
if (isEntityBodyByTopLevelArray) {
|
|
56
|
-
if (!Array.isArray(request.body)) return false;
|
|
57
|
-
return (0, _helpers.resolveEntityValues)({
|
|
58
|
-
actualValue: request.body,
|
|
59
|
-
descriptorValue: entityDescriptorOrValue,
|
|
60
|
-
checkMode: 'equals'
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
const actualEntity = (0, _flat.flatten)(request[entityName]);
|
|
64
|
-
const entityValueEntries = Object.entries(entityDescriptorOrValue);
|
|
65
|
-
return entityValueEntries.every(([entityPropertyKey, entityPropertyDescriptorOrValue])=>{
|
|
66
|
-
const entityPropertyDescriptor = (0, _helpers.convertToEntityDescriptor)(entityPropertyDescriptorOrValue);
|
|
67
|
-
// ✅ important: transform header keys to lower case because browsers send headers in lowercase
|
|
68
|
-
const actualPropertyKey = entityName === 'headers' ? entityPropertyKey.toLowerCase() : entityPropertyKey;
|
|
69
|
-
const actualPropertyValue = actualEntity[actualPropertyKey];
|
|
70
|
-
if (entityPropertyDescriptor.checkMode === 'exists' || entityPropertyDescriptor.checkMode === 'notExists') {
|
|
71
|
-
return (0, _helpers.resolveEntityValues)({
|
|
72
|
-
actualValue: actualPropertyValue,
|
|
73
|
-
checkMode: entityPropertyDescriptor.checkMode
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
var _entityPropertyDescriptor_oneOf;
|
|
77
|
-
return (0, _helpers.resolveEntityValues)({
|
|
78
|
-
actualValue: actualPropertyValue,
|
|
79
|
-
descriptorValue: entityPropertyDescriptor.value,
|
|
80
|
-
checkMode: entityPropertyDescriptor.checkMode,
|
|
81
|
-
oneOf: (_entityPropertyDescriptor_oneOf = entityPropertyDescriptor.oneOf) !== null && _entityPropertyDescriptor_oneOf !== void 0 ? _entityPropertyDescriptor_oneOf : false
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
if (!matchedRouteConfig) return next();
|
|
87
|
-
if ((_matchedRouteConfig_interceptors = matchedRouteConfig.interceptors) === null || _matchedRouteConfig_interceptors === void 0 ? void 0 : _matchedRouteConfig_interceptors.request) {
|
|
88
|
-
await (0, _helpers.callRequestInterceptor)({
|
|
89
|
-
request,
|
|
90
|
-
interceptor: matchedRouteConfig.interceptors.request
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
const matchedRouteConfigDataDescriptor = {};
|
|
94
|
-
if (((_matchedRouteConfig_settings = matchedRouteConfig.settings) === null || _matchedRouteConfig_settings === void 0 ? void 0 : _matchedRouteConfig_settings.polling) && 'queue' in matchedRouteConfig) {
|
|
95
|
-
if (!matchedRouteConfig.queue.length) return next();
|
|
96
|
-
const shallowMatchedRouteConfig = matchedRouteConfig;
|
|
97
|
-
var _shallowMatchedRouteConfig___pollingIndex;
|
|
98
|
-
let index = (_shallowMatchedRouteConfig___pollingIndex = shallowMatchedRouteConfig.__pollingIndex) !== null && _shallowMatchedRouteConfig___pollingIndex !== void 0 ? _shallowMatchedRouteConfig___pollingIndex : 0;
|
|
99
|
-
const { time } = matchedRouteConfig.queue[index];
|
|
100
|
-
const updateIndex = ()=>{
|
|
101
|
-
if (matchedRouteConfig.queue.length - 1 === index) {
|
|
102
|
-
index = 0;
|
|
103
|
-
} else {
|
|
104
|
-
index += 1;
|
|
105
|
-
}
|
|
106
|
-
shallowMatchedRouteConfig.__pollingIndex = index;
|
|
107
|
-
};
|
|
108
|
-
const queueItem = matchedRouteConfig.queue[index];
|
|
109
|
-
if (time && !shallowMatchedRouteConfig.__timeoutInProgress) {
|
|
110
|
-
shallowMatchedRouteConfig.__timeoutInProgress = true;
|
|
111
|
-
setTimeout(()=>{
|
|
112
|
-
shallowMatchedRouteConfig.__timeoutInProgress = false;
|
|
113
|
-
updateIndex();
|
|
114
|
-
}, time);
|
|
115
|
-
}
|
|
116
|
-
if (!time && !shallowMatchedRouteConfig.__timeoutInProgress) {
|
|
117
|
-
updateIndex();
|
|
118
|
-
}
|
|
119
|
-
if ('data' in queueItem) {
|
|
120
|
-
matchedRouteConfigDataDescriptor.data = queueItem.data;
|
|
121
|
-
}
|
|
122
|
-
if ('file' in queueItem) {
|
|
123
|
-
if (!(0, _helpers.isFilePathValid)(queueItem.file)) return next();
|
|
124
|
-
matchedRouteConfigDataDescriptor.file = queueItem.file;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
if ('data' in matchedRouteConfig) {
|
|
128
|
-
matchedRouteConfigDataDescriptor.data = matchedRouteConfig.data;
|
|
129
|
-
}
|
|
130
|
-
if ('file' in matchedRouteConfig) {
|
|
131
|
-
if (!(0, _helpers.isFilePathValid)(matchedRouteConfig.file)) return next();
|
|
132
|
-
matchedRouteConfigDataDescriptor.file = matchedRouteConfig.file;
|
|
133
|
-
}
|
|
134
|
-
if ((_matchedRouteConfig_settings1 = matchedRouteConfig.settings) === null || _matchedRouteConfig_settings1 === void 0 ? void 0 : _matchedRouteConfig_settings1.status) {
|
|
135
|
-
response.statusCode = matchedRouteConfig.settings.status;
|
|
136
|
-
}
|
|
137
|
-
// ✅ important:
|
|
138
|
-
// set 'Cache-Control' header for explicit browsers response revalidate: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
|
|
139
|
-
// this code should place before response interceptors for giving opportunity to rewrite 'Cache-Control' header
|
|
140
|
-
if (request.method === 'GET') response.set('Cache-control', 'no-cache');
|
|
141
|
-
let resolvedData = null;
|
|
142
|
-
if (matchedRouteConfigDataDescriptor.data) {
|
|
143
|
-
var _matchedRouteConfig_entities;
|
|
144
|
-
resolvedData = typeof matchedRouteConfigDataDescriptor.data === 'function' ? await matchedRouteConfigDataDescriptor.data(request, (_matchedRouteConfig_entities = matchedRouteConfig.entities) !== null && _matchedRouteConfig_entities !== void 0 ? _matchedRouteConfig_entities : {}) : matchedRouteConfigDataDescriptor.data;
|
|
145
|
-
}
|
|
146
|
-
if (matchedRouteConfigDataDescriptor.file) {
|
|
147
|
-
const buffer = _nodefs.default.readFileSync(_nodepath.default.resolve(matchedRouteConfigDataDescriptor.file));
|
|
148
|
-
resolvedData = {
|
|
149
|
-
path: matchedRouteConfigDataDescriptor.file,
|
|
150
|
-
file: buffer
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
const data = await (0, _helpers.callResponseInterceptors)({
|
|
154
|
-
data: resolvedData,
|
|
155
|
-
request,
|
|
156
|
-
response,
|
|
157
|
-
interceptors: {
|
|
158
|
-
routeInterceptor: (_matchedRouteConfig_interceptors1 = matchedRouteConfig.interceptors) === null || _matchedRouteConfig_interceptors1 === void 0 ? void 0 : _matchedRouteConfig_interceptors1.response,
|
|
159
|
-
requestInterceptor: (_requestConfig_interceptors1 = requestConfig.interceptors) === null || _requestConfig_interceptors1 === void 0 ? void 0 : _requestConfig_interceptors1.response,
|
|
160
|
-
apiInterceptor: (_restConfig_interceptors = restConfig.interceptors) === null || _restConfig_interceptors === void 0 ? void 0 : _restConfig_interceptors.response,
|
|
161
|
-
serverInterceptor: serverResponseInterceptor
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
if ((_matchedRouteConfig_settings2 = matchedRouteConfig.settings) === null || _matchedRouteConfig_settings2 === void 0 ? void 0 : _matchedRouteConfig_settings2.delay) {
|
|
165
|
-
await (0, _helpers.sleep)(matchedRouteConfig.settings.delay);
|
|
166
|
-
}
|
|
167
|
-
if ((0, _helpers.isFileDescriptor)(data)) {
|
|
168
|
-
const isFilePathChanged = matchedRouteConfigDataDescriptor.file !== data.path;
|
|
169
|
-
if (isFilePathChanged) {
|
|
170
|
-
if (!(0, _helpers.isFilePathValid)(data.path)) return next();
|
|
171
|
-
data.file = _nodefs.default.readFileSync(_nodepath.default.resolve(data.path));
|
|
172
|
-
}
|
|
173
|
-
// ✅ important: replace backslashes because windows can use them in file path
|
|
174
|
-
const fileName = data.path.replaceAll('\\', '/').split('/').at(-1);
|
|
175
|
-
const fileExtension = fileName.split('.').at(-1);
|
|
176
|
-
response.type(fileExtension);
|
|
177
|
-
response.set('Content-Disposition', `filename=${fileName}`);
|
|
178
|
-
return response.send(data.file);
|
|
179
|
-
}
|
|
180
|
-
response.json(data);
|
|
181
|
-
}));
|
|
182
|
-
});
|
|
183
|
-
return router;
|
|
184
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './prepareRestRequestConfigs/prepareRestRequestConfigs';
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "prepareRestRequestConfigs", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return prepareRestRequestConfigs;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _helpers = require("../../../../../utils/helpers");
|
|
12
|
-
const calculateRouteConfigWeight = (restRouteConfig)=>{
|
|
13
|
-
const { entities } = restRouteConfig;
|
|
14
|
-
if (!entities) return 0;
|
|
15
|
-
let routeConfigWeight = 0;
|
|
16
|
-
const { headers, cookies, query, params, body } = 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 (params) routeConfigWeight += Object.keys(params).length;
|
|
21
|
-
if (body) {
|
|
22
|
-
if ((0, _helpers.isPlainObject)(body) && body.checkMode) {
|
|
23
|
-
// ✅ important:
|
|
24
|
-
// check that actual value check modes does not have `value` for compare
|
|
25
|
-
if (body.checkMode === 'exists' || body.checkMode === 'notExists') {
|
|
26
|
-
routeConfigWeight += 1;
|
|
27
|
-
return routeConfigWeight;
|
|
28
|
-
}
|
|
29
|
-
routeConfigWeight += (0, _helpers.isPlainObject)(body.value) ? Object.keys(body.value).length : 1;
|
|
30
|
-
return routeConfigWeight;
|
|
31
|
-
}
|
|
32
|
-
routeConfigWeight += (0, _helpers.isPlainObject)(body) ? Object.keys(body).length : 1;
|
|
33
|
-
}
|
|
34
|
-
return routeConfigWeight;
|
|
35
|
-
};
|
|
36
|
-
const prepareRestRequestConfigs = (requestConfigs)=>{
|
|
37
|
-
const sortedByPathRequestConfigs = requestConfigs.sort(({ path: firstPath }, { path: secondPath })=>{
|
|
38
|
-
// ✅ important:
|
|
39
|
-
// do not compare RegExp paths and non-parameterized paths
|
|
40
|
-
if (firstPath instanceof RegExp || secondPath instanceof RegExp) return 0;
|
|
41
|
-
if (!firstPath.includes('/:') && !secondPath.includes('/:')) return 0;
|
|
42
|
-
const firstPathParts = firstPath.split('/');
|
|
43
|
-
const secondPathParts = secondPath.split('/');
|
|
44
|
-
const minimalPathPartsLength = Math.min(firstPathParts.length, secondPathParts.length);
|
|
45
|
-
// ✅ important:
|
|
46
|
-
// need to find the leftmost parameter/non-parameter pair and give priority to non-parameter one
|
|
47
|
-
for(let i = 0; i < minimalPathPartsLength; i += 1){
|
|
48
|
-
const firstPathPart = firstPathParts[i];
|
|
49
|
-
const secondPathPart = secondPathParts[i];
|
|
50
|
-
const isFirstPathPartParameter = firstPathPart.startsWith(':');
|
|
51
|
-
const isSecondPathPartParameter = secondPathPart.startsWith(':');
|
|
52
|
-
if (!isFirstPathPartParameter && !isSecondPathPartParameter) {
|
|
53
|
-
if (firstPathPart === secondPathPart) continue;
|
|
54
|
-
return 0;
|
|
55
|
-
}
|
|
56
|
-
if (isFirstPathPartParameter && isSecondPathPartParameter) continue;
|
|
57
|
-
return +isFirstPathPartParameter - +isSecondPathPartParameter;
|
|
58
|
-
}
|
|
59
|
-
return 0;
|
|
60
|
-
});
|
|
61
|
-
sortedByPathRequestConfigs.forEach((requestConfig)=>{
|
|
62
|
-
requestConfig.routes.sort((first, second)=>// ✅ important:
|
|
63
|
-
// Lift more specific configs for correct working of routes
|
|
64
|
-
calculateRouteConfigWeight(second) - calculateRouteConfigWeight(first));
|
|
65
|
-
});
|
|
66
|
-
return requestConfigs;
|
|
67
|
-
};
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "createDatabaseMockServer", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return createDatabaseMockServer;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _bodyparser = /*#__PURE__*/ _interop_require_default(require("body-parser"));
|
|
12
|
-
const _express = /*#__PURE__*/ _interop_require_default(require("express"));
|
|
13
|
-
const _database = require("../../core/database");
|
|
14
|
-
const _middlewares = require("../../core/middlewares");
|
|
15
|
-
function _interop_require_default(obj) {
|
|
16
|
-
return obj && obj.__esModule ? obj : {
|
|
17
|
-
default: obj
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
const createDatabaseMockServer = (databaseMockServerConfig, server = (0, _express.default)())=>{
|
|
21
|
-
var _databaseMockServerConfig_interceptors;
|
|
22
|
-
const { cors, staticPath, data, routes } = databaseMockServerConfig;
|
|
23
|
-
server.use(_bodyparser.default.urlencoded({
|
|
24
|
-
extended: false
|
|
25
|
-
}));
|
|
26
|
-
server.use(_bodyparser.default.json({
|
|
27
|
-
limit: '10mb'
|
|
28
|
-
}));
|
|
29
|
-
server.set('json spaces', 2);
|
|
30
|
-
server.use(_bodyparser.default.text());
|
|
31
|
-
(0, _middlewares.contextMiddleware)(server, {
|
|
32
|
-
database: {
|
|
33
|
-
data,
|
|
34
|
-
routes
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
(0, _middlewares.cookieParseMiddleware)(server);
|
|
38
|
-
const serverRequestInterceptor = (_databaseMockServerConfig_interceptors = databaseMockServerConfig.interceptors) === null || _databaseMockServerConfig_interceptors === void 0 ? void 0 : _databaseMockServerConfig_interceptors.request;
|
|
39
|
-
if (serverRequestInterceptor) {
|
|
40
|
-
(0, _middlewares.requestInterceptorMiddleware)({
|
|
41
|
-
server,
|
|
42
|
-
interceptor: serverRequestInterceptor
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
var _databaseMockServerConfig_baseUrl;
|
|
46
|
-
const baseUrl = (_databaseMockServerConfig_baseUrl = databaseMockServerConfig.baseUrl) !== null && _databaseMockServerConfig_baseUrl !== void 0 ? _databaseMockServerConfig_baseUrl : '/';
|
|
47
|
-
if (cors) {
|
|
48
|
-
(0, _middlewares.corsMiddleware)(server, cors);
|
|
49
|
-
} else {
|
|
50
|
-
(0, _middlewares.noCorsMiddleware)(server);
|
|
51
|
-
}
|
|
52
|
-
if (staticPath) {
|
|
53
|
-
(0, _middlewares.staticMiddleware)(server, baseUrl, staticPath);
|
|
54
|
-
}
|
|
55
|
-
const routerWithDatabaseRoutes = (0, _database.createDatabaseRoutes)(_express.default.Router(), {
|
|
56
|
-
data,
|
|
57
|
-
routes
|
|
58
|
-
});
|
|
59
|
-
server.use(baseUrl, routerWithDatabaseRoutes);
|
|
60
|
-
(0, _middlewares.errorMiddleware)(server);
|
|
61
|
-
return server;
|
|
62
|
-
};
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "createFlatMockServer", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return createFlatMockServer;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _bodyparser = /*#__PURE__*/ _interop_require_default(require("body-parser"));
|
|
12
|
-
const _express = /*#__PURE__*/ _interop_require_default(require("express"));
|
|
13
|
-
const _database = require("../../core/database");
|
|
14
|
-
const _graphql = require("../../core/graphql");
|
|
15
|
-
const _middlewares = require("../../core/middlewares");
|
|
16
|
-
const _rest = require("../../core/rest");
|
|
17
|
-
const _validate = require("../../utils/validate");
|
|
18
|
-
function _interop_require_default(obj) {
|
|
19
|
-
return obj && obj.__esModule ? obj : {
|
|
20
|
-
default: obj
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
const createFlatMockServer = (flatMockServerConfig, server = (0, _express.default)())=>{
|
|
24
|
-
(0, _validate.validateFlatMockServerConfig)(flatMockServerConfig);
|
|
25
|
-
const [option, ...flatMockServerComponents] = flatMockServerConfig;
|
|
26
|
-
const flatMockServerSettings = !('configs' in option) ? option : undefined;
|
|
27
|
-
const { cors, staticPath, interceptors, baseUrl: serverBaseUrl = '/', database } = flatMockServerSettings !== null && flatMockServerSettings !== void 0 ? flatMockServerSettings : {};
|
|
28
|
-
server.use(_bodyparser.default.urlencoded({
|
|
29
|
-
extended: false
|
|
30
|
-
}));
|
|
31
|
-
server.use(_bodyparser.default.json({
|
|
32
|
-
limit: '10mb'
|
|
33
|
-
}));
|
|
34
|
-
server.set('json spaces', 2);
|
|
35
|
-
server.use(_bodyparser.default.text());
|
|
36
|
-
(0, _middlewares.contextMiddleware)(server, {
|
|
37
|
-
database
|
|
38
|
-
});
|
|
39
|
-
(0, _middlewares.cookieParseMiddleware)(server);
|
|
40
|
-
const serverRequestInterceptor = interceptors === null || interceptors === void 0 ? void 0 : interceptors.request;
|
|
41
|
-
if (serverRequestInterceptor) {
|
|
42
|
-
(0, _middlewares.requestInterceptorMiddleware)({
|
|
43
|
-
server,
|
|
44
|
-
interceptor: serverRequestInterceptor
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
if (cors) {
|
|
48
|
-
(0, _middlewares.corsMiddleware)(server, cors);
|
|
49
|
-
} else {
|
|
50
|
-
(0, _middlewares.noCorsMiddleware)(server);
|
|
51
|
-
}
|
|
52
|
-
if (staticPath) {
|
|
53
|
-
(0, _middlewares.staticMiddleware)(server, serverBaseUrl, staticPath);
|
|
54
|
-
}
|
|
55
|
-
if (database) {
|
|
56
|
-
const routerWithDatabaseRoutes = (0, _database.createDatabaseRoutes)(_express.default.Router(), database);
|
|
57
|
-
server.use(serverBaseUrl, routerWithDatabaseRoutes);
|
|
58
|
-
}
|
|
59
|
-
const components = flatMockServerSettings ? flatMockServerComponents : flatMockServerConfig;
|
|
60
|
-
const { restRequestConfigs, graphQLRequestConfigs } = components.reduce((acc, component)=>{
|
|
61
|
-
const { baseUrl = '' } = component;
|
|
62
|
-
component.configs.forEach((config)=>{
|
|
63
|
-
var _component_interceptors, _config_interceptors, _component_interceptors1, _config_interceptors1;
|
|
64
|
-
const interceptors = {
|
|
65
|
-
...(((_component_interceptors = component.interceptors) === null || _component_interceptors === void 0 ? void 0 : _component_interceptors.request) || ((_config_interceptors = config.interceptors) === null || _config_interceptors === void 0 ? void 0 : _config_interceptors.request)) && {
|
|
66
|
-
request: (params)=>{
|
|
67
|
-
var _component_interceptors, _config_interceptors;
|
|
68
|
-
if ((_component_interceptors = component.interceptors) === null || _component_interceptors === void 0 ? void 0 : _component_interceptors.request) {
|
|
69
|
-
component.interceptors.request(params);
|
|
70
|
-
}
|
|
71
|
-
if ((_config_interceptors = config.interceptors) === null || _config_interceptors === void 0 ? void 0 : _config_interceptors.request) {
|
|
72
|
-
config.interceptors.request(params);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
...(((_component_interceptors1 = component.interceptors) === null || _component_interceptors1 === void 0 ? void 0 : _component_interceptors1.response) || ((_config_interceptors1 = config.interceptors) === null || _config_interceptors1 === void 0 ? void 0 : _config_interceptors1.response)) && {
|
|
77
|
-
response: (data, params)=>{
|
|
78
|
-
var _config_interceptors, _component_interceptors;
|
|
79
|
-
if ((_config_interceptors = config.interceptors) === null || _config_interceptors === void 0 ? void 0 : _config_interceptors.response) {
|
|
80
|
-
data = config.interceptors.response(data, params);
|
|
81
|
-
}
|
|
82
|
-
if ((_component_interceptors = component.interceptors) === null || _component_interceptors === void 0 ? void 0 : _component_interceptors.response) {
|
|
83
|
-
data = component.interceptors.response(data, params);
|
|
84
|
-
}
|
|
85
|
-
return data;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
const isRest = 'method' in config;
|
|
90
|
-
if (isRest) acc.restRequestConfigs.push({
|
|
91
|
-
...config,
|
|
92
|
-
interceptors,
|
|
93
|
-
path: config.path instanceof RegExp ? new RegExp(`${baseUrl}${config.path.source}`, config.path.flags) : `${baseUrl}${config.path}`
|
|
94
|
-
});
|
|
95
|
-
const isGraphql = 'operationType' in config;
|
|
96
|
-
if (isGraphql) acc.graphQLRequestConfigs.push({
|
|
97
|
-
...config,
|
|
98
|
-
interceptors
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
return acc;
|
|
102
|
-
}, {
|
|
103
|
-
restRequestConfigs: [],
|
|
104
|
-
graphQLRequestConfigs: []
|
|
105
|
-
});
|
|
106
|
-
if (restRequestConfigs.length) {
|
|
107
|
-
const routerWithRestRoutes = (0, _rest.createRestRoutes)({
|
|
108
|
-
router: _express.default.Router(),
|
|
109
|
-
restConfig: {
|
|
110
|
-
configs: restRequestConfigs
|
|
111
|
-
},
|
|
112
|
-
serverResponseInterceptor: interceptors === null || interceptors === void 0 ? void 0 : interceptors.response
|
|
113
|
-
});
|
|
114
|
-
server.use(serverBaseUrl, routerWithRestRoutes);
|
|
115
|
-
}
|
|
116
|
-
if (graphQLRequestConfigs.length) {
|
|
117
|
-
const routerWithGraphQLRoutes = (0, _graphql.createGraphQLRoutes)({
|
|
118
|
-
router: _express.default.Router(),
|
|
119
|
-
graphqlConfig: {
|
|
120
|
-
configs: graphQLRequestConfigs
|
|
121
|
-
},
|
|
122
|
-
serverResponseInterceptor: interceptors === null || interceptors === void 0 ? void 0 : interceptors.response
|
|
123
|
-
});
|
|
124
|
-
server.use(serverBaseUrl, routerWithGraphQLRoutes);
|
|
125
|
-
}
|
|
126
|
-
(0, _middlewares.errorMiddleware)(server);
|
|
127
|
-
return server;
|
|
128
|
-
};
|