mock-config-server 1.0.4 → 2.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.
Files changed (112) hide show
  1. package/README.md +277 -213
  2. package/dist/bin/mock-config-server.d.ts +2 -2
  3. package/dist/bin/mock-config-server.js +67 -65
  4. package/dist/bin/resolveExportsFromSourceCode.d.ts +1 -1
  5. package/dist/bin/resolveExportsFromSourceCode.js +11 -11
  6. package/dist/bin/validateMockServerConfig/validateBaseUrl/validateBaseUrl.d.ts +1 -0
  7. package/dist/bin/validateMockServerConfig/validateBaseUrl/validateBaseUrl.js +12 -0
  8. package/dist/bin/validateMockServerConfig/validateCors/validateCors.d.ts +1 -0
  9. package/dist/bin/validateMockServerConfig/validateCors/validateCors.js +82 -0
  10. package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateGraphqlConfig.d.ts +1 -0
  11. package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateGraphqlConfig.js +48 -0
  12. package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateRoutes/validateRoutes.d.ts +2 -0
  13. package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateRoutes/validateRoutes.js +71 -0
  14. package/dist/bin/validateMockServerConfig/validateInterceptors/validateInterceptors.d.ts +1 -0
  15. package/dist/bin/validateMockServerConfig/validateInterceptors/validateInterceptors.js +21 -0
  16. package/dist/bin/validateMockServerConfig/validateMockServerConfig.d.ts +1 -0
  17. package/dist/bin/validateMockServerConfig/validateMockServerConfig.js +34 -0
  18. package/dist/bin/validateMockServerConfig/validatePort/validatePort.d.ts +1 -0
  19. package/dist/bin/validateMockServerConfig/validatePort/validatePort.js +9 -0
  20. package/dist/bin/validateMockServerConfig/validateRestConfig/validateRestConfig.d.ts +1 -0
  21. package/dist/bin/validateMockServerConfig/validateRestConfig/validateRestConfig.js +52 -0
  22. package/dist/bin/validateMockServerConfig/validateRestConfig/validateRoutes/validateRoutes.d.ts +2 -0
  23. package/dist/bin/validateMockServerConfig/validateRestConfig/validateRoutes/validateRoutes.js +104 -0
  24. package/dist/bin/validateMockServerConfig/validateStaticPath/validateStaticPath.d.ts +1 -0
  25. package/dist/bin/validateMockServerConfig/validateStaticPath/validateStaticPath.js +44 -0
  26. package/dist/index.d.ts +1 -1
  27. package/dist/index.js +17 -17
  28. package/dist/src/configs/isEntitiesEqual/isEntityValuesEqual.d.ts +1 -2
  29. package/dist/src/configs/isEntitiesEqual/isEntityValuesEqual.js +29 -29
  30. package/dist/src/cors/corsMiddleware/corsMiddleware.d.ts +3 -3
  31. package/dist/src/cors/corsMiddleware/corsMiddleware.js +44 -32
  32. package/dist/src/cors/getOrigins/getAllowedOrigins.d.ts +2 -2
  33. package/dist/src/cors/getOrigins/getAllowedOrigins.js +13 -16
  34. package/dist/src/cors/noCorsMiddleware/noCorsMiddleware.d.ts +2 -2
  35. package/dist/src/cors/noCorsMiddleware/noCorsMiddleware.js +20 -15
  36. package/dist/src/graphql/createGraphQLRoutes/createGraphQLRoutes.d.ts +3 -0
  37. package/dist/src/graphql/createGraphQLRoutes/createGraphQLRoutes.js +73 -0
  38. package/dist/src/graphql/getGraphQLInput/getGraphQLInput.d.ts +3 -0
  39. package/dist/src/graphql/getGraphQLInput/getGraphQLInput.js +21 -0
  40. package/dist/src/graphql/parseGraphQLRequest/parseGraphQLRequest.d.ts +3 -0
  41. package/dist/src/graphql/parseGraphQLRequest/parseGraphQLRequest.js +12 -0
  42. package/dist/src/graphql/parseQuery/parseQuery.d.ts +7 -0
  43. package/dist/src/graphql/parseQuery/parseQuery.js +21 -0
  44. package/dist/src/graphql/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.d.ts +2 -0
  45. package/dist/src/graphql/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.js +28 -0
  46. package/dist/src/index.d.ts +3 -3
  47. package/dist/src/index.js +19 -19
  48. package/dist/src/notFound/notFoundMiddleware.d.ts +8 -0
  49. package/dist/src/notFound/notFoundMiddleware.js +47 -0
  50. package/dist/src/notFound/urlSuggestions/getGraphqlUrlSuggestions/getGraphqlUrlSuggestions.d.ts +6 -0
  51. package/dist/src/notFound/urlSuggestions/getGraphqlUrlSuggestions/getGraphqlUrlSuggestions.js +18 -0
  52. package/dist/src/notFound/urlSuggestions/getGraphqlUrlSuggestions/index.d.ts +1 -0
  53. package/dist/src/notFound/urlSuggestions/getGraphqlUrlSuggestions/index.js +17 -0
  54. package/dist/src/notFound/urlSuggestions/getLevenshteinDistance/getLevenshteinDistance.d.ts +1 -0
  55. package/dist/src/notFound/urlSuggestions/getLevenshteinDistance/getLevenshteinDistance.js +80 -0
  56. package/dist/src/notFound/urlSuggestions/getRestUrlSuggestions/getRestUrlSuggestions.d.ts +6 -0
  57. package/dist/src/notFound/urlSuggestions/getRestUrlSuggestions/getRestUrlSuggestions.js +34 -0
  58. package/dist/src/notFound/urlSuggestions/getRestUrlSuggestions/helpers/getActualRestUrlMeaningfulString/getActualRestUrlMeaningfulString.d.ts +1 -0
  59. package/dist/src/notFound/urlSuggestions/getRestUrlSuggestions/helpers/getActualRestUrlMeaningfulString/getActualRestUrlMeaningfulString.js +7 -0
  60. package/dist/src/notFound/urlSuggestions/getRestUrlSuggestions/helpers/getPatternRestUrlMeaningfulString/getPatternRestUrlMeaningfulString.d.ts +1 -0
  61. package/dist/src/notFound/urlSuggestions/getRestUrlSuggestions/helpers/getPatternRestUrlMeaningfulString/getPatternRestUrlMeaningfulString.js +5 -0
  62. package/dist/src/notFound/urlSuggestions/getRestUrlSuggestions/helpers/index.d.ts +2 -0
  63. package/dist/src/notFound/urlSuggestions/getRestUrlSuggestions/helpers/index.js +18 -0
  64. package/dist/src/notFound/urlSuggestions/getRestUrlSuggestions/index.d.ts +1 -0
  65. package/dist/src/notFound/urlSuggestions/getRestUrlSuggestions/index.js +17 -0
  66. package/dist/src/notFound/urlSuggestions/index.d.ts +2 -0
  67. package/dist/src/notFound/urlSuggestions/index.js +18 -0
  68. package/dist/src/rest/createRestRoutes/createRestRoutes.d.ts +3 -0
  69. package/dist/src/{routes/createRoutes/createRoutes.js → rest/createRestRoutes/createRestRoutes.js} +41 -43
  70. package/dist/src/rest/prepareRestRequestConfigs/prepareRestRequestConfigs.d.ts +2 -0
  71. package/dist/src/{configs/prepareRequestConfigs/prepareRequestConfigs.js → rest/prepareRestRequestConfigs/prepareRestRequestConfigs.js} +30 -30
  72. package/dist/src/routes/callRequestInterceptors/callRequestInterceptors.d.ts +12 -12
  73. package/dist/src/routes/callRequestInterceptors/callRequestInterceptors.js +13 -13
  74. package/dist/src/routes/callResponseInterceptors/callResponseInterceptors.d.ts +14 -14
  75. package/dist/src/routes/callResponseInterceptors/callResponseInterceptors.js +31 -31
  76. package/dist/src/server/createMockServer/createMockServer.d.ts +3 -3
  77. package/dist/src/server/createMockServer/createMockServer.js +48 -35
  78. package/dist/src/server/startMockServer/startMockServer.d.ts +2 -2
  79. package/dist/src/server/startMockServer/startMockServer.js +17 -13
  80. package/dist/src/static/staticMiddleware/staticMiddleware.d.ts +3 -3
  81. package/dist/src/static/staticMiddleware/staticMiddleware.js +30 -29
  82. package/dist/src/utils/constants/appPath.d.ts +1 -1
  83. package/dist/src/utils/constants/appPath.js +4 -4
  84. package/dist/src/utils/constants/default.d.ts +11 -10
  85. package/dist/src/utils/constants/default.js +14 -13
  86. package/dist/src/utils/constants/index.d.ts +2 -2
  87. package/dist/src/utils/constants/index.js +18 -18
  88. package/dist/src/utils/helpers/index.d.ts +3 -2
  89. package/dist/src/utils/helpers/index.js +19 -18
  90. package/dist/src/utils/helpers/isPlainObject.d.ts +1 -1
  91. package/dist/src/utils/helpers/isPlainObject.js +5 -5
  92. package/dist/src/utils/helpers/sleep.d.ts +1 -1
  93. package/dist/src/utils/helpers/sleep.js +7 -7
  94. package/dist/src/utils/helpers/url/getUrlParts/getUrlParts.d.ts +1 -0
  95. package/dist/src/utils/helpers/url/getUrlParts/getUrlParts.js +6 -0
  96. package/dist/src/utils/helpers/url/index.d.ts +2 -0
  97. package/dist/src/utils/helpers/url/index.js +18 -0
  98. package/dist/src/utils/helpers/url/removeLeadingAndTrailingSlashes/removeLeadingAndTrailingSlashes.d.ts +1 -0
  99. package/dist/src/utils/helpers/url/removeLeadingAndTrailingSlashes/removeLeadingAndTrailingSlashes.js +5 -0
  100. package/dist/src/utils/types/configs.d.ts +63 -34
  101. package/dist/src/utils/types/configs.js +2 -2
  102. package/dist/src/utils/types/graphql.d.ts +5 -0
  103. package/dist/src/utils/types/graphql.js +2 -0
  104. package/dist/src/utils/types/index.d.ts +4 -3
  105. package/dist/src/utils/types/index.js +20 -19
  106. package/dist/src/utils/types/interceptors.d.ts +15 -15
  107. package/dist/src/utils/types/interceptors.js +2 -2
  108. package/dist/src/utils/types/server.d.ts +32 -24
  109. package/dist/src/utils/types/server.js +2 -2
  110. package/package.json +102 -99
  111. package/dist/src/configs/prepareRequestConfigs/prepareRequestConfigs.d.ts +0 -2
  112. package/dist/src/routes/createRoutes/createRoutes.d.ts +0 -3
