mock-config-server 4.0.3 → 5.0.0-beta.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/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/ts/full/mock-server.config.ts +2 -2
- package/dist/bin/templates/ts/graphql/mock-server.config.ts +2 -2
- package/dist/bin/templates/ts/rest/mock-server.config.ts +2 -2
- 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 +114 -0
- package/dist/src/core/functions/rest/rest.js +171 -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 +2 -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
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
_export_star(require("./calculateGraphQLRouteConfigWeight/calculateGraphQLRouteConfigWeight"), exports);
|
|
6
|
+
_export_star(require("./matchGraphQLRequestArtifacts/matchGraphQLRequestArtifacts"), exports);
|
|
7
|
+
_export_star(require("./prepareGraphQLRequestArtifacts/prepareGraphQLRequestArtifacts"), exports);
|
|
8
|
+
function _export_star(from, to) {
|
|
9
|
+
Object.keys(from).forEach(function(k) {
|
|
10
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
|
|
11
|
+
Object.defineProperty(to, k, {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function() {
|
|
14
|
+
return from[k];
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
return from;
|
|
20
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { GraphQLRequestArtifact } from '../../../../../utils/types';
|
|
2
|
+
interface MatchGraphQLRequestArtifactsParams {
|
|
3
|
+
artifacts: GraphQLRequestArtifact[];
|
|
4
|
+
meta: {
|
|
5
|
+
path: string;
|
|
6
|
+
operationType: string;
|
|
7
|
+
query?: string;
|
|
8
|
+
operationName?: string;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export declare const matchGraphQLRequestArtifacts: ({ artifacts, meta }: MatchGraphQLRequestArtifactsParams) => GraphQLRequestArtifact[];
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "matchGraphQLRequestArtifacts", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return matchGraphQLRequestArtifacts;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _graphql = require("graphql");
|
|
12
|
+
const _helpers = require("../../../../../utils/helpers");
|
|
13
|
+
const matchGraphQLRequestArtifacts = ({ artifacts, meta })=>artifacts.filter((artifact)=>{
|
|
14
|
+
if ((0, _helpers.normalizeUrl)(meta.path) !== (0, _helpers.normalizeUrl)(artifact.baseUrl)) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
if (artifact.operationType !== meta.operationType) return false;
|
|
18
|
+
if (artifact.query) {
|
|
19
|
+
if (!meta.query) return false;
|
|
20
|
+
return (0, _graphql.stripIgnoredCharacters)((0, _graphql.print)((0, _graphql.parse)(artifact.query))) === (0, _graphql.stripIgnoredCharacters)((0, _graphql.print)((0, _graphql.parse)(meta.query)));
|
|
21
|
+
}
|
|
22
|
+
if (artifact.operationName) {
|
|
23
|
+
if (!meta.operationName) return false;
|
|
24
|
+
return artifact.operationName instanceof RegExp ? new RegExp(artifact.operationName).test(meta.operationName) : artifact.operationName === meta.operationName;
|
|
25
|
+
}
|
|
26
|
+
console.warn(`[mock-config] GraphQL artifact with no query or operationName was skipped: ${JSON.stringify(artifact)}`);
|
|
27
|
+
return false;
|
|
28
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "prepareGraphQLRequestArtifacts", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return prepareGraphQLRequestArtifacts;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const prepareGraphQLRequestArtifacts = (requestArtifacts)=>requestArtifacts.toSorted((first, second)=>second.weight - first.weight);
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export * from './
|
|
1
|
+
export * from './createGraphQLRoute/createGraphQLRoute';
|
|
2
|
+
export * from './createGraphQLRoute/helpers';
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", {
|
|
3
3
|
value: true
|
|
4
4
|
});
|
|
5
|
-
_export_star(require("./
|
|
5
|
+
_export_star(require("./createGraphQLRoute/createGraphQLRoute"), exports);
|
|
6
|
+
_export_star(require("./createGraphQLRoute/helpers"), exports);
|
|
6
7
|
function _export_star(from, to) {
|
|
7
8
|
Object.keys(from).forEach(function(k) {
|
|
8
9
|
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
|
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import type { Express } from 'express';
|
|
2
|
-
import type { GraphQLEntity, GraphQLOperationName, GraphQLOperationType
|
|
2
|
+
import type { DatabaseConfig, GraphQLEntity, GraphQLOperationName, GraphQLOperationType } from '../../../utils/types';
|
|
3
3
|
declare global {
|
|
4
4
|
namespace Express {
|
|
5
5
|
interface Request {
|
|
6
|
-
id: number;
|
|
7
|
-
timestamp: number;
|
|
8
6
|
graphQL: {
|
|
9
7
|
operationType: GraphQLOperationType;
|
|
10
8
|
operationName?: GraphQLOperationName;
|
|
11
9
|
query: string;
|
|
12
10
|
variables?: GraphQLEntity<'variables'>;
|
|
13
11
|
} | null;
|
|
12
|
+
id: number;
|
|
13
|
+
timestamp: number;
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
-
export declare const contextMiddleware: (server: Express, { database }:
|
|
17
|
+
export declare const contextMiddleware: (server: Express, { database }: {
|
|
18
|
+
database?: DatabaseConfig;
|
|
19
|
+
}) => void;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Express } from 'express';
|
|
2
|
+
import type { RestRequestArtifact } from '../../../utils/types';
|
|
3
|
+
interface CreateRestRoutesParams {
|
|
4
|
+
restRequestArtifacts: RestRequestArtifact[];
|
|
5
|
+
server: Express;
|
|
6
|
+
}
|
|
7
|
+
export declare const createRestRoute: ({ server, restRequestArtifacts }: CreateRestRoutesParams) => Express;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "createRestRoute", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return createRestRoute;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _flat = require("flat");
|
|
12
|
+
const _helpers = require("../../../utils/helpers");
|
|
13
|
+
const _helpers1 = require("./helpers");
|
|
14
|
+
const extractPathParams = (artifact, path)=>{
|
|
15
|
+
var _fullPath_match;
|
|
16
|
+
if (artifact.path instanceof RegExp) return {};
|
|
17
|
+
const fullPath = (0, _helpers.urlJoin)(artifact.baseUrl, artifact.path);
|
|
18
|
+
var _fullPath_match_map;
|
|
19
|
+
const keys = (_fullPath_match_map = (_fullPath_match = fullPath.match(/:[^/]+/g)) === null || _fullPath_match === void 0 ? void 0 : _fullPath_match.map((key)=>key.slice(1))) !== null && _fullPath_match_map !== void 0 ? _fullPath_match_map : [];
|
|
20
|
+
if (!keys.length) return {};
|
|
21
|
+
const match = path.match((0, _helpers1.generatePathRegex)(fullPath));
|
|
22
|
+
if (!match) return {};
|
|
23
|
+
return keys.reduce((acc, key, index)=>{
|
|
24
|
+
acc[key] = decodeURIComponent(match[index + 1]);
|
|
25
|
+
return acc;
|
|
26
|
+
}, {});
|
|
27
|
+
};
|
|
28
|
+
const createRestRoute = ({ server, restRequestArtifacts })=>server.use((0, _helpers.asyncHandler)(async (request, response, next)=>{
|
|
29
|
+
var _matchedRouteConfig_config_settings, _matchedRouteConfig_config_settings1;
|
|
30
|
+
const requestMethod = request.method.toLowerCase();
|
|
31
|
+
const previousParams = {
|
|
32
|
+
...request.params
|
|
33
|
+
};
|
|
34
|
+
const matchedRequestArtifacts = (0, _helpers1.matchRestRequestArtifacts)({
|
|
35
|
+
artifacts: restRequestArtifacts,
|
|
36
|
+
meta: {
|
|
37
|
+
method: requestMethod,
|
|
38
|
+
path: (0, _helpers.normalizeUrl)(request.path)
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
if (!matchedRequestArtifacts.length) return next();
|
|
42
|
+
const matchedRouteConfig = matchedRequestArtifacts.find((artifact)=>{
|
|
43
|
+
request.params = extractPathParams(artifact, request.path);
|
|
44
|
+
const { config } = artifact;
|
|
45
|
+
if (!config.entities) return true;
|
|
46
|
+
const entityEntries = Object.entries(config.entities);
|
|
47
|
+
return entityEntries.every(([entityName, entityDescriptorOrValue])=>{
|
|
48
|
+
// ✅ important:
|
|
49
|
+
// check whole body as plain value strictly if descriptor used for body
|
|
50
|
+
const isEntityBodyByTopLevelDescriptor = entityName === 'body' && (0, _helpers.isEntityDescriptor)(entityDescriptorOrValue);
|
|
51
|
+
if (isEntityBodyByTopLevelDescriptor) {
|
|
52
|
+
const bodyDescriptor = entityDescriptorOrValue;
|
|
53
|
+
if (bodyDescriptor.checkMode === 'exists' || bodyDescriptor.checkMode === 'notExists') {
|
|
54
|
+
return (0, _helpers.resolveEntityValues)({
|
|
55
|
+
actualValue: request.body,
|
|
56
|
+
checkMode: bodyDescriptor.checkMode
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
var _bodyDescriptor_oneOf;
|
|
60
|
+
return (0, _helpers.resolveEntityValues)({
|
|
61
|
+
actualValue: request.body,
|
|
62
|
+
descriptorValue: bodyDescriptor.value,
|
|
63
|
+
checkMode: bodyDescriptor.checkMode,
|
|
64
|
+
oneOf: (_bodyDescriptor_oneOf = bodyDescriptor.oneOf) !== null && _bodyDescriptor_oneOf !== void 0 ? _bodyDescriptor_oneOf : false
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
const isEntityBodyByTopLevelArray = entityName === 'body' && Array.isArray(entityDescriptorOrValue);
|
|
68
|
+
if (isEntityBodyByTopLevelArray) {
|
|
69
|
+
if (!Array.isArray(request.body)) return false;
|
|
70
|
+
return (0, _helpers.resolveEntityValues)({
|
|
71
|
+
actualValue: request.body,
|
|
72
|
+
descriptorValue: entityDescriptorOrValue,
|
|
73
|
+
checkMode: 'equals'
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
const actualEntity = (0, _flat.flatten)(request[entityName]);
|
|
77
|
+
const entityValueEntries = Object.entries(entityDescriptorOrValue);
|
|
78
|
+
return entityValueEntries.every(([entityPropertyKey, entityPropertyDescriptorOrValue])=>{
|
|
79
|
+
const entityPropertyDescriptor = (0, _helpers.convertToEntityDescriptor)(entityPropertyDescriptorOrValue);
|
|
80
|
+
// ✅ important: transform header keys to lower case because browsers send headers in lowercase
|
|
81
|
+
const actualPropertyKey = entityName === 'headers' ? entityPropertyKey.toLowerCase() : entityPropertyKey;
|
|
82
|
+
const actualPropertyValue = actualEntity[actualPropertyKey];
|
|
83
|
+
if (entityPropertyDescriptor.checkMode === 'exists' || entityPropertyDescriptor.checkMode === 'notExists') {
|
|
84
|
+
return (0, _helpers.resolveEntityValues)({
|
|
85
|
+
actualValue: actualPropertyValue,
|
|
86
|
+
checkMode: entityPropertyDescriptor.checkMode
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
var _entityPropertyDescriptor_oneOf;
|
|
90
|
+
return (0, _helpers.resolveEntityValues)({
|
|
91
|
+
actualValue: actualPropertyValue,
|
|
92
|
+
descriptorValue: entityPropertyDescriptor.value,
|
|
93
|
+
checkMode: entityPropertyDescriptor.checkMode,
|
|
94
|
+
oneOf: (_entityPropertyDescriptor_oneOf = entityPropertyDescriptor.oneOf) !== null && _entityPropertyDescriptor_oneOf !== void 0 ? _entityPropertyDescriptor_oneOf : false
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
if (!matchedRouteConfig) {
|
|
100
|
+
request.params = previousParams;
|
|
101
|
+
return next();
|
|
102
|
+
}
|
|
103
|
+
if (matchedRouteConfig.componentRequestInterceptor) {
|
|
104
|
+
await (0, _helpers.callRequestInterceptor)({
|
|
105
|
+
request,
|
|
106
|
+
interceptor: matchedRouteConfig.componentRequestInterceptor
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
if (matchedRouteConfig.requestRequestInterceptor) {
|
|
110
|
+
await (0, _helpers.callRequestInterceptor)({
|
|
111
|
+
request,
|
|
112
|
+
interceptor: matchedRouteConfig.requestRequestInterceptor
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
if (matchedRouteConfig.routeRequestInterceptor) {
|
|
116
|
+
await (0, _helpers.callRequestInterceptor)({
|
|
117
|
+
request,
|
|
118
|
+
interceptor: matchedRouteConfig.routeRequestInterceptor
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
if ((_matchedRouteConfig_config_settings = matchedRouteConfig.config.settings) === null || _matchedRouteConfig_config_settings === void 0 ? void 0 : _matchedRouteConfig_config_settings.status) {
|
|
122
|
+
response.statusCode = matchedRouteConfig.config.settings.status;
|
|
123
|
+
}
|
|
124
|
+
// ✅ important:
|
|
125
|
+
// set 'Cache-Control' header for explicit browsers response revalidate: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
|
|
126
|
+
// this code should place before response interceptors for giving opportunity to rewrite 'Cache-Control' header
|
|
127
|
+
if (request.method === 'GET') response.set('Cache-control', 'no-store');
|
|
128
|
+
var _matchedRouteConfig_config_entities;
|
|
129
|
+
const params = {
|
|
130
|
+
request,
|
|
131
|
+
response,
|
|
132
|
+
next,
|
|
133
|
+
entities: (_matchedRouteConfig_config_entities = matchedRouteConfig.config.entities) !== null && _matchedRouteConfig_config_entities !== void 0 ? _matchedRouteConfig_config_entities : {},
|
|
134
|
+
appendHeader: (field, value)=>{
|
|
135
|
+
response.append(field, value);
|
|
136
|
+
},
|
|
137
|
+
attachment: (filename)=>{
|
|
138
|
+
response.attachment(filename);
|
|
139
|
+
},
|
|
140
|
+
clearCookie: (name, options)=>{
|
|
141
|
+
response.clearCookie(name, options);
|
|
142
|
+
},
|
|
143
|
+
getCookie: (name)=>request.cookies[name],
|
|
144
|
+
getRequestHeader: (field)=>request.headers[field],
|
|
145
|
+
getRequestHeaders: ()=>request.headers,
|
|
146
|
+
getResponseHeader: (field)=>response.getHeader(field),
|
|
147
|
+
getResponseHeaders: ()=>response.getHeaders(),
|
|
148
|
+
setCookie: (name, value, options)=>{
|
|
149
|
+
if (options) {
|
|
150
|
+
response.cookie(name, value, options);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
response.cookie(name, value);
|
|
154
|
+
},
|
|
155
|
+
setDelay: async (delay)=>{
|
|
156
|
+
await (0, _helpers.sleep)(delay === Infinity ? 99999999 : delay);
|
|
157
|
+
},
|
|
158
|
+
setHeader: (field, value)=>{
|
|
159
|
+
response.set(field, value);
|
|
160
|
+
},
|
|
161
|
+
setStatusCode: (statusCode)=>{
|
|
162
|
+
response.statusCode = statusCode;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
const resolvedData = typeof matchedRouteConfig.config.data === 'function' ? await matchedRouteConfig.config.data(params) : matchedRouteConfig.config.data;
|
|
166
|
+
if (response.headersSent) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const data = await (0, _helpers.callResponseInterceptors)({
|
|
170
|
+
data: resolvedData,
|
|
171
|
+
request,
|
|
172
|
+
response,
|
|
173
|
+
interceptors: {
|
|
174
|
+
routeInterceptor: matchedRouteConfig.routeResponseInterceptor,
|
|
175
|
+
requestInterceptor: matchedRouteConfig.requestResponseInterceptor,
|
|
176
|
+
componentInterceptor: matchedRouteConfig.componentResponseInterceptor,
|
|
177
|
+
serverInterceptor: matchedRouteConfig.serverResponseInterceptor
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
if ((_matchedRouteConfig_config_settings1 = matchedRouteConfig.config.settings) === null || _matchedRouteConfig_config_settings1 === void 0 ? void 0 : _matchedRouteConfig_config_settings1.delay) {
|
|
181
|
+
await (0, _helpers.sleep)(matchedRouteConfig.config.settings.delay);
|
|
182
|
+
}
|
|
183
|
+
if (response.headersSent) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (response.getHeader('content-type')) {
|
|
187
|
+
return response.send(data);
|
|
188
|
+
}
|
|
189
|
+
return response.json(data);
|
|
190
|
+
}));
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "calculateRestRouteConfigWeight", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return calculateRestRouteConfigWeight;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _helpers = require("../../../../../utils/helpers");
|
|
12
|
+
const calculateRestRouteConfigWeight = (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
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
_export_star(require("./calculateRestRouteConfigWeight/calculateRestRouteConfigWeight"), exports);
|
|
6
|
+
_export_star(require("./matchRestRequestArtifacts/matchRestRequestArtifacts"), exports);
|
|
7
|
+
_export_star(require("./prepareRestRequestArtifacts/prepareRestRequestArtifacts"), exports);
|
|
8
|
+
function _export_star(from, to) {
|
|
9
|
+
Object.keys(from).forEach(function(k) {
|
|
10
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
|
|
11
|
+
Object.defineProperty(to, k, {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function() {
|
|
14
|
+
return from[k];
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
return from;
|
|
20
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { RestRequestArtifact } from '../../../../../utils/types';
|
|
2
|
+
export declare const generatePathRegex: (path: string) => RegExp;
|
|
3
|
+
interface MatchRestRequestArtifactsParams {
|
|
4
|
+
artifacts: RestRequestArtifact[];
|
|
5
|
+
meta: {
|
|
6
|
+
method: string;
|
|
7
|
+
path: string;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export declare const matchRestRequestArtifacts: ({ artifacts, meta }: MatchRestRequestArtifactsParams) => RestRequestArtifact[];
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
get generatePathRegex () {
|
|
13
|
+
return generatePathRegex;
|
|
14
|
+
},
|
|
15
|
+
get matchRestRequestArtifacts () {
|
|
16
|
+
return matchRestRequestArtifacts;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
const _helpers = require("../../../../../utils/helpers");
|
|
20
|
+
const generatePathRegex = (path)=>new RegExp(`^${path.split('/').map((part)=>part.startsWith(':') ? '([^/]+)' : part.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('/')}$`);
|
|
21
|
+
const matchRestRequestArtifacts = ({ artifacts, meta })=>artifacts.filter((artifact)=>{
|
|
22
|
+
if (!meta.path.startsWith(artifact.baseUrl)) return false;
|
|
23
|
+
if (artifact.method !== meta.method) return false;
|
|
24
|
+
if (artifact.path instanceof RegExp) {
|
|
25
|
+
if (artifact.baseUrl === '/') return new RegExp(artifact.path).test(meta.path);
|
|
26
|
+
const tail = meta.path === artifact.baseUrl ? '' : meta.path.slice(artifact.baseUrl.length);
|
|
27
|
+
if (!tail) return false;
|
|
28
|
+
return new RegExp(artifact.path).test(tail);
|
|
29
|
+
}
|
|
30
|
+
return generatePathRegex((0, _helpers.urlJoin)(artifact.baseUrl, artifact.path)).test(meta.path);
|
|
31
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "prepareRestRequestArtifacts", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return prepareRestRequestArtifacts;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const prepareRestRequestArtifacts = (requestArtifacts)=>{
|
|
12
|
+
const sortedByPathRequestArtifacts = requestArtifacts.toSorted((first, second)=>second.weight - first.weight).toSorted(({ path: firstPath }, { path: secondPath })=>{
|
|
13
|
+
// ✅ important:
|
|
14
|
+
// do not compare RegExp paths and non-parameterized paths
|
|
15
|
+
if (firstPath instanceof RegExp || secondPath instanceof RegExp) return 0;
|
|
16
|
+
if (!firstPath.includes('/:') && !secondPath.includes('/:')) return 0;
|
|
17
|
+
const firstPathParts = firstPath.split('/');
|
|
18
|
+
const secondPathParts = secondPath.split('/');
|
|
19
|
+
const minimalPathPartsLength = Math.min(firstPathParts.length, secondPathParts.length);
|
|
20
|
+
// ✅ important:
|
|
21
|
+
// need to find the leftmost parameter/non-parameter pair and give priority to non-parameter one
|
|
22
|
+
for(let i = 0; i < minimalPathPartsLength; i += 1){
|
|
23
|
+
const firstPathPart = firstPathParts[i];
|
|
24
|
+
const secondPathPart = secondPathParts[i];
|
|
25
|
+
const isFirstPathPartParameter = firstPathPart.startsWith(':');
|
|
26
|
+
const isSecondPathPartParameter = secondPathPart.startsWith(':');
|
|
27
|
+
if (!isFirstPathPartParameter && !isSecondPathPartParameter) {
|
|
28
|
+
if (firstPathPart === secondPathPart) continue;
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
if (isFirstPathPartParameter && isSecondPathPartParameter) continue;
|
|
32
|
+
return +isFirstPathPartParameter - +isSecondPathPartParameter;
|
|
33
|
+
}
|
|
34
|
+
return 0;
|
|
35
|
+
});
|
|
36
|
+
return sortedByPathRequestArtifacts;
|
|
37
|
+
};
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export * from './
|
|
1
|
+
export * from './createRestRoute/createRestRoute';
|
|
2
|
+
export * from './createRestRoute/helpers';
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", {
|
|
3
3
|
value: true
|
|
4
4
|
});
|
|
5
|
-
_export_star(require("./
|
|
5
|
+
_export_star(require("./createRestRoute/createRestRoute"), exports);
|
|
6
|
+
_export_star(require("./createRestRoute/helpers"), exports);
|
|
6
7
|
function _export_star(from, to) {
|
|
7
8
|
Object.keys(from).forEach(function(k) {
|
|
8
9
|
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
|
package/dist/src/index.d.ts
CHANGED
package/dist/src/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { Express } from 'express';
|
|
2
2
|
import type { MockServerConfig } from '../../utils/types';
|
|
3
|
-
export declare const createMockServer: (mockServerConfig:
|
|
3
|
+
export declare const createMockServer: (mockServerConfig: MockServerConfig, server?: Express) => Express;
|