mock-config-server 3.2.0 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +153 -3
  2. package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/isDescriptorValueValid.d.ts +2 -1
  3. package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/isDescriptorValueValid.js +30 -7
  4. package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateGraphqlConfig.js +9 -3
  5. package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateRoutes/validateRoutes.js +67 -18
  6. package/dist/bin/validateMockServerConfig/validateQueue/validateQueue.d.ts +1 -0
  7. package/dist/bin/validateMockServerConfig/validateQueue/validateQueue.js +29 -0
  8. package/dist/bin/validateMockServerConfig/validateRestConfig/validateRoutes/validateRoutes.js +67 -18
  9. package/dist/bin/validateMockServerConfig/validateSettings/validateSettings.d.ts +1 -0
  10. package/dist/bin/validateMockServerConfig/validateSettings/validateSettings.js +34 -0
  11. package/dist/src/core/database/createDatabaseRoutes/helpers/createNestedDatabaseRoutes/createNestedDatabaseRoutes.js +45 -2
  12. package/dist/src/core/database/createDatabaseRoutes/helpers/filter/filter.d.ts +2 -1
  13. package/dist/src/core/database/createDatabaseRoutes/helpers/pagination/pagination.d.ts +13 -0
  14. package/dist/src/core/database/createDatabaseRoutes/helpers/pagination/pagination.js +36 -0
  15. package/dist/src/core/database/createDatabaseRoutes/helpers/sort/sort.d.ts +2 -0
  16. package/dist/src/core/database/createDatabaseRoutes/helpers/sort/sort.js +42 -0
  17. package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.d.ts +7 -1
  18. package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.js +65 -24
  19. package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.js +1 -3
  20. package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.d.ts +7 -1
  21. package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.js +6 -2
  22. package/dist/src/core/rest/createRestRoutes/createRestRoutes.d.ts +7 -1
  23. package/dist/src/core/rest/createRestRoutes/createRestRoutes.js +55 -12
  24. package/dist/src/server/createDatabaseMockServer/createDatabaseMockServer.js +4 -1
  25. package/dist/src/server/createGraphQLMockServer/createGraphQLMockServer.js +11 -4
  26. package/dist/src/server/createMockServer/createMockServer.js +28 -9
  27. package/dist/src/server/createRestMockServer/createRestMockServer.js +11 -4
  28. package/dist/src/server/index.d.ts +3 -3
  29. package/dist/src/server/index.js +23 -23
  30. package/dist/src/static/views/components/header/index.js +1 -1
  31. package/dist/src/static/views/features/scheme/index.js +31 -31
  32. package/dist/src/static/views/features/tab/index.js +12 -12
  33. package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.d.ts +2 -1
  34. package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.d.ts +7 -2
  35. package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.js +6 -4
  36. package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.d.ts +1 -1
  37. package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.js +1 -1
  38. package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.js +19 -10
  39. package/dist/src/utils/types/graphql.d.ts +60 -51
  40. package/dist/src/utils/types/index.d.ts +1 -0
  41. package/dist/src/utils/types/index.js +11 -0
  42. package/dist/src/utils/types/interceptors.d.ts +1 -1
  43. package/dist/src/utils/types/rest.d.ts +47 -40
  44. package/dist/src/utils/types/utils.d.ts +8 -0
  45. package/dist/src/utils/types/utils.js +1 -0
  46. package/dist/src/utils/types/values.d.ts +0 -1
  47. package/package.json +1 -1
