mock-config-server 3.6.1 → 3.7.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 +291 -90
- 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/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/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/validateMockServerConfig/graphqlConfigSchema/graphqlConfigSchema.d.ts +6 -6
- package/dist/bin/validateMockServerConfig/restConfigSchema/restConfigSchema.d.ts +14 -14
- package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.d.ts +1 -1
- package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.js +1 -3
- package/dist/src/core/middlewares/contextMiddleware/contextMiddleware.d.ts +17 -0
- package/dist/src/core/middlewares/contextMiddleware/contextMiddleware.js +33 -0
- package/dist/src/core/middlewares/cookieParseMiddleware/helpers/parseCookie/parseCookie.d.ts +1 -2
- package/dist/src/core/middlewares/index.d.ts +1 -0
- package/dist/src/core/middlewares/index.js +1 -0
- package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.js +3 -2
- package/dist/src/core/rest/createRestRoutes/createRestRoutes.d.ts +1 -1
- package/dist/src/core/rest/createRestRoutes/createRestRoutes.js +1 -3
- package/dist/src/server/createDatabaseMockServer/createDatabaseMockServer.js +1 -0
- package/dist/src/server/createGraphQLMockServer/createGraphQLMockServer.js +1 -0
- package/dist/src/server/createMockServer/createMockServer.js +1 -0
- package/dist/src/server/createRestMockServer/createRestMockServer.js +1 -0
- package/dist/src/server/startDatabaseMockServer/startDatabaseMockServer.js +1 -1
- package/dist/src/server/startGraphQLMockServer/startGraphQLMockServer.js +1 -1
- package/dist/src/server/startMockServer/startMockServer.js +1 -1
- package/dist/src/server/startRestMockServer/startRestMockServer.js +1 -1
- package/dist/src/static/views/pages/404/index.ejs +66 -69
- package/dist/src/utils/helpers/date/formatTimestamp/formatTimestamp.d.ts +1 -0
- package/dist/src/utils/helpers/date/formatTimestamp/formatTimestamp.js +18 -0
- package/dist/src/utils/helpers/date/index.d.ts +1 -0
- package/dist/src/utils/helpers/date/index.js +18 -0
- package/dist/src/utils/helpers/index.d.ts +2 -0
- package/dist/src/utils/helpers/index.js +2 -0
- package/dist/src/utils/helpers/interceptors/callRequestInterceptor/callRequestInterceptor.js +7 -1
- package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.js +9 -1
- package/dist/src/utils/helpers/logger/callRequestLogger/callRequestLogger.d.ts +8 -0
- package/dist/src/utils/helpers/logger/callRequestLogger/callRequestLogger.js +50 -0
- package/dist/src/utils/helpers/logger/callResponseLogger/callResponseLogger.d.ts +10 -0
- package/dist/src/utils/helpers/logger/callResponseLogger/callResponseLogger.js +54 -0
- package/dist/src/utils/helpers/logger/helpers/filterTokens/filterTokens.d.ts +5 -0
- package/dist/src/utils/helpers/logger/helpers/filterTokens/filterTokens.js +44 -0
- package/dist/src/utils/helpers/logger/helpers/formatTokens/formatTokens.d.ts +2 -0
- package/dist/src/utils/helpers/logger/helpers/formatTokens/formatTokens.js +23 -0
- package/dist/src/utils/helpers/logger/helpers/index.d.ts +2 -0
- package/dist/src/utils/helpers/logger/helpers/index.js +19 -0
- package/dist/src/utils/helpers/logger/index.d.ts +2 -0
- package/dist/src/utils/helpers/logger/index.js +19 -0
- package/dist/src/utils/types/checkModes.d.ts +0 -1
- package/dist/src/utils/types/graphql.d.ts +2 -5
- package/dist/src/utils/types/index.d.ts +2 -0
- package/dist/src/utils/types/index.js +2 -0
- package/dist/src/utils/types/interceptors.d.ts +11 -7
- package/dist/src/utils/types/logger.d.ts +43 -0
- package/dist/src/utils/types/logger.js +4 -0
- package/dist/src/utils/types/rest.d.ts +2 -2
- package/dist/src/utils/types/server.d.ts +2 -2
- package/dist/src/utils/types/shared.d.ts +1 -0
- package/dist/src/utils/types/shared.js +4 -0
- package/dist/src/utils/types/values.d.ts +7 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -66,57 +66,40 @@ $ npx mock-config-server
|
|
|
66
66
|
## 🎭 Parameters for mock-server.config.(js|ts)
|
|
67
67
|
|
|
68
68
|
- `rest?` Rest configs for mock requests
|
|
69
|
-
- `baseUrl?`
|
|
70
|
-
- `configs`
|
|
71
|
-
- `interceptors?`
|
|
69
|
+
- `baseUrl?` `string` part of the url that will be substituted at the beginning of rest request url (default: `'/'`)
|
|
70
|
+
- `configs` `Array<RestRequestConfig>` configs for mock requests, [read](#configs)
|
|
71
|
+
- `interceptors?` `Interceptors` functions to change request or response parameters, [read](#interceptors)
|
|
72
72
|
- `graphql?` GraphQL configs for mock requests
|
|
73
|
-
- `baseUrl?`
|
|
74
|
-
- `configs`
|
|
75
|
-
- `interceptors?`
|
|
73
|
+
- `baseUrl?` `string` part of the url that will be substituted at the beginning of graphql request url (default: `'/'`)
|
|
74
|
+
- `configs` `Array<GraphQLRequestConfig>` configs for mock requests, [read](#configs)
|
|
75
|
+
- `interceptors?` `Interceptors` functions to change request or response parameters, [read](#interceptors)
|
|
76
76
|
- `database?` Database config for mock requests [read](#database)
|
|
77
|
-
- `data`
|
|
78
|
-
- `routes?`
|
|
79
|
-
- `staticPath?`
|
|
80
|
-
- `interceptors?`
|
|
81
|
-
- `cors?`
|
|
82
|
-
- `port?`
|
|
83
|
-
- `baseUrl?`
|
|
77
|
+
- `data` `Object | string` initial data for database
|
|
78
|
+
- `routes?` `Object | string` map of custom routes for database
|
|
79
|
+
- `staticPath?` `StaticPath` entity for working with static files, [read](#static-path)
|
|
80
|
+
- `interceptors?` `Interceptors` functions to change request or response parameters, [read](#interceptors)
|
|
81
|
+
- `cors?` `Cors` CORS settings object (default: `CORS is turn off`), [read](#cors)
|
|
82
|
+
- `port?` `number` server port (default: `31299`)
|
|
83
|
+
- `baseUrl?` `string` part of the url that will be substituted at the beginning of the request url (default: `'/'`)
|
|
84
84
|
|
|
85
85
|
### Configs
|
|
86
86
|
|
|
87
87
|
Configs are the fundamental part of the mock server. These configs are easy to fill and maintain. Config entities is an object with which you can emulate various application behaviors. You can specify `headers` | `cookies` | `query` | `params` | `body` for Rest request or `headers` | `cookies` | `query` | `variables` for GraphQL request to define what contract data you need to get. Using this mechanism, you can easily simulate the operation of the server and emulate various cases
|
|
88
88
|
|
|
89
|
-
|
|
89
|
+
#### Rest request config
|
|
90
90
|
|
|
91
91
|
Every route must be configured to handle response content in one of three ways: data or [queue](#polling) or [file](#file-responses).
|
|
92
92
|
|
|
93
|
-
- `path`
|
|
94
|
-
- `method`
|
|
95
|
-
- `routes`
|
|
96
|
-
- `data?`
|
|
97
|
-
- `queue?`
|
|
98
|
-
- `file?`
|
|
99
|
-
- `settings?`
|
|
100
|
-
- `entities?` Object<headers | cookies | query | params | body
|
|
101
|
-
- `interceptors?`
|
|
102
|
-
- `interceptors?`
|
|
103
|
-
|
|
104
|
-
##### GraphQL request config
|
|
105
|
-
|
|
106
|
-
Every route must be configured to handle response content in one of two ways: data or [queue](#polling).
|
|
107
|
-
|
|
108
|
-
- `operationType` {query | mutation} graphql operation type
|
|
109
|
-
- `operationName?` {string | RegExp} graphql operation name
|
|
110
|
-
- `query?`: {string} graphql query as string
|
|
111
|
-
- `routes` {GraphQLRouteConfig[]} request routes
|
|
112
|
-
- `data?` {any} mock data of request
|
|
113
|
-
- `queue?` {Array<{ time?: number; data: any}>} queue for polling with opportunity to set time for each response
|
|
114
|
-
- `settings?` {Settings} settings for route (polling on/off, etc.)
|
|
115
|
-
- `entities?` Object<headers | cookies | query | variables> object that helps in data retrieval
|
|
116
|
-
- `interceptors?` {Interceptors} functions to change request or response parameters, [read](#interceptors)
|
|
117
|
-
- `interceptors?` {Interceptors} functions to change request or response parameters, [read](#interceptors)
|
|
118
|
-
|
|
119
|
-
> Every graphql config should contain `operationName` or `query` or both of them
|
|
93
|
+
- `path` `string | RegExp` request path
|
|
94
|
+
- `method` `'get' | 'post' | 'delete' | 'put' | 'patch' | 'options'` rest api method
|
|
95
|
+
- `routes` `RestRouteConfig[]` request routes
|
|
96
|
+
- `data?` `any` mock data of request
|
|
97
|
+
- `queue?` `Array<{ time?: number; data: any}>` queue for polling with opportunity to set time for each response
|
|
98
|
+
- `file?` `string` path to file for return in response
|
|
99
|
+
- `settings?` `Settings` settings for route (polling on/off, etc.)
|
|
100
|
+
- `entities?` `Object<headers | cookies | query | params | body>` object that helps in data retrieval
|
|
101
|
+
- `interceptors?` `Interceptors` functions to change request or response parameters, [read](#interceptors)
|
|
102
|
+
- `interceptors?` `Interceptors` functions to change request or response parameters, [read](#interceptors)
|
|
120
103
|
|
|
121
104
|
##### Rest example
|
|
122
105
|
|
|
@@ -164,6 +147,23 @@ fetch('http://localhost:31299/api/user', {
|
|
|
164
147
|
.then((data) => console.log(data)); // { emoji: '🦁', name: 'Nursultan' }
|
|
165
148
|
```
|
|
166
149
|
|
|
150
|
+
#### GraphQL request config
|
|
151
|
+
|
|
152
|
+
Every route must be configured to handle response content in one of two ways: data or [queue](#polling).
|
|
153
|
+
|
|
154
|
+
- `operationType` `query | mutation` graphql operation type
|
|
155
|
+
- `operationName?` `string | RegExp` graphql operation name
|
|
156
|
+
- `query?`: `string` graphql query as string
|
|
157
|
+
- `routes` `GraphQLRouteConfig[]` request routes
|
|
158
|
+
- `data?` `any` mock data of request
|
|
159
|
+
- `queue?` `Array<{ time?: number; data: any}>` queue for polling with opportunity to set time for each response
|
|
160
|
+
- `settings?` `Settings` settings for route (polling on/off, etc.)
|
|
161
|
+
- `entities?` `Object<headers | cookies | query | variables>` object that helps in data retrieval
|
|
162
|
+
- `interceptors?` `Interceptors` functions to change request or response parameters, [read](#interceptors)
|
|
163
|
+
- `interceptors?` `Interceptors` functions to change request or response parameters, [read](#interceptors)
|
|
164
|
+
|
|
165
|
+
> Every graphql config should contain `operationName` or `query` or both of them
|
|
166
|
+
|
|
167
167
|
##### GraphQL example
|
|
168
168
|
|
|
169
169
|
```javascript
|
|
@@ -319,6 +319,44 @@ Return `true` if `actualValue` matches your logic or `false` otherwise.
|
|
|
319
319
|
You can use the `checkFunction` from second argument if you want to describe your logic in a more declarative way.
|
|
320
320
|
`checkFunction` has the following signature `(checkMode, actualValue, descriptorValue?) => boolean`.
|
|
321
321
|
|
|
322
|
+
```javascript
|
|
323
|
+
/** @type {import('mock-config-server').MockServerConfig} */
|
|
324
|
+
const mockServerConfig = {
|
|
325
|
+
rest: {
|
|
326
|
+
baseUrl: '/api',
|
|
327
|
+
configs: [
|
|
328
|
+
{
|
|
329
|
+
path: '/posts/:postId',
|
|
330
|
+
method: 'post',
|
|
331
|
+
routes: [
|
|
332
|
+
{
|
|
333
|
+
entities: {
|
|
334
|
+
params: {
|
|
335
|
+
postId: {
|
|
336
|
+
checkMode: 'function',
|
|
337
|
+
value: (actualValue) => +actualValue >= 0 && +actualValue <= 50
|
|
338
|
+
},
|
|
339
|
+
},
|
|
340
|
+
cookies: {
|
|
341
|
+
authToken: {
|
|
342
|
+
checkMode: 'function',
|
|
343
|
+
value: (actualValue, checkFunction) =>
|
|
344
|
+
checkFunction('equals', actualValue, 123) ||
|
|
345
|
+
checkFunction('startsWith', actualValue, 2)
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
data: 'Some user data'
|
|
350
|
+
}
|
|
351
|
+
]
|
|
352
|
+
}
|
|
353
|
+
]
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
module.exports = mockServerConfig;
|
|
358
|
+
```
|
|
359
|
+
|
|
322
360
|
##### Using descriptors for part of REST body or GraphQL variables
|
|
323
361
|
|
|
324
362
|
If you want to check a certain field of your body or variables, you can use descriptors in flatten object style. In this case server will check every field in entity with corresponding actual field.
|
|
@@ -548,27 +586,27 @@ Entity for connecting statics to the server, like HTML, JSON, PNG, etc.
|
|
|
548
586
|
|
|
549
587
|
- `string` path to your static files
|
|
550
588
|
- `Object<{prefix, path}`
|
|
551
|
-
- `prefix`
|
|
552
|
-
- `path`
|
|
589
|
+
- `prefix` `string` path prefix for request
|
|
590
|
+
- `path` `string` path to your static files
|
|
553
591
|
- `Array<string | Object<{prefix, path}>>`
|
|
554
592
|
|
|
555
593
|
#### Cors
|
|
556
594
|
|
|
557
595
|
Object with settings for [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS). You can flexibly configure the required origin, methods, headers, credentials, maxAge for the entire server. If you do not specify `CORS` settings, then it will be disabled.
|
|
558
596
|
|
|
559
|
-
- `origin`
|
|
560
|
-
- `methods?`
|
|
561
|
-
- `allowedHeaders?`
|
|
562
|
-
- `exposedHeaders?`
|
|
563
|
-
- `credentials?`
|
|
564
|
-
- `maxAge?`
|
|
597
|
+
- `origin` `string | RegExp | Array<string | RegExp> | Function | Promise` available origins from which requests can be made
|
|
598
|
+
- `methods?` `Array<GET | POST | DELETE | PUT | PATCH>` available methods (default: `GET,OPTIONS,PUT,PATCH,POST,DELETE`)
|
|
599
|
+
- `allowedHeaders?` `Array<string>` allowed headers (default: `*`)
|
|
600
|
+
- `exposedHeaders?` `Array<string>` exposed headers (default: `*`)
|
|
601
|
+
- `credentials?` `boolean` param tells browsers whether to expose the response to the frontend JavaScript code (default: `true`)
|
|
602
|
+
- `maxAge?` `number` how long the results can be cached (default: `3600`)
|
|
565
603
|
|
|
566
604
|
#### Interceptors
|
|
567
605
|
|
|
568
606
|
Functions to change request or response parameters
|
|
569
607
|
|
|
570
|
-
- `request?` (params) => void
|
|
571
|
-
- `response?` (data, params) => any
|
|
608
|
+
- `request?` `(params) => void`
|
|
609
|
+
- `response?` `(data, params) => any`
|
|
572
610
|
|
|
573
611
|
> request interceptors (except interceptor for route) are called regardless of whether the server found a route match or not. So changes in request interceptors can affect whether the server finds the route or not
|
|
574
612
|
|
|
@@ -576,51 +614,214 @@ Functions to change request or response parameters
|
|
|
576
614
|
|
|
577
615
|
- `params`
|
|
578
616
|
- `request` request object
|
|
579
|
-
- `setDelay` (delay) => Promise<void
|
|
580
|
-
- `delay`
|
|
581
|
-
- `getHeader` (field) => string | number | string[] | undefined
|
|
582
|
-
- `field`
|
|
583
|
-
- `getHeaders` () => Record<string | number | string[] | undefined
|
|
584
|
-
- `getCookie` (name) => string | undefined
|
|
585
|
-
- `name`
|
|
617
|
+
- `setDelay` `(delay) => Promise<void>`
|
|
618
|
+
- `delay` `number` milliseconds of delay time
|
|
619
|
+
- `getHeader` `(field) => string | number | string[] | undefined`
|
|
620
|
+
- `field` `string` name of response header
|
|
621
|
+
- `getHeaders` `() => Record<string | number | string[] | undefined>`
|
|
622
|
+
- `getCookie` `(name) => string | undefined`
|
|
623
|
+
- `name` `string` name of cookie
|
|
624
|
+
- `log` `(logger) => Partial<LoggerTokens>` logger function [read](#logger)
|
|
625
|
+
- `logger` `Logger | undefined` logger options
|
|
586
626
|
|
|
587
627
|
##### Response
|
|
588
628
|
|
|
589
|
-
- `data`
|
|
629
|
+
- `data` `any` mock data of request
|
|
590
630
|
- `params`
|
|
591
631
|
- `request` request object
|
|
592
632
|
- `response` response object
|
|
593
|
-
- `setDelay` (delay) => Promise<void
|
|
594
|
-
- `delay`
|
|
595
|
-
- `setStatusCode` (statusCode) => void
|
|
596
|
-
- `statusCode`
|
|
597
|
-
- `setHeader` (field, value) => void
|
|
598
|
-
- `field`
|
|
599
|
-
- `value`
|
|
600
|
-
- `appendHeader` (field, value) => void
|
|
601
|
-
- `field`
|
|
602
|
-
- `value`
|
|
603
|
-
- `getHeader` (field) => string | number | string[] | undefined
|
|
604
|
-
- `field`
|
|
605
|
-
- `getHeaders` () => Record<string | number | string[] | undefined
|
|
606
|
-
- `setCookie` (name, value, options) => void
|
|
607
|
-
- `name`
|
|
608
|
-
- `value`
|
|
609
|
-
- `options`
|
|
610
|
-
- `getCookie` (name) => string | undefined
|
|
611
|
-
- `name`
|
|
612
|
-
- `clearCookie` (name, options) => void
|
|
613
|
-
- `name`
|
|
614
|
-
- `options`
|
|
615
|
-
- `attachment` (filename) => void
|
|
616
|
-
- `filename`
|
|
633
|
+
- `setDelay` `(delay) => Promise<void>`
|
|
634
|
+
- `delay` `number` milliseconds of delay time
|
|
635
|
+
- `setStatusCode` `(statusCode) => void`
|
|
636
|
+
- `statusCode` `number` status code for response
|
|
637
|
+
- `setHeader` `(field, value) => void`
|
|
638
|
+
- `field` `string` name of response header
|
|
639
|
+
- `value` `string | string[] | undefined` value of response header
|
|
640
|
+
- `appendHeader` `(field, value) => void`
|
|
641
|
+
- `field` `string` name of response header
|
|
642
|
+
- `value` `string | string[] | undefined` value of response header
|
|
643
|
+
- `getHeader` `(field) => string | number | string[] | undefined`
|
|
644
|
+
- `field` `string` name of response header
|
|
645
|
+
- `getHeaders` `() => Record<string | number | string[] | undefined>`
|
|
646
|
+
- `setCookie` `(name, value, options) => void`
|
|
647
|
+
- `name` `string` name of cookie
|
|
648
|
+
- `value` `string` value of cookie
|
|
649
|
+
- `options` `[CookieOptions](https://expressjs.com/en/resources/middleware/cookie-session.html) | undefined` cookie options (like path, expires, etc.)
|
|
650
|
+
- `getCookie` `(name) => string | undefined`
|
|
651
|
+
- `name` `string` name of cookie
|
|
652
|
+
- `clearCookie` `(name, options) => void`
|
|
653
|
+
- `name` `string` name of cookie
|
|
654
|
+
- `options` `[CookieOptions](https://expressjs.com/en/resources/middleware/cookie-session.html) | undefined` cookie options (like path, expires, etc.)
|
|
655
|
+
- `attachment` `(filename) => void`
|
|
656
|
+
- `filename` `string` name of file in 'Content-Disposition' header
|
|
657
|
+
- `log` `(logger) => Partial<LoggerTokens>` logger function [read](#logger)
|
|
658
|
+
- `logger` `Logger | undefined` logger options
|
|
659
|
+
|
|
660
|
+
#### Logger
|
|
661
|
+
|
|
662
|
+
You can log requests and responses using `log` function in any [interceptor](#interceptors).
|
|
663
|
+
|
|
664
|
+
`log` has the following signature `(logger?: Logger) => Partial<LoggerTokens>`.
|
|
665
|
+
|
|
666
|
+
`logger` parameter has the following optional properties
|
|
667
|
+
|
|
668
|
+
- `options?` `LoggerOptions` object map containing tokens to log. Keys is token names, values is boolean. `true` will add token into log, `false` will remove. If `options` property is not passed, following tokens will be logged
|
|
669
|
+
- Request
|
|
670
|
+
- type
|
|
671
|
+
- id
|
|
672
|
+
- timestamp
|
|
673
|
+
- method
|
|
674
|
+
- url
|
|
675
|
+
- Response
|
|
676
|
+
- type
|
|
677
|
+
- id
|
|
678
|
+
- timestamp
|
|
679
|
+
- method
|
|
680
|
+
- url
|
|
681
|
+
- statusCode
|
|
682
|
+
- data
|
|
683
|
+
- `rewrite?` `(tokens: Partial<LoggerTokens>) => void` function to replace default `console.dir(tokens, { depth: null })` appearance
|
|
684
|
+
|
|
685
|
+
`log` function returns object with logged token values
|
|
686
|
+
|
|
687
|
+
```javascript
|
|
688
|
+
/** @type {import('mock-config-server').MockServerConfig} */
|
|
689
|
+
const mockServerConfig = {
|
|
690
|
+
rest: {
|
|
691
|
+
baseUrl: '/api',
|
|
692
|
+
configs: [
|
|
693
|
+
{
|
|
694
|
+
path: '/posts',
|
|
695
|
+
method: 'get',
|
|
696
|
+
routes: [
|
|
697
|
+
{
|
|
698
|
+
interceptors: {
|
|
699
|
+
request: ({ log }) => {
|
|
700
|
+
log({ // logs following object in terminal
|
|
701
|
+
options: { // {
|
|
702
|
+
id: true, // id: 1,
|
|
703
|
+
type: true, // type: 'request',
|
|
704
|
+
timestamp: true, // timestamp: '31.12.2024, 23:59:59,999',
|
|
705
|
+
method: true, // method: 'GET',
|
|
706
|
+
url: true // url: 'http://localhost:31299/api/rest/posts/1'
|
|
707
|
+
} // }
|
|
708
|
+
});
|
|
709
|
+
},
|
|
710
|
+
response: (data, { log }) => {
|
|
711
|
+
log({ // logs following string in terminal
|
|
712
|
+
options: { // response get: http://localhost:31299/api/rest/posts/1 => 200
|
|
713
|
+
type: true,
|
|
714
|
+
statusCode: true,
|
|
715
|
+
method: true,
|
|
716
|
+
url: true
|
|
717
|
+
},
|
|
718
|
+
rewrite: ({ type, statusCode, method, url }) => {
|
|
719
|
+
console.info(`${type} ${method}: ${url} => ${statusCode}`);
|
|
720
|
+
}
|
|
721
|
+
});
|
|
722
|
+
return data;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
]
|
|
727
|
+
}
|
|
728
|
+
]
|
|
729
|
+
}
|
|
730
|
+
};
|
|
731
|
+
|
|
732
|
+
export default mockServerConfig;
|
|
733
|
+
```
|
|
734
|
+
|
|
735
|
+
> By default, `timestamp` and `method` tokens are prettified.
|
|
736
|
+
> Timestamp transforms from UNIX-timestamp number to `DD.MM.YYYY, HH:mm:ss,sss` string.
|
|
737
|
+
> 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.
|
|
739
|
+
|
|
740
|
+
##### Logger tokens
|
|
741
|
+
|
|
742
|
+
- `type?` `'request' | 'response'` type of log
|
|
743
|
+
- `id?` `number` unique id of request to reference request log with response log
|
|
744
|
+
- `timestamp?` `number` UNIX-timestamp in milliseconds
|
|
745
|
+
- `method?` `'get' | 'post' | 'delete' | 'put' | 'patch' | 'options'` HTTP method
|
|
746
|
+
- `url?` `string` requested URL
|
|
747
|
+
- `graphQLOperationType?` `'query' | 'mutation' | null` GraphQL operation type. `null` if request is not GraphQL
|
|
748
|
+
- `graphQLOperationName?` `string` GraphQL operation name. `null` if request is not GraphQL
|
|
749
|
+
- `graphQLQuery?` `string` GraphQL query. `null` if request is not GraphQL
|
|
750
|
+
- `variables?`: `Record<string, any>` GraphQL variables. `null` if request is not GraphQL or variables is not passed
|
|
751
|
+
- `headers?` `Record<string, any>` headers object
|
|
752
|
+
- `cookies?` `Record<string, any>` cookies object
|
|
753
|
+
- `query?` `Record<string, any>` query object
|
|
754
|
+
- `params?` `Record<string, any>` params object
|
|
755
|
+
- `body?` `any` body
|
|
756
|
+
|
|
757
|
+
Response logger has additional tokens
|
|
758
|
+
- `statusCode?` `number` response status code
|
|
759
|
+
- `data?` `any` data returned to client
|
|
760
|
+
|
|
761
|
+
If you need to log specific properties in mapped entities (headers, cookies, query, params), use `Record<string, boolean>` object instead of boolean.
|
|
762
|
+
In that case logger will use following logic:
|
|
763
|
+
|
|
764
|
+
- if some token is `true`, entity will be filtered by `whitelist` logic. *Only* enabled ones will be logged.
|
|
765
|
+
- if all tokens is `false`, entity will be filtered by `blacklist` logic. All entities will be logged *except* disabled ones.
|
|
766
|
+
|
|
767
|
+
> Whitelist logic have priority over blacklist if you pass `true` and `false` in same entity.
|
|
768
|
+
|
|
769
|
+
```javascript
|
|
770
|
+
/** @type {import('mock-config-server').MockServerConfig} */
|
|
771
|
+
const mockServerConfig = {
|
|
772
|
+
rest: {
|
|
773
|
+
baseUrl: '/api',
|
|
774
|
+
configs: [
|
|
775
|
+
{
|
|
776
|
+
path: '/posts',
|
|
777
|
+
method: 'get',
|
|
778
|
+
routes: [
|
|
779
|
+
{
|
|
780
|
+
interceptors: {
|
|
781
|
+
request: ({ log }) => {
|
|
782
|
+
log({ // whitelist. only query1 and query2 will be logged
|
|
783
|
+
options: {
|
|
784
|
+
query: {
|
|
785
|
+
query1: true,
|
|
786
|
+
query2: true
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
});
|
|
790
|
+
log({ // whitelist. only cookie1 and cookie2 will be logged
|
|
791
|
+
options: {
|
|
792
|
+
cookies: {
|
|
793
|
+
cookie1: true,
|
|
794
|
+
cookie2: true,
|
|
795
|
+
cookie3: false
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
});
|
|
799
|
+
log({ // blacklist. all headers will be logged except header1
|
|
800
|
+
options: {
|
|
801
|
+
headers: {
|
|
802
|
+
header1: false
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
},
|
|
808
|
+
data: {}
|
|
809
|
+
}
|
|
810
|
+
]
|
|
811
|
+
}
|
|
812
|
+
]
|
|
813
|
+
}
|
|
814
|
+
};
|
|
815
|
+
|
|
816
|
+
export default mockServerConfig;
|
|
817
|
+
```
|
|
617
818
|
|
|
618
819
|
## Database
|
|
619
820
|
|
|
620
821
|
With `mock-config-server` you can create your own mock database with all CRUD operations
|
|
621
822
|
|
|
622
|
-
- `data`
|
|
623
|
-
- `routes?`
|
|
823
|
+
- `data` `Object | string` initial data for database
|
|
824
|
+
- `routes?` `Object | string` map of custom routes for database
|
|
624
825
|
|
|
625
826
|
### Basic example
|
|
626
827
|
|
|
@@ -734,10 +935,10 @@ In the **Link** header you'll get **count**, **pages**, **next** and **prev** li
|
|
|
734
935
|
|
|
735
936
|
#### Link
|
|
736
937
|
|
|
737
|
-
- `count`
|
|
738
|
-
- `pages`
|
|
739
|
-
- `next`
|
|
740
|
-
- `prev`
|
|
938
|
+
- `count` `number` total count of elements
|
|
939
|
+
- `pages` `number` count of pages
|
|
940
|
+
- `next` `string | null` query string for next link
|
|
941
|
+
- `prev` `string | null` query string for prev link
|
|
741
942
|
|
|
742
943
|
### Sort
|
|
743
944
|
|
|
@@ -815,7 +1016,7 @@ Examples:
|
|
|
815
1016
|
mcs --help
|
|
816
1017
|
```
|
|
817
1018
|
|
|
818
|
-
|
|
1019
|
+
## Init Command
|
|
819
1020
|
|
|
820
1021
|
The init command is used to initialize a new project or set up the initial configuration for a tool. It helps users get started with a new project by providing a streamlined setup process.
|
|
821
1022
|
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './user';
|
|
2
|
-
export * from './users';
|
|
1
|
+
export * from './user';
|
|
2
|
+
export * from './users';
|
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import type { GraphQLRequestConfig } from 'mock-config-server';
|
|
2
|
-
|
|
3
|
-
export const getUserQuery: GraphQLRequestConfig = {
|
|
4
|
-
operationName: 'getUser',
|
|
5
|
-
operationType: 'query',
|
|
6
|
-
routes: [
|
|
7
|
-
{
|
|
8
|
-
data: { id: 1, emoji: '🎉' }
|
|
9
|
-
},
|
|
10
|
-
{
|
|
11
|
-
data: { id: 2, emoji: '🔥' },
|
|
12
|
-
entities: {
|
|
13
|
-
variables: {
|
|
14
|
-
id: 2
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
]
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export const createUserMutation: GraphQLRequestConfig = {
|
|
22
|
-
operationName: 'createUser',
|
|
23
|
-
operationType: 'mutation',
|
|
24
|
-
routes: [
|
|
25
|
-
{
|
|
26
|
-
data: { id: 1, emoji: '🎉' }
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
data: { id: 2, emoji: '🔥' },
|
|
30
|
-
entities: {
|
|
31
|
-
variables: {
|
|
32
|
-
emoji: '🔥'
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
]
|
|
37
|
-
};
|
|
1
|
+
import type { GraphQLRequestConfig } from 'mock-config-server';
|
|
2
|
+
|
|
3
|
+
export const getUserQuery: GraphQLRequestConfig = {
|
|
4
|
+
operationName: 'getUser',
|
|
5
|
+
operationType: 'query',
|
|
6
|
+
routes: [
|
|
7
|
+
{
|
|
8
|
+
data: { id: 1, emoji: '🎉' }
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
data: { id: 2, emoji: '🔥' },
|
|
12
|
+
entities: {
|
|
13
|
+
variables: {
|
|
14
|
+
id: 2
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
]
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const createUserMutation: GraphQLRequestConfig = {
|
|
22
|
+
operationName: 'createUser',
|
|
23
|
+
operationType: 'mutation',
|
|
24
|
+
routes: [
|
|
25
|
+
{
|
|
26
|
+
data: { id: 1, emoji: '🎉' }
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
data: { id: 2, emoji: '🔥' },
|
|
30
|
+
entities: {
|
|
31
|
+
variables: {
|
|
32
|
+
emoji: '🔥'
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
};
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import type { GraphQLRequestConfig } from 'mock-config-server';
|
|
2
|
-
|
|
3
|
-
export const getUsersQuery: GraphQLRequestConfig = {
|
|
4
|
-
operationName: 'getUsers',
|
|
5
|
-
operationType: 'query',
|
|
6
|
-
routes: [
|
|
7
|
-
{
|
|
8
|
-
data: [
|
|
9
|
-
{ id: 1, emoji: '🎉' },
|
|
10
|
-
{ id: 2, emoji: '🔥' }
|
|
11
|
-
]
|
|
12
|
-
}
|
|
13
|
-
]
|
|
14
|
-
};
|
|
1
|
+
import type { GraphQLRequestConfig } from 'mock-config-server';
|
|
2
|
+
|
|
3
|
+
export const getUsersQuery: GraphQLRequestConfig = {
|
|
4
|
+
operationName: 'getUsers',
|
|
5
|
+
operationType: 'query',
|
|
6
|
+
routes: [
|
|
7
|
+
{
|
|
8
|
+
data: [
|
|
9
|
+
{ id: 1, emoji: '🎉' },
|
|
10
|
+
{ id: 2, emoji: '🔥' }
|
|
11
|
+
]
|
|
12
|
+
}
|
|
13
|
+
]
|
|
14
|
+
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './user';
|
|
2
|
-
export * from './users';
|
|
1
|
+
export * from './user';
|
|
2
|
+
export * from './users';
|
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import type { RestRequestConfig } from 'mock-config-server';
|
|
2
|
-
|
|
3
|
-
export const getUserRequest: RestRequestConfig = {
|
|
4
|
-
method: 'get',
|
|
5
|
-
path: '/users/:id',
|
|
6
|
-
routes: [
|
|
7
|
-
{
|
|
8
|
-
data: { id: 1, emoji: '🎉' }
|
|
9
|
-
},
|
|
10
|
-
{
|
|
11
|
-
data: { id: 2, emoji: '🔥' },
|
|
12
|
-
entities: {
|
|
13
|
-
params: {
|
|
14
|
-
id: 2
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
]
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export const postUserRequest: RestRequestConfig = {
|
|
22
|
-
method: 'post',
|
|
23
|
-
path: '/users',
|
|
24
|
-
routes: [
|
|
25
|
-
{
|
|
26
|
-
data: { id: 1, emoji: '🎉' }
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
data: { id: 2, emoji: '🔥' },
|
|
30
|
-
entities: {
|
|
31
|
-
body: {
|
|
32
|
-
emoji: '🔥'
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
]
|
|
37
|
-
};
|
|
1
|
+
import type { RestRequestConfig } from 'mock-config-server';
|
|
2
|
+
|
|
3
|
+
export const getUserRequest: RestRequestConfig = {
|
|
4
|
+
method: 'get',
|
|
5
|
+
path: '/users/:id',
|
|
6
|
+
routes: [
|
|
7
|
+
{
|
|
8
|
+
data: { id: 1, emoji: '🎉' }
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
data: { id: 2, emoji: '🔥' },
|
|
12
|
+
entities: {
|
|
13
|
+
params: {
|
|
14
|
+
id: 2
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
]
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const postUserRequest: RestRequestConfig = {
|
|
22
|
+
method: 'post',
|
|
23
|
+
path: '/users',
|
|
24
|
+
routes: [
|
|
25
|
+
{
|
|
26
|
+
data: { id: 1, emoji: '🎉' }
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
data: { id: 2, emoji: '🔥' },
|
|
30
|
+
entities: {
|
|
31
|
+
body: {
|
|
32
|
+
emoji: '🔥'
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
};
|