mock-config-server 5.0.0-beta.1 → 5.0.0-beta.2
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 +6 -53
- package/dist/bin/init.js +1 -1
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileWriter.js +1 -1
- package/dist/src/core/entities/handlers/endsWith.d.ts +1 -0
- package/dist/src/core/entities/handlers/endsWith.js +12 -0
- package/dist/src/core/entities/handlers/equals.d.ts +1 -0
- package/dist/src/core/entities/handlers/equals.js +12 -0
- package/dist/src/core/entities/handlers/every.d.ts +2 -0
- package/dist/src/core/entities/handlers/every.js +12 -0
- package/dist/src/core/entities/handlers/exists.d.ts +1 -0
- package/dist/src/core/entities/handlers/exists.js +12 -0
- package/dist/src/core/entities/handlers/fn.d.ts +2 -0
- package/dist/src/core/entities/handlers/fn.js +12 -0
- package/dist/src/core/entities/handlers/greater.d.ts +1 -0
- package/dist/src/core/entities/handlers/greater.js +12 -0
- package/dist/src/core/entities/handlers/greaterOrEquals.d.ts +1 -0
- package/dist/src/core/entities/handlers/greaterOrEquals.js +12 -0
- package/dist/src/core/entities/handlers/haveEntries.d.ts +2 -0
- package/dist/src/core/entities/handlers/haveEntries.js +12 -0
- package/dist/src/core/entities/handlers/haveType.d.ts +2 -0
- package/dist/src/core/entities/handlers/haveType.js +12 -0
- package/dist/src/core/entities/handlers/inRange.d.ts +1 -0
- package/dist/src/core/entities/handlers/inRange.js +12 -0
- package/dist/src/core/entities/handlers/includes.d.ts +1 -0
- package/dist/src/core/entities/handlers/includes.js +12 -0
- package/dist/src/core/entities/handlers/index.d.ts +21 -0
- package/dist/src/core/entities/handlers/index.js +38 -0
- package/dist/src/core/entities/handlers/length.d.ts +1 -0
- package/dist/src/core/entities/handlers/length.js +12 -0
- package/dist/src/core/entities/handlers/less.d.ts +1 -0
- package/dist/src/core/entities/handlers/less.js +12 -0
- package/dist/src/core/entities/handlers/lessOrEquals.d.ts +1 -0
- package/dist/src/core/entities/handlers/lessOrEquals.js +12 -0
- package/dist/src/core/entities/handlers/maxLength.d.ts +1 -0
- package/dist/src/core/entities/handlers/maxLength.js +12 -0
- package/dist/src/core/entities/handlers/minLength.d.ts +1 -0
- package/dist/src/core/entities/handlers/minLength.js +12 -0
- package/dist/src/core/entities/handlers/not.d.ts +2 -0
- package/dist/src/core/entities/handlers/not.js +12 -0
- package/dist/src/core/entities/handlers/oneOf.d.ts +2 -0
- package/dist/src/core/entities/handlers/oneOf.js +12 -0
- package/dist/src/core/entities/handlers/regExp.d.ts +1 -0
- package/dist/src/core/entities/handlers/regExp.js +12 -0
- package/dist/src/core/entities/handlers/some.d.ts +2 -0
- package/dist/src/core/entities/handlers/some.js +12 -0
- package/dist/src/core/entities/handlers/startsWith.d.ts +1 -0
- package/dist/src/core/entities/handlers/startsWith.js +12 -0
- package/dist/src/core/entities/index.d.ts +1 -0
- package/dist/src/{utils/helpers/config → core/entities}/index.js +1 -1
- package/dist/src/core/functions/graphql/graphql.js +3 -3
- package/dist/src/core/functions/index.d.ts +1 -0
- package/dist/src/core/functions/index.js +1 -0
- package/dist/src/core/functions/ws/ws.d.ts +15 -0
- package/dist/src/core/functions/ws/ws.js +52 -0
- package/dist/src/core/graphql/createGraphQLRoute/createGraphQLRoute.js +29 -33
- package/dist/src/core/graphql/createGraphQLRoute/helpers/calculateGraphQLRouteConfigWeight/calculateGraphQLRouteConfigWeight.js +3 -17
- package/dist/src/core/middlewares/contextMiddleware/contextMiddleware.d.ts +3 -1
- package/dist/src/core/middlewares/contextMiddleware/contextMiddleware.js +23 -4
- package/dist/src/core/middlewares/cookieParseMiddleware/cookieParseMiddleware.js +1 -1
- package/dist/src/core/middlewares/corsMiddleware/corsMiddleware.js +2 -5
- package/dist/src/core/middlewares/errorMiddleware/errorMiddleware.js +1 -1
- package/dist/src/core/rest/createRestRoute/createRestRoute.js +27 -43
- package/dist/src/core/rest/createRestRoute/helpers/calculateRestRouteConfigWeight/calculateRestRouteConfigWeight.js +3 -18
- package/dist/src/core/ws/createWsRoute/createWsRoute.d.ts +8 -0
- package/dist/src/core/ws/createWsRoute/createWsRoute.js +114 -0
- package/dist/src/core/ws/index.d.ts +1 -0
- package/dist/src/core/ws/index.js +18 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/server/createMockServer/createMockServer.js +63 -14
- package/dist/src/utils/constants/entitites.d.ts +1 -0
- package/dist/src/utils/constants/entitites.js +11 -0
- package/dist/src/utils/constants/index.d.ts +1 -1
- package/dist/src/utils/constants/index.js +1 -1
- package/dist/src/utils/helpers/entities/createComparator/createComparator.d.ts +3 -0
- package/dist/src/utils/helpers/entities/createComparator/createComparator.js +15 -0
- package/dist/src/utils/helpers/entities/index.d.ts +3 -2
- package/dist/src/utils/helpers/entities/index.js +3 -2
- package/dist/src/utils/helpers/entities/isComparator/isComparator.d.ts +2 -0
- package/dist/src/utils/helpers/entities/isComparator/isComparator.js +12 -0
- package/dist/src/utils/helpers/entities/resolveEntityValues/resolveEntityValues.d.ts +28 -0
- package/dist/src/utils/helpers/entities/resolveEntityValues/resolveEntityValues.js +119 -0
- package/dist/src/utils/helpers/graphql/index.d.ts +1 -1
- package/dist/src/utils/helpers/graphql/index.js +1 -1
- package/dist/src/utils/helpers/graphql/{parseQuery/parseQuery.d.ts → parseGraphQLQuery/parseGraphQLQuery.d.ts} +1 -1
- package/dist/src/utils/helpers/graphql/{parseQuery/parseQuery.js → parseGraphQLQuery/parseGraphQLQuery.js} +5 -5
- package/dist/src/utils/helpers/graphql/parseGraphQLRequest/parseGraphQLRequest.d.ts +2 -2
- package/dist/src/utils/helpers/graphql/parseGraphQLRequest/parseGraphQLRequest.js +2 -2
- package/dist/src/utils/helpers/index.d.ts +1 -1
- package/dist/src/utils/helpers/index.js +1 -1
- package/dist/src/utils/helpers/logger/callRequestLogger/callRequestLogger.js +7 -8
- package/dist/src/utils/helpers/logger/callResponseLogger/callResponseLogger.js +7 -8
- package/dist/src/{core/middlewares/cookieParseMiddleware/helpers → utils/helpers/request}/index.d.ts +1 -0
- package/dist/src/{core/middlewares/cookieParseMiddleware/helpers → utils/helpers/request}/index.js +1 -0
- package/dist/src/{core/middlewares/cookieParseMiddleware/helpers → utils/helpers/request}/parseCookie/parseCookie.d.ts +1 -1
- package/dist/src/{core/middlewares/cookieParseMiddleware/helpers → utils/helpers/request}/parseCookie/parseCookie.js +2 -2
- package/dist/src/utils/helpers/request/parseQuery/parseQuery.d.ts +1 -0
- package/dist/src/utils/helpers/request/parseQuery/parseQuery.js +32 -0
- package/dist/src/utils/types/entities.d.ts +10 -16
- package/dist/src/utils/types/graphql.d.ts +4 -3
- package/dist/src/utils/types/index.d.ts +1 -1
- package/dist/src/utils/types/index.js +1 -1
- package/dist/src/utils/types/logger.d.ts +1 -1
- package/dist/src/utils/types/rest.d.ts +4 -3
- package/dist/src/utils/types/server.d.ts +11 -2
- package/dist/src/utils/types/utils.d.ts +1 -1
- package/dist/src/utils/types/ws.d.ts +66 -0
- package/dist/src/utils/validate/graphqlConfigSchema/graphqlConfigSchema.d.ts +0 -113
- package/dist/src/utils/validate/graphqlConfigSchema/graphqlConfigSchema.js +3 -17
- package/dist/src/utils/validate/graphqlConfigSchema/routeConfigSchema/routeConfigSchema.js +2 -2
- package/dist/src/utils/validate/index.d.ts +1 -0
- package/dist/src/utils/validate/index.js +1 -0
- package/dist/src/utils/validate/restConfigSchema/restConfigSchema.d.ts +0 -238
- package/dist/src/utils/validate/restConfigSchema/restConfigSchema.js +3 -17
- package/dist/src/utils/validate/restConfigSchema/routeConfigSchema/routeConfigSchema.js +2 -2
- package/dist/src/utils/validate/utils/entitiesSchema/entitiesSchema.d.ts +4 -3
- package/dist/src/utils/validate/utils/entitiesSchema/entitiesSchema.js +24 -71
- package/dist/src/utils/validate/utils/index.d.ts +0 -2
- package/dist/src/utils/validate/utils/index.js +0 -2
- package/dist/src/utils/validate/validateMockServerConfig.js +3 -1
- package/dist/src/utils/validate/wsConfigSchema/routeConfigSchema/routeConfigSchema.d.ts +25 -0
- package/dist/src/utils/validate/wsConfigSchema/routeConfigSchema/routeConfigSchema.js +36 -0
- package/dist/src/utils/validate/wsConfigSchema/wsConfigSchema.d.ts +45 -0
- package/dist/src/utils/validate/wsConfigSchema/wsConfigSchema.js +22 -0
- package/package.json +3 -1
- package/dist/src/utils/constants/checkModes.d.ts +0 -6
- package/dist/src/utils/constants/checkModes.js +0 -61
- package/dist/src/utils/helpers/config/index.d.ts +0 -1
- package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.d.ts +0 -20
- package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.js +0 -77
- package/dist/src/utils/helpers/entities/convertToEntityDescriptor/convertToEntityDescriptor.d.ts +0 -2
- package/dist/src/utils/helpers/entities/convertToEntityDescriptor/convertToEntityDescriptor.js +0 -15
- package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.d.ts +0 -2
- package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.js +0 -12
- package/dist/src/utils/types/checkModes.d.ts +0 -18
- package/dist/src/utils/validate/utils/checkModeSchema/checkModeSchema.d.ts +0 -23
- package/dist/src/utils/validate/utils/checkModeSchema/checkModeSchema.js +0 -53
- package/dist/src/utils/validate/utils/extendedDiscriminatedUnion/extendedDiscriminatedUnion.d.ts +0 -8
- package/dist/src/utils/validate/utils/extendedDiscriminatedUnion/extendedDiscriminatedUnion.js +0 -46
- package/dist/src/utils/validate/validateFlatMockServerConfig.d.ts +0 -2
- package/dist/src/utils/validate/validateFlatMockServerConfig.js +0 -53
- /package/dist/src/utils/types/{checkModes.js → ws.js} +0 -0
|
@@ -0,0 +1,52 @@
|
|
|
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 createWsConnectionRequestConfig () {
|
|
13
|
+
return createWsConnectionRequestConfig;
|
|
14
|
+
},
|
|
15
|
+
get createWsMessageRequestConfig () {
|
|
16
|
+
return createWsMessageRequestConfig;
|
|
17
|
+
},
|
|
18
|
+
get ws () {
|
|
19
|
+
return ws;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
const createConnectionRouteConfig = (config)=>{
|
|
23
|
+
if (typeof config === 'function') {
|
|
24
|
+
return {
|
|
25
|
+
data: config
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
data: config.handler,
|
|
30
|
+
entities: config.match
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
const createWsMessageRequestConfig = (handler)=>({
|
|
34
|
+
type: 'raw',
|
|
35
|
+
routes: [
|
|
36
|
+
{
|
|
37
|
+
data: handler
|
|
38
|
+
}
|
|
39
|
+
]
|
|
40
|
+
});
|
|
41
|
+
function createWsConnectionRequestConfig(config) {
|
|
42
|
+
return {
|
|
43
|
+
type: 'connection',
|
|
44
|
+
routes: [
|
|
45
|
+
createConnectionRouteConfig(config)
|
|
46
|
+
]
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const ws = {
|
|
50
|
+
connection: createWsConnectionRequestConfig,
|
|
51
|
+
message: createWsMessageRequestConfig
|
|
52
|
+
};
|
|
@@ -8,16 +8,18 @@ Object.defineProperty(exports, "createGraphQLRoute", {
|
|
|
8
8
|
return createGraphQLRoute;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
-
const _flat = require("flat");
|
|
12
11
|
const _helpers = require("../../../utils/helpers");
|
|
12
|
+
const _entities = require("../../entities");
|
|
13
13
|
const _helpers1 = require("./helpers");
|
|
14
14
|
const createGraphQLRoute = ({ server, graphQLRequestArtifacts })=>server.use((0, _helpers.asyncHandler)(async (request, response, next)=>{
|
|
15
|
+
var _matchedRouteConfig_config_entities;
|
|
15
16
|
var _matchedRouteConfig_config_settings, _matchedRouteConfig_config_settings1;
|
|
16
17
|
if (request.method !== 'GET' && request.method !== 'POST') return next();
|
|
17
18
|
const graphQLInput = (0, _helpers.getGraphQLInput)(request);
|
|
18
19
|
if (!graphQLInput.query) return next();
|
|
19
|
-
const query = (0, _helpers.
|
|
20
|
+
const query = (0, _helpers.parseGraphQLQuery)(graphQLInput.query);
|
|
20
21
|
if (!query) return next();
|
|
22
|
+
request.queries = request.query;
|
|
21
23
|
const matchedRequestArtifacts = (0, _helpers1.matchGraphQLRequestArtifacts)({
|
|
22
24
|
artifacts: graphQLRequestArtifacts,
|
|
23
25
|
meta: {
|
|
@@ -31,42 +33,34 @@ const createGraphQLRoute = ({ server, graphQLRequestArtifacts })=>server.use((0,
|
|
|
31
33
|
const matchedRouteConfig = matchedRequestArtifacts.find(({ config })=>{
|
|
32
34
|
if (!config.entities) return true;
|
|
33
35
|
const entityEntries = Object.entries(config.entities);
|
|
34
|
-
return entityEntries.every(([entityName,
|
|
35
|
-
const
|
|
36
|
-
if (
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
36
|
+
return entityEntries.every(([entityName, valueOrComparator])=>{
|
|
37
|
+
const actualEntity = entityName === 'variables' ? graphQLInput.variables : request[entityName];
|
|
38
|
+
if ((0, _helpers.isComparator)(valueOrComparator)) {
|
|
39
|
+
const comparator = valueOrComparator;
|
|
40
|
+
return (0, _helpers.resolveEntityValues)({
|
|
41
|
+
actual: actualEntity,
|
|
42
|
+
comparator
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const isVariables = entityName === 'variables';
|
|
46
|
+
if (isVariables) {
|
|
47
|
+
const comparator = (0, _entities.equals)(valueOrComparator);
|
|
45
48
|
return (0, _helpers.resolveEntityValues)({
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
checkMode: variablesDescriptor.checkMode,
|
|
49
|
-
oneOf: (_variablesDescriptor_oneOf = variablesDescriptor.oneOf) !== null && _variablesDescriptor_oneOf !== void 0 ? _variablesDescriptor_oneOf : false
|
|
49
|
+
actual: actualEntity,
|
|
50
|
+
comparator
|
|
50
51
|
});
|
|
51
52
|
}
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
const mappedEntityEntries = Object.entries(valueOrComparator);
|
|
54
|
+
return mappedEntityEntries.every(([entityPropertyKey, valueOrComparator])=>{
|
|
55
|
+
// ✅ important:
|
|
56
|
+
// transform header keys to lower case
|
|
57
|
+
// because browsers send headers in lowercase
|
|
56
58
|
const actualPropertyKey = entityName === 'headers' ? entityPropertyKey.toLowerCase() : entityPropertyKey;
|
|
57
59
|
const actualPropertyValue = actualEntity[actualPropertyKey];
|
|
58
|
-
|
|
59
|
-
return (0, _helpers.resolveEntityValues)({
|
|
60
|
-
actualValue: actualPropertyValue,
|
|
61
|
-
checkMode: entityPropertyDescriptor.checkMode
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
var _entityPropertyDescriptor_oneOf;
|
|
60
|
+
const comparator = (0, _helpers.isComparator)(valueOrComparator) ? valueOrComparator : (0, _entities.equals)(valueOrComparator);
|
|
65
61
|
return (0, _helpers.resolveEntityValues)({
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
checkMode: entityPropertyDescriptor.checkMode,
|
|
69
|
-
oneOf: (_entityPropertyDescriptor_oneOf = entityPropertyDescriptor.oneOf) !== null && _entityPropertyDescriptor_oneOf !== void 0 ? _entityPropertyDescriptor_oneOf : false
|
|
62
|
+
actual: actualPropertyValue,
|
|
63
|
+
comparator
|
|
70
64
|
});
|
|
71
65
|
});
|
|
72
66
|
});
|
|
@@ -90,12 +84,14 @@ const createGraphQLRoute = ({ server, graphQLRequestArtifacts })=>server.use((0,
|
|
|
90
84
|
interceptor: matchedRouteConfig.routeRequestInterceptor
|
|
91
85
|
});
|
|
92
86
|
}
|
|
93
|
-
var _matchedRouteConfig_config_entities;
|
|
94
87
|
const params = {
|
|
95
88
|
request,
|
|
96
89
|
response,
|
|
97
90
|
next,
|
|
98
91
|
entities: (_matchedRouteConfig_config_entities = matchedRouteConfig.config.entities) !== null && _matchedRouteConfig_config_entities !== void 0 ? _matchedRouteConfig_config_entities : {},
|
|
92
|
+
broadcast: (payload)=>{
|
|
93
|
+
request.context.broadcast(payload);
|
|
94
|
+
},
|
|
99
95
|
appendHeader: (field, value)=>{
|
|
100
96
|
response.append(field, value);
|
|
101
97
|
},
|
|
@@ -13,22 +13,8 @@ const calculateGraphQLRouteConfigWeight = (graphQLRouteConfig)=>{
|
|
|
13
13
|
const { entities } = graphQLRouteConfig;
|
|
14
14
|
if (!entities) return 0;
|
|
15
15
|
let routeConfigWeight = 0;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (query) routeConfigWeight += Object.keys(query).length;
|
|
20
|
-
if (variables) {
|
|
21
|
-
if (variables.checkMode) {
|
|
22
|
-
// ✅ important:
|
|
23
|
-
// check that actual value check modes does not have `value` for compare
|
|
24
|
-
if (variables.checkMode === 'exists' || variables.checkMode === 'notExists') {
|
|
25
|
-
routeConfigWeight += 1;
|
|
26
|
-
return routeConfigWeight;
|
|
27
|
-
}
|
|
28
|
-
routeConfigWeight += (0, _helpers.isPlainObject)(variables.value) ? Object.keys(variables.value).length : 1;
|
|
29
|
-
return routeConfigWeight;
|
|
30
|
-
}
|
|
31
|
-
routeConfigWeight += Object.keys(variables).length;
|
|
32
|
-
}
|
|
16
|
+
Object.values(entities).forEach((entityValue)=>{
|
|
17
|
+
routeConfigWeight += (0, _helpers.isPlainObject)(entityValue) ? Object.keys(entityValue).length : 1;
|
|
18
|
+
});
|
|
33
19
|
return routeConfigWeight;
|
|
34
20
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Express } from 'express';
|
|
2
|
+
import type { WebSocketServer } from 'ws';
|
|
2
3
|
import type { DatabaseConfig, GraphQLEntity, GraphQLOperationName, GraphQLOperationType } from '../../../utils/types';
|
|
3
4
|
declare global {
|
|
4
5
|
namespace Express {
|
|
@@ -14,6 +15,7 @@ declare global {
|
|
|
14
15
|
}
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
|
-
export declare const contextMiddleware: (server: Express, { database }: {
|
|
18
|
+
export declare const contextMiddleware: (server: Express, { database, ws }: {
|
|
18
19
|
database?: DatabaseConfig;
|
|
20
|
+
ws: WebSocketServer;
|
|
19
21
|
}) => void;
|
|
@@ -8,12 +8,31 @@ Object.defineProperty(exports, "contextMiddleware", {
|
|
|
8
8
|
return contextMiddleware;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
+
const _buffer = require("buffer");
|
|
12
|
+
const _ws = require("ws");
|
|
11
13
|
const _database = require("../../database");
|
|
12
14
|
const _helpers = require("../../../utils/helpers");
|
|
13
|
-
const contextMiddleware = (server, { database })=>{
|
|
15
|
+
const contextMiddleware = (server, { database, ws })=>{
|
|
16
|
+
const broadcast = (data)=>{
|
|
17
|
+
if (data === undefined) return;
|
|
18
|
+
for (const client of ws.clients){
|
|
19
|
+
if (client.readyState !== _ws.WebSocket.OPEN) continue;
|
|
20
|
+
if (typeof data === 'string') {
|
|
21
|
+
client.send(data);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const isBinary = data instanceof ArrayBuffer || ArrayBuffer.isView(data) || data instanceof Blob || _buffer.Buffer.isBuffer(data);
|
|
25
|
+
if (isBinary) {
|
|
26
|
+
client.send(data);
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
client.send(JSON.stringify(data));
|
|
30
|
+
}
|
|
31
|
+
};
|
|
14
32
|
let requestId = 0;
|
|
15
33
|
const context = {
|
|
16
|
-
orm: {}
|
|
34
|
+
orm: {},
|
|
35
|
+
broadcast: (payload)=>broadcast(payload)
|
|
17
36
|
};
|
|
18
37
|
if (database) {
|
|
19
38
|
const storage = (0, _database.createStorage)(database.data);
|
|
@@ -26,9 +45,9 @@ const contextMiddleware = (server, { database })=>{
|
|
|
26
45
|
request.timestamp = Date.now();
|
|
27
46
|
request.graphQL = null;
|
|
28
47
|
if (request.method === 'GET' || request.method === 'POST') {
|
|
29
|
-
const graphQLInput = (0, _helpers.getGraphQLInput)(request);
|
|
30
48
|
var _graphQLInput_query;
|
|
31
|
-
const
|
|
49
|
+
const graphQLInput = (0, _helpers.getGraphQLInput)(request);
|
|
50
|
+
const graphQLQuery = (0, _helpers.parseGraphQLQuery)((_graphQLInput_query = graphQLInput.query) !== null && _graphQLInput_query !== void 0 ? _graphQLInput_query : '');
|
|
32
51
|
if (graphQLInput.query && graphQLQuery) {
|
|
33
52
|
request.graphQL = {
|
|
34
53
|
operationType: graphQLQuery.operationType,
|
|
@@ -8,7 +8,7 @@ Object.defineProperty(exports, "cookieParseMiddleware", {
|
|
|
8
8
|
return cookieParseMiddleware;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
-
const _helpers = require("
|
|
11
|
+
const _helpers = require("../../../utils/helpers");
|
|
12
12
|
const cookieParseMiddleware = (server)=>{
|
|
13
13
|
server.use((request, _response, next)=>{
|
|
14
14
|
if (request.headers.cookie) {
|
|
@@ -34,17 +34,14 @@ const corsMiddleware = (server, cors)=>{
|
|
|
34
34
|
return allowedOrigin === origin;
|
|
35
35
|
});
|
|
36
36
|
if (isRequestOriginAllowed) {
|
|
37
|
+
var _cors_credentials, _cors_exposedHeaders;
|
|
37
38
|
response.setHeader('Access-Control-Allow-Origin', origin);
|
|
38
|
-
var _cors_credentials;
|
|
39
39
|
response.setHeader('Access-Control-Allow-Credentials', `${(_cors_credentials = cors.credentials) !== null && _cors_credentials !== void 0 ? _cors_credentials : _constants.DEFAULT.CORS.CREDENTIALS}`);
|
|
40
|
-
var _cors_exposedHeaders;
|
|
41
40
|
response.setHeader('Access-Control-Expose-Headers', (_cors_exposedHeaders = cors.exposedHeaders) !== null && _cors_exposedHeaders !== void 0 ? _cors_exposedHeaders : _constants.DEFAULT.CORS.EXPOSED_HEADERS);
|
|
42
41
|
if (request.method === 'OPTIONS') {
|
|
43
|
-
var _cors_methods;
|
|
42
|
+
var _cors_methods, _cors_allowedHeaders, _cors_maxAge;
|
|
44
43
|
response.setHeader('Access-Control-Allow-Methods', (_cors_methods = cors.methods) !== null && _cors_methods !== void 0 ? _cors_methods : _constants.DEFAULT.CORS.METHODS);
|
|
45
|
-
var _cors_allowedHeaders;
|
|
46
44
|
response.setHeader('Access-Control-Allow-Headers', (_cors_allowedHeaders = cors.allowedHeaders) !== null && _cors_allowedHeaders !== void 0 ? _cors_allowedHeaders : _constants.DEFAULT.CORS.ALLOWED_HEADERS);
|
|
47
|
-
var _cors_maxAge;
|
|
48
45
|
response.setHeader('Access-Control-Max-Age', (_cors_maxAge = cors.maxAge) !== null && _cors_maxAge !== void 0 ? _cors_maxAge : _constants.DEFAULT.CORS.MAX_AGE);
|
|
49
46
|
response.sendStatus(204);
|
|
50
47
|
return response.end();
|
|
@@ -16,8 +16,8 @@ function _interop_require_default(obj) {
|
|
|
16
16
|
}
|
|
17
17
|
const errorMiddleware = (server)=>{
|
|
18
18
|
server.use((error, request, response, next)=>{
|
|
19
|
-
console.error(_ansicolors.default.bgRed(`\nError on ${request.method} ${request.url} request\n`));
|
|
20
19
|
var _error_message;
|
|
20
|
+
console.error(_ansicolors.default.bgRed(`\nError on ${request.method} ${request.url} request\n`));
|
|
21
21
|
const message = `Message: ${(_error_message = error.message) !== null && _error_message !== void 0 ? _error_message : 'Internal server error'}\n\n${error.stack}`;
|
|
22
22
|
response.status(error.status || 500).send(message);
|
|
23
23
|
// ✅ important:
|
|
@@ -8,15 +8,15 @@ Object.defineProperty(exports, "createRestRoute", {
|
|
|
8
8
|
return createRestRoute;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
-
const _flat = require("flat");
|
|
12
11
|
const _helpers = require("../../../utils/helpers");
|
|
12
|
+
const _entities = require("../../entities");
|
|
13
13
|
const _helpers1 = require("./helpers");
|
|
14
14
|
const extractPathParams = (artifact, path)=>{
|
|
15
|
+
var _ref;
|
|
15
16
|
var _fullPath_match;
|
|
16
17
|
if (artifact.path instanceof RegExp) return {};
|
|
17
18
|
const fullPath = (0, _helpers.urlJoin)(artifact.baseUrl, artifact.path);
|
|
18
|
-
|
|
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 : [];
|
|
19
|
+
const keys = (_ref = (_fullPath_match = fullPath.match(/:[^/]+/g)) === null || _fullPath_match === void 0 ? void 0 : _fullPath_match.map((key)=>key.slice(1))) !== null && _ref !== void 0 ? _ref : [];
|
|
20
20
|
if (!keys.length) return {};
|
|
21
21
|
const match = path.match((0, _helpers1.generatePathRegex)(fullPath));
|
|
22
22
|
if (!match) return {};
|
|
@@ -26,8 +26,10 @@ const extractPathParams = (artifact, path)=>{
|
|
|
26
26
|
}, {});
|
|
27
27
|
};
|
|
28
28
|
const createRestRoute = ({ server, restRequestArtifacts })=>server.use((0, _helpers.asyncHandler)(async (request, response, next)=>{
|
|
29
|
+
var _matchedRouteConfig_config_entities;
|
|
29
30
|
var _matchedRouteConfig_config_settings, _matchedRouteConfig_config_settings1;
|
|
30
31
|
const requestMethod = request.method.toLowerCase();
|
|
32
|
+
request.queries = request.query;
|
|
31
33
|
const previousParams = {
|
|
32
34
|
...request.params
|
|
33
35
|
};
|
|
@@ -44,54 +46,34 @@ const createRestRoute = ({ server, restRequestArtifacts })=>server.use((0, _help
|
|
|
44
46
|
const { config } = artifact;
|
|
45
47
|
if (!config.entities) return true;
|
|
46
48
|
const entityEntries = Object.entries(config.entities);
|
|
47
|
-
return entityEntries.every(([entityName,
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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;
|
|
49
|
+
return entityEntries.every(([entityName, valueOrComparator])=>{
|
|
50
|
+
const actualEntity = request[entityName];
|
|
51
|
+
if ((0, _helpers.isComparator)(valueOrComparator)) {
|
|
52
|
+
const comparator = valueOrComparator;
|
|
60
53
|
return (0, _helpers.resolveEntityValues)({
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
checkMode: bodyDescriptor.checkMode,
|
|
64
|
-
oneOf: (_bodyDescriptor_oneOf = bodyDescriptor.oneOf) !== null && _bodyDescriptor_oneOf !== void 0 ? _bodyDescriptor_oneOf : false
|
|
54
|
+
actual: actualEntity,
|
|
55
|
+
comparator
|
|
65
56
|
});
|
|
66
57
|
}
|
|
67
|
-
const
|
|
68
|
-
if (
|
|
69
|
-
|
|
58
|
+
const isBody = entityName === 'body';
|
|
59
|
+
if (isBody) {
|
|
60
|
+
const comparator = (0, _entities.equals)(valueOrComparator);
|
|
70
61
|
return (0, _helpers.resolveEntityValues)({
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
checkMode: 'equals'
|
|
62
|
+
actual: actualEntity,
|
|
63
|
+
comparator
|
|
74
64
|
});
|
|
75
65
|
}
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
//
|
|
66
|
+
const mappedEntityEntries = Object.entries(valueOrComparator);
|
|
67
|
+
return mappedEntityEntries.every(([entityPropertyKey, valueOrComparator])=>{
|
|
68
|
+
// ✅ important:
|
|
69
|
+
// transform header keys to lower case
|
|
70
|
+
// because browsers send headers in lowercase
|
|
81
71
|
const actualPropertyKey = entityName === 'headers' ? entityPropertyKey.toLowerCase() : entityPropertyKey;
|
|
82
72
|
const actualPropertyValue = actualEntity[actualPropertyKey];
|
|
83
|
-
|
|
84
|
-
return (0, _helpers.resolveEntityValues)({
|
|
85
|
-
actualValue: actualPropertyValue,
|
|
86
|
-
checkMode: entityPropertyDescriptor.checkMode
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
var _entityPropertyDescriptor_oneOf;
|
|
73
|
+
const comparator = (0, _helpers.isComparator)(valueOrComparator) ? valueOrComparator : (0, _entities.equals)(valueOrComparator);
|
|
90
74
|
return (0, _helpers.resolveEntityValues)({
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
checkMode: entityPropertyDescriptor.checkMode,
|
|
94
|
-
oneOf: (_entityPropertyDescriptor_oneOf = entityPropertyDescriptor.oneOf) !== null && _entityPropertyDescriptor_oneOf !== void 0 ? _entityPropertyDescriptor_oneOf : false
|
|
75
|
+
actual: actualPropertyValue,
|
|
76
|
+
comparator
|
|
95
77
|
});
|
|
96
78
|
});
|
|
97
79
|
});
|
|
@@ -125,12 +107,14 @@ const createRestRoute = ({ server, restRequestArtifacts })=>server.use((0, _help
|
|
|
125
107
|
// set 'Cache-Control' header for explicit browsers response revalidate: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
|
|
126
108
|
// this code should place before response interceptors for giving opportunity to rewrite 'Cache-Control' header
|
|
127
109
|
if (request.method === 'GET') response.set('Cache-control', 'no-store');
|
|
128
|
-
var _matchedRouteConfig_config_entities;
|
|
129
110
|
const params = {
|
|
130
111
|
request,
|
|
131
112
|
response,
|
|
132
113
|
next,
|
|
133
114
|
entities: (_matchedRouteConfig_config_entities = matchedRouteConfig.config.entities) !== null && _matchedRouteConfig_config_entities !== void 0 ? _matchedRouteConfig_config_entities : {},
|
|
115
|
+
broadcast: (payload)=>{
|
|
116
|
+
request.context.broadcast(payload);
|
|
117
|
+
},
|
|
134
118
|
appendHeader: (field, value)=>{
|
|
135
119
|
response.append(field, value);
|
|
136
120
|
},
|
|
@@ -13,23 +13,8 @@ const calculateRestRouteConfigWeight = (restRouteConfig)=>{
|
|
|
13
13
|
const { entities } = restRouteConfig;
|
|
14
14
|
if (!entities) return 0;
|
|
15
15
|
let routeConfigWeight = 0;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
}
|
|
16
|
+
Object.values(entities).forEach((entityValue)=>{
|
|
17
|
+
routeConfigWeight += (0, _helpers.isPlainObject)(entityValue) ? Object.keys(entityValue).length : 1;
|
|
18
|
+
});
|
|
34
19
|
return routeConfigWeight;
|
|
35
20
|
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { WebSocketServer } from 'ws';
|
|
2
|
+
import type { WsRequestArtifact } from '../../../utils/types';
|
|
3
|
+
interface CreateWsRouteParams {
|
|
4
|
+
server: WebSocketServer;
|
|
5
|
+
wsRequestArtifacts: WsRequestArtifact[];
|
|
6
|
+
}
|
|
7
|
+
export declare const createWsRoute: ({ server, wsRequestArtifacts }: CreateWsRouteParams) => void;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "createWsRoute", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return createWsRoute;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _buffer = require("buffer");
|
|
12
|
+
const _ws = require("ws");
|
|
13
|
+
const _helpers = require("../../../utils/helpers");
|
|
14
|
+
const _entities = require("../../entities");
|
|
15
|
+
const sendWsData = (socket, data)=>{
|
|
16
|
+
if (data === undefined) return;
|
|
17
|
+
if (typeof data === 'string') {
|
|
18
|
+
socket.send(data);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const isBinary = data instanceof ArrayBuffer || ArrayBuffer.isView(data) || data instanceof Blob || _buffer.Buffer.isBuffer(data);
|
|
22
|
+
if (isBinary) {
|
|
23
|
+
socket.send(data);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
socket.send(JSON.stringify(data));
|
|
27
|
+
};
|
|
28
|
+
const broadcastWsData = (server, data)=>{
|
|
29
|
+
if (data === undefined) return;
|
|
30
|
+
for (const client of server.clients){
|
|
31
|
+
if (client.readyState !== _ws.WebSocket.OPEN) continue;
|
|
32
|
+
sendWsData(client, data);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const createWsRoute = ({ server, wsRequestArtifacts })=>{
|
|
36
|
+
server.on('connection', async (socket, request)=>{
|
|
37
|
+
var _request_headers_cookie;
|
|
38
|
+
const [requestPathname] = request.url.split('?');
|
|
39
|
+
const matchedRequestArtifacts = wsRequestArtifacts.filter((artifact)=>{
|
|
40
|
+
if (artifact.baseUrl === '/') return true;
|
|
41
|
+
return requestPathname === artifact.baseUrl || requestPathname.startsWith(`${artifact.baseUrl}/`);
|
|
42
|
+
});
|
|
43
|
+
const connectionArtifacts = matchedRequestArtifacts.filter((artifact)=>artifact.type === 'connection');
|
|
44
|
+
const rawArtifacts = matchedRequestArtifacts.filter((artifact)=>artifact.type === 'raw');
|
|
45
|
+
request.queries = (0, _helpers.parseQuery)(request.url);
|
|
46
|
+
request.cookies = (0, _helpers.parseCookie)((_request_headers_cookie = request.headers.cookie) !== null && _request_headers_cookie !== void 0 ? _request_headers_cookie : '');
|
|
47
|
+
for (const artifact of connectionArtifacts){
|
|
48
|
+
if (artifact.config.entities) {
|
|
49
|
+
const entityEntries = Object.entries(artifact.config.entities);
|
|
50
|
+
const isMatchedByEntities = entityEntries.every(([entityName, valueOrComparator])=>{
|
|
51
|
+
const actualEntity = request[entityName];
|
|
52
|
+
if ((0, _helpers.isComparator)(valueOrComparator)) {
|
|
53
|
+
const comparator = valueOrComparator;
|
|
54
|
+
return (0, _helpers.resolveEntityValues)({
|
|
55
|
+
actual: actualEntity,
|
|
56
|
+
comparator
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
const mappedEntityEntries = Object.entries(valueOrComparator);
|
|
60
|
+
return mappedEntityEntries.every(([entityPropertyKey, valueOrComparator])=>{
|
|
61
|
+
// ✅ important:
|
|
62
|
+
// transform header keys to lower case
|
|
63
|
+
// because browsers send headers in lowercase
|
|
64
|
+
const actualPropertyKey = entityName === 'headers' ? entityPropertyKey.toLowerCase() : entityPropertyKey;
|
|
65
|
+
const actualPropertyValue = actualEntity[actualPropertyKey];
|
|
66
|
+
const comparator = (0, _helpers.isComparator)(valueOrComparator) ? valueOrComparator : (0, _entities.equals)(valueOrComparator);
|
|
67
|
+
return (0, _helpers.resolveEntityValues)({
|
|
68
|
+
actual: actualPropertyValue,
|
|
69
|
+
comparator
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
if (!isMatchedByEntities) continue;
|
|
74
|
+
}
|
|
75
|
+
const params = {
|
|
76
|
+
broadcast: (data)=>broadcastWsData(server, data),
|
|
77
|
+
request,
|
|
78
|
+
socket,
|
|
79
|
+
send: (data)=>sendWsData(socket, data),
|
|
80
|
+
setDelay: async (delay)=>{
|
|
81
|
+
await (0, _helpers.sleep)(delay === Infinity ? 99999999 : delay);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
const resolvedData = await artifact.config.data(params);
|
|
85
|
+
sendWsData(socket, resolvedData);
|
|
86
|
+
}
|
|
87
|
+
socket.on('message', async (raw, isBinary)=>{
|
|
88
|
+
const frame = isBinary ? {
|
|
89
|
+
isBinary: true,
|
|
90
|
+
raw: raw
|
|
91
|
+
} : {
|
|
92
|
+
isBinary: false,
|
|
93
|
+
raw: raw.toString()
|
|
94
|
+
};
|
|
95
|
+
const params = {
|
|
96
|
+
...frame,
|
|
97
|
+
broadcast: (data)=>broadcastWsData(server, data),
|
|
98
|
+
socket,
|
|
99
|
+
send: (data)=>sendWsData(socket, data),
|
|
100
|
+
setDelay: async (delay)=>{
|
|
101
|
+
await (0, _helpers.sleep)(delay === Infinity ? 99999999 : delay);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
for (const artifact of rawArtifacts){
|
|
105
|
+
if (artifact.componentRequestInterceptor) {
|
|
106
|
+
await artifact.componentRequestInterceptor(params);
|
|
107
|
+
}
|
|
108
|
+
const resolvedData = await artifact.config.data(params);
|
|
109
|
+
const data = artifact.componentResponseInterceptor ? artifact.componentResponseInterceptor(resolvedData, params) : resolvedData;
|
|
110
|
+
sendWsData(socket, data);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './createWsRoute/createWsRoute';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
_export_star(require("./createWsRoute/createWsRoute"), exports);
|
|
6
|
+
function _export_star(from, to) {
|
|
7
|
+
Object.keys(from).forEach(function(k) {
|
|
8
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
|
|
9
|
+
Object.defineProperty(to, k, {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function() {
|
|
12
|
+
return from[k];
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
return from;
|
|
18
|
+
}
|
package/dist/src/index.d.ts
CHANGED
package/dist/src/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", {
|
|
3
3
|
value: true
|
|
4
4
|
});
|
|
5
|
+
_export_star(require("./core/entities"), exports);
|
|
5
6
|
_export_star(require("./core/functions"), exports);
|
|
6
7
|
_export_star(require("./server"), exports);
|
|
7
8
|
_export_star(require("./utils/types"), exports);
|