@@ -1,31 +1,31 @@
1
- const lightStyles = document.querySelector(
2
- 'link[rel=stylesheet][media*=prefers-color-scheme][media*=light]'
3
- );
4
- const darkStyles = document.querySelector(
5
- 'link[rel=stylesheet][media*=prefers-color-scheme][media*=dark]'
6
- );
7
-
8
- function getSavedScheme() {
9
- return localStorage.getItem('color-scheme');
10
- }
11
-
12
- function setScheme(scheme) {
13
- const lightMedia = scheme === 'light' ? 'all' : 'not all';
14
- const darkMedia = scheme === 'dark' ? 'all' : 'not all';
15
-
16
- lightStyles.media = lightMedia;
17
- darkStyles.media = darkMedia;
18
-
19
- localStorage.setItem('color-scheme', scheme);
20
- }
21
-
22
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
23
- function switchScheme() {
24
- setScheme(getSavedScheme() === 'light' ? 'dark' : 'light');
25
- }
26
-
27
- function initScheme() {
28
- setScheme(getSavedScheme() ?? 'dark');
29
- }
30
-
31
- initScheme();
1
+ const lightStyles = document.querySelector(
2
+ 'link[rel=stylesheet][media*=prefers-color-scheme][media*=light]'
3
+ );
4
+ const darkStyles = document.querySelector(
5
+ 'link[rel=stylesheet][media*=prefers-color-scheme][media*=dark]'
6
+ );
7
+
8
+ function getSavedScheme() {
9
+ return localStorage.getItem('color-scheme');
10
+ }
11
+
12
+ function setScheme(scheme) {
13
+ const lightMedia = scheme === 'light' ? 'all' : 'not all';
14
+ const darkMedia = scheme === 'dark' ? 'all' : 'not all';
15
+
16
+ lightStyles.media = lightMedia;
17
+ darkStyles.media = darkMedia;
18
+
19
+ localStorage.setItem('color-scheme', scheme);
20
+ }
21
+
22
+ // eslint-disable-next-line no-unused-vars
23
+ function switchScheme() {
24
+ setScheme(getSavedScheme() === 'light' ? 'dark' : 'light');
25
+ }
26
+
27
+ function initScheme() {
28
+ setScheme(getSavedScheme() ?? 'dark');
29
+ }
30
+
31
+ initScheme();
@@ -1,12 +1,12 @@
1
- function switchTab(activeTabId) {
2
- document.querySelector('body').className = activeTabId;
3
- }
4
-
5
- function initTab() {
6
- const tabItems = document.getElementsByClassName('tab_item');
7
- for (let i = 0; i < tabItems.length; i += 1) {
8
- tabItems[i].addEventListener('click', () => switchTab(tabItems[i].id));
9
- }
10
- }
11
-
12
- initTab();
1
+ function switchTab(activeTabId) {
2
+ document.querySelector('body').className = activeTabId;
3
+ }
4
+
5
+ function initTab() {
6
+ const tabItems = document.getElementsByClassName('tab_item');
7
+ for (let i = 0; i < tabItems.length; i += 1) {
8
+ tabItems[i].addEventListener('click', () => switchTab(tabItems[i].id));
9
+ }
10
+ }
11
+
12
+ initTab();
@@ -1 +1,2 @@
1
- export declare const isEntityDescriptor: (value: any) => boolean;
1
+ import type { EntityDescriptor } from '../../../types';
2
+ export declare const isEntityDescriptor: (value: any) => value is EntityDescriptor;
@@ -1,3 +1,8 @@
1
1
  import type { Request } from 'express';
2
- import type { GraphQLInput } from '../../../types';
3
- export declare const getGraphQLInput: (request: Request) => GraphQLInput;
2
+ import type { PlainObject } from '../../../types';
3
+ interface GetGraphQLInputResult {
4
+ query: string | undefined;
5
+ variables: PlainObject | undefined;
6
+ }
7
+ export declare const getGraphQLInput: (request: Request) => GetGraphQLInputResult;
8
+ export {};
@@ -6,14 +6,16 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.getGraphQLInput = void 0;
7
7
  const getGraphQLInput = request => {
8
8
  if (request.method === 'GET') {
9
- var _ref;
10
9
  const {
11
10
  query,
12
11
  variables
13
12
  } = request.query;
13
+
14
+ // ✅ important:
15
+ // if 'variables' was sent as encoded uri component then it already decoded into object and we do not need to use JSON.parse
14
16
  return {
15
17
  query: query === null || query === void 0 ? void 0 : query.toString(),
16
- variables: JSON.parse((_ref = variables) !== null && _ref !== void 0 ? _ref : '{}')
18
+ variables: typeof variables === 'string' ? JSON.parse(variables) : variables
17
19
  };
18
20
  }
19
21
  if (request.method === 'POST') {
@@ -23,9 +25,9 @@ const getGraphQLInput = request => {
23
25
  } = request.body;
24
26
  return {
25
27
  query,
26
- variables: variables !== null && variables !== void 0 ? variables : {}
28
+ variables
27
29
  };
28
30
  }
29
- throw new Error('Not allowed request method for graphql request');
31
+ throw new Error(`Not allowed request method ${request.method} for graphql request`);
30
32
  };
