mock-config-server 3.6.1 → 3.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +291 -90
  2. package/dist/bin/validateMockServerConfig/graphqlConfigSchema/graphqlConfigSchema.d.ts +6 -6
  3. package/dist/bin/validateMockServerConfig/restConfigSchema/restConfigSchema.d.ts +14 -14
  4. package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.d.ts +1 -1
  5. package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.js +1 -3
  6. package/dist/src/core/middlewares/contextMiddleware/contextMiddleware.d.ts +17 -0
  7. package/dist/src/core/middlewares/contextMiddleware/contextMiddleware.js +35 -0
  8. package/dist/src/core/middlewares/cookieParseMiddleware/helpers/parseCookie/parseCookie.d.ts +1 -2
  9. package/dist/src/core/middlewares/index.d.ts +1 -0
  10. package/dist/src/core/middlewares/index.js +1 -0
  11. package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.js +4 -3
  12. package/dist/src/core/rest/createRestRoutes/createRestRoutes.d.ts +1 -1
  13. package/dist/src/core/rest/createRestRoutes/createRestRoutes.js +1 -3
  14. package/dist/src/server/createDatabaseMockServer/createDatabaseMockServer.js +1 -0
  15. package/dist/src/server/createGraphQLMockServer/createGraphQLMockServer.js +1 -0
  16. package/dist/src/server/createMockServer/createMockServer.js +1 -0
  17. package/dist/src/server/createRestMockServer/createRestMockServer.js +1 -0
  18. package/dist/src/server/startDatabaseMockServer/startDatabaseMockServer.js +1 -1
  19. package/dist/src/server/startGraphQLMockServer/startGraphQLMockServer.js +1 -1
  20. package/dist/src/server/startMockServer/startMockServer.js +1 -1
  21. package/dist/src/server/startRestMockServer/startRestMockServer.js +1 -1
  22. package/dist/src/static/views/pages/404/index.ejs +66 -69
  23. package/dist/src/utils/helpers/date/formatTimestamp/formatTimestamp.d.ts +1 -0
  24. package/dist/src/utils/helpers/date/formatTimestamp/formatTimestamp.js +18 -0
  25. package/dist/src/utils/helpers/date/index.d.ts +1 -0
  26. package/dist/src/utils/helpers/date/index.js +18 -0
  27. package/dist/src/utils/helpers/index.d.ts +2 -0
  28. package/dist/src/utils/helpers/index.js +2 -0
  29. package/dist/src/utils/helpers/interceptors/callRequestInterceptor/callRequestInterceptor.js +7 -1
  30. package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.js +9 -1
  31. package/dist/src/utils/helpers/logger/callRequestLogger/callRequestLogger.d.ts +8 -0
  32. package/dist/src/utils/helpers/logger/callRequestLogger/callRequestLogger.js +50 -0
  33. package/dist/src/utils/helpers/logger/callResponseLogger/callResponseLogger.d.ts +10 -0
  34. package/dist/src/utils/helpers/logger/callResponseLogger/callResponseLogger.js +54 -0
  35. package/dist/src/utils/helpers/logger/helpers/filterTokens/filterTokens.d.ts +5 -0
  36. package/dist/src/utils/helpers/logger/helpers/filterTokens/filterTokens.js +44 -0
  37. package/dist/src/utils/helpers/logger/helpers/formatTokens/formatTokens.d.ts +2 -0
  38. package/dist/src/utils/helpers/logger/helpers/formatTokens/formatTokens.js +23 -0
  39. package/dist/src/utils/helpers/logger/helpers/index.d.ts +2 -0
  40. package/dist/src/utils/helpers/logger/helpers/index.js +19 -0
  41. package/dist/src/utils/helpers/logger/index.d.ts +2 -0
  42. package/dist/src/utils/helpers/logger/index.js +19 -0
  43. package/dist/src/utils/types/checkModes.d.ts +0 -1
  44. package/dist/src/utils/types/graphql.d.ts +2 -5
  45. package/dist/src/utils/types/index.d.ts +2 -0
  46. package/dist/src/utils/types/index.js +2 -0
  47. package/dist/src/utils/types/interceptors.d.ts +11 -7
  48. package/dist/src/utils/types/logger.d.ts +43 -0
  49. package/dist/src/utils/types/logger.js +4 -0
  50. package/dist/src/utils/types/rest.d.ts +2 -2
  51. package/dist/src/utils/types/server.d.ts +2 -2
  52. package/dist/src/utils/types/shared.d.ts +1 -0
  53. package/dist/src/utils/types/shared.js +4 -0
  54. package/dist/src/utils/types/values.d.ts +7 -0
  55. 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?` {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)
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?` {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)
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` {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: `'/'`)
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
- ##### Rest request config
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` {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)
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` {string} path prefix for request
552
- - `path` {string} path to your static files
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` {string | RegExp | Array<string | RegExp> | Function | Promise } available origins from which requests can be made
560
- - `methods?` {Array<GET | POST | DELETE | PUT | PATCH>} available methods (default: `GET,OPTIONS,PUT,PATCH,POST,DELETE`)
561
- - `allowedHeaders?` {Array<string>} allowed headers (default: `*`)
562
- - `exposedHeaders?` {Array<string>} exposed headers (default: `*`)
563
- - `credentials?` {boolean} param tells browsers whether to expose the response to the frontend JavaScript code (default: `true`)
564
- - `maxAge?` {number} how long the results can be cached (default: `3600`)
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` {number} milliseconds of delay time
581
- - `getHeader` (field) => string | number | string[] | undefined
582
- - `field` {string} name of response header
583
- - `getHeaders` () => Record<string | number | string[] | undefined>
584
- - `getCookie` (name) => string | undefined
585
- - `name` {string} name of cookie
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` {any} mock data of request
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` {number} milliseconds of delay time
595
- - `setStatusCode` (statusCode) => void
596
- - `statusCode` {number} status code for response
597
- - `setHeader` (field, value) => void
598
- - `field` {string} name of response header
599
- - `value` {string | string[] | undefined} value of response header
600
- - `appendHeader` (field, value) => void
601
- - `field` {string} name of response header
602
- - `value` {string | string[] | undefined} value of response header
603
- - `getHeader` (field) => string | number | string[] | undefined
604
- - `field` {string} name of response header
605
- - `getHeaders` () => Record<string | number | string[] | undefined>
606
- - `setCookie` (name, value, options) => void
607
- - `name` {string} name of cookie
608
- - `value` {string} value of cookie
609
- - `options` {[CookieOptions](https://expressjs.com/en/resources/middleware/cookie-session.html) | undefined} cookie options (like path, expires, etc.)
610
- - `getCookie` (name) => string | undefined
611
- - `name` {string} name of cookie
612
- - `clearCookie` (name, options) => void
613
- - `name` {string} name of cookie
614
- - `options` {[CookieOptions](https://expressjs.com/en/resources/middleware/cookie-session.html) | undefined} cookie options (like path, expires, etc.)
615
- - `attachment` (filename) => void
616
- - `filename` {string} name of file in 'Content-Disposition' header
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` {Object | string} initial data for database
623
- - `routes?` {Object | string} map of custom routes for database
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` {number} total count of elements
738
- - `pages` {number} count of pages
739
- - `next` {string | null} query string for next link
740
- - `prev` {string | null} query string for prev link
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
- # Init Command
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
 
@@ -90,14 +90,14 @@ export declare const graphqlConfigSchema: z.ZodObject<{
90
90
  interceptors?: any;
91
91
  entities?: any;
92
92
  })[];
93
- operationName: string | RegExp;
94
93
  operationType: "query" | "mutation";
94
+ operationName: string | RegExp;
95
95
  query?: string | undefined;
96
96
  interceptors?: any;
97
97
  }, {
98
98
  routes: unknown[];
99
- operationName: string | RegExp;
100
99
  operationType: "query" | "mutation";
100
+ operationName: string | RegExp;
101
101
  query?: string | undefined;
102
102
  interceptors?: unknown;
103
103
  }>>, z.ZodPipeline<z.ZodType<unknown, z.ZodTypeDef, unknown>, z.ZodObject<z.objectUtil.extendShape<{
@@ -191,14 +191,14 @@ export declare const graphqlConfigSchema: z.ZodObject<{
191
191
  entities?: any;
192
192
  })[];
193
193
  operationType: "query" | "mutation";
194
- interceptors?: any;
195
194
  operationName?: string | RegExp | undefined;
195
+ interceptors?: any;
196
196
  }, {
197
197
  query: string;
198
198
  routes: unknown[];
199
199
  operationType: "query" | "mutation";
200
- interceptors?: unknown;
201
200
  operationName?: string | RegExp | undefined;
201
+ interceptors?: unknown;
202
202
  }>>]>, "many">;
203
203
  interceptors: z.ZodOptional<z.ZodPipeline<z.ZodType<unknown, z.ZodTypeDef, unknown>, z.ZodTypeAny>>;
204
204
  }, "strict", z.ZodTypeAny, {
@@ -221,8 +221,8 @@ export declare const graphqlConfigSchema: z.ZodObject<{
221
221
  interceptors?: any;
222
222
  entities?: any;
223
223
  })[];
224
- operationName: string | RegExp;
225
224
  operationType: "query" | "mutation";
225
+ operationName: string | RegExp;
226
226
  query?: string | undefined;
227
227
  interceptors?: any;
228
228
  } | {
@@ -246,8 +246,8 @@ export declare const graphqlConfigSchema: z.ZodObject<{
246
246
  entities?: any;
247
247
  })[];
248
248
  operationType: "query" | "mutation";
249
- interceptors?: any;
250
249
  operationName?: string | RegExp | undefined;
250
+ interceptors?: any;
251
251
  })[];
252
252
  interceptors?: any;
253
253
  baseUrl?: string | undefined;
@@ -110,13 +110,13 @@ export declare const restConfigSchema: z.ZodObject<{
110
110
  interceptors?: any;
111
111
  entities?: any;
112
112
  })[];
113
- path: string | RegExp;
114
113
  method: RestMethod;
114
+ path: string | RegExp;
115
115
  interceptors?: any;
116
116
  }, {
117
117
  routes: unknown[];
118
- path: string | RegExp;
119
118
  method: RestMethod;
119
+ path: string | RegExp;
120
120
  interceptors?: unknown;
121
121
  }>, z.ZodObject<{
122
122
  path: z.ZodUnion<[z.ZodString, z.ZodType<RegExp, z.ZodTypeDef, RegExp>]>;
@@ -226,13 +226,13 @@ export declare const restConfigSchema: z.ZodObject<{
226
226
  interceptors?: any;
227
227
  entities?: any;
228
228
  })[];
229
- path: string | RegExp;
230
229
  method: RestMethod;
230
+ path: string | RegExp;
231
231
  interceptors?: any;
232
232
  }, {
233
233
  routes: unknown[];
234
- path: string | RegExp;
235
234
  method: RestMethod;
235
+ path: string | RegExp;
236
236
  interceptors?: unknown;
237
237
  }>, z.ZodObject<{
238
238
  path: z.ZodUnion<[z.ZodString, z.ZodType<RegExp, z.ZodTypeDef, RegExp>]>;
@@ -342,13 +342,13 @@ export declare const restConfigSchema: z.ZodObject<{
342
342
  interceptors?: any;
343
343
  entities?: any;
344
344
  })[];
345
- path: string | RegExp;
346
345
  method: RestMethod;
346
+ path: string | RegExp;
347
347
  interceptors?: any;
348
348
  }, {
349
349
  routes: unknown[];
350
- path: string | RegExp;
351
350
  method: RestMethod;
351
+ path: string | RegExp;
352
352
  interceptors?: unknown;
353
353
  }>, z.ZodObject<{
354
354
  path: z.ZodUnion<[z.ZodString, z.ZodType<RegExp, z.ZodTypeDef, RegExp>]>;
@@ -458,13 +458,13 @@ export declare const restConfigSchema: z.ZodObject<{
458
458
  interceptors?: any;
459
459
  entities?: any;
460
460
  })[];
461
- path: string | RegExp;
462
461
  method: RestMethod;
462
+ path: string | RegExp;
463
463
  interceptors?: any;
464
464
  }, {
465
465
  routes: unknown[];
466
- path: string | RegExp;
467
466
  method: RestMethod;
467
+ path: string | RegExp;
468
468
  interceptors?: unknown;
469
469
  }>, z.ZodObject<{
470
470
  path: z.ZodUnion<[z.ZodString, z.ZodType<RegExp, z.ZodTypeDef, RegExp>]>;
@@ -574,13 +574,13 @@ export declare const restConfigSchema: z.ZodObject<{
574
574
  interceptors?: any;
575
575
  entities?: any;
576
576
  })[];
577
- path: string | RegExp;
578
577
  method: RestMethod;
578
+ path: string | RegExp;
579
579
  interceptors?: any;
580
580
  }, {
581
581
  routes: unknown[];
582
- path: string | RegExp;
583
582
  method: RestMethod;
583
+ path: string | RegExp;
584
584
  interceptors?: unknown;
585
585
  }>, z.ZodObject<{
586
586
  path: z.ZodUnion<[z.ZodString, z.ZodType<RegExp, z.ZodTypeDef, RegExp>]>;
@@ -690,13 +690,13 @@ export declare const restConfigSchema: z.ZodObject<{
690
690
  interceptors?: any;
691
691
  entities?: any;
692
692
  })[];
693
- path: string | RegExp;
694
693
  method: RestMethod;
694
+ path: string | RegExp;
695
695
  interceptors?: any;
696
696
  }, {
697
697
  routes: unknown[];
698
- path: string | RegExp;
699
698
  method: RestMethod;
699
+ path: string | RegExp;
700
700
  interceptors?: unknown;
701
701
  }>]>, "many">;
702
702
  interceptors: z.ZodOptional<z.ZodPipeline<z.ZodType<unknown, z.ZodTypeDef, unknown>, z.ZodTypeAny>>;
@@ -725,8 +725,8 @@ export declare const restConfigSchema: z.ZodObject<{
725
725
  interceptors?: any;
726
726
  entities?: any;
727
727
  })[];
728
- path: string | RegExp;
729
728
  method: RestMethod;
729
+ path: string | RegExp;
730
730
  interceptors?: any;
731
731
  }[];
732
732
  interceptors?: any;
@@ -734,8 +734,8 @@ export declare const restConfigSchema: z.ZodObject<{
734
734
  }, {
735
735
  configs: {
736
736
  routes: unknown[];
737
- path: string | RegExp;
738
737
  method: RestMethod;
738
+ path: string | RegExp;
739
739
  interceptors?: unknown;
740
740
  }[];
741
741
  interceptors?: unknown;
@@ -3,7 +3,7 @@ import type { GraphqlConfig, Interceptors } from '../../../utils/types';
3
3
  interface CreateGraphQLRoutesParams {
4
4
  router: IRouter;
5
5
  graphqlConfig: GraphqlConfig;
6
- serverResponseInterceptor?: Interceptors['response'];
6
+ serverResponseInterceptor?: Interceptors<'graphql'>['response'];
7
7
  }
8
8
  export declare const createGraphQLRoutes: ({ router, graphqlConfig, serverResponseInterceptor }: CreateGraphQLRoutesParams) => IRouter;
9
9
  export {};
@@ -70,9 +70,7 @@ const createGraphQLRoutes = ({ router, graphqlConfig, serverResponseInterceptor
70
70
  });
71
71
  });
72
72
  });
73
- if (!matchedRouteConfig) {
74
- return next();
75
- }
73
+ if (!matchedRouteConfig) return next();
76
74
  if ((_matchedRouteConfig_interceptors = matchedRouteConfig.interceptors) === null || _matchedRouteConfig_interceptors === void 0 ? void 0 : _matchedRouteConfig_interceptors.request) {
77
75
  await (0, _helpers.callRequestInterceptor)({
78
76
  request,
@@ -0,0 +1,17 @@
1
+ import type { Express } from 'express';
2
+ import type { GraphQLEntity, GraphQLOperationName, GraphQLOperationType } from '../../../utils/types';
3
+ declare global {
4
+ namespace Express {
5
+ interface Request {
6
+ id: number;
7
+ timestamp: number;
8
+ graphQL: {
9
+ operationType: GraphQLOperationType;
10
+ operationName?: GraphQLOperationName;
11
+ query: string;
12
+ variables?: GraphQLEntity<'variables'>;
13
+ } | null;
14
+ }
15
+ }
16
+ }
17
+ export declare const contextMiddleware: (server: Express) => void;