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.
Files changed (145) hide show
  1. package/README.md +229 -237
  2. package/dist/bin/build.d.ts +3 -3
  3. package/dist/bin/build.js +1 -11
  4. package/dist/bin/helpers/createTemplate.js +6 -6
  5. package/dist/bin/helpers/resolveConfigFile.d.ts +2 -2
  6. package/dist/bin/helpers/resolveConfigFile.js +2 -3
  7. package/dist/bin/helpers/resolveConfigFilePath.js +4 -4
  8. package/dist/bin/init.d.ts +2 -2
  9. package/dist/bin/init.js +7 -7
  10. package/dist/bin/run.d.ts +3 -3
  11. package/dist/bin/run.js +17 -24
  12. package/dist/bin/templates/ts/full/mock-server.config.ts +2 -2
  13. package/dist/bin/templates/ts/graphql/mock-server.config.ts +2 -2
  14. package/dist/bin/templates/ts/rest/mock-server.config.ts +2 -2
  15. package/dist/src/core/database/createDatabaseRoutes/helpers/search/search.js +3 -3
  16. package/dist/src/core/database/createDatabaseRoutes/storages/File/FileStorage.js +4 -4
  17. package/dist/src/core/database/createDatabaseRoutes/storages/File/FileWriter.js +2 -2
  18. package/dist/src/core/functions/graphql/graphql.d.ts +54 -0
  19. package/dist/src/core/functions/graphql/graphql.js +126 -0
  20. package/dist/src/core/functions/graphql/helpers/createQueueHandler/createQueueHandler.d.ts +5 -0
  21. package/dist/src/core/functions/graphql/helpers/createQueueHandler/createQueueHandler.js +35 -0
  22. package/dist/src/core/functions/graphql/helpers/index.d.ts +1 -0
  23. package/dist/src/core/{rest/createRestRoutes → functions/graphql}/helpers/index.js +1 -1
  24. package/dist/src/core/functions/index.d.ts +3 -0
  25. package/dist/src/core/functions/index.js +20 -0
  26. package/dist/src/core/functions/mock.d.ts +2 -0
  27. package/dist/src/core/functions/mock.js +11 -0
  28. package/dist/src/core/functions/rest/helpers/createFileHandler/createFileHandler.d.ts +2 -0
  29. package/dist/src/core/functions/rest/helpers/createFileHandler/createFileHandler.js +29 -0
  30. package/dist/src/core/functions/rest/helpers/createQueueHandler/createQueueHandler.d.ts +5 -0
  31. package/dist/src/core/functions/rest/helpers/createQueueHandler/createQueueHandler.js +35 -0
  32. package/dist/src/core/functions/rest/helpers/formatSsePayload/formatSsePayload.d.ts +5 -0
  33. package/dist/src/core/functions/rest/helpers/formatSsePayload/formatSsePayload.js +50 -0
  34. package/dist/src/core/functions/rest/helpers/index.d.ts +3 -0
  35. package/dist/src/core/functions/rest/helpers/index.js +20 -0
  36. package/dist/src/core/functions/rest/index.d.ts +1 -0
  37. package/dist/src/core/{graphql/createGraphQLRoutes/helpers → functions/rest}/index.js +1 -1
  38. package/dist/src/core/functions/rest/rest.d.ts +114 -0
  39. package/dist/src/core/functions/rest/rest.js +171 -0
  40. package/dist/src/core/graphql/createGraphQLRoute/createGraphQLRoute.d.ts +8 -0
  41. package/dist/src/core/graphql/createGraphQLRoute/createGraphQLRoute.js +158 -0
  42. package/dist/src/core/graphql/createGraphQLRoute/helpers/calculateGraphQLRouteConfigWeight/calculateGraphQLRouteConfigWeight.d.ts +2 -0
  43. package/dist/src/core/graphql/{createGraphQLRoutes/helpers/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.js → createGraphQLRoute/helpers/calculateGraphQLRouteConfigWeight/calculateGraphQLRouteConfigWeight.js} +3 -11
  44. package/dist/src/core/graphql/createGraphQLRoute/helpers/index.d.ts +3 -0
  45. package/dist/src/core/graphql/createGraphQLRoute/helpers/index.js +20 -0
  46. package/dist/src/core/graphql/createGraphQLRoute/helpers/matchGraphQLRequestArtifacts/matchGraphQLRequestArtifacts.d.ts +12 -0
  47. package/dist/src/core/graphql/createGraphQLRoute/helpers/matchGraphQLRequestArtifacts/matchGraphQLRequestArtifacts.js +28 -0
  48. package/dist/src/core/graphql/createGraphQLRoute/helpers/prepareGraphQLRequestArtifacts/prepareGraphQLRequestArtifacts.d.ts +2 -0
  49. package/dist/src/core/graphql/createGraphQLRoute/helpers/prepareGraphQLRequestArtifacts/prepareGraphQLRequestArtifacts.js +11 -0
  50. package/dist/src/core/graphql/index.d.ts +2 -1
  51. package/dist/src/core/graphql/index.js +2 -1
  52. package/dist/src/core/middlewares/contextMiddleware/contextMiddleware.d.ts +6 -4
  53. package/dist/src/core/rest/createRestRoute/createRestRoute.d.ts +8 -0
  54. package/dist/src/core/rest/createRestRoute/createRestRoute.js +190 -0
  55. package/dist/src/core/rest/createRestRoute/helpers/calculateRestRouteConfigWeight/calculateRestRouteConfigWeight.d.ts +2 -0
  56. package/dist/src/core/rest/createRestRoute/helpers/calculateRestRouteConfigWeight/calculateRestRouteConfigWeight.js +35 -0
  57. package/dist/src/core/rest/createRestRoute/helpers/index.d.ts +3 -0
  58. package/dist/src/core/rest/createRestRoute/helpers/index.js +20 -0
  59. package/dist/src/core/rest/createRestRoute/helpers/matchRestRequestArtifacts/matchRestRequestArtifacts.d.ts +11 -0
  60. package/dist/src/core/rest/createRestRoute/helpers/matchRestRequestArtifacts/matchRestRequestArtifacts.js +31 -0
  61. package/dist/src/core/rest/createRestRoute/helpers/prepareRestRequestArtifacts/prepareRestRequestArtifacts.d.ts +2 -0
  62. package/dist/src/core/rest/createRestRoute/helpers/prepareRestRequestArtifacts/prepareRestRequestArtifacts.js +37 -0
  63. package/dist/src/core/rest/index.d.ts +2 -1
  64. package/dist/src/core/rest/index.js +2 -1
  65. package/dist/src/index.d.ts +1 -0
  66. package/dist/src/index.js +1 -0
  67. package/dist/src/server/createMockServer/createMockServer.d.ts +1 -1
  68. package/dist/src/server/createMockServer/createMockServer.js +76 -45
  69. package/dist/src/server/index.d.ts +0 -8
  70. package/dist/src/server/index.js +0 -8
  71. package/dist/src/server/startMockServer/startMockServer.d.ts +2 -2
  72. package/dist/src/server/startMockServer/startMockServer.js +4 -3
  73. package/dist/src/utils/constants/checkModes.js +7 -7
  74. package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.js +1 -1
  75. package/dist/src/utils/helpers/files/isFileDescriptor/isFileDescriptor.js +2 -2
  76. package/dist/src/utils/helpers/files/isFilePathValid/isFilePathValid.js +3 -3
  77. package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.d.ts +3 -3
  78. package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.js +2 -2
  79. package/dist/src/utils/helpers/tests/createTmpDir.js +4 -4
  80. package/dist/src/utils/helpers/url/index.d.ts +1 -2
  81. package/dist/src/utils/helpers/url/index.js +1 -2
  82. package/dist/src/utils/helpers/url/normalizeUrl/normalizeUrl.d.ts +1 -0
  83. package/dist/src/utils/helpers/url/normalizeUrl/normalizeUrl.js +15 -0
  84. package/dist/src/utils/helpers/url/urlJoin/urlJoin.js +4 -4
  85. package/dist/src/utils/types/graphql.d.ts +44 -18
  86. package/dist/src/utils/types/rest.d.ts +44 -27
  87. package/dist/src/utils/types/server.d.ts +7 -24
  88. package/dist/src/utils/types/values.d.ts +2 -1
  89. package/dist/src/utils/validate/graphqlConfigSchema/graphqlConfigSchema.d.ts +41 -383
  90. package/dist/src/utils/validate/graphqlConfigSchema/graphqlConfigSchema.js +3 -3
  91. package/dist/src/utils/validate/graphqlConfigSchema/routeConfigSchema/routeConfigSchema.d.ts +3 -66
  92. package/dist/src/utils/validate/graphqlConfigSchema/routeConfigSchema/routeConfigSchema.js +7 -22
  93. package/dist/src/utils/validate/index.d.ts +0 -2
  94. package/dist/src/utils/validate/index.js +0 -2
  95. package/dist/src/utils/validate/queueSchema/queueSchema.d.ts +2 -11
  96. package/dist/src/utils/validate/queueSchema/queueSchema.js +7 -13
  97. package/dist/src/utils/validate/restConfigSchema/restConfigSchema.d.ts +101 -1309
  98. package/dist/src/utils/validate/restConfigSchema/restConfigSchema.js +3 -3
  99. package/dist/src/utils/validate/restConfigSchema/routeConfigSchema/routeConfigSchema.d.ts +3 -82
  100. package/dist/src/utils/validate/restConfigSchema/routeConfigSchema/routeConfigSchema.js +6 -28
  101. package/dist/src/utils/validate/settingsSchema/settingsSchema.d.ts +0 -3
  102. package/dist/src/utils/validate/settingsSchema/settingsSchema.js +0 -1
  103. package/dist/src/utils/validate/utils/checkModeSchema/checkModeSchema.js +6 -6
  104. package/dist/src/utils/validate/utils/entitiesSchema/entitiesSchema.js +4 -4
  105. package/dist/src/utils/validate/utils/jsonSchema/jsonSchema.js +3 -3
  106. package/dist/src/utils/validate/utils/requiredPropertiesSchema/requiredPropertiesSchema.js +1 -1
  107. package/dist/src/utils/validate/utils/sharedSchema/sharedSchema.js +3 -3
  108. package/dist/src/utils/validate/validateMockServerConfig.js +18 -8
  109. package/package.json +17 -40
  110. package/LICENSE +0 -21
  111. package/dist/bin/runFlatConfig.d.ts +0 -5
  112. package/dist/bin/runFlatConfig.js +0 -36
  113. package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.d.ts +0 -9
  114. package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.js +0 -160
  115. package/dist/src/core/graphql/createGraphQLRoutes/helpers/index.d.ts +0 -1
  116. package/dist/src/core/graphql/createGraphQLRoutes/helpers/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.d.ts +0 -2
  117. package/dist/src/core/rest/createRestRoutes/createRestRoutes.d.ts +0 -9
  118. package/dist/src/core/rest/createRestRoutes/createRestRoutes.js +0 -184
  119. package/dist/src/core/rest/createRestRoutes/helpers/index.d.ts +0 -1
  120. package/dist/src/core/rest/createRestRoutes/helpers/prepareRestRequestConfigs/prepareRestRequestConfigs.d.ts +0 -2
  121. package/dist/src/core/rest/createRestRoutes/helpers/prepareRestRequestConfigs/prepareRestRequestConfigs.js +0 -67
  122. package/dist/src/server/createDatabaseMockServer/createDatabaseMockServer.d.ts +0 -3
  123. package/dist/src/server/createDatabaseMockServer/createDatabaseMockServer.js +0 -62
  124. package/dist/src/server/createFlatMockServer/createFlatMockServer.d.ts +0 -3
  125. package/dist/src/server/createFlatMockServer/createFlatMockServer.js +0 -128
  126. package/dist/src/server/createGraphQLMockServer/createGraphQLMockServer.d.ts +0 -3
  127. package/dist/src/server/createGraphQLMockServer/createGraphQLMockServer.js +0 -67
  128. package/dist/src/server/createRestMockServer/createRestMockServer.d.ts +0 -3
  129. package/dist/src/server/createRestMockServer/createRestMockServer.js +0 -67
  130. package/dist/src/server/startDatabaseMockServer/startDatabaseMockServer.d.ts +0 -4
  131. package/dist/src/server/startDatabaseMockServer/startDatabaseMockServer.js +0 -29
  132. package/dist/src/server/startFlatMockServer/startFlatMockServer.d.ts +0 -4
  133. package/dist/src/server/startFlatMockServer/startFlatMockServer.js +0 -30
  134. package/dist/src/server/startGraphQLMockServer/startGraphQLMockServer.d.ts +0 -4
  135. package/dist/src/server/startGraphQLMockServer/startGraphQLMockServer.js +0 -29
  136. package/dist/src/server/startRestMockServer/startRestMockServer.d.ts +0 -4
  137. package/dist/src/server/startRestMockServer/startRestMockServer.js +0 -29
  138. package/dist/src/utils/helpers/url/getUrlParts/getUrlParts.d.ts +0 -1
  139. package/dist/src/utils/helpers/url/getUrlParts/getUrlParts.js +0 -12
  140. package/dist/src/utils/helpers/url/removeLeadingAndTrailingSlashes/removeLeadingAndTrailingSlashes.d.ts +0 -1
  141. package/dist/src/utils/helpers/url/removeLeadingAndTrailingSlashes/removeLeadingAndTrailingSlashes.js +0 -11
  142. package/dist/src/utils/validate/isOnlyRequestedDataResolvingPropertyExists.d.ts +0 -4
  143. package/dist/src/utils/validate/isOnlyRequestedDataResolvingPropertyExists.js +0 -16
  144. package/dist/src/utils/validate/validateApiMockServerConfig.d.ts +0 -2
  145. package/dist/src/utils/validate/validateApiMockServerConfig.js +0 -48
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "createFileHandler", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return createFileHandler;
9
+ }
10
+ });
11
+ const _fs = /*#__PURE__*/ _interop_require_default(require("fs"));
12
+ const _path = /*#__PURE__*/ _interop_require_default(require("path"));
13
+ const _helpers = require("../../../../../utils/helpers");
14
+ function _interop_require_default(obj) {
15
+ return obj && obj.__esModule ? obj : {
16
+ default: obj
17
+ };
18
+ }
19
+ const createFileHandler = (filePath)=>({ response, setHeader, next })=>{
20
+ if (!(0, _helpers.isFilePathValid)(filePath)) {
21
+ return next();
22
+ }
23
+ const buffer = _fs.default.readFileSync(_path.default.resolve(filePath));
24
+ const fileName = filePath.replaceAll('\\', '/').split('/').at(-1);
25
+ const fileExtension = fileName.split('.').at(-1);
26
+ response.type(fileExtension);
27
+ setHeader('Content-Disposition', `attachment; filename="${fileName}"`);
28
+ return buffer;
29
+ };
@@ -0,0 +1,5 @@
1
+ import type { RestDataResponse, RestDataResponseFunction, RestMethod } from '../../../../../utils/types';
2
+ export declare const createQueueHandler: <Method extends RestMethod>(queue: {
3
+ data: RestDataResponse<Method>;
4
+ time?: number;
5
+ }[]) => RestDataResponseFunction<Method>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "createQueueHandler", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return createQueueHandler;
9
+ }
10
+ });
11
+ const createQueueHandler = (queue)=>{
12
+ let queueIndex = 0;
13
+ let timeoutInProgress = false;
14
+ const updateQueueIndex = ()=>{
15
+ queueIndex = queue.length - 1 === queueIndex ? 0 : queueIndex + 1;
16
+ };
17
+ return async (params)=>{
18
+ if (!queue.length) {
19
+ return params.next();
20
+ }
21
+ const queueItem = queue[queueIndex];
22
+ const { time } = queueItem;
23
+ if (time && !timeoutInProgress) {
24
+ timeoutInProgress = true;
25
+ setTimeout(()=>{
26
+ timeoutInProgress = false;
27
+ updateQueueIndex();
28
+ }, time);
29
+ }
30
+ if (!time) {
31
+ updateQueueIndex();
32
+ }
33
+ return typeof queueItem.data === 'function' ? queueItem.data(params) : queueItem.data;
34
+ };
35
+ };
@@ -0,0 +1,5 @@
1
+ export declare const formatSsePayload: (data: string, meta?: {
2
+ event?: string;
3
+ id?: string;
4
+ retry?: number;
5
+ }) => string;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "formatSsePayload", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return formatSsePayload;
9
+ }
10
+ });
11
+ const _zod = /*#__PURE__*/ _interop_require_default(require("zod"));
12
+ function _interop_require_default(obj) {
13
+ return obj && obj.__esModule ? obj : {
14
+ default: obj
15
+ };
16
+ }
17
+ const sseMetaSchema = _zod.default.object({
18
+ event: _zod.default.string().optional(),
19
+ id: _zod.default.string().optional(),
20
+ retry: _zod.default.number().int().nonnegative().optional()
21
+ }).optional();
22
+ // ✅ important:
23
+ // SSE is a line-based protocol. `id` and `event` must be single-line values.
24
+ // Strip CR/LF so metadata cannot break frame structure.
25
+ const normalizeSseMetaValue = (value)=>value.replaceAll('\r', '').replaceAll('\n', '');
26
+ const formatSsePayload = (data, meta)=>{
27
+ const parseMetaResult = sseMetaSchema.safeParse(meta);
28
+ if (!parseMetaResult.success) {
29
+ var _parseMetaResult_error_issues_;
30
+ throw new Error(`Invalid SSE meta: ${(_parseMetaResult_error_issues_ = parseMetaResult.error.issues[0]) === null || _parseMetaResult_error_issues_ === void 0 ? void 0 : _parseMetaResult_error_issues_.message}`);
31
+ }
32
+ const parsedMeta = parseMetaResult.data;
33
+ const lines = [];
34
+ if (parsedMeta === null || parsedMeta === void 0 ? void 0 : parsedMeta.id) {
35
+ lines.push(`id: ${normalizeSseMetaValue(parsedMeta.id)}`);
36
+ }
37
+ if (parsedMeta === null || parsedMeta === void 0 ? void 0 : parsedMeta.event) {
38
+ lines.push(`event: ${normalizeSseMetaValue(parsedMeta.event)}`);
39
+ }
40
+ if ((parsedMeta === null || parsedMeta === void 0 ? void 0 : parsedMeta.retry) != null) {
41
+ lines.push(`retry: ${parsedMeta.retry}`);
42
+ }
43
+ // ✅ important:
44
+ // Multiline payloads are encoded as multiple `data:` lines.
45
+ // SSE clients concatenate consecutive `data:` lines with '\n' into one message.
46
+ data.split(/\r\n|\r|\n/).forEach((line)=>{
47
+ lines.push(`data: ${line}`);
48
+ });
49
+ return `${lines.join('\n')}\n\n`;
50
+ };
@@ -0,0 +1,3 @@
1
+ export * from './createFileHandler/createFileHandler';
2
+ export * from './createQueueHandler/createQueueHandler';
3
+ export * from './formatSsePayload/formatSsePayload';
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ _export_star(require("./createFileHandler/createFileHandler"), exports);
6
+ _export_star(require("./createQueueHandler/createQueueHandler"), exports);
7
+ _export_star(require("./formatSsePayload/formatSsePayload"), 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 @@
1
+ export * from './rest';
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- _export_star(require("./prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs"), exports);
5
+ _export_star(require("./rest"), exports);
6
6
  function _export_star(from, to) {
7
7
  Object.keys(from).forEach(function(k) {
8
8
  if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
@@ -0,0 +1,114 @@
1
+ import type { BaseRestRequestConfig, Data, RestEntitiesByEntityName, RestFileResponse, RestMethod, RestParams, RestRequestConfig, RestSettings } from '../../../utils/types';
2
+ interface RestRequestInput {
3
+ body?: unknown;
4
+ params?: unknown;
5
+ query?: unknown;
6
+ response?: Data;
7
+ }
8
+ type ReservedRestConfigKeys = {
9
+ [K in 'file' | 'handler' | 'match' | 'queue' | 'response']?: never;
10
+ };
11
+ type RestInlineResponse<Response> = Response extends Record<string, unknown> ? Response & ReservedRestConfigKeys : Response;
12
+ type RestFunction<Method extends RestMethod, Options extends RestRequestInput, AdditionalParams = {}> = (params: RestParams<Method, Options['query'], Options['body'], Options['params'], Options['response']> & AdditionalParams) => Options['response'] | Promise<Options['response']>;
13
+ interface RestResponseObject<Method extends RestMethod, Response> {
14
+ match?: RestEntitiesByEntityName<Method>;
15
+ response: Response;
16
+ }
17
+ interface RestHandlerObject<Method extends RestMethod, Options extends RestRequestInput> {
18
+ handler: RestFunction<Method, Options>;
19
+ match?: RestEntitiesByEntityName<Method>;
20
+ }
21
+ interface RestFileObject<Method extends RestMethod> {
22
+ file: RestFileResponse;
23
+ match?: RestEntitiesByEntityName<Method>;
24
+ }
25
+ interface RestQueueObject<Method extends RestMethod, Options extends RestRequestInput> {
26
+ match?: RestEntitiesByEntityName<Method>;
27
+ queue: Array<{
28
+ file: RestFileResponse;
29
+ time?: number;
30
+ } | {
31
+ handler: RestFunction<Method, Options>;
32
+ time?: number;
33
+ } | {
34
+ response: Options['response'];
35
+ time?: number;
36
+ }>;
37
+ }
38
+ interface RestSseClient<Response extends string> {
39
+ close: () => void;
40
+ send: (data: Response, meta?: {
41
+ event?: string;
42
+ id?: string;
43
+ retry?: number;
44
+ }) => void;
45
+ }
46
+ interface SseRestHandlerObject<Method extends 'get' | 'post', Options extends RestRequestInput, Response extends string> {
47
+ handler: RestFunction<Method, Options, {
48
+ client: RestSseClient<Response>;
49
+ }>;
50
+ match?: RestEntitiesByEntityName<Method>;
51
+ }
52
+ export declare const rest: {
53
+ delete: {
54
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestResponseObject<"delete", Options["response"]>, settings?: RestSettings): BaseRestRequestConfig<"delete">;
55
+ (path: RestRequestConfig["path"], config: RestFileObject<"delete">, settings?: RestSettings): BaseRestRequestConfig<"delete">;
56
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestHandlerObject<"delete", Options>, settings?: RestSettings): BaseRestRequestConfig<"delete">;
57
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestFunction<"delete", Options, {}>, settings?: RestSettings): BaseRestRequestConfig<"delete">;
58
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestQueueObject<"delete", Options>, settings?: RestSettings): BaseRestRequestConfig<"delete">;
59
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestInlineResponse<Options["response"]>, settings?: RestSettings): BaseRestRequestConfig<"delete">;
60
+ };
61
+ get: {
62
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestResponseObject<"get", Options["response"]>, settings?: RestSettings): BaseRestRequestConfig<"get">;
63
+ (path: RestRequestConfig["path"], config: RestFileObject<"get">, settings?: RestSettings): BaseRestRequestConfig<"get">;
64
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestHandlerObject<"get", Options>, settings?: RestSettings): BaseRestRequestConfig<"get">;
65
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestFunction<"get", Options, {}>, settings?: RestSettings): BaseRestRequestConfig<"get">;
66
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestQueueObject<"get", Options>, settings?: RestSettings): BaseRestRequestConfig<"get">;
67
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestInlineResponse<Options["response"]>, settings?: RestSettings): BaseRestRequestConfig<"get">;
68
+ };
69
+ options: {
70
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestResponseObject<"options", Options["response"]>, settings?: RestSettings): BaseRestRequestConfig<"options">;
71
+ (path: RestRequestConfig["path"], config: RestFileObject<"options">, settings?: RestSettings): BaseRestRequestConfig<"options">;
72
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestHandlerObject<"options", Options>, settings?: RestSettings): BaseRestRequestConfig<"options">;
73
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestFunction<"options", Options, {}>, settings?: RestSettings): BaseRestRequestConfig<"options">;
74
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestQueueObject<"options", Options>, settings?: RestSettings): BaseRestRequestConfig<"options">;
75
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestInlineResponse<Options["response"]>, settings?: RestSettings): BaseRestRequestConfig<"options">;
76
+ };
77
+ patch: {
78
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestResponseObject<"patch", Options["response"]>, settings?: RestSettings): BaseRestRequestConfig<"patch">;
79
+ (path: RestRequestConfig["path"], config: RestFileObject<"patch">, settings?: RestSettings): BaseRestRequestConfig<"patch">;
80
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestHandlerObject<"patch", Options>, settings?: RestSettings): BaseRestRequestConfig<"patch">;
81
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestFunction<"patch", Options, {}>, settings?: RestSettings): BaseRestRequestConfig<"patch">;
82
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestQueueObject<"patch", Options>, settings?: RestSettings): BaseRestRequestConfig<"patch">;
83
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestInlineResponse<Options["response"]>, settings?: RestSettings): BaseRestRequestConfig<"patch">;
84
+ };
85
+ post: {
86
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestResponseObject<"post", Options["response"]>, settings?: RestSettings): BaseRestRequestConfig<"post">;
87
+ (path: RestRequestConfig["path"], config: RestFileObject<"post">, settings?: RestSettings): BaseRestRequestConfig<"post">;
88
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestHandlerObject<"post", Options>, settings?: RestSettings): BaseRestRequestConfig<"post">;
89
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestFunction<"post", Options, {}>, settings?: RestSettings): BaseRestRequestConfig<"post">;
90
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestQueueObject<"post", Options>, settings?: RestSettings): BaseRestRequestConfig<"post">;
91
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestInlineResponse<Options["response"]>, settings?: RestSettings): BaseRestRequestConfig<"post">;
92
+ };
93
+ put: {
94
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestResponseObject<"put", Options["response"]>, settings?: RestSettings): BaseRestRequestConfig<"put">;
95
+ (path: RestRequestConfig["path"], config: RestFileObject<"put">, settings?: RestSettings): BaseRestRequestConfig<"put">;
96
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestHandlerObject<"put", Options>, settings?: RestSettings): BaseRestRequestConfig<"put">;
97
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestFunction<"put", Options, {}>, settings?: RestSettings): BaseRestRequestConfig<"put">;
98
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestQueueObject<"put", Options>, settings?: RestSettings): BaseRestRequestConfig<"put">;
99
+ <Options extends RestRequestInput = Partial<RestRequestInput>>(path: RestRequestConfig["path"], config: RestInlineResponse<Options["response"]>, settings?: RestSettings): BaseRestRequestConfig<"put">;
100
+ };
101
+ sse: {
102
+ <Options extends RestRequestInput = Partial<RestRequestInput>, Response extends string = string>(path: RestRequestConfig["path"], config: SseRestHandlerObject<"get", Options, Response>, settings?: RestSettings): BaseRestRequestConfig<"get">;
103
+ <Options extends RestRequestInput = Partial<RestRequestInput>, Response extends string = string>(path: RestRequestConfig["path"], config: RestFunction<"get", Options, {
104
+ client: RestSseClient<Response>;
105
+ }>, settings?: RestSettings): BaseRestRequestConfig<"get">;
106
+ };
107
+ stream: {
108
+ <Options extends RestRequestInput = Partial<RestRequestInput>, Response extends string = string>(path: RestRequestConfig["path"], config: SseRestHandlerObject<"post", Options, Response>, settings?: RestSettings): BaseRestRequestConfig<"post">;
109
+ <Options extends RestRequestInput = Partial<RestRequestInput>, Response extends string = string>(path: RestRequestConfig["path"], config: RestFunction<"post", Options, {
110
+ client: RestSseClient<Response>;
111
+ }>, settings?: RestSettings): BaseRestRequestConfig<"post">;
112
+ };
113
+ };
114
+ export {};
@@ -0,0 +1,171 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "rest", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return rest;
9
+ }
10
+ });
11
+ const _helpers = require("./helpers");
12
+ const resolveConfigType = (config)=>{
13
+ if (typeof config === 'function') return {
14
+ type: 'inlineHandler',
15
+ config
16
+ };
17
+ if (typeof config !== 'object' || config === null) return {
18
+ type: 'inlineResponse',
19
+ config
20
+ };
21
+ if ('queue' in config) return {
22
+ type: 'queue',
23
+ config
24
+ };
25
+ if ('file' in config) return {
26
+ type: 'file',
27
+ config
28
+ };
29
+ if ('response' in config) return {
30
+ type: 'data',
31
+ config
32
+ };
33
+ if ('handler' in config) return {
34
+ type: 'handler',
35
+ config
36
+ };
37
+ return {
38
+ type: 'inlineResponse',
39
+ config
40
+ };
41
+ };
42
+ const createConfigResolver = (config, settings = {})=>{
43
+ const resolvedConfig = resolveConfigType(config);
44
+ switch(resolvedConfig.type){
45
+ case 'inlineResponse':
46
+ return {
47
+ data: resolvedConfig.config,
48
+ settings
49
+ };
50
+ case 'data':
51
+ {
52
+ return {
53
+ data: resolvedConfig.config.response,
54
+ entities: resolvedConfig.config.match,
55
+ settings
56
+ };
57
+ }
58
+ case 'file':
59
+ {
60
+ return {
61
+ data: (0, _helpers.createFileHandler)(resolvedConfig.config.file),
62
+ entities: resolvedConfig.config.match,
63
+ settings
64
+ };
65
+ }
66
+ case 'queue':
67
+ {
68
+ const normalizedQueue = resolvedConfig.config.queue.map((item)=>{
69
+ if ('handler' in item) {
70
+ return {
71
+ data: item.handler,
72
+ time: item.time
73
+ };
74
+ }
75
+ if ('response' in item) {
76
+ return {
77
+ data: item.response,
78
+ time: item.time
79
+ };
80
+ }
81
+ if ('file' in item) {
82
+ return {
83
+ data: (0, _helpers.createFileHandler)(item.file),
84
+ time: item.time
85
+ };
86
+ }
87
+ throw new Error(`Unexpected queue item kind: ${JSON.stringify(item, null, 2)}`);
88
+ });
89
+ return {
90
+ data: (0, _helpers.createQueueHandler)(normalizedQueue),
91
+ entities: resolvedConfig.config.match,
92
+ settings
93
+ };
94
+ }
95
+ case 'inlineHandler':
96
+ return {
97
+ data: resolvedConfig.config,
98
+ settings
99
+ };
100
+ case 'handler':
101
+ {
102
+ return {
103
+ data: resolvedConfig.config.handler,
104
+ entities: resolvedConfig.config.match,
105
+ settings
106
+ };
107
+ }
108
+ default:
109
+ {
110
+ throw new Error(`Unexpected route config kind: ${JSON.stringify(config, null, 2)}`);
111
+ }
112
+ }
113
+ };
114
+ const createRestFactory = (method)=>{
115
+ function createRequestConfig(path, config, settings) {
116
+ return {
117
+ method,
118
+ path,
119
+ routes: [
120
+ createConfigResolver(config, settings)
121
+ ]
122
+ };
123
+ }
124
+ return createRequestConfig;
125
+ };
126
+ const createSseRestFactory = (method)=>{
127
+ function createSseRequestConfig(path, config, settings) {
128
+ const normalizedConfig = typeof config === 'function' ? {
129
+ handler: config
130
+ } : config;
131
+ const wrapperHandler = (params)=>{
132
+ params.setHeader('connection', 'keep-alive');
133
+ params.setHeader('content-type', 'text/event-stream');
134
+ params.setHeader('cache-control', 'no-cache');
135
+ const client = {
136
+ send (message, meta) {
137
+ const payload = (0, _helpers.formatSsePayload)(message, meta);
138
+ params.response.write(payload);
139
+ },
140
+ close () {
141
+ params.response.end();
142
+ }
143
+ };
144
+ return normalizedConfig.handler({
145
+ ...params,
146
+ client
147
+ });
148
+ };
149
+ return {
150
+ method,
151
+ path,
152
+ routes: [
153
+ createConfigResolver({
154
+ handler: wrapperHandler,
155
+ match: normalizedConfig.match
156
+ }, settings)
157
+ ]
158
+ };
159
+ }
160
+ return createSseRequestConfig;
161
+ };
162
+ const rest = {
163
+ delete: createRestFactory('delete'),
164
+ get: createRestFactory('get'),
165
+ options: createRestFactory('options'),
166
+ patch: createRestFactory('patch'),
167
+ post: createRestFactory('post'),
168
+ put: createRestFactory('put'),
169
+ sse: createSseRestFactory('get'),
170
+ stream: createSseRestFactory('post')
171
+ };
@@ -0,0 +1,8 @@
1
+ import type { Express } from 'express';
2
+ import type { GraphQLRequestArtifact } from '../../../utils/types';
3
+ interface CreateGraphQLRouteParams {
4
+ graphQLRequestArtifacts: GraphQLRequestArtifact[];
5
+ server: Express;
6
+ }
7
+ export declare const createGraphQLRoute: ({ server, graphQLRequestArtifacts }: CreateGraphQLRouteParams) => Express;
8
+ export {};
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "createGraphQLRoute", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return createGraphQLRoute;
9
+ }
10
+ });
11
+ const _flat = require("flat");
12
+ const _helpers = require("../../../utils/helpers");
13
+ const _helpers1 = require("./helpers");
14
+ const createGraphQLRoute = ({ server, graphQLRequestArtifacts })=>server.use((0, _helpers.asyncHandler)(async (request, response, next)=>{
15
+ var _matchedRouteConfig_config_settings, _matchedRouteConfig_config_settings1;
16
+ if (request.method !== 'GET' && request.method !== 'POST') return next();
17
+ const graphQLInput = (0, _helpers.getGraphQLInput)(request);
18
+ if (!graphQLInput.query) return next();
19
+ const query = (0, _helpers.parseQuery)(graphQLInput.query);
20
+ if (!query) return next();
21
+ const matchedRequestArtifacts = (0, _helpers1.matchGraphQLRequestArtifacts)({
22
+ artifacts: graphQLRequestArtifacts,
23
+ meta: {
24
+ path: (0, _helpers.normalizeUrl)(request.path),
25
+ query: graphQLInput.query,
26
+ operationType: query.operationType,
27
+ operationName: query.operationName
28
+ }
29
+ });
30
+ if (!matchedRequestArtifacts.length) return next();
31
+ const matchedRouteConfig = matchedRequestArtifacts.find(({ config })=>{
32
+ if (!config.entities) return true;
33
+ const entityEntries = Object.entries(config.entities);
34
+ return entityEntries.every(([entityName, entityDescriptorOrValue])=>{
35
+ const isEntityVariablesByTopLevelDescriptor = entityName === 'variables' && (0, _helpers.isEntityDescriptor)(entityDescriptorOrValue);
36
+ if (isEntityVariablesByTopLevelDescriptor) {
37
+ const variablesDescriptor = entityDescriptorOrValue;
38
+ if (variablesDescriptor.checkMode === 'exists' || variablesDescriptor.checkMode === 'notExists') {
39
+ return (0, _helpers.resolveEntityValues)({
40
+ actualValue: graphQLInput.variables,
41
+ checkMode: variablesDescriptor.checkMode
42
+ });
43
+ }
44
+ var _variablesDescriptor_oneOf;
45
+ return (0, _helpers.resolveEntityValues)({
46
+ actualValue: graphQLInput.variables,
47
+ descriptorValue: variablesDescriptor.value,
48
+ checkMode: variablesDescriptor.checkMode,
49
+ oneOf: (_variablesDescriptor_oneOf = variablesDescriptor.oneOf) !== null && _variablesDescriptor_oneOf !== void 0 ? _variablesDescriptor_oneOf : false
50
+ });
51
+ }
52
+ const actualEntity = (0, _flat.flatten)(entityName === 'variables' ? graphQLInput.variables : request[entityName]);
53
+ const entityValueEntries = Object.entries(entityDescriptorOrValue);
54
+ return entityValueEntries.every(([entityPropertyKey, entityPropertyDescriptorOrValue])=>{
55
+ const entityPropertyDescriptor = (0, _helpers.convertToEntityDescriptor)(entityPropertyDescriptorOrValue);
56
+ const actualPropertyKey = entityName === 'headers' ? entityPropertyKey.toLowerCase() : entityPropertyKey;
57
+ const actualPropertyValue = actualEntity[actualPropertyKey];
58
+ if (entityPropertyDescriptor.checkMode === 'exists' || entityPropertyDescriptor.checkMode === 'notExists') {
59
+ return (0, _helpers.resolveEntityValues)({
60
+ actualValue: actualPropertyValue,
61
+ checkMode: entityPropertyDescriptor.checkMode
62
+ });
63
+ }
64
+ var _entityPropertyDescriptor_oneOf;
65
+ return (0, _helpers.resolveEntityValues)({
66
+ actualValue: actualPropertyValue,
67
+ descriptorValue: entityPropertyDescriptor.value,
68
+ checkMode: entityPropertyDescriptor.checkMode,
69
+ oneOf: (_entityPropertyDescriptor_oneOf = entityPropertyDescriptor.oneOf) !== null && _entityPropertyDescriptor_oneOf !== void 0 ? _entityPropertyDescriptor_oneOf : false
70
+ });
71
+ });
72
+ });
73
+ });
74
+ if (!matchedRouteConfig) return next();
75
+ if (matchedRouteConfig.componentRequestInterceptor) {
76
+ await (0, _helpers.callRequestInterceptor)({
77
+ request,
78
+ interceptor: matchedRouteConfig.componentRequestInterceptor
79
+ });
80
+ }
81
+ if (matchedRouteConfig.requestRequestInterceptor) {
82
+ await (0, _helpers.callRequestInterceptor)({
83
+ request,
84
+ interceptor: matchedRouteConfig.requestRequestInterceptor
85
+ });
86
+ }
87
+ if (matchedRouteConfig.routeRequestInterceptor) {
88
+ await (0, _helpers.callRequestInterceptor)({
89
+ request,
90
+ interceptor: matchedRouteConfig.routeRequestInterceptor
91
+ });
92
+ }
93
+ var _matchedRouteConfig_config_entities;
94
+ const params = {
95
+ request,
96
+ response,
97
+ next,
98
+ entities: (_matchedRouteConfig_config_entities = matchedRouteConfig.config.entities) !== null && _matchedRouteConfig_config_entities !== void 0 ? _matchedRouteConfig_config_entities : {},
99
+ appendHeader: (field, value)=>{
100
+ response.append(field, value);
101
+ },
102
+ attachment: (filename)=>{
103
+ response.attachment(filename);
104
+ },
105
+ clearCookie: (name, options)=>{
106
+ response.clearCookie(name, options);
107
+ },
108
+ getCookie: (name)=>request.cookies[name],
109
+ getRequestHeader: (field)=>request.headers[field],
110
+ getRequestHeaders: ()=>request.headers,
111
+ getResponseHeader: (field)=>response.getHeader(field),
112
+ getResponseHeaders: ()=>response.getHeaders(),
113
+ setCookie: (name, value, options)=>{
114
+ if (options) {
115
+ response.cookie(name, value, options);
116
+ return;
117
+ }
118
+ response.cookie(name, value);
119
+ },
120
+ setDelay: async (delay)=>{
121
+ await (0, _helpers.sleep)(delay === Infinity ? 99999999 : delay);
122
+ },
123
+ setHeader: (field, value)=>{
124
+ response.set(field, value);
125
+ },
126
+ setStatusCode: (statusCode)=>{
127
+ response.statusCode = statusCode;
128
+ }
129
+ };
130
+ const resolvedData = typeof matchedRouteConfig.config.data === 'function' ? await matchedRouteConfig.config.data(params) : matchedRouteConfig.config.data;
131
+ if (response.headersSent) {
132
+ return;
133
+ }
134
+ if ((_matchedRouteConfig_config_settings = matchedRouteConfig.config.settings) === null || _matchedRouteConfig_config_settings === void 0 ? void 0 : _matchedRouteConfig_config_settings.status) {
135
+ response.statusCode = matchedRouteConfig.config.settings.status;
136
+ }
137
+ if (matchedRouteConfig.operationType === 'query') {
138
+ response.set('Cache-control', 'no-cache');
139
+ }
140
+ const data = await (0, _helpers.callResponseInterceptors)({
141
+ data: resolvedData,
142
+ request,
143
+ response,
144
+ interceptors: {
145
+ routeInterceptor: matchedRouteConfig.routeResponseInterceptor,
146
+ componentInterceptor: matchedRouteConfig.componentResponseInterceptor,
147
+ requestInterceptor: matchedRouteConfig.requestResponseInterceptor,
148
+ serverInterceptor: matchedRouteConfig.serverResponseInterceptor
149
+ }
150
+ });
151
+ if ((_matchedRouteConfig_config_settings1 = matchedRouteConfig.config.settings) === null || _matchedRouteConfig_config_settings1 === void 0 ? void 0 : _matchedRouteConfig_config_settings1.delay) {
152
+ await (0, _helpers.sleep)(matchedRouteConfig.config.settings.delay);
153
+ }
154
+ if (response.headersSent) {
155
+ return;
156
+ }
157
+ return response.json(data);
158
+ }));
@@ -0,0 +1,2 @@
1
+ import type { GraphQLRouteConfig } from '../../../../../utils/types';
2
+ export declare const calculateGraphQLRouteConfigWeight: (graphQLRouteConfig: GraphQLRouteConfig) => number;
@@ -2,14 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- Object.defineProperty(exports, "prepareGraphQLRequestConfigs", {
5
+ Object.defineProperty(exports, "calculateGraphQLRouteConfigWeight", {
6
6
  enumerable: true,
7
7
  get: function() {
8
- return prepareGraphQLRequestConfigs;
8
+ return calculateGraphQLRouteConfigWeight;
9
9
  }
10
10
  });
11
11
  const _helpers = require("../../../../../utils/helpers");
12
- const calculateRouteConfigWeight = (graphQLRouteConfig)=>{
12
+ const calculateGraphQLRouteConfigWeight = (graphQLRouteConfig)=>{
13
13
  const { entities } = graphQLRouteConfig;
14
14
  if (!entities) return 0;
15
15
  let routeConfigWeight = 0;
@@ -32,11 +32,3 @@ const calculateRouteConfigWeight = (graphQLRouteConfig)=>{
32
32
  }
33
33
  return routeConfigWeight;
34
34
  };
35
- const prepareGraphQLRequestConfigs = (requestConfigs)=>{
36
- requestConfigs.forEach((requestConfig)=>{
37
- requestConfig.routes.sort((first, second)=>// ✅ important:
38
- // Lift more specific configs for correct working of routes
39
- calculateRouteConfigWeight(second) - calculateRouteConfigWeight(first));
40
- });
41
- return requestConfigs;
42
- };