31
33
  exports.getGraphQLInput = getGraphQLInput;
@@ -1,7 +1,7 @@
1
1
  import type { OperationTypeNode } from 'graphql';
2
2
  interface ParseDocumentNodeResult {
3
3
  operationType: OperationTypeNode;
4
- operationName: string;
4
+ operationName: string | undefined;
5
5
  }
6
6
  export declare const parseQuery: (query: string) => ParseDocumentNodeResult | null;
7
7
  export {};
@@ -10,7 +10,7 @@ const parseDocumentNode = node => {
10
10
  const operationDefinition = node.definitions.find(definition => definition.kind === 'OperationDefinition');
11
11
  return {
12
12
  operationType: operationDefinition.operation,
13
- operationName: (_operationDefinition$ = (_operationDefinition$2 = operationDefinition.name) === null || _operationDefinition$2 === void 0 ? void 0 : _operationDefinition$2.value) !== null && _operationDefinition$ !== void 0 ? _operationDefinition$ : ''
13
+ operationName: (_operationDefinition$ = (_operationDefinition$2 = operationDefinition.name) === null || _operationDefinition$2 === void 0 ? void 0 : _operationDefinition$2.value) !== null && _operationDefinition$ !== void 0 ? _operationDefinition$ : undefined
14
14
  };
15
15
  };
