mock-config-server 3.7.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +272 -224
- package/dist/bin/bin.js +1 -1
- package/dist/bin/build.d.ts +3 -1
- package/dist/bin/build.js +10 -0
- package/dist/bin/helpers/createTemplate.d.ts +3 -3
- package/dist/bin/helpers/createTemplate.js +1 -1
- package/dist/bin/helpers/index.d.ts +0 -3
- package/dist/bin/helpers/index.js +0 -3
- package/dist/bin/helpers/resolveConfigFile.d.ts +2 -2
- package/dist/bin/helpers/resolveConfigFile.js +2 -2
- package/dist/bin/helpers/resolveConfigFilePath.js +4 -4
- package/dist/bin/helpers/resolveExportsFromSourceCode.js +1 -2
- package/dist/bin/init.js +5 -8
- package/dist/bin/run.d.ts +1 -2
- package/dist/bin/run.js +1 -6
- package/dist/bin/runFlatConfig.d.ts +5 -0
- package/dist/bin/runFlatConfig.js +36 -0
- package/dist/bin/templates/js/full/mock-server.config.js +10 -7
- package/dist/bin/templates/js/graphql/mock-server.config.js +11 -6
- package/dist/bin/templates/js/rest/mock-server.config.js +11 -6
- package/dist/bin/templates/ts/full/mock-requests/graphql/index.ts +2 -2
- package/dist/bin/templates/ts/full/mock-requests/graphql/user.ts +37 -37
- package/dist/bin/templates/ts/full/mock-requests/graphql/users.ts +14 -14
- package/dist/bin/templates/ts/full/mock-requests/rest/index.ts +2 -2
- package/dist/bin/templates/ts/full/mock-requests/rest/user.ts +37 -37
- package/dist/bin/templates/ts/full/mock-requests/rest/users.ts +14 -14
- package/dist/bin/templates/ts/full/mock-server.config.ts +11 -8
- package/dist/bin/templates/ts/graphql/mock-requests/index.ts +2 -2
- package/dist/bin/templates/ts/graphql/mock-requests/user.ts +37 -37
- package/dist/bin/templates/ts/graphql/mock-requests/users.ts +14 -14
- package/dist/bin/templates/ts/graphql/mock-server.config.ts +12 -7
- package/dist/bin/templates/ts/rest/mock-requests/index.ts +2 -2
- package/dist/bin/templates/ts/rest/mock-requests/user.ts +37 -37
- package/dist/bin/templates/ts/rest/mock-requests/users.ts +14 -14
- package/dist/bin/templates/ts/rest/mock-server.config.ts +12 -7
- package/dist/src/core/database/createDatabaseRoutes/createDatabaseRoutes.js +2 -1
- package/dist/src/core/database/createDatabaseRoutes/helpers/createNestedDatabaseRoutes/createNestedDatabaseRoutes.js +8 -12
- package/dist/src/core/database/createDatabaseRoutes/helpers/filter/filter.d.ts +0 -1
- package/dist/src/core/database/createDatabaseRoutes/helpers/pagination/pagination.d.ts +0 -1
- package/dist/src/core/database/createDatabaseRoutes/helpers/search/search.d.ts +0 -1
- package/dist/src/core/database/createDatabaseRoutes/helpers/sort/sort.d.ts +0 -1
- package/dist/src/core/database/createDatabaseRoutes/helpers/splitDatabaseByNesting/splitDatabaseByNesting.d.ts +1 -1
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileStorage.d.ts +5 -6
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileStorage.js +4 -4
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileWriter.js +2 -2
- package/dist/src/core/database/createDatabaseRoutes/storages/Memory/MemoryStorage.d.ts +6 -7
- package/dist/src/core/database/createOrm/createOrm.d.ts +2 -0
- package/dist/src/core/database/createOrm/createOrm.js +119 -0
- package/dist/src/core/database/createStorage/createStorage.d.ts +2 -0
- package/dist/src/core/database/createStorage/createStorage.js +13 -0
- package/dist/src/core/database/index.d.ts +2 -0
- package/dist/src/core/database/index.js +2 -0
- package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.d.ts +1 -1
- package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.js +39 -17
- package/dist/src/core/graphql/createGraphQLRoutes/helpers/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.js +2 -2
- package/dist/src/core/middlewares/contextMiddleware/contextMiddleware.d.ts +2 -2
- package/dist/src/core/middlewares/contextMiddleware/contextMiddleware.js +24 -12
- package/dist/src/core/middlewares/destroyerMiddleware/destroyerMiddleware.d.ts +1 -2
- package/dist/src/core/middlewares/index.d.ts +0 -1
- package/dist/src/core/middlewares/index.js +0 -1
- package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.d.ts +2 -2
- package/dist/src/core/rest/createRestRoutes/createRestRoutes.d.ts +1 -1
- package/dist/src/core/rest/createRestRoutes/createRestRoutes.js +80 -24
- package/dist/src/server/createDatabaseMockServer/createDatabaseMockServer.d.ts +1 -1
- package/dist/src/server/createDatabaseMockServer/createDatabaseMockServer.js +6 -6
- package/dist/src/server/createFlatMockServer/createFlatMockServer.d.ts +3 -0
- package/dist/src/server/createFlatMockServer/createFlatMockServer.js +127 -0
- package/dist/src/server/createGraphQLMockServer/createGraphQLMockServer.d.ts +1 -1
- package/dist/src/server/createGraphQLMockServer/createGraphQLMockServer.js +3 -6
- package/dist/src/server/createMockServer/createMockServer.d.ts +1 -1
- package/dist/src/server/createMockServer/createMockServer.js +3 -5
- package/dist/src/server/createRestMockServer/createRestMockServer.d.ts +1 -1
- package/dist/src/server/createRestMockServer/createRestMockServer.js +3 -6
- package/dist/src/server/index.d.ts +2 -0
- package/dist/src/server/index.js +2 -0
- package/dist/src/server/startDatabaseMockServer/startDatabaseMockServer.d.ts +1 -2
- package/dist/src/server/startFlatMockServer/startFlatMockServer.d.ts +4 -0
- package/dist/src/server/startFlatMockServer/startFlatMockServer.js +28 -0
- package/dist/src/server/startGraphQLMockServer/startGraphQLMockServer.d.ts +1 -2
- package/dist/src/server/startMockServer/startMockServer.d.ts +1 -2
- package/dist/src/server/startRestMockServer/startRestMockServer.d.ts +1 -2
- package/dist/src/utils/constants/checkModes.d.ts +1 -3
- package/dist/src/utils/constants/checkModes.js +3 -19
- package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.d.ts +20 -2
- package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.js +37 -71
- package/dist/src/utils/helpers/files/index.d.ts +1 -0
- package/dist/src/utils/helpers/files/index.js +1 -0
- package/dist/src/utils/helpers/files/isFileDescriptor/isFileDescriptor.d.ts +2 -0
- package/dist/src/utils/helpers/files/isFileDescriptor/isFileDescriptor.js +16 -0
- package/dist/src/utils/helpers/files/isFilePathValid/isFilePathValid.js +3 -3
- package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.d.ts +1 -1
- package/dist/src/utils/helpers/index.d.ts +0 -1
- package/dist/src/utils/helpers/index.js +0 -1
- package/dist/src/utils/helpers/interceptors/callRequestInterceptor/callRequestInterceptor.d.ts +1 -1
- package/dist/src/utils/helpers/interceptors/callRequestInterceptor/callRequestInterceptor.js +2 -1
- package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.js +10 -5
- package/dist/src/utils/helpers/logger/callResponseLogger/callResponseLogger.d.ts +1 -1
- package/dist/src/utils/helpers/tests/createTmpDir.js +4 -4
- package/dist/src/utils/helpers/url/convertWin32PathToUnix/convertWin32PathToUnix.js +1 -1
- package/dist/src/utils/helpers/url/urlJoin/urlJoin.js +4 -4
- package/dist/src/utils/types/checkModes.d.ts +14 -7
- package/dist/src/utils/types/database.d.ts +30 -0
- package/dist/src/utils/types/entities.d.ts +13 -43
- package/dist/src/utils/types/files.d.ts +5 -0
- package/dist/src/utils/types/files.js +4 -0
- package/dist/src/utils/types/graphql.d.ts +9 -8
- package/dist/src/utils/types/index.d.ts +1 -0
- package/dist/src/utils/types/index.js +1 -0
- package/dist/src/utils/types/interceptors.d.ts +21 -16
- package/dist/src/utils/types/logger.d.ts +11 -11
- package/dist/src/utils/types/rest.d.ts +17 -12
- package/dist/src/utils/types/server.d.ts +48 -20
- package/dist/src/utils/types/shared.d.ts +1 -1
- package/dist/src/utils/types/utils.d.ts +4 -4
- package/dist/src/utils/types/values.d.ts +3 -4
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/corsSchema/corsSchema.d.ts +2 -2
- package/dist/{bin/helpers → src/utils/validate}/getMostSpecificPathFromError.d.ts +1 -1
- package/dist/src/utils/validate/getValidationMessageFromPath.d.ts +1 -0
- package/dist/src/utils/validate/graphqlConfigSchema/graphqlConfigSchema.d.ts +535 -0
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/graphqlConfigSchema/graphqlConfigSchema.js +14 -6
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/graphqlConfigSchema/routeConfigSchema/routeConfigSchema.d.ts +20 -5
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/graphqlConfigSchema/routeConfigSchema/routeConfigSchema.js +5 -5
- package/dist/src/utils/validate/index.d.ts +14 -0
- package/dist/src/utils/validate/index.js +31 -0
- package/dist/{bin/helpers → src/utils/validate}/isOnlyRequestedDataResolvingPropertyExists.d.ts +1 -1
- package/dist/src/utils/validate/queueSchema/queueSchema.d.ts +20 -0
- package/dist/src/utils/validate/queueSchema/queueSchema.js +24 -0
- package/dist/src/utils/validate/restConfigSchema/restConfigSchema.d.ts +1659 -0
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/restConfigSchema/restConfigSchema.js +13 -5
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/restConfigSchema/routeConfigSchema/routeConfigSchema.d.ts +21 -6
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/restConfigSchema/routeConfigSchema/routeConfigSchema.js +6 -6
- package/dist/src/utils/validate/utils/checkModeSchema/checkModeSchema.d.ts +23 -0
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/utils/checkModeSchema/checkModeSchema.js +24 -1
- package/dist/src/utils/validate/utils/entitiesSchema/entitiesSchema.d.ts +4 -0
- package/dist/src/utils/validate/utils/entitiesSchema/entitiesSchema.js +94 -0
- package/dist/src/utils/validate/utils/extendedDiscriminatedUnion/extendedDiscriminatedUnion.d.ts +8 -0
- package/dist/src/utils/validate/utils/extendedDiscriminatedUnion/extendedDiscriminatedUnion.js +46 -0
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/utils/index.d.ts +2 -2
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/utils/index.js +2 -2
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/utils/jsonSchema/jsonSchema.d.ts +2 -2
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/utils/jsonSchema/jsonSchema.js +1 -1
- package/dist/src/utils/validate/utils/nestedObjectOrArraySchema/nestedObjectOrArraySchema.d.ts +3 -0
- package/dist/src/utils/validate/utils/nestedObjectOrArraySchema/nestedObjectOrArraySchema.js +22 -0
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/utils/requiredPropertiesSchema/requiredPropertiesSchema.d.ts +1 -1
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/utils/requiredPropertiesSchema/requiredPropertiesSchema.js +1 -1
- package/dist/src/utils/validate/validateApiMockServerConfig.d.ts +2 -0
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/validateApiMockServerConfig.js +4 -3
- package/dist/src/utils/validate/validateFlatMockServerConfig.d.ts +2 -0
- package/dist/src/utils/validate/validateFlatMockServerConfig.js +53 -0
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/validateMockServerConfig.d.ts +1 -1
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/validateMockServerConfig.js +4 -3
- package/package.json +51 -52
- package/dist/bin/helpers/getValidationMessageFromPath.d.ts +0 -1
- package/dist/bin/validateMockServerConfig/graphqlConfigSchema/graphqlConfigSchema.d.ts +0 -258
- package/dist/bin/validateMockServerConfig/queueSchema/queueSchema.d.ts +0 -8
- package/dist/bin/validateMockServerConfig/queueSchema/queueSchema.js +0 -21
- package/dist/bin/validateMockServerConfig/restConfigSchema/restConfigSchema.d.ts +0 -743
- package/dist/bin/validateMockServerConfig/utils/checkModeSchema/checkModeSchema.d.ts +0 -5
- package/dist/bin/validateMockServerConfig/utils/entitiesSchema/entitiesSchema.d.ts +0 -71
- package/dist/bin/validateMockServerConfig/utils/entitiesSchema/entitiesSchema.js +0 -124
- package/dist/bin/validateMockServerConfig/validateApiMockServerConfig.d.ts +0 -2
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getGraphqlUrlSuggestions/getGraphqlUrlSuggestions.d.ts +0 -11
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getGraphqlUrlSuggestions/getGraphqlUrlSuggestions.js +0 -25
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getLevenshteinDistance/getLevenshteinDistance.d.ts +0 -1
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getLevenshteinDistance/getLevenshteinDistance.js +0 -85
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/getRestUrlSuggestions.d.ts +0 -11
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/getRestUrlSuggestions.js +0 -39
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getActualRestUrlMeaningfulString/getActualRestUrlMeaningfulString.d.ts +0 -1
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getActualRestUrlMeaningfulString/getActualRestUrlMeaningfulString.js +0 -11
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getPatternRestUrlMeaningfulString/getPatternRestUrlMeaningfulString.d.ts +0 -1
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getPatternRestUrlMeaningfulString/getPatternRestUrlMeaningfulString.js +0 -11
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/index.d.ts +0 -2
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/index.js +0 -19
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/index.d.ts +0 -3
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/index.js +0 -20
- package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.d.ts +0 -3
- package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.js +0 -65
- package/dist/src/static/views/assets/icons/scheme-dark.svg +0 -3
- package/dist/src/static/views/assets/icons/scheme-light.svg +0 -3
- package/dist/src/static/views/assets/images/404.png +0 -0
- package/dist/src/static/views/assets/images/logo.png +0 -0
- package/dist/src/static/views/assets/styles/global.css +0 -88
- package/dist/src/static/views/components/header/index.css +0 -55
- package/dist/src/static/views/components/header/index.ejs +0 -40
- package/dist/src/static/views/components/header/index.js +0 -1
- package/dist/src/static/views/features/scheme/dark.css +0 -13
- package/dist/src/static/views/features/scheme/index.ejs +0 -3
- package/dist/src/static/views/features/scheme/index.js +0 -31
- package/dist/src/static/views/features/scheme/light.css +0 -13
- package/dist/src/static/views/features/tab/index.css +0 -30
- package/dist/src/static/views/features/tab/index.ejs +0 -2
- package/dist/src/static/views/features/tab/index.js +0 -12
- package/dist/src/static/views/pages/404/index.css +0 -10
- package/dist/src/static/views/pages/404/index.ejs +0 -82
- package/dist/src/utils/helpers/isRegExp/isRegExp.d.ts +0 -1
- package/dist/src/utils/helpers/isRegExp/isRegExp.js +0 -11
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/baseUrlSchema/baseUrlSchema.d.ts +0 -0
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/baseUrlSchema/baseUrlSchema.js +0 -0
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/corsSchema/corsSchema.js +0 -0
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/databaseConfigSchema/databaseConfigSchema.d.ts +0 -0
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/databaseConfigSchema/databaseConfigSchema.js +0 -0
- package/dist/{bin/helpers → src/utils/validate}/getMostSpecificPathFromError.js +0 -0
- package/dist/{bin/helpers → src/utils/validate}/getValidationMessageFromPath.js +0 -0
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/interceptorsSchema/interceptorsSchema.d.ts +0 -0
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/interceptorsSchema/interceptorsSchema.js +0 -0
- package/dist/{bin/helpers → src/utils/validate}/isOnlyRequestedDataResolvingPropertyExists.js +0 -0
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/portSchema/portSchema.d.ts +0 -0
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/portSchema/portSchema.js +0 -0
- package/dist/{bin/validateMockServerConfig → src/utils/validate}/settingsSchema/settingsSchema.d.ts +2 -2
- /package/dist/{bin/validateMockServerConfig → src/utils/validate}/settingsSchema/settingsSchema.js +0 -0
- /package/dist/{bin/validateMockServerConfig → src/utils/validate}/staticPathSchema/staticPathSchema.d.ts +0 -0
- /package/dist/{bin/validateMockServerConfig → src/utils/validate}/staticPathSchema/staticPathSchema.js +0 -0
- /package/dist/{bin/validateMockServerConfig → src/utils/validate}/utils/plainObjectSchema/plainObjectSchema.d.ts +0 -0
- /package/dist/{bin/validateMockServerConfig → src/utils/validate}/utils/plainObjectSchema/plainObjectSchema.js +0 -0
- /package/dist/{bin/validateMockServerConfig → src/utils/validate}/utils/sharedSchema/sharedSchema.d.ts +0 -0
- /package/dist/{bin/validateMockServerConfig → src/utils/validate}/utils/sharedSchema/sharedSchema.js +0 -0
package/README.md
CHANGED
|
@@ -38,10 +38,12 @@ $ yarn add mock-config-server --dev
|
|
|
38
38
|
Create a `mock-server.config.js` file with server configuration
|
|
39
39
|
|
|
40
40
|
```javascript
|
|
41
|
-
/** @type {import('mock-config-server').
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
baseUrl: '/api'
|
|
41
|
+
/** @type {import('mock-config-server').FlatMockServerConfig} */
|
|
42
|
+
const flatMockServerConfig = [
|
|
43
|
+
{
|
|
44
|
+
baseUrl: '/api'
|
|
45
|
+
},
|
|
46
|
+
{
|
|
45
47
|
configs: [
|
|
46
48
|
{
|
|
47
49
|
path: '/user',
|
|
@@ -50,9 +52,9 @@ const mockServerConfig = {
|
|
|
50
52
|
}
|
|
51
53
|
]
|
|
52
54
|
}
|
|
53
|
-
|
|
55
|
+
];
|
|
54
56
|
|
|
55
|
-
export default
|
|
57
|
+
export default flatMockServerConfig;
|
|
56
58
|
```
|
|
57
59
|
|
|
58
60
|
Start **🎉 Mock Config Server**
|
|
@@ -65,22 +67,23 @@ $ npx mock-config-server
|
|
|
65
67
|
|
|
66
68
|
## 🎭 Parameters for mock-server.config.(js|ts)
|
|
67
69
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
- `
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
- `interceptors?` `Interceptors` functions to change request or response parameters, [read](#interceptors)
|
|
70
|
+
### Settings
|
|
71
|
+
|
|
72
|
+
- `staticPath?` {StaticPath} entity for working with static files, [read](#static-path)
|
|
73
|
+
- `interceptors?` {Interceptors} functions to change request or response parameters, [read](#interceptors)
|
|
74
|
+
- `cors?` {Cors} CORS settings object (default: `CORS is turn off`), [read](#cors)
|
|
75
|
+
- `port?` {number} server port (default: `31299`)
|
|
76
|
+
- `baseUrl?` {string} part of the url that will be substituted at the beginning of the request url (default: `'/'`)
|
|
76
77
|
- `database?` Database config for mock requests [read](#database)
|
|
77
|
-
- `data`
|
|
78
|
-
- `routes?`
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
- `
|
|
83
|
-
- `baseUrl?`
|
|
78
|
+
- `data` {Object | string} initial data for database
|
|
79
|
+
- `routes?` {Object | string} map of custom routes for database
|
|
80
|
+
|
|
81
|
+
### Components
|
|
82
|
+
|
|
83
|
+
- `name` {string} name of component
|
|
84
|
+
- `baseUrl?` {string} part of the url that will be substituted at the beginning of rest request url (default: `'/'`)
|
|
85
|
+
- `configs` {Array<RestRequestConfig | GraphQLRequestConfig>} configs for mock requests, [read](#configs)
|
|
86
|
+
- `interceptors?` {Interceptors} functions to change request or response parameters, [read](#interceptors)
|
|
84
87
|
|
|
85
88
|
### Configs
|
|
86
89
|
|
|
@@ -104,10 +107,12 @@ Every route must be configured to handle response content in one of three ways:
|
|
|
104
107
|
##### Rest example
|
|
105
108
|
|
|
106
109
|
```javascript
|
|
107
|
-
/** @type {import('mock-config-server').
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
baseUrl: '/api'
|
|
110
|
+
/** @type {import('mock-config-server').FlatMockServerConfig} */
|
|
111
|
+
const flatMockServerConfig = [
|
|
112
|
+
{
|
|
113
|
+
baseUrl: '/api'
|
|
114
|
+
},
|
|
115
|
+
{
|
|
111
116
|
configs: [
|
|
112
117
|
{
|
|
113
118
|
path: '/user',
|
|
@@ -129,9 +134,9 @@ const mockServerConfig = {
|
|
|
129
134
|
}
|
|
130
135
|
]
|
|
131
136
|
}
|
|
132
|
-
|
|
137
|
+
];
|
|
133
138
|
|
|
134
|
-
|
|
139
|
+
export default flatMockServerConfig;
|
|
135
140
|
```
|
|
136
141
|
|
|
137
142
|
Now you can make a request with an additional header and get the desired result
|
|
@@ -167,10 +172,12 @@ Every route must be configured to handle response content in one of two ways: da
|
|
|
167
172
|
##### GraphQL example
|
|
168
173
|
|
|
169
174
|
```javascript
|
|
170
|
-
/** @type {import('mock-config-server').
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
baseUrl: '/graphql'
|
|
175
|
+
/** @type {import('mock-config-server').FlatMockServerConfig} */
|
|
176
|
+
const flatMockServerConfig = [
|
|
177
|
+
{
|
|
178
|
+
baseUrl: '/graphql'
|
|
179
|
+
},
|
|
180
|
+
{
|
|
174
181
|
configs: [
|
|
175
182
|
{
|
|
176
183
|
operationType: 'query',
|
|
@@ -192,9 +199,9 @@ const mockServerConfig = {
|
|
|
192
199
|
}
|
|
193
200
|
]
|
|
194
201
|
}
|
|
195
|
-
|
|
202
|
+
];
|
|
196
203
|
|
|
197
|
-
|
|
204
|
+
export default flatMockServerConfig;
|
|
198
205
|
```
|
|
199
206
|
|
|
200
207
|
Now you can make a request with an additional header and get the desired result
|
|
@@ -236,13 +243,13 @@ Allowed `checkModes`
|
|
|
236
243
|
- regExp - checks actual value with descriptor regExp.
|
|
237
244
|
- function - checks actual value with descriptor function.
|
|
238
245
|
|
|
239
|
-
Value for `checkMode` except `function` | `exists` | `notExists` can be array, so you can write even more complex logic. For example "does not contain these values" or "must be match to one of these regExp".
|
|
240
|
-
|
|
241
246
|
```javascript
|
|
242
|
-
/** @type {import('mock-config-server').
|
|
243
|
-
const
|
|
244
|
-
|
|
245
|
-
baseUrl: '/api'
|
|
247
|
+
/** @type {import('mock-config-server').FlatMockServerConfig} */
|
|
248
|
+
const flatMockServerConfig = [
|
|
249
|
+
{
|
|
250
|
+
baseUrl: '/api'
|
|
251
|
+
},
|
|
252
|
+
{
|
|
246
253
|
configs: [
|
|
247
254
|
{
|
|
248
255
|
path: '/user',
|
|
@@ -251,12 +258,13 @@ const mockServerConfig = {
|
|
|
251
258
|
{
|
|
252
259
|
entities: {
|
|
253
260
|
headers: {
|
|
254
|
-
// 'name-header' is 'Dmitriy'
|
|
261
|
+
// 'name-header' is 'Dmitriy'
|
|
255
262
|
'name-header': {
|
|
256
263
|
checkMode: 'equals',
|
|
257
|
-
value:
|
|
264
|
+
value: 'Dmitriy'
|
|
258
265
|
},
|
|
259
266
|
// check for 'equals' if descriptor not provided
|
|
267
|
+
// i.e. it is the same as `role: { checkMode: 'equals', value: 'developer' }`
|
|
260
268
|
role: 'developer'
|
|
261
269
|
},
|
|
262
270
|
cookies: {
|
|
@@ -264,32 +272,37 @@ const mockServerConfig = {
|
|
|
264
272
|
token: {
|
|
265
273
|
checkMode: 'exists'
|
|
266
274
|
},
|
|
267
|
-
// 'someSecretToken' cookie can be '123-abc' or 'abc
|
|
275
|
+
// 'someSecretToken' cookie can be '123-abc' or '456-abc' for example
|
|
268
276
|
someSecretToken: {
|
|
269
277
|
checkMode: 'regExp',
|
|
270
|
-
value:
|
|
278
|
+
value: /^\d\d\d-abc$/
|
|
271
279
|
}
|
|
272
280
|
}
|
|
273
281
|
},
|
|
274
|
-
data: 'Some user data for Dmitriy
|
|
282
|
+
data: 'Some user data for Dmitriy'
|
|
275
283
|
}
|
|
276
284
|
]
|
|
277
285
|
}
|
|
278
286
|
]
|
|
279
287
|
}
|
|
280
|
-
|
|
288
|
+
];
|
|
281
289
|
|
|
282
|
-
|
|
290
|
+
export default flatMockServerConfig;
|
|
283
291
|
```
|
|
284
292
|
|
|
285
|
-
|
|
286
|
-
|
|
293
|
+
#### Descriptor _oneOf_ property
|
|
294
|
+
|
|
295
|
+
For `checkMode` with the `value` property (all `checkMode` options except `exists` and `notExists`) you can use an array as value.
|
|
296
|
+
Mock server will find matches by iterating through the array until **some** match is found.
|
|
297
|
+
To be able to use this functionality you need to explicitly set `oneOf: true` property in descriptor object.
|
|
287
298
|
|
|
288
299
|
```javascript
|
|
289
|
-
/** @type {import('mock-config-server').
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
baseUrl: '/api'
|
|
300
|
+
/** @type {import('mock-config-server').FlatMockServerConfig} */
|
|
301
|
+
const flatMockServerConfig = [
|
|
302
|
+
{
|
|
303
|
+
baseUrl: '/api'
|
|
304
|
+
},
|
|
305
|
+
{
|
|
293
306
|
configs: [
|
|
294
307
|
{
|
|
295
308
|
path: '/user',
|
|
@@ -297,20 +310,37 @@ const mockServerConfig = {
|
|
|
297
310
|
routes: [
|
|
298
311
|
{
|
|
299
312
|
entities: {
|
|
300
|
-
// if body equals to { key1: 'value1' }
|
|
301
|
-
body:
|
|
313
|
+
// if body equals to { key1: 'value1' } OR { key2: 'value2' } then mock-config-server return 'Some user data 1'
|
|
314
|
+
body: {
|
|
315
|
+
checkMode: 'equals',
|
|
316
|
+
value: [{ key1: 'value1' }, { key2: 'value2' }],
|
|
317
|
+
oneOf: true
|
|
318
|
+
}
|
|
302
319
|
},
|
|
303
|
-
data: 'Some user data'
|
|
320
|
+
data: 'Some user data 1'
|
|
321
|
+
},
|
|
322
|
+
{
|
|
323
|
+
entities: {
|
|
324
|
+
// if body equals to [{ key1: 'value1' }, { key2: 'value2' }] then mock-config-server return 'Some user data 2'
|
|
325
|
+
// NO `oneOf` => array processed entirely
|
|
326
|
+
body: {
|
|
327
|
+
checkMode: 'equals',
|
|
328
|
+
value: [{ key1: 'value1' }, { key2: 'value2' }]
|
|
329
|
+
}
|
|
330
|
+
},
|
|
331
|
+
data: 'Some user data 2'
|
|
304
332
|
}
|
|
305
333
|
]
|
|
306
334
|
}
|
|
307
335
|
]
|
|
308
336
|
}
|
|
309
|
-
|
|
337
|
+
];
|
|
310
338
|
|
|
311
|
-
|
|
339
|
+
export default flatMockServerConfig;
|
|
312
340
|
```
|
|
313
341
|
|
|
342
|
+
#### Function check mode
|
|
343
|
+
|
|
314
344
|
`function checkMode` is the most powerful way to describe your `entities` logic, but in most cases you will be fine using other `checkModes`.
|
|
315
345
|
|
|
316
346
|
`Function value` has the following signature `(actualValue, checkFunction) => boolean`.
|
|
@@ -320,10 +350,12 @@ You can use the `checkFunction` from second argument if you want to describe you
|
|
|
320
350
|
`checkFunction` has the following signature `(checkMode, actualValue, descriptorValue?) => boolean`.
|
|
321
351
|
|
|
322
352
|
```javascript
|
|
323
|
-
/** @type {import('mock-config-server').
|
|
324
|
-
const
|
|
325
|
-
|
|
326
|
-
baseUrl: '/api'
|
|
353
|
+
/** @type {import('mock-config-server').FlatMockServerConfig} */
|
|
354
|
+
const flatMockServerConfig = [
|
|
355
|
+
{
|
|
356
|
+
baseUrl: '/api'
|
|
357
|
+
},
|
|
358
|
+
{
|
|
327
359
|
configs: [
|
|
328
360
|
{
|
|
329
361
|
path: '/posts/:postId',
|
|
@@ -340,7 +372,7 @@ const mockServerConfig = {
|
|
|
340
372
|
cookies: {
|
|
341
373
|
authToken: {
|
|
342
374
|
checkMode: 'function',
|
|
343
|
-
value: (actualValue, checkFunction) =>
|
|
375
|
+
value: (actualValue, checkFunction) =>
|
|
344
376
|
checkFunction('equals', actualValue, 123) ||
|
|
345
377
|
checkFunction('startsWith', actualValue, 2)
|
|
346
378
|
}
|
|
@@ -352,21 +384,22 @@ const mockServerConfig = {
|
|
|
352
384
|
}
|
|
353
385
|
]
|
|
354
386
|
}
|
|
355
|
-
|
|
387
|
+
];
|
|
356
388
|
|
|
357
|
-
module.exports =
|
|
389
|
+
module.exports = flatMockServerConfig;
|
|
358
390
|
```
|
|
359
391
|
|
|
360
392
|
##### Using descriptors for part of REST body or GraphQL variables
|
|
361
393
|
|
|
362
|
-
If you want to check a
|
|
363
|
-
You can use descriptors for array body elements as well.
|
|
394
|
+
If you want to check a deep nested property of your body or variables via descriptor you can use flatten object style. In this case server will check every field in entity with corresponding actual field. I.e. you can use descriptors only for properties of entity object (not for properties of nested objects).
|
|
364
395
|
|
|
365
396
|
```javascript
|
|
366
|
-
/** @type {import('mock-config-server').
|
|
367
|
-
const
|
|
368
|
-
|
|
369
|
-
baseUrl: '/api'
|
|
397
|
+
/** @type {import('mock-config-server').FlatMockServerConfig} */
|
|
398
|
+
const flatMockServerConfig = [
|
|
399
|
+
{
|
|
400
|
+
baseUrl: '/api'
|
|
401
|
+
},
|
|
402
|
+
{
|
|
370
403
|
configs: [
|
|
371
404
|
{
|
|
372
405
|
path: '/users',
|
|
@@ -375,62 +408,35 @@ const mockServerConfig = {
|
|
|
375
408
|
{
|
|
376
409
|
entities: {
|
|
377
410
|
body: {
|
|
378
|
-
'user.name': 'Sergey'
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
}
|
|
383
|
-
]
|
|
384
|
-
},
|
|
385
|
-
{
|
|
386
|
-
path: '/posts',
|
|
387
|
-
method: 'post',
|
|
388
|
-
routes: [
|
|
389
|
-
{
|
|
390
|
-
entities: {
|
|
391
|
-
body: {
|
|
392
|
-
title: {
|
|
393
|
-
checkMode: 'startsWith',
|
|
394
|
-
value: 'A'
|
|
411
|
+
// if body has properties like { user: { name: 'Sergey' } } OR { 'user.name': 'Sergey' } then mock-config-server return data
|
|
412
|
+
'user.name': {
|
|
413
|
+
checkMode: 'equals',
|
|
414
|
+
value: 'Sergey'
|
|
395
415
|
}
|
|
396
416
|
}
|
|
397
417
|
},
|
|
398
|
-
data: '
|
|
399
|
-
}
|
|
400
|
-
]
|
|
401
|
-
},
|
|
402
|
-
{
|
|
403
|
-
path: '/posts',
|
|
404
|
-
method: 'post',
|
|
405
|
-
routes: [
|
|
406
|
-
{
|
|
407
|
-
entities: {
|
|
408
|
-
body: [
|
|
409
|
-
{
|
|
410
|
-
checkMode: 'startsWith',
|
|
411
|
-
value: 1
|
|
412
|
-
},
|
|
413
|
-
2
|
|
414
|
-
]
|
|
415
|
-
},
|
|
416
|
-
data: 'array[0] starts with "1" and array[1] equals "2"'
|
|
418
|
+
data: 'user.name in body is "Sergey"'
|
|
417
419
|
}
|
|
418
420
|
]
|
|
419
421
|
}
|
|
420
422
|
]
|
|
421
423
|
}
|
|
422
|
-
|
|
424
|
+
];
|
|
423
425
|
|
|
424
|
-
|
|
426
|
+
export default flatMockServerConfig;
|
|
425
427
|
```
|
|
426
428
|
|
|
427
|
-
|
|
429
|
+
You can also use descriptor for whole body or variables entity.
|
|
430
|
+
|
|
431
|
+
> When you use 'equals'/'notEquals' check mode for whole body or variables mock-config-server is strictly compare entity and actual value. It means that you must specify **ALL** properties from actual body or variables.
|
|
428
432
|
|
|
429
433
|
```javascript
|
|
430
|
-
/** @type {import('mock-config-server').
|
|
431
|
-
const
|
|
432
|
-
|
|
433
|
-
baseUrl: '/api'
|
|
434
|
+
/** @type {import('mock-config-server').FlatMockServerConfig} */
|
|
435
|
+
const flatMockServerConfig = [
|
|
436
|
+
{
|
|
437
|
+
baseUrl: '/api'
|
|
438
|
+
},
|
|
439
|
+
{
|
|
434
440
|
configs: [
|
|
435
441
|
{
|
|
436
442
|
path: '/users',
|
|
@@ -439,6 +445,7 @@ const mockServerConfig = {
|
|
|
439
445
|
{
|
|
440
446
|
entities: {
|
|
441
447
|
body: {
|
|
448
|
+
// if actual body contains some extra property(-ies) then this entity won't match
|
|
442
449
|
checkMode: 'equals',
|
|
443
450
|
value: {
|
|
444
451
|
user: {
|
|
@@ -455,131 +462,154 @@ const mockServerConfig = {
|
|
|
455
462
|
}
|
|
456
463
|
]
|
|
457
464
|
}
|
|
458
|
-
|
|
465
|
+
];
|
|
459
466
|
|
|
460
|
-
|
|
467
|
+
export default flatMockServerConfig;
|
|
461
468
|
```
|
|
462
469
|
|
|
463
|
-
####
|
|
464
|
-
|
|
465
|
-
Routes support polling for data. To add polling for data, you must specify the `polling setting` and change `data` property to `queue`.
|
|
470
|
+
#### File responses
|
|
466
471
|
|
|
467
|
-
|
|
472
|
+
Rest routes support paths to files. If a route is matched, the server will send data from the file. If the file is not found, the server will return 404.
|
|
468
473
|
|
|
469
474
|
```javascript
|
|
470
|
-
/** @type {import('mock-config-server').
|
|
471
|
-
const
|
|
472
|
-
|
|
473
|
-
baseUrl: '/api'
|
|
475
|
+
/** @type {import('mock-config-server').FlatMockServerConfig} */
|
|
476
|
+
const flatMockServerConfig = [
|
|
477
|
+
{
|
|
478
|
+
baseUrl: '/api'
|
|
479
|
+
},
|
|
480
|
+
{
|
|
474
481
|
configs: [
|
|
475
482
|
{
|
|
476
|
-
path: '/
|
|
483
|
+
path: '/files/settings',
|
|
477
484
|
method: 'get',
|
|
478
485
|
routes: [
|
|
479
486
|
{
|
|
480
|
-
|
|
481
|
-
queue: [
|
|
482
|
-
{ data: { emoji: '🦁', name: 'Nursultan' } },
|
|
483
|
-
{ data: { emoji: '☄', name: 'Dmitriy' } }
|
|
484
|
-
]
|
|
487
|
+
file: './settings.json'
|
|
485
488
|
}
|
|
486
489
|
]
|
|
487
490
|
}
|
|
488
491
|
]
|
|
489
492
|
}
|
|
490
|
-
|
|
493
|
+
];
|
|
491
494
|
|
|
492
|
-
export default
|
|
495
|
+
export default flatMockServerConfig;
|
|
493
496
|
```
|
|
494
497
|
|
|
495
|
-
|
|
498
|
+
> If the file path is absolute, then this path will be used as is. If the file path is relative, it will be appended to the current working directory.
|
|
499
|
+
|
|
500
|
+
If the file exists, response interceptors will receive `file descriptor` as the `data` argument:
|
|
501
|
+
|
|
502
|
+
`File descriptor` is an object with `path` and `file` fields that describe file location and file content.
|
|
503
|
+
|
|
504
|
+
- `path` `string` path to the file. Same as `file` passed in route
|
|
505
|
+
- `file` `Buffer` file content as binary buffer
|
|
506
|
+
|
|
507
|
+
> Note to return file descriptor from interceptor. Server will send a buffer from `data.file` with corresponding `Content-Type` and `Content-Disposition` headers.
|
|
508
|
+
> If you return invalid file descriptor, server will send it as json data.
|
|
496
509
|
|
|
497
510
|
```javascript
|
|
498
|
-
/** @type {import('mock-config-server').
|
|
499
|
-
const
|
|
500
|
-
|
|
501
|
-
baseUrl: '/api'
|
|
511
|
+
/** @type {import('mock-config-server').FlatMockServerConfig} */
|
|
512
|
+
const flatMockServerConfig = [
|
|
513
|
+
{
|
|
514
|
+
baseUrl: '/api'
|
|
515
|
+
},
|
|
516
|
+
{
|
|
502
517
|
configs: [
|
|
503
518
|
{
|
|
504
|
-
path: '/
|
|
519
|
+
path: '/files/settings',
|
|
505
520
|
method: 'get',
|
|
506
521
|
routes: [
|
|
507
522
|
{
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
523
|
+
file: './settings.json',
|
|
524
|
+
interceptors: {
|
|
525
|
+
response: (data) => {
|
|
526
|
+
const { file, path } = data;
|
|
527
|
+
const buffer = file; // some logic with buffer
|
|
528
|
+
fs.writeFileSync(path, buffer); // rewrite ./settings.json file on disk with new content
|
|
529
|
+
return { path, file: buffer };
|
|
530
|
+
}
|
|
531
|
+
}
|
|
513
532
|
}
|
|
514
533
|
]
|
|
515
534
|
}
|
|
516
535
|
]
|
|
517
536
|
}
|
|
518
|
-
|
|
537
|
+
];
|
|
519
538
|
|
|
520
|
-
export default
|
|
539
|
+
export default flatMockServerConfig;
|
|
521
540
|
```
|
|
522
541
|
|
|
523
|
-
|
|
542
|
+
> Any changes to the data will not affect the file on disk unless you manually rewrite it.
|
|
524
543
|
|
|
525
|
-
|
|
544
|
+
> If you return a new `path` from interceptor, server will send file corresponding to this path or 404 error otherwise.
|
|
545
|
+
|
|
546
|
+
#### Polling
|
|
547
|
+
|
|
548
|
+
Routes support polling for data. To add polling for data, you must specify the `polling setting` and use `queue` property instead of `data` or `file`.
|
|
549
|
+
|
|
550
|
+
`queue` is an array containing `data` or `file` that should be returned in order.
|
|
551
|
+
|
|
552
|
+
> After receiving the last value from polling, the queue is reset and the next request will return the first value from the queue.
|
|
526
553
|
|
|
527
554
|
```javascript
|
|
528
|
-
/** @type {import('mock-config-server').
|
|
529
|
-
const
|
|
530
|
-
|
|
531
|
-
baseUrl: '/api'
|
|
555
|
+
/** @type {import('mock-config-server').FlatMockServerConfig} */
|
|
556
|
+
const flatMockServerConfig = [
|
|
557
|
+
{
|
|
558
|
+
baseUrl: '/api'
|
|
559
|
+
},
|
|
560
|
+
{
|
|
532
561
|
configs: [
|
|
533
562
|
{
|
|
534
|
-
path: '/
|
|
563
|
+
path: '/user',
|
|
535
564
|
method: 'get',
|
|
536
565
|
routes: [
|
|
537
566
|
{
|
|
538
|
-
|
|
567
|
+
settings: { polling: true },
|
|
568
|
+
queue: [
|
|
569
|
+
{ data: { emoji: '🦁', name: 'Nursultan' } },
|
|
570
|
+
{ data: { emoji: '☄', name: 'Dmitriy' } },
|
|
571
|
+
{ file: './users/Sergey.json' }
|
|
572
|
+
]
|
|
539
573
|
}
|
|
540
574
|
]
|
|
541
575
|
}
|
|
542
576
|
]
|
|
543
577
|
}
|
|
544
|
-
|
|
578
|
+
];
|
|
545
579
|
|
|
546
|
-
export default
|
|
580
|
+
export default flatMockServerConfig;
|
|
547
581
|
```
|
|
548
582
|
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
If the file exists, response interceptors will receive null as the data argument.
|
|
583
|
+
Using the additional `time` properties in milliseconds, you can specify how much time certain data should be returned
|
|
552
584
|
|
|
553
585
|
```javascript
|
|
554
|
-
/** @type {import('mock-config-server').
|
|
555
|
-
const
|
|
556
|
-
|
|
557
|
-
baseUrl: '/api'
|
|
586
|
+
/** @type {import('mock-config-server').FlatMockServerConfig} */
|
|
587
|
+
const flatMockServerConfig = [
|
|
588
|
+
{
|
|
589
|
+
baseUrl: '/api'
|
|
590
|
+
},
|
|
591
|
+
{
|
|
558
592
|
configs: [
|
|
559
593
|
{
|
|
560
|
-
path: '/
|
|
594
|
+
path: '/user',
|
|
561
595
|
method: 'get',
|
|
562
596
|
routes: [
|
|
563
597
|
{
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
}
|
|
570
|
-
}
|
|
598
|
+
settings: { polling: true },
|
|
599
|
+
queue: [
|
|
600
|
+
{ time: 5000, data: { emoji: '🦁', name: 'Nursultan' } },
|
|
601
|
+
{ data: { emoji: '☄', name: 'Dmitriy' } }
|
|
602
|
+
]
|
|
571
603
|
}
|
|
572
604
|
]
|
|
573
605
|
}
|
|
574
606
|
]
|
|
575
607
|
}
|
|
576
|
-
|
|
608
|
+
];
|
|
577
609
|
|
|
578
|
-
export default
|
|
610
|
+
export default flatMockServerConfig;
|
|
579
611
|
```
|
|
580
612
|
|
|
581
|
-
> Any changes to the data will not affect the file (and the response, respectively).
|
|
582
|
-
|
|
583
613
|
#### Static Path
|
|
584
614
|
|
|
585
615
|
Entity for connecting statics to the server, like HTML, JSON, PNG, etc.
|
|
@@ -685,10 +715,12 @@ You can log requests and responses using `log` function in any [interceptor](#in
|
|
|
685
715
|
`log` function returns object with logged token values
|
|
686
716
|
|
|
687
717
|
```javascript
|
|
688
|
-
/** @type {import('mock-config-server').
|
|
689
|
-
const
|
|
690
|
-
|
|
691
|
-
baseUrl: '/api'
|
|
718
|
+
/** @type {import('mock-config-server').FlatMockServerConfig} */
|
|
719
|
+
const flatMockServerConfig = [
|
|
720
|
+
{
|
|
721
|
+
baseUrl: '/api'
|
|
722
|
+
},
|
|
723
|
+
{
|
|
692
724
|
configs: [
|
|
693
725
|
{
|
|
694
726
|
path: '/posts',
|
|
@@ -697,19 +729,23 @@ const mockServerConfig = {
|
|
|
697
729
|
{
|
|
698
730
|
interceptors: {
|
|
699
731
|
request: ({ log }) => {
|
|
700
|
-
log({
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
732
|
+
log({
|
|
733
|
+
// logs following object in terminal
|
|
734
|
+
options: {
|
|
735
|
+
// {
|
|
736
|
+
id: true, // id: 1,
|
|
737
|
+
type: true, // type: 'request',
|
|
738
|
+
timestamp: true, // timestamp: '31.12.2024, 23:59:59,999',
|
|
739
|
+
method: true, // method: 'GET',
|
|
740
|
+
url: true // url: 'http://localhost:31299/api/rest/posts/1'
|
|
741
|
+
} // }
|
|
708
742
|
});
|
|
709
743
|
},
|
|
710
744
|
response: (data, { log }) => {
|
|
711
|
-
log({
|
|
712
|
-
|
|
745
|
+
log({
|
|
746
|
+
// logs following string in terminal
|
|
747
|
+
options: {
|
|
748
|
+
// response get: http://localhost:31299/api/rest/posts/1 => 200
|
|
713
749
|
type: true,
|
|
714
750
|
statusCode: true,
|
|
715
751
|
method: true,
|
|
@@ -727,15 +763,15 @@ const mockServerConfig = {
|
|
|
727
763
|
}
|
|
728
764
|
]
|
|
729
765
|
}
|
|
730
|
-
|
|
766
|
+
];
|
|
731
767
|
|
|
732
|
-
export default
|
|
768
|
+
export default flatMockServerConfig;
|
|
733
769
|
```
|
|
734
770
|
|
|
735
771
|
> By default, `timestamp` and `method` tokens are prettified.
|
|
736
772
|
> Timestamp transforms from UNIX-timestamp number to `DD.MM.YYYY, HH:mm:ss,sss` string.
|
|
737
773
|
> Method transforms from lower case to upper case.
|
|
738
|
-
> If `rewrite` function is used, those tokens will remain unformatted. You can format them as you need.
|
|
774
|
+
> If `rewrite` function is used, those tokens will remain unformatted. You can format them as you need.
|
|
739
775
|
|
|
740
776
|
##### Logger tokens
|
|
741
777
|
|
|
@@ -755,22 +791,25 @@ export default mockServerConfig;
|
|
|
755
791
|
- `body?` `any` body
|
|
756
792
|
|
|
757
793
|
Response logger has additional tokens
|
|
794
|
+
|
|
758
795
|
- `statusCode?` `number` response status code
|
|
759
796
|
- `data?` `any` data returned to client
|
|
760
797
|
|
|
761
798
|
If you need to log specific properties in mapped entities (headers, cookies, query, params), use `Record<string, boolean>` object instead of boolean.
|
|
762
799
|
In that case logger will use following logic:
|
|
763
800
|
|
|
764
|
-
- if some token is `true`, entity will be filtered by `whitelist` logic.
|
|
765
|
-
- if all tokens is `false`, entity will be filtered by `blacklist` logic. All entities will be logged
|
|
801
|
+
- if some token is `true`, entity will be filtered by `whitelist` logic. _Only_ enabled ones will be logged.
|
|
802
|
+
- if all tokens is `false`, entity will be filtered by `blacklist` logic. All entities will be logged _except_ disabled ones.
|
|
766
803
|
|
|
767
804
|
> Whitelist logic have priority over blacklist if you pass `true` and `false` in same entity.
|
|
768
805
|
|
|
769
806
|
```javascript
|
|
770
|
-
/** @type {import('mock-config-server').
|
|
771
|
-
const
|
|
772
|
-
|
|
773
|
-
baseUrl: '/api'
|
|
807
|
+
/** @type {import('mock-config-server').FlatMockServerConfig} */
|
|
808
|
+
const flatMockServerConfig = [
|
|
809
|
+
{
|
|
810
|
+
baseUrl: '/api'
|
|
811
|
+
},
|
|
812
|
+
{
|
|
774
813
|
configs: [
|
|
775
814
|
{
|
|
776
815
|
path: '/posts',
|
|
@@ -779,7 +818,8 @@ const mockServerConfig = {
|
|
|
779
818
|
{
|
|
780
819
|
interceptors: {
|
|
781
820
|
request: ({ log }) => {
|
|
782
|
-
log({
|
|
821
|
+
log({
|
|
822
|
+
// whitelist. only query1 and query2 will be logged
|
|
783
823
|
options: {
|
|
784
824
|
query: {
|
|
785
825
|
query1: true,
|
|
@@ -787,7 +827,8 @@ const mockServerConfig = {
|
|
|
787
827
|
}
|
|
788
828
|
}
|
|
789
829
|
});
|
|
790
|
-
log({
|
|
830
|
+
log({
|
|
831
|
+
// whitelist. only cookie1 and cookie2 will be logged
|
|
791
832
|
options: {
|
|
792
833
|
cookies: {
|
|
793
834
|
cookie1: true,
|
|
@@ -796,7 +837,8 @@ const mockServerConfig = {
|
|
|
796
837
|
}
|
|
797
838
|
}
|
|
798
839
|
});
|
|
799
|
-
log({
|
|
840
|
+
log({
|
|
841
|
+
// blacklist. all headers will be logged except header1
|
|
800
842
|
options: {
|
|
801
843
|
headers: {
|
|
802
844
|
header1: false
|
|
@@ -811,9 +853,9 @@ const mockServerConfig = {
|
|
|
811
853
|
}
|
|
812
854
|
]
|
|
813
855
|
}
|
|
814
|
-
|
|
856
|
+
];
|
|
815
857
|
|
|
816
|
-
export default
|
|
858
|
+
export default flatMockServerConfig;
|
|
817
859
|
```
|
|
818
860
|
|
|
819
861
|
## Database
|
|
@@ -826,16 +868,18 @@ With `mock-config-server` you can create your own mock database with all CRUD op
|
|
|
826
868
|
### Basic example
|
|
827
869
|
|
|
828
870
|
```javascript
|
|
829
|
-
const
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
871
|
+
const flatMockServerConfig = [
|
|
872
|
+
{
|
|
873
|
+
database: {
|
|
874
|
+
data: {
|
|
875
|
+
users: [{ id: 1, name: 'John' }],
|
|
876
|
+
settings: {
|
|
877
|
+
blocked: false
|
|
878
|
+
}
|
|
835
879
|
}
|
|
836
880
|
}
|
|
837
881
|
}
|
|
838
|
-
|
|
882
|
+
];
|
|
839
883
|
```
|
|
840
884
|
|
|
841
885
|
Now you have the following routes for requests
|
|
@@ -872,20 +916,22 @@ __routes -> return routes from database config
|
|
|
872
916
|
### Routes example
|
|
873
917
|
|
|
874
918
|
```javascript
|
|
875
|
-
const
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
919
|
+
const flatMockServerConfig = [
|
|
920
|
+
{
|
|
921
|
+
database: {
|
|
922
|
+
data: {
|
|
923
|
+
users: [{ id: 1, name: 'John' }],
|
|
924
|
+
settings: {
|
|
925
|
+
blocked: false
|
|
926
|
+
}
|
|
927
|
+
},
|
|
928
|
+
routes: {
|
|
929
|
+
'/api/users/:id': '/users/:id',
|
|
930
|
+
'/*/my-settings': '/settings'
|
|
881
931
|
}
|
|
882
|
-
},
|
|
883
|
-
routes: {
|
|
884
|
-
'/api/users/:id': '/users/:id',
|
|
885
|
-
'/*/my-settings': '/settings'
|
|
886
932
|
}
|
|
887
933
|
}
|
|
888
|
-
|
|
934
|
+
];
|
|
889
935
|
```
|
|
890
936
|
|
|
891
937
|
Now following routes will work correctly
|
|
@@ -986,12 +1032,14 @@ GET /users?_q=siberia&_q=24
|
|
|
986
1032
|
### File example
|
|
987
1033
|
|
|
988
1034
|
```javascript
|
|
989
|
-
const
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
1035
|
+
const flatMockServerConfig = [
|
|
1036
|
+
{
|
|
1037
|
+
database: {
|
|
1038
|
+
data: './data.json',
|
|
1039
|
+
routes: './routes.json'
|
|
1040
|
+
}
|
|
993
1041
|
}
|
|
994
|
-
|
|
1042
|
+
];
|
|
995
1043
|
```
|
|
996
1044
|
|
|
997
1045
|
Instead of objects you can use paths to **JSON** files which contain needed data or routes
|