mock-config-server 3.2.0 → 3.3.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.
- package/README.md +153 -3
- package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/isDescriptorValueValid.d.ts +2 -1
- package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/isDescriptorValueValid.js +30 -7
- package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateGraphqlConfig.js +9 -3
- package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateRoutes/validateRoutes.js +67 -18
- package/dist/bin/validateMockServerConfig/validateQueue/validateQueue.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validateQueue/validateQueue.js +29 -0
- package/dist/bin/validateMockServerConfig/validateRestConfig/validateRoutes/validateRoutes.js +67 -18
- package/dist/bin/validateMockServerConfig/validateSettings/validateSettings.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validateSettings/validateSettings.js +34 -0
- package/dist/src/core/database/createDatabaseRoutes/createDatabaseRoutes.test.ts +112 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/createNewId/createNewId.test.ts +13 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/findIndexById/findIndexById.test.ts +17 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/isIndex/isIndex.test.ts +30 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/createNestedDatabaseRoutes/createNestedDatabaseRoutes.js +45 -2
- package/dist/src/core/database/createDatabaseRoutes/helpers/createNestedDatabaseRoutes/createNestedDatabaseRoutes.test.ts +399 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/createShallowDatabaseRoutes/createShallowDatabaseRoutes.test.ts +118 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/filter/filter.d.ts +2 -1
- package/dist/src/core/database/createDatabaseRoutes/helpers/operators/operators.d.ts +3 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/operators/operators.js +30 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/pagination/pagination.d.ts +13 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/pagination/pagination.js +36 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/search/search.d.ts +3 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/search/search.js +31 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/sort/sort.d.ts +2 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/sort/sort.js +42 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/splitDatabaseByNesting/splitDatabaseByNesting.test.ts +25 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileStorage.test.ts +156 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileWriter.test.ts +48 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/Memory/MemoryStorage.test.ts +96 -0
- package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.d.ts +7 -1
- package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.js +65 -24
- package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.test.ts +851 -0
- package/dist/src/core/graphql/createGraphQLRoutes/helpers/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.test.ts +116 -0
- package/dist/src/core/middlewares/cookieParseMiddleware/cookieParseMiddleware.test.ts +22 -0
- package/dist/src/core/middlewares/cookieParseMiddleware/helpers/parseCookie/parseCookie.test.ts +45 -0
- package/dist/src/core/middlewares/corsMiddleware/corsMiddleware.test.ts +152 -0
- package/dist/src/core/middlewares/corsMiddleware/helpers/getAllowedOrigins/getAllowedOrigins.test.ts +15 -0
- package/dist/src/core/middlewares/errorMiddleware/errorMiddleware.test.ts +29 -0
- package/dist/src/core/middlewares/noCorsMiddleware/noCorsMiddleware.test.ts +49 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getGraphqlUrlSuggestions/getGraphqlUrlSuggestions.test.ts +27 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getLevenshteinDistance/getLevenshteinDistance.test.ts +12 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/getRestUrlSuggestions.test.ts +54 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getActualRestUrlMeaningfulString/getActualRestUrlMeaningfulString.test.ts +12 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getPatternRestUrlMeaningfulString/getPatternRestUrlMeaningfulString.test.ts +10 -0
- package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.js +1 -3
- package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.test.ts +285 -0
- package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.d.ts +7 -1
- package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.js +6 -2
- package/dist/src/core/rest/createRestRoutes/createRestRoutes.d.ts +7 -1
- package/dist/src/core/rest/createRestRoutes/createRestRoutes.js +55 -12
- package/dist/src/core/rest/createRestRoutes/createRestRoutes.test.ts +648 -0
- package/dist/src/core/rest/createRestRoutes/helpers/prepareRestRequestConfigs/prepareRestRequestConfigs.test.ts +154 -0
- package/dist/src/server/createDatabaseMockServer/createDatabaseMockServer.js +4 -1
- package/dist/src/server/createGraphQLMockServer/createGraphQLMockServer.js +11 -4
- package/dist/src/server/createMockServer/createMockServer.js +28 -9
- package/dist/src/server/createRestMockServer/createRestMockServer.js +11 -4
- package/dist/src/server/index.d.ts +3 -3
- package/dist/src/server/index.js +23 -23
- package/dist/src/static/views/features/scheme/index.js +31 -31
- package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.test.ts +1452 -0
- package/dist/src/utils/helpers/entities/convertToEntityDescriptor/convertToEntityDescriptor.test.ts +27 -0
- package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.d.ts +2 -1
- package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.test.ts +15 -0
- package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.d.ts +7 -2
- package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.js +6 -4
- package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.test.ts +140 -0
- package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.d.ts +1 -1
- package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.js +1 -1
- package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.test.ts +32 -0
- package/dist/src/utils/helpers/interceptors/callRequestInterceptor/callRequestInterceptors.test.ts +53 -0
- package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.js +19 -10
- package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.test.ts +262 -0
- package/dist/src/utils/helpers/isPlainObject/isPlainObject.test.ts +20 -0
- package/dist/src/utils/helpers/isPrimitive/isPrimitive.test.ts +26 -0
- package/dist/src/utils/helpers/isRegExp/isRegExp.test.ts +20 -0
- package/dist/src/utils/helpers/url/convertWin32PathToUnix/convertWin32PathToUnix.test.ts +21 -0
- package/dist/src/utils/helpers/url/getUrlParts/getUrlParts.test.ts +8 -0
- package/dist/src/utils/helpers/url/removeLeadingAndTrailingSlashes/removeLeadingAndTrailingSlashes.test.ts +10 -0
- package/dist/src/utils/helpers/url/urlJoin/urlJoin.test.ts +9 -0
- package/dist/src/utils/types/graphql.d.ts +60 -51
- package/dist/src/utils/types/index.d.ts +1 -0
- package/dist/src/utils/types/index.js +11 -0
- package/dist/src/utils/types/interceptors.d.ts +1 -1
- package/dist/src/utils/types/rest.d.ts +47 -40
- package/dist/src/utils/types/utils.d.ts +8 -0
- package/dist/src/utils/types/utils.js +1 -0
- package/dist/src/utils/types/values.d.ts +0 -1
- package/package.json +2 -3
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import request from 'supertest';
|
|
3
|
+
|
|
4
|
+
import { createGraphQLRoutes } from '@/core/graphql';
|
|
5
|
+
import { createRestRoutes } from '@/core/rest';
|
|
6
|
+
import { urlJoin } from '@/utils/helpers';
|
|
7
|
+
import type { BaseUrl, MockServerConfig } from '@/utils/types';
|
|
8
|
+
|
|
9
|
+
import type { GraphqlRequestSuggestionConfigs } from './helpers/getGraphqlUrlSuggestions/getGraphqlUrlSuggestions';
|
|
10
|
+
import type { RestRequestSuggestionConfigs } from './helpers/getRestUrlSuggestions/getRestUrlSuggestions';
|
|
11
|
+
import { notFoundMiddleware } from './notFoundMiddleware';
|
|
12
|
+
|
|
13
|
+
const createServer = (
|
|
14
|
+
mockServerConfig: Pick<MockServerConfig, 'rest' | 'graphql' | 'interceptors' | 'baseUrl'>
|
|
15
|
+
) => {
|
|
16
|
+
const { baseUrl, rest, interceptors, graphql } = mockServerConfig;
|
|
17
|
+
|
|
18
|
+
const server = express();
|
|
19
|
+
|
|
20
|
+
const serverBaseUrl = baseUrl ?? '/';
|
|
21
|
+
|
|
22
|
+
const restBaseUrl = urlJoin(serverBaseUrl, rest?.baseUrl ?? '/');
|
|
23
|
+
const routerWithRestRoutes = createRestRoutes({
|
|
24
|
+
router: express.Router(),
|
|
25
|
+
restConfig: { configs: rest?.configs ?? [] },
|
|
26
|
+
serverResponseInterceptor: interceptors?.response
|
|
27
|
+
});
|
|
28
|
+
server.use(restBaseUrl, routerWithRestRoutes);
|
|
29
|
+
|
|
30
|
+
const graphqlBaseUrl = urlJoin(serverBaseUrl, graphql?.baseUrl ?? '/');
|
|
31
|
+
const routerWithGraphqlRoutes = createGraphQLRoutes({
|
|
32
|
+
router: express.Router(),
|
|
33
|
+
graphqlConfig: { configs: graphql?.configs ?? [] },
|
|
34
|
+
serverResponseInterceptor: interceptors?.response
|
|
35
|
+
});
|
|
36
|
+
server.use(graphqlBaseUrl, routerWithGraphqlRoutes);
|
|
37
|
+
|
|
38
|
+
server.set('view engine', 'ejs');
|
|
39
|
+
server.set('views', urlJoin(__dirname, '../../../static/views'));
|
|
40
|
+
server.use(express.static(urlJoin(__dirname, '../../../static/views')));
|
|
41
|
+
server.use(express.json());
|
|
42
|
+
|
|
43
|
+
notFoundMiddleware(server, mockServerConfig);
|
|
44
|
+
|
|
45
|
+
return server;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
describe('notFoundMiddleware: HTML response', () => {
|
|
49
|
+
const server = createServer({
|
|
50
|
+
rest: {
|
|
51
|
+
configs: [
|
|
52
|
+
{
|
|
53
|
+
path: '/posts',
|
|
54
|
+
method: 'get',
|
|
55
|
+
routes: [{ data: {} }]
|
|
56
|
+
}
|
|
57
|
+
]
|
|
58
|
+
},
|
|
59
|
+
graphql: {
|
|
60
|
+
configs: [
|
|
61
|
+
{
|
|
62
|
+
operationName: 'GetPosts',
|
|
63
|
+
operationType: 'query',
|
|
64
|
+
routes: [{ data: {} }]
|
|
65
|
+
}
|
|
66
|
+
]
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('Should send correct HTML REST response', async () => {
|
|
71
|
+
const response = await request(server).get('/pstss').set('accept', 'text/html');
|
|
72
|
+
|
|
73
|
+
expect(response.statusCode).toBe(404);
|
|
74
|
+
expect(response.get('Content-Type')).toContain('text/html');
|
|
75
|
+
expect(response.text).toContain('GET /posts');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('Should send correct HTML GraphQL response', async () => {
|
|
79
|
+
const response = await request(server)
|
|
80
|
+
.get('/?query=query getPost { posts }')
|
|
81
|
+
.set('accept', 'text/html');
|
|
82
|
+
|
|
83
|
+
expect(response.statusCode).toBe(404);
|
|
84
|
+
expect(response.get('Content-Type')).toContain('text/html');
|
|
85
|
+
expect(response.text).toContain('query GetPosts');
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
interface ResponseBody {
|
|
90
|
+
restRequestSuggestions?: RestRequestSuggestionConfigs;
|
|
91
|
+
graphqlRequestSuggestions?: GraphqlRequestSuggestionConfigs;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const responseBody = ({
|
|
95
|
+
restRequestSuggestions = [],
|
|
96
|
+
graphqlRequestSuggestions = []
|
|
97
|
+
}: ResponseBody) => ({
|
|
98
|
+
message: 'Request or page not found. Similar requests in data',
|
|
99
|
+
data: {
|
|
100
|
+
restRequestSuggestions,
|
|
101
|
+
graphqlRequestSuggestions
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe('notFoundMiddleware: REST', () => {
|
|
106
|
+
const serverBaseUrl: BaseUrl = '/base';
|
|
107
|
+
const restBaseUrl: BaseUrl = '/rest';
|
|
108
|
+
const rest: MockServerConfig['rest'] = {
|
|
109
|
+
configs: [
|
|
110
|
+
{
|
|
111
|
+
path: '/posts',
|
|
112
|
+
method: 'get',
|
|
113
|
+
routes: [{ data: {} }]
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
path: '/posts/:postId',
|
|
117
|
+
method: 'get',
|
|
118
|
+
routes: [{ data: {}, entities: { params: { postId: '1' } } }]
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
{
|
|
122
|
+
path: '/developers',
|
|
123
|
+
method: 'post',
|
|
124
|
+
routes: [{ data: {} }]
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
path: '/developers/:developerId',
|
|
128
|
+
method: 'post',
|
|
129
|
+
routes: [{ data: {}, entities: { params: { developerId: '1' } } }]
|
|
130
|
+
}
|
|
131
|
+
]
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
test('Should send correct REST suggestions', async () => {
|
|
135
|
+
const server = createServer({
|
|
136
|
+
rest
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const response = await request(server).get('/pstss');
|
|
140
|
+
|
|
141
|
+
expect(response.statusCode).toBe(404);
|
|
142
|
+
expect(response.body).toStrictEqual(
|
|
143
|
+
responseBody({ restRequestSuggestions: [{ path: '/posts', method: 'get' }] })
|
|
144
|
+
);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test('Should send correct REST suggestions with serverBaseUrl', async () => {
|
|
148
|
+
const server = createServer({
|
|
149
|
+
baseUrl: serverBaseUrl,
|
|
150
|
+
rest
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
const response = await request(server).get('/bas/dveloprs');
|
|
154
|
+
|
|
155
|
+
expect(response.statusCode).toBe(404);
|
|
156
|
+
expect(response.body).toStrictEqual(
|
|
157
|
+
responseBody({ restRequestSuggestions: [{ path: '/base/developers', method: 'post' }] })
|
|
158
|
+
);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test('Should send correct REST suggestions with restBaseUrl', async () => {
|
|
162
|
+
const server = createServer({
|
|
163
|
+
rest: {
|
|
164
|
+
...rest,
|
|
165
|
+
baseUrl: restBaseUrl
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
const response = await request(server).get('/res/pstss');
|
|
170
|
+
|
|
171
|
+
expect(response.statusCode).toBe(404);
|
|
172
|
+
expect(response.body).toStrictEqual(
|
|
173
|
+
responseBody({ restRequestSuggestions: [{ path: '/rest/posts', method: 'get' }] })
|
|
174
|
+
);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test('Should send correct REST suggestions with serverBaseUrl and restBaseUrl', async () => {
|
|
178
|
+
const server = createServer({
|
|
179
|
+
baseUrl: serverBaseUrl,
|
|
180
|
+
rest: {
|
|
181
|
+
...rest,
|
|
182
|
+
baseUrl: restBaseUrl
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const response = await request(server).get('/bas/res/post');
|
|
187
|
+
|
|
188
|
+
expect(response.statusCode).toBe(404);
|
|
189
|
+
expect(response.body).toStrictEqual(
|
|
190
|
+
responseBody({ restRequestSuggestions: [{ path: '/base/rest/posts', method: 'get' }] })
|
|
191
|
+
);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
describe('notFoundMiddleware: GraphQL', () => {
|
|
196
|
+
const serverBaseUrl: MockServerConfig['baseUrl'] = '/base';
|
|
197
|
+
const graphqlBaseUrl: BaseUrl = '/graphql';
|
|
198
|
+
|
|
199
|
+
const graphql: MockServerConfig['graphql'] = {
|
|
200
|
+
configs: [
|
|
201
|
+
{
|
|
202
|
+
operationName: 'GetPosts',
|
|
203
|
+
operationType: 'query',
|
|
204
|
+
routes: [{ data: {} }]
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
operationName: 'GetDevelopers',
|
|
208
|
+
operationType: 'mutation',
|
|
209
|
+
routes: [{ data: {} }]
|
|
210
|
+
}
|
|
211
|
+
]
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
test('Should send correct GraphQL suggestions', async () => {
|
|
215
|
+
const server = createServer({
|
|
216
|
+
graphql
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
const response = await request(server).get('/?query=query getPost { posts }');
|
|
220
|
+
|
|
221
|
+
expect(response.statusCode).toBe(404);
|
|
222
|
+
expect(response.body).toStrictEqual(
|
|
223
|
+
responseBody({
|
|
224
|
+
graphqlRequestSuggestions: [{ operationName: ' GetPosts', operationType: 'query' }]
|
|
225
|
+
})
|
|
226
|
+
);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test('Should send correct GraphQL suggestions with serverBaseUrl', async () => {
|
|
230
|
+
const server = createServer({
|
|
231
|
+
baseUrl: serverBaseUrl,
|
|
232
|
+
graphql
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
const response = await request(server).get('/bse?query=query developers { posts }');
|
|
236
|
+
|
|
237
|
+
expect(response.statusCode).toBe(404);
|
|
238
|
+
expect(response.body).toStrictEqual(
|
|
239
|
+
responseBody({
|
|
240
|
+
graphqlRequestSuggestions: [
|
|
241
|
+
{ operationName: '/base GetDevelopers', operationType: 'mutation' }
|
|
242
|
+
]
|
|
243
|
+
})
|
|
244
|
+
);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test('Should send correct GraphQL suggestions with graphqlBaseUrl', async () => {
|
|
248
|
+
const server = createServer({
|
|
249
|
+
graphql: {
|
|
250
|
+
...graphql,
|
|
251
|
+
baseUrl: graphqlBaseUrl
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
const response = await request(server).get('/graph?query=query posts { posts }');
|
|
256
|
+
|
|
257
|
+
expect(response.statusCode).toBe(404);
|
|
258
|
+
expect(response.body).toStrictEqual(
|
|
259
|
+
responseBody({
|
|
260
|
+
graphqlRequestSuggestions: [{ operationName: '/graphql GetPosts', operationType: 'query' }]
|
|
261
|
+
})
|
|
262
|
+
);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
test('Should send correct GraphQL suggestions with serverBaseUrl and graphqlBaseUrl', async () => {
|
|
266
|
+
const server = createServer({
|
|
267
|
+
baseUrl: serverBaseUrl,
|
|
268
|
+
graphql: {
|
|
269
|
+
...graphql,
|
|
270
|
+
baseUrl: graphqlBaseUrl
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
const response = await request(server).get('/bas/graphq?query=query posts { posts }');
|
|
275
|
+
|
|
276
|
+
expect(response.statusCode).toBe(404);
|
|
277
|
+
expect(response.body).toStrictEqual(
|
|
278
|
+
responseBody({
|
|
279
|
+
graphqlRequestSuggestions: [
|
|
280
|
+
{ operationName: '/base/graphql GetPosts', operationType: 'query' }
|
|
281
|
+
]
|
|
282
|
+
})
|
|
283
|
+
);
|
|
284
|
+
});
|
|
285
|
+
});
|
package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
1
|
import type { Express } from 'express';
|
|
2
2
|
import type { RequestInterceptor } from '../../../utils/types';
|
|
3
|
-
|
|
3
|
+
interface RequestInterceptorMiddlewareParams {
|
|
4
|
+
server: Express;
|
|
5
|
+
path?: string;
|
|
6
|
+
interceptor: RequestInterceptor;
|
|
7
|
+
}
|
|
8
|
+
export declare const requestInterceptorMiddleware: ({ server, path, interceptor }: RequestInterceptorMiddlewareParams) => void;
|
|
9
|
+
export {};
|
package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.js
CHANGED
|
@@ -5,8 +5,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.requestInterceptorMiddleware = void 0;
|
|
7
7
|
var _helpers = require("../../../utils/helpers");
|
|
8
|
-
const requestInterceptorMiddleware = (
|
|
9
|
-
server
|
|
8
|
+
const requestInterceptorMiddleware = ({
|
|
9
|
+
server,
|
|
10
|
+
path = '*',
|
|
11
|
+
interceptor
|
|
12
|
+
}) => {
|
|
13
|
+
server.use(path, (0, _helpers.asyncHandler)(async (request, _response, next) => {
|
|
10
14
|
await (0, _helpers.callRequestInterceptor)({
|
|
11
15
|
request,
|
|
12
16
|
interceptor
|
|
@@ -1,3 +1,9 @@
|
|
|
1
1
|
import type { IRouter } from 'express';
|
|
2
2
|
import type { Interceptors, RestConfig } from '../../../utils/types';
|
|
3
|
-
|
|
3
|
+
interface CreateRestRoutesParams {
|
|
4
|
+
router: IRouter;
|
|
5
|
+
restConfig: RestConfig;
|
|
6
|
+
serverResponseInterceptor?: Interceptors['response'];
|
|
7
|
+
}
|
|
8
|
+
export declare const createRestRoutes: ({ router, restConfig, serverResponseInterceptor }: CreateRestRoutesParams) => IRouter;
|
|
9
|
+
export {};
|
|
@@ -7,10 +7,14 @@ exports.createRestRoutes = void 0;
|
|
|
7
7
|
var _flat = require("flat");
|
|
8
8
|
var _helpers = require("../../../utils/helpers");
|
|
9
9
|
var _helpers2 = require("./helpers");
|
|
10
|
-
const createRestRoutes = (
|
|
10
|
+
const createRestRoutes = ({
|
|
11
|
+
router,
|
|
12
|
+
restConfig,
|
|
13
|
+
serverResponseInterceptor
|
|
14
|
+
}) => {
|
|
11
15
|
(0, _helpers2.prepareRestRequestConfigs)(restConfig.configs).forEach(requestConfig => {
|
|
12
16
|
router.route(requestConfig.path)[requestConfig.method]((0, _helpers.asyncHandler)(async (request, response, next) => {
|
|
13
|
-
var _requestConfig$interc, _matchedRouteConfig$e, _matchedRouteConfig$i, _requestConfig$interc2, _restConfig$intercept;
|
|
17
|
+
var _requestConfig$interc, _matchedRouteConfig$s, _matchedRouteConfig$e, _matchedRouteConfig$i, _requestConfig$interc2, _restConfig$intercept;
|
|
14
18
|
const requestInterceptor = (_requestConfig$interc = requestConfig.interceptors) === null || _requestConfig$interc === void 0 ? void 0 : _requestConfig$interc.request;
|
|
15
19
|
if (requestInterceptor) {
|
|
16
20
|
await (0, _helpers.callRequestInterceptor)({
|
|
@@ -23,20 +27,26 @@ const createRestRoutes = (router, restConfig, serverResponseInterceptors) => {
|
|
|
23
27
|
}) => {
|
|
24
28
|
if (!entities) return true;
|
|
25
29
|
const entries = Object.entries(entities);
|
|
26
|
-
return entries.every(([entityName,
|
|
30
|
+
return entries.every(([entityName, entityDescriptorOrValue]) => {
|
|
27
31
|
const {
|
|
28
32
|
checkMode,
|
|
29
33
|
value: descriptorValue
|
|
30
|
-
} = (0, _helpers.convertToEntityDescriptor)(
|
|
34
|
+
} = (0, _helpers.convertToEntityDescriptor)(entityDescriptorOrValue);
|
|
31
35
|
|
|
32
36
|
// ✅ important: check whole body as plain value strictly if descriptor used for body
|
|
33
|
-
const
|
|
34
|
-
if (
|
|
35
|
-
// ✅ important: bodyParser sets body to empty object if body not sent or invalid, so
|
|
37
|
+
const isEntityBodyByTopLevelDescriptor = entityName === 'body' && (0, _helpers.isEntityDescriptor)(entityDescriptorOrValue);
|
|
38
|
+
if (isEntityBodyByTopLevelDescriptor) {
|
|
39
|
+
// ✅ important: bodyParser sets body to empty object if body not sent or invalid, so assume {} as undefined
|
|
36
40
|
return (0, _helpers.resolveEntityValues)(checkMode, Object.keys(request.body).length ? request.body : undefined, descriptorValue);
|
|
37
41
|
}
|
|
38
|
-
const
|
|
39
|
-
|
|
42
|
+
const isEntityBodyByTopLevelArray = entityName === 'body' && Array.isArray(entityDescriptorOrValue);
|
|
43
|
+
if (isEntityBodyByTopLevelArray) {
|
|
44
|
+
return entityDescriptorOrValue.some(entityDescriptorOrValueElement =>
|
|
45
|
+
// ✅ important: bodyParser sets body to empty object if body not sent or invalid, so assume {} as undefined
|
|
46
|
+
(0, _helpers.resolveEntityValues)(checkMode, Object.keys(request.body).length ? request.body : undefined, entityDescriptorOrValueElement));
|
|
47
|
+
}
|
|
48
|
+
const recordOrArrayEntries = Object.entries(entityDescriptorOrValue);
|
|
49
|
+
return recordOrArrayEntries.every(([entityKey, mappedEntityDescriptor]) => {
|
|
40
50
|
const {
|
|
41
51
|
checkMode,
|
|
42
52
|
value: descriptorValue
|
|
@@ -50,16 +60,49 @@ const createRestRoutes = (router, restConfig, serverResponseInterceptors) => {
|
|
|
50
60
|
if (!matchedRouteConfig) {
|
|
51
61
|
return next();
|
|
52
62
|
}
|
|
53
|
-
|
|
63
|
+
let matchedRouteConfigData = null;
|
|
64
|
+
if ((_matchedRouteConfig$s = matchedRouteConfig.settings) !== null && _matchedRouteConfig$s !== void 0 && _matchedRouteConfig$s.polling && 'queue' in matchedRouteConfig) {
|
|
65
|
+
var _shallowMatchedRouteC;
|
|
66
|
+
if (!matchedRouteConfig.queue.length) return next();
|
|
67
|
+
const shallowMatchedRouteConfig = matchedRouteConfig;
|
|
68
|
+
let index = (_shallowMatchedRouteC = shallowMatchedRouteConfig.__pollingIndex) !== null && _shallowMatchedRouteC !== void 0 ? _shallowMatchedRouteC : 0;
|
|
69
|
+
const {
|
|
70
|
+
time,
|
|
71
|
+
data
|
|
72
|
+
} = matchedRouteConfig.queue[index];
|
|
73
|
+
const updateIndex = () => {
|
|
74
|
+
if (matchedRouteConfig.queue.length - 1 === index) {
|
|
75
|
+
index = 0;
|
|
76
|
+
} else {
|
|
77
|
+
index += 1;
|
|
78
|
+
}
|
|
79
|
+
shallowMatchedRouteConfig.__pollingIndex = index;
|
|
80
|
+
};
|
|
81
|
+
if (time && !shallowMatchedRouteConfig.__timeoutInProgress) {
|
|
82
|
+
shallowMatchedRouteConfig.__timeoutInProgress = true;
|
|
83
|
+
setTimeout(() => {
|
|
84
|
+
shallowMatchedRouteConfig.__timeoutInProgress = false;
|
|
85
|
+
updateIndex();
|
|
86
|
+
}, time);
|
|
87
|
+
}
|
|
88
|
+
if (!time && !shallowMatchedRouteConfig.__timeoutInProgress) {
|
|
89
|
+
updateIndex();
|
|
90
|
+
}
|
|
91
|
+
matchedRouteConfigData = data;
|
|
92
|
+
}
|
|
93
|
+
if ('data' in matchedRouteConfig) {
|
|
94
|
+
matchedRouteConfigData = matchedRouteConfig.data;
|
|
95
|
+
}
|
|
96
|
+
const resolvedData = typeof matchedRouteConfigData === 'function' ? await matchedRouteConfigData(request, (_matchedRouteConfig$e = matchedRouteConfig.entities) !== null && _matchedRouteConfig$e !== void 0 ? _matchedRouteConfig$e : {}) : matchedRouteConfigData;
|
|
54
97
|
const data = await (0, _helpers.callResponseInterceptors)({
|
|
55
|
-
data:
|
|
98
|
+
data: resolvedData,
|
|
56
99
|
request,
|
|
57
100
|
response,
|
|
58
101
|
interceptors: {
|
|
59
102
|
routeInterceptor: (_matchedRouteConfig$i = matchedRouteConfig.interceptors) === null || _matchedRouteConfig$i === void 0 ? void 0 : _matchedRouteConfig$i.response,
|
|
60
103
|
requestInterceptor: (_requestConfig$interc2 = requestConfig.interceptors) === null || _requestConfig$interc2 === void 0 ? void 0 : _requestConfig$interc2.response,
|
|
61
104
|
apiInterceptor: (_restConfig$intercept = restConfig.interceptors) === null || _restConfig$intercept === void 0 ? void 0 : _restConfig$intercept.response,
|
|
62
|
-
serverInterceptor:
|
|
105
|
+
serverInterceptor: serverResponseInterceptor
|
|
63
106
|
}
|
|
64
107
|
});
|
|
65
108
|
|