16
16
  const parseQuery = query => {
@@ -14,21 +14,30 @@ const callResponseInterceptors = async params => {
14
14
  } = params;
15
15
  const getHeader = field => response.getHeader(field);
16
16
  const getHeaders = () => response.getHeaders();
17
- const getCookie = name => request.cookies[name];
17
+ const setHeader = (field, value) => {
18
+ response.set(field, value);
19
+ };
20
+ const appendHeader = (field, value) => {
21
+ response.append(field, value);
22
+ };
18
23
  const setStatusCode = statusCode => {
19
24
  response.statusCode = statusCode;
20
25
  };
21
- const setHeader = (field, value) => response.set(field, value);
22
- const appendHeader = (field, value) => response.append(field, value);
26
+ const getCookie = name => request.cookies[name];
23
27
  const setCookie = (name, value, options) => {
24
28
  if (options) {
25
29
  response.cookie(name, value, options);
30
+ return;
26
31
  }
27
32
  response.cookie(name, value);
28
33
  };
29
- const clearCookie = (name, options) => response.clearCookie(name, options);
30
- const attachment = filename => response.attachment(filename);
31
- const ResponseInterceptorParams = {
34
+ const clearCookie = (name, options) => {
35
+ response.clearCookie(name, options);
36
+ };
37
+ const attachment = filename => {
38
+ response.attachment(filename);
39
+ };
40
+ const responseInterceptorParams = {
32
41
  request,
33
42
  response,
34
43
  setDelay: _setDelay.setDelay,
@@ -44,16 +53,16 @@ const callResponseInterceptors = async params => {
44
53
  };
45
54
  let updatedData = data;
46
55
  if (interceptors !== null && interceptors !== void 0 && interceptors.routeInterceptor) {
47
- updatedData = await interceptors.routeInterceptor(updatedData, ResponseInterceptorParams);
56
+ updatedData = await interceptors.routeInterceptor(updatedData, responseInterceptorParams);
48
57
  }
49
58
  if (interceptors !== null && interceptors !== void 0 && interceptors.requestInterceptor) {
50
- updatedData = await interceptors.requestInterceptor(updatedData, ResponseInterceptorParams);
59
+ updatedData = await interceptors.requestInterceptor(updatedData, responseInterceptorParams);
51
60
  }
52
61
  if (interceptors !== null && interceptors !== void 0 && interceptors.apiInterceptor) {
53
- updatedData = await interceptors.apiInterceptor(updatedData, ResponseInterceptorParams);
62
+ updatedData = await interceptors.apiInterceptor(updatedData, responseInterceptorParams);
54
63
  }
55
64
  if (interceptors !== null && interceptors !== void 0 && interceptors.serverInterceptor) {
56
- updatedData = await interceptors.serverInterceptor(updatedData, ResponseInterceptorParams);
65
+ updatedData = await interceptors.serverInterceptor(updatedData, responseInterceptorParams);
57
66
  }
58
67
  return updatedData;
59
68
  };
@@ -1,74 +1,83 @@
1
1
  import type { Request } from 'express';
2
- import type { CalculateByDescriptorValueCheckMode, CheckActualValueCheckMode, CheckFunction, CheckMode, CompareWithDescriptorAnyValueCheckMode, CompareWithDescriptorValueCheckMode } from './checkModes';
2
+ import type { CheckActualValueCheckMode, CheckFunction, CheckMode, CompareWithDescriptorAnyValueCheckMode, CompareWithDescriptorValueCheckMode } from './checkModes';
3
3
  import type { Interceptors } from './interceptors';
4
- import type { Data, Primitive } from './values';
4
+ import type { NestedObjectOrArray } from './utils';
5
+ import type { Data } from './values';
5
6
  export type GraphQLEntityName = 'headers' | 'cookies' | 'query' | 'variables';
6
- export type GraphQLMappedEntityName = string;
7
+ type GraphQLPlainEntityValue = string | number | boolean | null;
7
8
  type GraphQLMappedEntityValue = string | number | boolean;
8
- type GraphQLPlainEntityInnerValue = {
9
- checkMode?: undefined;
10
- call?: undefined;
11
- dotAll?: undefined;
12
- [key: string]: Primitive | GraphQLPlainEntityInnerValue;
13
- };
14
- type GraphQLPlainEntityValue = {
15
- checkMode?: undefined;
16
- call?: undefined;
17
- forEach?: undefined;
18
- dotAll?: undefined;
19
- [key: string]: GraphQLPlainEntityInnerValue | Primitive | GraphQLEntityDescriptor;
20
- };
21
- export type GraphQLOperationType = 'query' | 'mutation';
22
- type GraphQLOperationName = string | RegExp;
23
- interface GraphQLQuery {
24
- operationType: GraphQLOperationType;
25
- operationName: GraphQLOperationName;
26
- }
27
- type GraphQLVariables = Record<string, any>;
28
- export interface GraphQLInput {
29
- query?: string;
30
- variables: GraphQLVariables;
31
- }
32
- type GraphQLEntityValue<EntityName = GraphQLEntityName> = EntityName extends 'variables' ? GraphQLPlainEntityValue : GraphQLMappedEntityValue;
33
- type GraphQLEntityValueOrValues<EntityName = GraphQLEntityName> = GraphQLEntityValue<EntityName> | GraphQLEntityValue<EntityName>[];
34
- type GraphQLEntityDescriptor<EntityName extends GraphQLEntityName = GraphQLEntityName, Check extends CheckMode = CheckMode> = EntityName extends 'variables' ? Check extends Extract<CalculateByDescriptorValueCheckMode, 'function'> ? {
9
+ export type GraphQLTopLevelPlainEntityDescriptor<Check extends CheckMode = CheckMode> = Check extends 'function' ? {
10
+ checkMode: Check;
11
+ value: (actualValue: NestedObjectOrArray<GraphQLPlainEntityValue>, checkFunction: CheckFunction) => boolean;
12
+ } : Check extends CompareWithDescriptorAnyValueCheckMode ? {
13
+ checkMode: Check;
14
+ value: NestedObjectOrArray<GraphQLPlainEntityValue>;
15
+ } : Check extends CheckActualValueCheckMode ? {
16
+ checkMode: Check;
17
+ value: never;
18
+ } : never;
19
+ type GraphQLPropertyLevelPlainEntityDescriptor<Check extends CheckMode = CheckMode> = Check extends 'function' ? {
35
20
  checkMode: Check;
36
- value: (actualValue: any, checkFunction: CheckFunction) => boolean;
21
+ value: (actualValue: GraphQLPlainEntityValue | NestedObjectOrArray<GraphQLPlainEntityValue>, checkFunction: CheckFunction) => boolean;
37
22
  } : Check extends CompareWithDescriptorAnyValueCheckMode ? {
38
23
  checkMode: Check;
39
- value: GraphQLEntityValueOrValues<EntityName>;
24
+ value: GraphQLPlainEntityValue | NestedObjectOrArray<GraphQLPlainEntityValue>;
40
25
  } : Check extends CheckActualValueCheckMode ? {
41
26
  checkMode: Check;
42
- value?: undefined;
43
- } : never : Check extends Extract<CalculateByDescriptorValueCheckMode, 'function'> ? {
27
+ value: never;
28
+ } : never;
29
+ type GraphQLMappedEntityDescriptor<Check extends CheckMode = CheckMode> = Check extends 'function' ? {
44
30
  checkMode: Check;
45
- value: (actualValue: any, checkFunction: CheckFunction) => boolean;
46
- } : Check extends Extract<CalculateByDescriptorValueCheckMode, 'regExp'> ? {
31
+ value: (actualValue: GraphQLMappedEntityValue, checkFunction: CheckFunction) => boolean;
32
+ } : Check extends 'regExp' ? {
47
33
  checkMode: Check;
48
34
  value: RegExp | RegExp[];
49
35
  } : Check extends CompareWithDescriptorValueCheckMode ? {
50
36
  checkMode: Check;
51
- value: GraphQLEntityValueOrValues<EntityName>;
37
+ value: GraphQLMappedEntityValue | GraphQLMappedEntityValue[];
52
38
  } : Check extends CheckActualValueCheckMode ? {
53
39
  checkMode: Check;
54
- value?: undefined;
40
+ value: never;
55
41
  } : never;
56
- export type GraphQLEntityDescriptorOrValue<EntityName extends GraphQLEntityName = GraphQLEntityName> = EntityName extends 'variables' ? GraphQLEntityDescriptor<EntityName> | GraphQLEntityValue<EntityName> : Record<GraphQLMappedEntityName, GraphQLEntityDescriptor<EntityName> | GraphQLEntityValueOrValues<EntityName>>;
57
- export type GraphQLEntityDescriptorOnly<EntityName extends GraphQLEntityName = GraphQLEntityName> = EntityName extends 'variables' ? GraphQLEntityDescriptor<EntityName> : Record<GraphQLMappedEntityName, GraphQLEntityDescriptor<EntityName>>;
58
- export interface GraphQLEntityNamesByOperationType {
59
- query: GraphQLEntityName;
60
- mutation: GraphQLEntityName;
61
- }
62
- type GraphQLEntityByEntityName<OperationType extends GraphQLOperationType> = {
63
- [EntityName in GraphQLEntityNamesByOperationType[OperationType]]?: GraphQLEntityDescriptorOrValue<EntityName>;
42
+ export type GraphQLEntityDescriptorOrValue<EntityName extends GraphQLEntityName = GraphQLEntityName> = EntityName extends 'variables' ? GraphQLTopLevelPlainEntityDescriptor | Record<string, GraphQLPropertyLevelPlainEntityDescriptor> | NestedObjectOrArray<GraphQLPlainEntityValue> : Record<string, GraphQLMappedEntityDescriptor | GraphQLMappedEntityValue | GraphQLMappedEntityValue[]>;
43
+ export type GraphQLOperationType = 'query' | 'mutation';
44
+ export type GraphQLOperationName = string | RegExp;
45
+ export type GraphQLEntityNamesByOperationType = {
46
+ [operationType in GraphQLOperationType]: GraphQLEntityName;
47
+ };
48
+ export type GraphQLEntitiesByEntityName = {
49
+ [EntityName in GraphQLEntityName]?: GraphQLEntityDescriptorOrValue<EntityName>;
50
+ };
51
+ type GraphQLSettings = {
52
+ readonly polling: boolean;
64
53
  };
65
- export interface GraphQLRouteConfig<OperationType extends GraphQLOperationType = GraphQLOperationType, Entities extends GraphQLEntityByEntityName<OperationType> = GraphQLEntityByEntityName<OperationType>> {
66
- entities?: Entities;
67
- data: ((request: Request, entities: Entities) => Data | Promise<Data>) | Data;
54
+ export type GraphQLRouteConfig<Settings extends GraphQLSettings = GraphQLSettings> = ({
55
+ settings: Settings & {
56
+ polling: true;
57
+ };
58
+ queue: Array<{
59
+ time?: number;
60
+ data: ((request: Request, entities: GraphQLEntitiesByEntityName) => Data | Promise<Data>) | Data;
61
+ }>;
62
+ } | {
63
+ settings?: Settings & {
64
+ polling?: false;
65
+ };
66
+ data: ((request: Request, entities: GraphQLEntitiesByEntityName) => Data | Promise<Data>) | Data;
67
+ }) & {
68
+ entities?: GraphQLEntitiesByEntityName;
68
69
  interceptors?: Pick<Interceptors, 'response'>;
69
- }
70
- export interface GraphQLRequestConfig extends GraphQLQuery {
70
+ };
71
+ interface BaseGraphQLRequestConfig {
72
+ operationType: GraphQLOperationType;
71
73
  routes: GraphQLRouteConfig[];
72
74
  interceptors?: Interceptors;
73
75
  }
76
+ export interface OperationNameGraphQLRequestConfig extends BaseGraphQLRequestConfig {
77
+ operationName: GraphQLOperationName;
78
+ }
79
+ interface QueryGraphQLRequestConfig extends BaseGraphQLRequestConfig {
80
+ query: string;
81
+ }
82
+ export type GraphQLRequestConfig = OperationNameGraphQLRequestConfig | QueryGraphQLRequestConfig;
74
83
  export {};
@@ -4,4 +4,5 @@ export * from './graphql';
4
4
  export * from './interceptors';
5
5
  export * from './rest';
6
6
  export * from './server';
7
+ export * from './utils';
7
8
  export * from './values';
@@ -69,6 +69,17 @@ Object.keys(_server).forEach(function (key) {
69
69
  }
70
70
  });
71
71
  });
72
+ var _utils = require("./utils");
73
+ Object.keys(_utils).forEach(function (key) {
74
+ if (key === "default" || key === "__esModule") return;
75
+ if (key in exports && exports[key] === _utils[key]) return;
76
+ Object.defineProperty(exports, key, {
77
+ enumerable: true,
78
+ get: function () {
79
+ return _utils[key];
80
+ }
81
+ });
82
+ });
72
83
  var _values = require("./values");
73
84
  Object.keys(_values).forEach(function (key) {
74
85
  if (key === "default" || key === "__esModule") return;
@@ -8,7 +8,7 @@ export interface RequestInterceptorParams {
8
8
  getHeader: (field: string) => RequestInterceptorHeaderValue;
9
9
  getHeaders: () => Record<string, RequestInterceptorHeaderValue>;
10
10
  }
11
- export type RequestInterceptor = (params: RequestInterceptorParams) => void;
11
+ export type RequestInterceptor = (params: RequestInterceptorParams) => void | Promise<void>;
12
12
  export interface ResponseInterceptorParams {
13
13
  request: Request;
14
14
  response: Response;
@@ -1,65 +1,72 @@
1
1
  import type { Request } from 'express';
2
- import type { CalculateByDescriptorValueCheckMode, CheckActualValueCheckMode, CheckFunction, CheckMode, CompareWithDescriptorAnyValueCheckMode, CompareWithDescriptorValueCheckMode } from './checkModes';
2
+ import type { CheckActualValueCheckMode, CheckFunction, CheckMode, CompareWithDescriptorAnyValueCheckMode, CompareWithDescriptorValueCheckMode } from './checkModes';
3
3
  import type { Interceptors } from './interceptors';
4
- import type { Data, Primitive } from './values';
4
+ import type { NestedObjectOrArray } from './utils';
5
+ import type { Data } from './values';
5
6
  export type RestMethod = 'get' | 'post' | 'delete' | 'put' | 'patch' | 'options';
6
7
  export type RestEntityName = 'headers' | 'cookies' | 'query' | 'params' | 'body';
7
- export type RestMappedEntityKey = string;
8
+ type RestPlainEntityValue = string | number | boolean | null;
8
9
  type RestMappedEntityValue = string | number | boolean;
9
- type RestPlainEntityInnerValue = {
10
- checkMode?: undefined;
11
- call?: undefined;
12
- dotAll?: undefined;
13
- [key: string]: Primitive | RestPlainEntityInnerValue;
14
- };
15
- type RestPlainEntityValue = {
16
- checkMode?: undefined;
17
- call?: undefined;
18
- dotAll?: undefined;
19
- [key: string]: RestPlainEntityInnerValue | Primitive | RestEntityDescriptor;
20
- } | (RestPlainEntityInnerValue | Primitive | RestEntityDescriptor)[];
21
- type RestEntityValue<EntityName = RestEntityName> = EntityName extends 'body' ? RestPlainEntityValue : RestMappedEntityValue;
22
- type RestEntityValueOrValues<EntityName = RestEntityName> = RestEntityValue<EntityName> | RestEntityValue<EntityName>[];
23
- type RestEntityDescriptor<EntityName extends RestEntityName = RestEntityName, Check extends CheckMode = CheckMode> = EntityName extends 'body' ? Check extends Extract<CalculateByDescriptorValueCheckMode, 'function'> ? {
10
+ export type RestTopLevelPlainEntityDescriptor<Check extends CheckMode = CheckMode> = Check extends 'function' ? {
11
+ checkMode: Check;
12
+ value: (actualValue: NestedObjectOrArray<RestPlainEntityValue>, checkFunction: CheckFunction) => boolean;
13
+ } : Check extends CompareWithDescriptorAnyValueCheckMode ? {
14
+ checkMode: Check;
15
+ value: NestedObjectOrArray<RestPlainEntityValue>;
16
+ } : Check extends CheckActualValueCheckMode ? {
17
+ checkMode: Check;
18
+ value: never;
19
+ } : never;
20
+ type RestPropertyLevelPlainEntityDescriptor<Check extends CheckMode = CheckMode> = Check extends 'function' ? {
24
21
  checkMode: Check;
25
- value: (actualValue: any, checkFunction: CheckFunction) => boolean;
22
+ value: (actualValue: RestPlainEntityValue | NestedObjectOrArray<RestPlainEntityValue>, checkFunction: CheckFunction) => boolean;
26
23
  } : Check extends CompareWithDescriptorAnyValueCheckMode ? {
27
24
  checkMode: Check;
28
- value: RestEntityValueOrValues<EntityName>;
25
+ value: RestPlainEntityValue | NestedObjectOrArray<RestPlainEntityValue>;
29
26
  } : Check extends CheckActualValueCheckMode ? {
30
27
  checkMode: Check;
31
- value?: undefined;
32
- } : never : Check extends Extract<CalculateByDescriptorValueCheckMode, 'function'> ? {
28
+ value: never;
29
+ } : never;
30
+ type RestMappedEntityDescriptor<Check extends CheckMode = CheckMode> = Check extends 'function' ? {
33
31
  checkMode: Check;
34
- value: (actualValue: any, checkFunction: CheckFunction) => boolean;
35
- } : Check extends Extract<CalculateByDescriptorValueCheckMode, 'regExp'> ? {
32
+ value: (actualValue: RestMappedEntityValue, checkFunction: CheckFunction) => boolean;
33
+ } : Check extends 'regExp' ? {
36
34
  checkMode: Check;
37
35
  value: RegExp | RegExp[];
38
36
  } : Check extends CompareWithDescriptorValueCheckMode ? {
39
37
  checkMode: Check;
40
- value: RestEntityValueOrValues<EntityName>;
38
+ value: RestMappedEntityValue | RestMappedEntityValue[];
41
39
  } : Check extends CheckActualValueCheckMode ? {
42
40
  checkMode: Check;
43
- value?: undefined;
41
+ value: never;
44
42
  } : never;
45
- export type RestEntityDescriptorOrValue<EntityName extends RestEntityName = RestEntityName> = EntityName extends 'body' ? RestEntityDescriptor<EntityName> | RestEntityValue<EntityName> : Record<RestMappedEntityKey, RestEntityDescriptor<EntityName> | RestEntityValueOrValues<EntityName>>;
46
- export type RestEntityDescriptorOnly<EntityName extends RestEntityName = RestEntityName> = EntityName extends 'body' ? RestEntityDescriptor<EntityName> : Record<RestMappedEntityKey, RestEntityDescriptor<EntityName>>;
47
- export interface RestEntityNamesByMethod {
48
- get: Exclude<RestEntityName, 'body'>;
49
- delete: Exclude<RestEntityName, 'body'>;
50
- post: RestEntityName;
51
- put: RestEntityName;
52
- patch: RestEntityName;
53
- options: Exclude<RestEntityName, 'body'>;
54
- }
55
- export type RestEntityByEntityName<Method extends RestMethod> = {
43
+ export type RestEntityDescriptorOrValue<EntityName extends RestEntityName = RestEntityName> = EntityName extends 'body' ? RestTopLevelPlainEntityDescriptor | Record<string, RestPropertyLevelPlainEntityDescriptor> | NestedObjectOrArray<RestPlainEntityValue> : Record<string, RestMappedEntityDescriptor | RestMappedEntityValue | RestMappedEntityValue[]>;
44
+ export type RestEntityNamesByMethod = {
45
+ [key in RestMethod]: key extends 'get' | 'delete' | 'options' ? Exclude<RestEntityName, 'body'> : RestEntityName;
46
+ };
47
+ export type RestEntitiesByEntityName<Method extends RestMethod = RestMethod> = {
56
48
  [EntityName in RestEntityNamesByMethod[Method]]?: RestEntityDescriptorOrValue<EntityName>;
57
49
  };
58
- export interface RestRouteConfig<Method extends RestMethod, Entities extends RestEntityByEntityName<Method> = RestEntityByEntityName<Method>> {
59
- entities?: Entities;
50
+ interface RestSettings {
51
+ readonly polling?: boolean;
52
+ }
53
+ export type RestRouteConfig<Method extends RestMethod, Entities extends RestEntitiesByEntityName<Method> = RestEntitiesByEntityName<Method>, Settings extends RestSettings = RestSettings> = ({
54
+ settings: Settings & {
55
+ polling: true;
56
+ };
57
+ queue: Array<{
58
+ time?: number;
59
+ data: ((request: Request, entities: Entities) => Data | Promise<Data>) | Data;
60
+ }>;
61
+ } | {
62
+ settings?: Settings & {
63
+ polling: false;
64
+ };
60
65
  data: ((request: Request, entities: Entities) => Data | Promise<Data>) | Data;
66
+ }) & {
67
+ entities?: Entities;
61
68
  interceptors?: Pick<Interceptors, 'response'>;
62
- }
69
+ };
63
70
  export type RestPathString = `/${string}`;
64
71
  interface BaseRestRequestConfig<Method extends RestMethod> {
65
72
  path: RestPathString | RegExp;
@@ -0,0 +1,8 @@
1
+ import type { PlainObject } from './values';
2
+ export type ValueOf<T extends PlainObject | Array<any>> = T extends Array<any> ? T[number] : T[keyof T];
3
+ export type Entries<T extends PlainObject | Array<any>> = ValueOf<{
4
+ [K in keyof T]-?: [K, T[K]];
5
+ }>[];
6
+ export type NestedObjectOrArray<T> = {
7
+ [key: string]: NestedObjectOrArray<T> | T;
8
+ } | Array<NestedObjectOrArray<T> | T>;
@@ -0,0 +1 @@
1
+ "use strict";
@@ -1,4 +1,3 @@
1
1
  export type PlainObject = Record<string, any>;
2
- export type PlainFunction = (...args: any[]) => any;
3
2
  export type Primitive = boolean | number | bigint | string | null | undefined | symbol;
4
3
  export type Data = boolean | number | string | any[] | Record<any, any> | null | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mock-config-server",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "description": "Tool that easily and quickly imitates server operation, create full fake api in few steps",
5
5
  "author": {
6
6
  "name": "SIBERIA CAN CODE 🧊",