package/README.md CHANGED
@@ -1,213 +1,277 @@
1
- # 🎉 Mock Config Server
2
-
3
- tool that easily and quickly imitates server operation, create full fake api in few steps
4
-
5
- ## Install
6
-
7
- Install with [npm](https://www.npmjs.com/) or [yarn](https://yarnpkg.com/)
8
-
9
- ```bash
10
- $ npm i mock-config-server --save --dev
11
- # or
12
- $ yarn add mock-config-server --dev
13
- ```
14
-
15
- ## 🦉 Philosophy
16
-
17
- **🎉 Mock Config Server** it is a tool that, easily, quickly simulates the work of a server. The main difference from solutions such as [json-server](https://www.npmjs.com/package/json-server) and [mock-service-worker](https://mswjs.io/) is the ease of filling in data and flexible emulation of any and usual cases. Our goal is to create a simple and flexible system for users, with the help of which they can create, test, and support their products.
18
-
19
- ## Features
20
-
21
- - **TypeScript support out of the box** - full typed package
22
- - **Full Rest Api support** - using simple configs of a certain format, you can easily simulate the operation of servers
23
- - **CORS setup** - turn on and off CORS, fully customizable when CORS is turned on
24
- - **Support for any kind of static** - server can return any type of static file if needed. Images, HTML, CSS, JSON, etc
25
-
26
- ## Usage
27
-
28
- Install **🎉 Mock Config Server** with [npm](https://www.npmjs.com/) or [yarn](https://yarnpkg.com/)
29
-
30
- ```bash
31
- $ npm i mock-config-server --save --dev
32
- # or
33
- $ yarn add mock-config-server --dev
34
- ```
35
-
36
- Create a `mock-server.config.js` file with server configuration
37
-
38
- ```javascript
39
- /** @type {import('mock-config-server').Mock.ServerConfig} */
40
- const mockServerConfig = {
41
- configs: [
42
- {
43
- path: '/user',
44
- method: 'get',
45
- routes: [{ data: { emoji: '🦁', name: 'Nursultan' } }]
46
- }
47
- ]
48
- };
49
-
50
- export default mockServerConfig;
51
- ```
52
-
53
- Start **🎉 Mock Config Server**
54
-
55
- ```bash
56
- $ npx mock-config-server
57
-
58
- # 🎉 Mock Config Server is running at http://localhost:31299
59
- ```
60
-
61
- ## 🎭 Parameters for mock-server.config.(js|ts)
62
-
63
- - `configs` {Array<RequestConfig>} configs for mock requests, [read](#configs)
64
- - `staticPath?` {StaticPath} entity for working with static files, [read](#static-path)
65
- - `interceptors?` {Interceptors} functions to change request or response parameters, [read](#interceptors)
66
- - `cors?` {Cors} CORS settings object (default: `CORS is turn off`), [read](#cors)
67
- - `port?` {number} server port (default: `31299`)
68
- - `baseUrl?` {string} part of the url that will be substituted at the beginning of the request url (default: `'/'`)
69
-
70
- ### Configs
71
-
72
- 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` | `query` | `params` | `body` 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
73
-
74
- ##### request config
75
-
76
- - `path` {string | RegExp} request path
77
- - `method` {GET | POST | DELETE | PUT | PATCH} rest api method
78
- - `routes` {RouteConfig} request routes
79
- - `interceptors?` {Interceptors} functions to change request or response parameters, [read](#interceptors)
80
-
81
- ##### route config
82
-
83
- - `data` {any} mock data of request
84
- - `entities?` Object<headers | query | params | body> object that helps in data retrieval
85
- - `interceptors?` {Interceptors} functions to change request or response parameters, [read](#interceptors)
86
-
87
- ##### entities
88
-
89
- ```typescript
90
- interface Entities {
91
- headers?: { [string]: string | number };
92
- query?: { [string]: string | number };
93
- params?: { [string]: string | number };
94
- body?: any;
95
- }
96
- ```
97
-
98
- ##### Example
99
-
100
- ```javascript
101
- /** @type {import('mock-config-server').MockServerConfig} */
102
- const mockServerConfig = {
103
- configs: [
104
- {
105
- path: '/user',
106
- method: 'get',
107
- routes: [
108
- {
109
- entities: {
110
- headers: { 'name-header': 'Nursultan' }
111
- },
112
- data: { emoji: '🦁', name: 'Nursultan' }
113
- },
114
- {
115
- entities: {
116
- headers: { 'name-header': 'Dmitriy' }
117
- },
118
- data: { emoji: '☄', name: 'Dmitriy' }
119
- }
120
- ]
121
- }
122
- ]
123
- };
124
-
125
- module.exports = mockServerConfig;
126
- ```
127
-
128
- Now you can make a request with an additional header and get the desired result
129
-
130
- ```javascript
131
- fetch('http://localhost:31299/user', {
132
- headers: { 'name-header': 'Nursultan' }
133
- })
134
- .then((response) => response.json())
135
- .then((data) => console.log(data)); // { emoji: '🦁', name: 'Nursultan' }
136
- ```
137
-
138
- #### Static Path
139
-
140
- Entity for connecting statics to the server, like HTML, JSON, PNG, etc.
141
-
142
- - `string` path to your static files
143
- - `Object<{prefix, path}`
144
- - `prefix` {string} path prefix for request
145
- - `path` {string} path to your static files
146
- - `Array<string | Object<{prefix, path}>>`
147
-
148
- #### Cors
149
-
150
- 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.
151
-
152
- - `origin` {string | RegExp | Array<string | RegExp> | Function | Promise } available origins from which requests can be made
153
- - `methods?` {Array<GET | POST | DELETE | PUT | PATCH>} available methods (default: `*`)
154
- - `headers?` {Array<string>} available methods (default: `*`)
155
- - `credentials?` {boolean} param tells browsers whether to expose the response to the frontend JavaScript code (default: `true`)
156
- - `maxAge?` {number} how long the results can be cached (default: `3600`)
157
-
158
- #### Interceptors
159
-
160
- Functions to change request or response parameters
161
-
162
- - `request?` (params) => void
163
- - `response?` (data, params) => any
164
-
165
- ##### Request
166
-
167
- - `params`
168
- - `request` request object
169
-
170
- ##### Response
171
-
172
- - `data` {any} mock data of request
173
- - `params`
174
- - `request` request object
175
- - `response` response object
176
- - `setDelay` (delay) => Promise<void>
177
- - `delay` {number} seconds of delay time
178
- - `setStatusCode` (statusCode) => void
179
- - `statusCode` {number} status code for response
180
-
181
- ## ✨ Contributors
182
-
183
- <table>
184
- <tr>
185
- <td align="center" style="word-wrap: break-word; width: 100.0; height: 100.0">
186
- <a href="https://github.com/debabin">
187
- <img src="https://avatars.githubusercontent.com/u/45297354?v=4"
188
- width="100;"
189
- alt="debabin" />
190
- <br />
191
- <sub style="font-size:13px"><b>☄️ debabin</b></sub>
192
- </a>
193
- </td>
194
- <td align="center" style="word-wrap: break-word; width: 100.0; height: 100.0">
195
- <a href="https://github.com/MiaInturi">
196
- <img src="https://avatars.githubusercontent.com/u/39031929?v=4"
197
- width="100;"
198
- alt="MiaInturi" />
199
- <br />
200
- <sub style="font-size:13px"><b>👹 MiaInturi</b></sub>
201
- </a>
202
- </td>
203
- <td align="center" style="word-wrap: break-word; width: 100.0; height: 100.0">
204
- <a href="https://github.com/RiceWithMeat">
205
- <img src="https://avatars.githubusercontent.com/u/47690223?v=4"
206
- width="100;"
207
- alt="RiceWithMeat" />
208
- <br />
209
- <sub style="font-size:13px"><b>🐘 RiceWithMeat</b></sub>
210
- </a>
211
- </td>
212
- </tr>
213
- </table>
1
+ # 🎉 Mock Config Server
2
+
3
+ tool that easily and quickly imitates server operation, create full fake api in few steps
4
+
5
+ ## Install
6
+
7
+ Install with [npm](https://www.npmjs.com/) or [yarn](https://yarnpkg.com/)
8
+
9
+ ```bash
10
+ $ npm i mock-config-server --save --dev
11
+ # or
12
+ $ yarn add mock-config-server --dev
13
+ ```
14
+
15
+ ## 🦉 Philosophy
16
+
17
+ **🎉 Mock Config Server** it is a tool that, easily, quickly simulates the work of a server. The main difference from solutions such as [json-server](https://www.npmjs.com/package/json-server) and [mock-service-worker](https://mswjs.io/) is the ease of filling in data and flexible emulation of any and usual cases. Our goal is to create a simple and flexible system for users, with the help of which they can create, test, and support their products.
18
+
19
+ ## Features
20
+
21
+ - **TypeScript support out of the box** - full typed package
22
+ - **Full Rest Api support** - using simple configs of a certain format, you can easily simulate rest operation of servers
23
+ - **GraphQL support** - using simple configs of a certain format, you can easily simulate graphlql operation of servers
24
+ - **CORS setup** - turn on and off CORS, fully customizable when CORS is turned on
25
+ - **Support for any kind of static** - server can return any type of static file if needed. Images, HTML, CSS, JSON, etc
26
+
27
+ ## Usage
28
+
29
+ Install **🎉 Mock Config Server** with [npm](https://www.npmjs.com/) or [yarn](https://yarnpkg.com/)
30
+
31
+ ```bash
32
+ $ npm i mock-config-server --save --dev
33
+ # or
34
+ $ yarn add mock-config-server --dev
35
+ ```
36
+
37
+ Create a `mock-server.config.js` file with server configuration
38
+
39
+ ```javascript
40
+ /** @type {import('mock-config-server').MockServerConfig} */
41
+ const mockServerConfig = {
42
+ rest: {
43
+ baseUrl: '/api',
44
+ configs: [
45
+ {
46
+ path: '/user',
47
+ method: 'get',
48
+ routes: [{ data: { emoji: '🦁', name: 'Nursultan' } }]
49
+ }
50
+ ]
51
+ }
52
+ };
53
+
54
+ export default mockServerConfig;
55
+ ```
56
+
57
+ Start **🎉 Mock Config Server**
58
+
59
+ ```bash
60
+ $ npx mock-config-server
61
+
62
+ # 🎉 Mock Config Server is running at http://localhost:31299
63
+ ```
64
+
65
+ ## 🎭 Parameters for mock-server.config.(js|ts)
66
+
67
+ - `rest?` Rest configs for mock requests
68
+ - `baseUrl?` {string} part of the url that will be substituted at the beginning of rest request url (default: `'/'`)
69
+ - `configs` {Array<RestRequestConfig>} configs for mock requests, [read](#configs)
70
+ - `graphql?` GraphQL configs for mock requests
71
+ - `baseUrl?` {string} part of the url that will be substituted at the beginning of graphql request url (default: `'/'`)
72
+ - `configs` {Array<GraphQLRequestConfig>} configs for mock requests, [read](#configs)
73
+ - `staticPath?` {StaticPath} entity for working with static files, [read](#static-path)
74
+ - `interceptors?` {Interceptors} functions to change request or response parameters, [read](#interceptors)
75
+ - `cors?` {Cors} CORS settings object (default: `CORS is turn off`), [read](#cors)
76
+ - `port?` {number} server port (default: `31299`)
77
+ - `baseUrl?` {string} part of the url that will be substituted at the beginning of the request url (default: `'/'`)
78
+
79
+ ### Configs
80
+
81
+ 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` | `query` | `params` | `body` for Rest request or `headers` | `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
82
+
83
+ ##### Rest request config
84
+
85
+ - `path` {string | RegExp} request path
86
+ - `method` {GET | POST | DELETE | PUT | PATCH} rest api method
87
+ - `routes` {RestRouteConfig[]} request routes
88
+ - `data` {any} mock data of request
89
+ - `entities?` Object<headers | query | params | body> object that helps in data retrieval
90
+ - `interceptors?` {Interceptors} functions to change request or response parameters, [read](#interceptors)
91
+ - `interceptors?` {Interceptors} functions to change request or response parameters, [read](#interceptors)
92
+
93
+ ##### GraphQL request config
94
+
95
+ - `operationType` {query | mutation} graphql operation type
96
+ - `operationName` {string} graphql operation name
97
+ - `routes` {GraphQLRouteConfig[]} request routes
98
+ - `data` {any} mock data of request
99
+ - `entities?` Object<headers | query | variables> object that helps in data retrieval
100
+ - `interceptors?` {Interceptors} functions to change request or response parameters, [read](#interceptors)
101
+ - `interceptors?` {Interceptors} functions to change request or response parameters, [read](#interceptors)
102
+
103
+ ##### Rest example
104
+
105
+ ```javascript
106
+ /** @type {import('mock-config-server').MockServerConfig} */
107
+ const mockServerConfig = {
108
+ rest: {
109
+ baseUrl: '/api',
110
+ configs: [
111
+ {
112
+ path: '/user',
113
+ method: 'get',
114
+ routes: [
115
+ {
116
+ entities: {
117
+ headers: { 'name-header': 'Nursultan' }
118
+ },
119
+ data: { emoji: '🦁', name: 'Nursultan' }
120
+ },
121
+ {
122
+ entities: {
123
+ headers: { 'name-header': 'Dmitriy' }
124
+ },
125
+ data: { emoji: '☄', name: 'Dmitriy' }
126
+ }
127
+ ]
128
+ }
129
+ ]
130
+ }
131
+ };
132
+
133
+ module.exports = mockServerConfig;
134
+ ```
135
+
136
+ Now you can make a request with an additional header and get the desired result
137
+
138
+ ```javascript
139
+ fetch('http://localhost:31299/api/user', {
140
+ headers: {
141
+ 'name-header': 'Nursultan',
142
+ 'Content-Type': 'application/json'
143
+ }
144
+ })
145
+ .then((response) => response.json())
146
+ .then((data) => console.log(data)); // { emoji: '🦁', name: 'Nursultan' }
147
+ ```
148
+
149
+ ##### GraphQL example
150
+
151
+ ```javascript
152
+ /** @type {import('mock-config-server').MockServerConfig} */
153
+ const mockServerConfig = {
154
+ graphql: {
155
+ baseUrl: '/graphql',
156
+ configs: [
157
+ {
158
+ operationType: 'query',
159
+ operationName: 'GetUser',
160
+ routes: [
161
+ {
162
+ entities: {
163
+ headers: { 'name-header': 'Nursultan' }
164
+ },
165
+ data: { emoji: '🦁', name: 'Nursultan' }
166
+ },
167
+ {
168
+ entities: {
169
+ headers: { 'name-header': 'Dmitriy' }
170
+ },
171
+ data: { emoji: '☄', name: 'Dmitriy' }
172
+ }
173
+ ]
174
+ }
175
+ ]
176
+ }
177
+ };
178
+
179
+ module.exports = mockServerConfig;
180
+ ```
181
+
182
+ Now you can make a request with an additional header and get the desired result
183
+
184
+ ```javascript
185
+ const body = JSON.stringify({
186
+ query: 'query GetUser { name }'
187
+ });
188
+
189
+ fetch('http://localhost:31299/graphql', {
190
+ method: 'POST',
191
+ headers: {
192
+ 'name-header': 'Nursultan',
193
+ 'Content-Type': 'application/json'
194
+ },
195
+ body
196
+ })
197
+ .then((response) => response.json())
198
+ .then((data) => console.log(data)); // { emoji: '🦁', name: 'Nursultan' }
199
+ ```
200
+
201
+ #### Static Path
202
+
203
+ Entity for connecting statics to the server, like HTML, JSON, PNG, etc.
204
+
205
+ - `string` path to your static files
206
+ - `Object<{prefix, path}`
207
+ - `prefix` {string} path prefix for request
208
+ - `path` {string} path to your static files
209
+ - `Array<string | Object<{prefix, path}>>`
210
+
211
+ #### Cors
212
+
213
+ 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.
214
+
215
+ - `origin` {string | RegExp | Array<string | RegExp> | Function | Promise } available origins from which requests can be made
216
+ - `methods?` {Array<GET | POST | DELETE | PUT | PATCH>} available methods (default: `*`)
217
+ - `allowedHeaders?` {Array<string>} allowed headers (default: `*`)
218
+ - `exposedHeaders?` {Array<string>} exposed headers (default: `*`)
219
+ - `credentials?` {boolean} param tells browsers whether to expose the response to the frontend JavaScript code (default: `true`)
220
+ - `maxAge?` {number} how long the results can be cached (default: `3600`)
221
+
222
+ #### Interceptors
223
+
224
+ Functions to change request or response parameters
225
+
226
+ - `request?` (params) => void
227
+ - `response?` (data, params) => any
228
+
229
+ ##### Request
230
+
231
+ - `params`
232
+ - `request` request object
233
+
234
+ ##### Response
235
+
236
+ - `data` {any} mock data of request
237
+ - `params`
238
+ - `request` request object
239
+ - `response` response object
240
+ - `setDelay` (delay) => Promise<void>
241
+ - `delay` {number} seconds of delay time
242
+ - `setStatusCode` (statusCode) => void
243
+ - `statusCode` {number} status code for response
244
+
245
+ ## ✨ Contributors
246
+
247
+ <table>
248
+ <tr>
249
+ <td align="center" style="word-wrap: break-word; width: 100.0; height: 100.0">
250
+ <a href="https://github.com/debabin">
251
+ <img src="https://avatars.githubusercontent.com/u/45297354?v=4"
252
+ width="100;"
253
+ alt="debabin" />
254
+ <br />
255
+ <sub style="font-size:13px"><b>☄️ debabin</b></sub>
256
+ </a>
257
+ </td>
258
+ <td align="center" style="word-wrap: break-word; width: 100.0; height: 100.0">
259
+ <a href="https://github.com/MiaInturi">
260
+ <img src="https://avatars.githubusercontent.com/u/39031929?v=4"
261
+ width="100;"
262
+ alt="MiaInturi" />
263
+ <br />
264
+ <sub style="font-size:13px"><b>👹 MiaInturi</b></sub>
265
+ </a>
266
+ </td>
267
+ <td align="center" style="word-wrap: break-word; width: 100.0; height: 100.0">
268
+ <a href="https://github.com/RiceWithMeat">
269
+ <img src="https://avatars.githubusercontent.com/u/47690223?v=4"
270
+ width="100;"
271
+ alt="RiceWithMeat" />
272
+ <br />
273
+ <sub style="font-size:13px"><b>🐘 RiceWithMeat</b></sub>
274
+ </a>
275
+ </td>
276
+ </tr>
277
+ </table>
@@ -1,2 +1,2 @@
1
- #!/usr/bin/env node
2
- export {};
1
+ #!/usr/bin/env node
2
+ export {};
@@ -1,65 +1,67 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
- if (k2 === undefined) k2 = k;
5
- var desc = Object.getOwnPropertyDescriptor(m, k);
6
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
- desc = { enumerable: true, get: function() { return m[k]; } };
8
- }
9
- Object.defineProperty(o, k2, desc);
10
- }) : (function(o, m, k, k2) {
11
- if (k2 === undefined) k2 = k;
12
- o[k2] = m[k];
13
- }));
14
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
- Object.defineProperty(o, "default", { enumerable: true, value: v });
16
- }) : function(o, v) {
17
- o["default"] = v;
18
- });
19
- var __importStar = (this && this.__importStar) || function (mod) {
20
- if (mod && mod.__esModule) return mod;
21
- var result = {};
22
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
- __setModuleDefault(result, mod);
24
- return result;
25
- };
26
- Object.defineProperty(exports, "__esModule", { value: true });
27
- const esbuild_1 = require("esbuild");
28
- const fs = __importStar(require("fs"));
29
- const src_1 = require("../src");
30
- const resolveExportsFromSourceCode_1 = require("./resolveExportsFromSourceCode");
31
- const start = async () => {
32
- try {
33
- const appPath = process.cwd();
34
- const mockServerConfigFileRegex = /mock-server.config.(?:ts|js)/;
35
- const mockServerConfigFile = fs
36
- .readdirSync(appPath)
37
- .find((file) => mockServerConfigFileRegex.test(file));
38
- if (!mockServerConfigFile) {
39
- throw new Error('Cannot find config file mock-server.config.(ts|js)');
40
- }
41
- const { outputFiles } = await (0, esbuild_1.build)({
42
- entryPoints: [mockServerConfigFile],
43
- bundle: true,
44
- platform: 'node',
45
- target: 'esnext',
46
- minifySyntax: true,
47
- minify: true,
48
- write: false,
49
- metafile: false
50
- });
51
- const mockServerConfigSourceCode = outputFiles[0]?.text;
52
- if (!mockServerConfigSourceCode) {
53
- throw new Error('Cannot handle source code of mock-server.config.(ts|js)');
54
- }
55
- const mockServerConfigExports = (0, resolveExportsFromSourceCode_1.resolveExportsFromSourceCode)(mockServerConfigSourceCode);
56
- if (!mockServerConfigExports?.default) {
57
- throw new Error('Cannot handle exports of mock-server.config.(ts|js)');
58
- }
59
- (0, src_1.startMockServer)(mockServerConfigExports.default);
60
- }
61
- catch (e) {
62
- console.error(e.message);
63
- }
64
- };
65
- start();
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ const esbuild_1 = require("esbuild");
28
+ const fs = __importStar(require("fs"));
29
+ const src_1 = require("../src");
30
+ const validateMockServerConfig_1 = require("./validateMockServerConfig/validateMockServerConfig");
31
+ const resolveExportsFromSourceCode_1 = require("./resolveExportsFromSourceCode");
32
+ const start = async () => {
33
+ try {
34
+ const appPath = process.cwd();
35
+ const mockServerConfigFileRegex = /mock-server.config.(?:ts|js)/;
36
+ const mockServerConfigFile = fs
37
+ .readdirSync(appPath)
38
+ .find((file) => mockServerConfigFileRegex.test(file));
39
+ if (!mockServerConfigFile) {
40
+ throw new Error('Cannot find config file mock-server.config.(ts|js)');
41
+ }
42
+ const { outputFiles } = await (0, esbuild_1.build)({
43
+ entryPoints: [mockServerConfigFile],
44
+ bundle: true,
45
+ platform: 'node',
46
+ target: 'esnext',
47
+ minifySyntax: true,
48
+ minify: true,
49
+ write: false,
50
+ metafile: false
51
+ });
52
+ const mockServerConfigSourceCode = outputFiles[0]?.text;
53
+ if (!mockServerConfigSourceCode) {
54
+ throw new Error('Cannot handle source code of mock-server.config.(ts|js)');
55
+ }
56
+ const mockServerConfigExports = (0, resolveExportsFromSourceCode_1.resolveExportsFromSourceCode)(mockServerConfigSourceCode);
57
+ if (!mockServerConfigExports?.default) {
58
+ throw new Error('Cannot handle exports of mock-server.config.(ts|js)');
59
+ }
60
+ (0, validateMockServerConfig_1.validateMockServerConfig)(mockServerConfigExports.default);
61
+ (0, src_1.startMockServer)(mockServerConfigExports.default);
62
+ }
63
+ catch (error) {
64
+ console.error(error.message);
65
+ }
66
+ };
67
+ start();
@@ -1 +1 @@
1
- export declare const resolveExportsFromSourceCode: (sourceCode: string) => any;
1
+ export declare const resolveExportsFromSourceCode: (sourceCode: string) => any;