mock-config-server 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +614 -502
- package/dist/bin/bin.d.ts +2 -0
- package/dist/bin/bin.js +13 -0
- package/dist/bin/build.d.ts +2 -0
- package/dist/bin/build.js +55 -0
- package/dist/bin/cli.d.ts +1 -0
- package/dist/bin/cli.js +41 -0
- package/dist/bin/helpers/index.d.ts +3 -0
- package/dist/bin/helpers/index.js +38 -0
- package/dist/bin/helpers/resolveConfigFile.d.ts +2 -0
- package/dist/bin/helpers/resolveConfigFile.js +23 -0
- package/dist/bin/helpers/resolveConfigFilePath.d.ts +1 -0
- package/dist/bin/helpers/resolveConfigFilePath.js +16 -0
- package/dist/bin/helpers/resolveExportsFromSourceCode.d.ts +1 -0
- package/dist/bin/helpers/resolveExportsFromSourceCode.js +14 -0
- package/dist/bin/resolveConfigFilePath/resolveConfigFilePath.d.ts +1 -0
- package/dist/bin/resolveConfigFilePath/resolveConfigFilePath.js +16 -0
- package/dist/bin/run.d.ts +6 -0
- package/dist/bin/run.js +22 -0
- package/dist/bin/validateMockServerConfig/helpers/index.d.ts +2 -0
- package/dist/bin/validateMockServerConfig/helpers/index.js +27 -0
- package/dist/bin/validateMockServerConfig/helpers/isCheckModeValid/index.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/helpers/isCheckModeValid/index.js +16 -0
- package/dist/bin/validateMockServerConfig/helpers/isCheckModeValid/isCheckModeValid.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/helpers/isCheckModeValid/isCheckModeValid.js +12 -0
- package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/index.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/index.js +16 -0
- package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/isDescriptorValueValid.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/isDescriptorValueValid.js +20 -0
- package/dist/bin/validateMockServerConfig/validateBaseUrl/validateBaseUrl.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validateBaseUrl/validateBaseUrl.js +15 -0
- package/dist/bin/validateMockServerConfig/validateCors/validateCors.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validateCors/validateCors.js +84 -0
- package/dist/bin/validateMockServerConfig/validateDatabaseConfig/validateDatabaseConfig.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validateDatabaseConfig/validateDatabaseConfig.js +45 -0
- package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateGraphqlConfig.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateGraphqlConfig.js +52 -0
- package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateRoutes/validateRoutes.d.ts +2 -0
- package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateRoutes/validateRoutes.js +104 -0
- package/dist/bin/validateMockServerConfig/validateInterceptors/validateInterceptors.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validateInterceptors/validateInterceptors.js +27 -0
- package/dist/bin/validateMockServerConfig/validateMockServerConfig.d.ts +2 -0
- package/dist/bin/validateMockServerConfig/validateMockServerConfig.js +32 -0
- package/dist/bin/validateMockServerConfig/validatePort/validatePort.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validatePort/validatePort.js +12 -0
- package/dist/bin/validateMockServerConfig/validateRestConfig/validateRestConfig.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validateRestConfig/validateRestConfig.js +57 -0
- package/dist/bin/validateMockServerConfig/validateRestConfig/validateRoutes/validateRoutes.d.ts +2 -0
- package/dist/bin/validateMockServerConfig/validateRestConfig/validateRoutes/validateRoutes.js +108 -0
- package/dist/bin/validateMockServerConfig/validateStaticPath/validateStaticPath.d.ts +1 -0
- package/dist/bin/validateMockServerConfig/validateStaticPath/validateStaticPath.js +53 -0
- package/dist/index.d.ts +1 -0
- package/dist/src/core/database/createDatabaseRoutes/createDatabaseRoutes.d.ts +3 -0
- package/dist/src/core/database/createDatabaseRoutes/createDatabaseRoutes.js +33 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/createNewId/createNewId.d.ts +4 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/createNewId/createNewId.js +16 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/findIndexById/findIndexById.d.ts +4 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/findIndexById/findIndexById.js +8 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/index.d.ts +3 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/index.js +38 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/isIndex/isIndex.d.ts +1 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/array/isIndex/isIndex.js +8 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/createNestedDatabaseRoutes/createNestedDatabaseRoutes.d.ts +4 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/createNestedDatabaseRoutes/createNestedDatabaseRoutes.js +88 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/createRewrittenDatabaseRoutes/createRewrittenDatabaseRoutes.d.ts +2 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/createRewrittenDatabaseRoutes/createRewrittenDatabaseRoutes.js +12 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/createShallowDatabaseRoutes/createShallowDatabaseRoutes.d.ts +4 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/createShallowDatabaseRoutes/createShallowDatabaseRoutes.js +38 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/index.d.ts +5 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/index.js +60 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/splitDatabaseByNesting/splitDatabaseByNesting.d.ts +5 -0
- package/dist/src/core/database/createDatabaseRoutes/helpers/splitDatabaseByNesting/splitDatabaseByNesting.js +30 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileStorage.d.ts +11 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileStorage.js +64 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileWriter.d.ts +11 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/File/FileWriter.js +58 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/Memory/MemoryStorage.d.ts +10 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/Memory/MemoryStorage.js +54 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/index.d.ts +2 -0
- package/dist/src/core/database/createDatabaseRoutes/storages/index.js +27 -0
- package/dist/src/core/database/index.d.ts +1 -0
- package/dist/src/core/database/index.js +16 -0
- package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.d.ts +3 -0
- package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.js +102 -0
- package/dist/src/core/graphql/createGraphQLRoutes/helpers/index.d.ts +1 -0
- package/dist/src/core/graphql/createGraphQLRoutes/helpers/index.js +16 -0
- package/dist/src/core/graphql/createGraphQLRoutes/helpers/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.d.ts +2 -0
- package/dist/src/core/graphql/createGraphQLRoutes/helpers/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.js +41 -0
- package/dist/src/core/graphql/index.d.ts +1 -0
- package/dist/src/core/graphql/index.js +16 -0
- package/dist/src/core/middlewares/cookieParseMiddleware/cookieParseMiddleware.d.ts +2 -0
- package/dist/src/core/middlewares/cookieParseMiddleware/cookieParseMiddleware.js +18 -0
- package/dist/src/core/middlewares/cookieParseMiddleware/helpers/index.d.ts +1 -0
- package/dist/src/core/middlewares/cookieParseMiddleware/helpers/index.js +16 -0
- package/dist/src/core/middlewares/cookieParseMiddleware/helpers/parseCookie/parseCookie.d.ts +3 -0
- package/dist/src/core/middlewares/cookieParseMiddleware/helpers/parseCookie/parseCookie.js +19 -0
- package/dist/src/core/middlewares/corsMiddleware/corsMiddleware.d.ts +3 -0
- package/dist/src/core/middlewares/corsMiddleware/corsMiddleware.js +52 -0
- package/dist/src/core/middlewares/corsMiddleware/helpers/getAllowedOrigins/getAllowedOrigins.d.ts +2 -0
- package/dist/src/core/middlewares/corsMiddleware/helpers/getAllowedOrigins/getAllowedOrigins.js +16 -0
- package/dist/src/core/middlewares/corsMiddleware/helpers/index.d.ts +1 -0
- package/dist/src/core/middlewares/corsMiddleware/helpers/index.js +16 -0
- package/dist/src/core/middlewares/destroyerMiddleware/destroyerMiddleware.d.ts +7 -0
- package/dist/src/core/middlewares/destroyerMiddleware/destroyerMiddleware.js +26 -0
- package/dist/src/core/middlewares/errorMiddleware/errorMiddleware.d.ts +2 -0
- package/dist/src/core/middlewares/errorMiddleware/errorMiddleware.js +21 -0
- package/dist/src/core/middlewares/index.d.ts +8 -0
- package/dist/src/core/middlewares/index.js +93 -0
- package/dist/src/core/middlewares/noCorsMiddleware/noCorsMiddleware.d.ts +2 -0
- package/dist/src/core/middlewares/noCorsMiddleware/noCorsMiddleware.js +24 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getGraphqlUrlSuggestions/getGraphqlUrlSuggestions.d.ts +11 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getGraphqlUrlSuggestions/getGraphqlUrlSuggestions.js +27 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getLevenshteinDistance/getLevenshteinDistance.d.ts +1 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getLevenshteinDistance/getLevenshteinDistance.js +84 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/getRestUrlSuggestions.d.ts +11 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/getRestUrlSuggestions.js +39 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getActualRestUrlMeaningfulString/getActualRestUrlMeaningfulString.d.ts +1 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getActualRestUrlMeaningfulString/getActualRestUrlMeaningfulString.js +8 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getPatternRestUrlMeaningfulString/getPatternRestUrlMeaningfulString.d.ts +1 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getPatternRestUrlMeaningfulString/getPatternRestUrlMeaningfulString.js +8 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/index.d.ts +2 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/index.js +27 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/index.d.ts +3 -0
- package/dist/src/core/middlewares/notFoundMiddleware/helpers/index.js +38 -0
- package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.d.ts +3 -0
- package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.js +68 -0
- package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.d.ts +3 -0
- package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.js +17 -0
- package/dist/src/core/middlewares/staticMiddleware/staticMiddleware.d.ts +3 -0
- package/dist/src/core/middlewares/staticMiddleware/staticMiddleware.js +31 -0
- package/dist/src/core/rest/createRestRoutes/createRestRoutes.d.ts +3 -0
- package/dist/src/core/rest/createRestRoutes/createRestRoutes.js +75 -0
- package/dist/src/core/rest/createRestRoutes/helpers/index.d.ts +1 -0
- package/dist/src/core/rest/createRestRoutes/helpers/index.js +16 -0
- package/dist/src/core/rest/createRestRoutes/helpers/prepareRestRequestConfigs/prepareRestRequestConfigs.d.ts +2 -0
- package/dist/src/core/rest/createRestRoutes/helpers/prepareRestRequestConfigs/prepareRestRequestConfigs.js +43 -0
- package/dist/src/core/rest/index.d.ts +1 -0
- package/dist/src/core/rest/index.js +16 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.js +38 -0
- package/dist/src/server/createMockServer/createMockServer.d.ts +3 -0
- package/dist/src/server/createMockServer/createMockServer.js +79 -0
- package/dist/src/server/index.d.ts +2 -0
- package/dist/src/server/index.js +27 -0
- package/dist/src/server/startMockServer/startMockServer.d.ts +5 -0
- package/dist/src/server/startMockServer/startMockServer.js +23 -0
- package/dist/src/static/views/assets/icons/scheme-dark.svg +3 -0
- package/dist/src/static/views/assets/icons/scheme-light.svg +3 -0
- package/dist/src/static/views/assets/images/404.png +0 -0
- package/dist/src/static/views/assets/images/logo.png +0 -0
- package/dist/src/static/views/assets/styles/global.css +88 -0
- package/dist/src/static/views/components/header/index.css +55 -0
- package/dist/src/static/views/components/header/index.ejs +40 -0
- package/dist/src/static/views/components/header/index.js +1 -0
- package/dist/src/static/views/features/scheme/dark.css +12 -0
- package/dist/src/static/views/features/scheme/index.ejs +3 -0
- package/dist/src/static/views/features/scheme/index.js +31 -0
- package/dist/src/static/views/features/scheme/light.css +12 -0
- package/dist/src/static/views/features/tab/index.css +30 -0
- package/dist/src/static/views/features/tab/index.ejs +2 -0
- package/dist/src/static/views/features/tab/index.js +12 -0
- package/dist/src/static/views/pages/404/index.css +10 -0
- package/dist/src/static/views/pages/404/index.ejs +85 -0
- package/dist/src/utils/constants/appPath.d.ts +1 -0
- package/dist/src/utils/constants/appPath.js +8 -0
- package/dist/src/utils/constants/checkModes.d.ts +9 -0
- package/dist/src/utils/constants/checkModes.js +22 -0
- package/dist/src/utils/constants/default.d.ts +11 -0
- package/dist/src/utils/constants/default.js +18 -0
- package/dist/src/utils/constants/index.d.ts +3 -0
- package/dist/src/utils/constants/index.js +38 -0
- package/dist/src/utils/helpers/asyncHandler.d.ts +2 -0
- package/dist/src/utils/helpers/asyncHandler.js +8 -0
- package/dist/src/utils/helpers/config/index.d.ts +1 -0
- package/dist/src/utils/helpers/config/index.js +16 -0
- package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.d.ts +2 -0
- package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.js +118 -0
- package/dist/src/utils/helpers/entities/convertToEntityDescriptor/convertToEntityDescriptor.d.ts +2 -0
- package/dist/src/utils/helpers/entities/convertToEntityDescriptor/convertToEntityDescriptor.js +12 -0
- package/dist/src/utils/helpers/entities/index.d.ts +2 -0
- package/dist/src/utils/helpers/entities/index.js +27 -0
- package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.d.ts +1 -0
- package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.js +9 -0
- package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.d.ts +3 -0
- package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.js +31 -0
- package/dist/src/utils/helpers/graphql/index.d.ts +3 -0
- package/dist/src/utils/helpers/graphql/index.js +38 -0
- package/dist/src/utils/helpers/graphql/parseGraphQLRequest/parseGraphQLRequest.d.ts +3 -0
- package/dist/src/utils/helpers/graphql/parseGraphQLRequest/parseGraphQLRequest.js +14 -0
- package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.d.ts +7 -0
- package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.js +24 -0
- package/dist/src/utils/helpers/index.d.ts +10 -0
- package/dist/src/utils/helpers/index.js +115 -0
- package/dist/src/utils/helpers/interceptors/callRequestInterceptor/callRequestInterceptor.d.ts +8 -0
- package/dist/src/utils/helpers/interceptors/callRequestInterceptor/callRequestInterceptor.js +25 -0
- package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.d.ts +15 -0
- package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.js +60 -0
- package/dist/src/utils/helpers/interceptors/helpers/setDelay.d.ts +1 -0
- package/dist/src/utils/helpers/interceptors/helpers/setDelay.js +11 -0
- package/dist/src/utils/helpers/interceptors/index.d.ts +2 -0
- package/dist/src/utils/helpers/interceptors/index.js +27 -0
- package/dist/src/utils/helpers/isPlainObject/isPlainObject.d.ts +1 -0
- package/dist/src/utils/helpers/isPlainObject/isPlainObject.js +8 -0
- package/dist/src/utils/helpers/isPrimitive/isPrimitive.d.ts +2 -0
- package/dist/src/utils/helpers/isPrimitive/isPrimitive.js +8 -0
- package/dist/src/utils/helpers/isRegExp/isRegExp.d.ts +1 -0
- package/dist/src/utils/helpers/isRegExp/isRegExp.js +8 -0
- package/dist/src/utils/helpers/sleep.d.ts +1 -0
- package/dist/src/utils/helpers/sleep.js +10 -0
- package/dist/src/utils/helpers/url/convertWin32PathToUnix/convertWin32PathToUnix.d.ts +1 -0
- package/dist/src/utils/helpers/url/convertWin32PathToUnix/convertWin32PathToUnix.js +8 -0
- package/dist/src/utils/helpers/url/getUrlParts/getUrlParts.d.ts +1 -0
- package/dist/src/utils/helpers/url/getUrlParts/getUrlParts.js +9 -0
- package/dist/src/utils/helpers/url/index.d.ts +4 -0
- package/dist/src/utils/helpers/url/index.js +49 -0
- package/dist/src/utils/helpers/url/removeLeadingAndTrailingSlashes/removeLeadingAndTrailingSlashes.d.ts +1 -0
- package/dist/src/utils/helpers/url/removeLeadingAndTrailingSlashes/removeLeadingAndTrailingSlashes.js +8 -0
- package/dist/src/utils/helpers/url/urlJoin/urlJoin.d.ts +1 -0
- package/dist/src/utils/helpers/url/urlJoin/urlJoin.js +15 -0
- package/dist/src/utils/types/checkModes.d.ts +12 -0
- package/dist/src/utils/types/checkModes.js +1 -0
- package/dist/src/utils/types/database.d.ts +6 -0
- package/dist/src/utils/types/database.js +1 -0
- package/dist/src/utils/types/graphql.d.ts +74 -0
- package/dist/src/utils/types/graphql.js +1 -0
- package/dist/src/utils/types/index.d.ts +7 -0
- package/dist/src/utils/types/index.js +82 -0
- package/dist/src/utils/types/interceptors.d.ts +31 -0
- package/dist/src/utils/types/interceptors.js +1 -0
- package/dist/src/utils/types/rest.d.ts +77 -0
- package/dist/src/utils/types/rest.js +1 -0
- package/dist/src/utils/types/server.d.ts +53 -0
- package/dist/src/utils/types/server.js +1 -0
- package/dist/src/utils/types/values.d.ts +4 -0
- package/dist/src/utils/types/values.js +1 -0
- package/package.json +130 -113
package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateRoutes/validateRoutes.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.validateRoutes = void 0;
|
|
7
|
+
var _helpers = require("../../../../src/utils/helpers");
|
|
8
|
+
var _helpers2 = require("../../helpers");
|
|
9
|
+
var _validateInterceptors = require("../../validateInterceptors/validateInterceptors");
|
|
10
|
+
const ALLOWED_ENTITIES_BY_OPERATION_TYPE = {
|
|
11
|
+
query: ['headers', 'cookies', 'query', 'variables'],
|
|
12
|
+
mutation: ['headers', 'cookies', 'query', 'variables']
|
|
13
|
+
};
|
|
14
|
+
const validateEntity = (entity, entityName) => {
|
|
15
|
+
const {
|
|
16
|
+
checkMode: topLevelCheckMode,
|
|
17
|
+
value: topLevelValue
|
|
18
|
+
} = (0, _helpers.convertToEntityDescriptor)(entity);
|
|
19
|
+
const isVariables = entityName === 'variables';
|
|
20
|
+
const isTopLevelDescriptor = (0, _helpers.isEntityDescriptor)(entity);
|
|
21
|
+
if (isTopLevelDescriptor && isVariables) {
|
|
22
|
+
if (!(0, _helpers2.isCheckModeValid)(topLevelCheckMode, 'variables')) {
|
|
23
|
+
throw new Error('variables.checkMode');
|
|
24
|
+
}
|
|
25
|
+
if (!(0, _helpers2.isDescriptorValueValid)(topLevelCheckMode, topLevelValue, 'variables')) {
|
|
26
|
+
const errorMessage = 'variables.value';
|
|
27
|
+
throw new Error(errorMessage);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const isEntityObject = (0, _helpers.isPlainObject)(entity) && !(entity instanceof RegExp);
|
|
31
|
+
const isEntityArray = Array.isArray(entity) && isVariables;
|
|
32
|
+
if (isEntityObject || isEntityArray) {
|
|
33
|
+
Object.entries(topLevelValue).forEach(([key, valueOrDescriptor]) => {
|
|
34
|
+
const {
|
|
35
|
+
checkMode,
|
|
36
|
+
value
|
|
37
|
+
} = (0, _helpers.convertToEntityDescriptor)(valueOrDescriptor);
|
|
38
|
+
if (!(0, _helpers2.isCheckModeValid)(checkMode)) {
|
|
39
|
+
throw new Error(`${entityName}.${key}.checkMode`);
|
|
40
|
+
}
|
|
41
|
+
const isDescriptor = (0, _helpers.isEntityDescriptor)(valueOrDescriptor);
|
|
42
|
+
const errorMessage = `${entityName}.${key}${isDescriptor ? '.value' : ''}`;
|
|
43
|
+
const isValueArray = Array.isArray(value);
|
|
44
|
+
if (isValueArray && !isVariables) {
|
|
45
|
+
value.forEach((element, index) => {
|
|
46
|
+
if (!(0, _helpers2.isDescriptorValueValid)(checkMode, element)) {
|
|
47
|
+
throw new Error(`${errorMessage}[${index}]`);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (!(0, _helpers2.isDescriptorValueValid)(checkMode, value)) {
|
|
53
|
+
throw new Error(errorMessage);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
throw new Error(entityName);
|
|
59
|
+
};
|
|
60
|
+
const validateEntities = (entities, operationType) => {
|
|
61
|
+
const isEntitiesObject = (0, _helpers.isPlainObject)(entities);
|
|
62
|
+
if (isEntitiesObject) {
|
|
63
|
+
Object.keys(entities).forEach(entityName => {
|
|
64
|
+
const isEntityAllowed = ALLOWED_ENTITIES_BY_OPERATION_TYPE[operationType].includes(entityName);
|
|
65
|
+
if (!isEntityAllowed) {
|
|
66
|
+
throw new Error(`entities.${entityName}`);
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
validateEntity(entities[entityName], entityName);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
throw new Error(`entities.${error.message}`);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (typeof entities !== 'undefined') {
|
|
77
|
+
throw new Error('entities');
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const validateRoutes = (routes, operationType) => {
|
|
81
|
+
const isRoutesArray = Array.isArray(routes);
|
|
82
|
+
if (isRoutesArray) {
|
|
83
|
+
routes.forEach((route, index) => {
|
|
84
|
+
const isRouteObject = (0, _helpers.isPlainObject)(route);
|
|
85
|
+
if (isRouteObject) {
|
|
86
|
+
const isRouteHasDataProperty = ('data' in route);
|
|
87
|
+
if (!isRouteHasDataProperty) {
|
|
88
|
+
throw new Error(`routes[${index}]`);
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
validateEntities(route.entities, operationType);
|
|
92
|
+
(0, _validateInterceptors.validateInterceptors)(route.interceptors);
|
|
93
|
+
} catch (error) {
|
|
94
|
+
throw new Error(`routes[${index}].${error.message}`);
|
|
95
|
+
}
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
throw new Error(`routes[${index}]`);
|
|
99
|
+
});
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
throw new Error('routes');
|
|
103
|
+
};
|
|
104
|
+
exports.validateRoutes = validateRoutes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const validateInterceptors: (interceptors: unknown) => void;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.validateInterceptors = void 0;
|
|
7
|
+
var _helpers = require("../../../src/utils/helpers");
|
|
8
|
+
const validateInterceptors = interceptors => {
|
|
9
|
+
const isInterceptorsObject = (0, _helpers.isPlainObject)(interceptors);
|
|
10
|
+
if (isInterceptorsObject) {
|
|
11
|
+
const {
|
|
12
|
+
request,
|
|
13
|
+
response
|
|
14
|
+
} = interceptors;
|
|
15
|
+
if (typeof request !== 'function' && typeof request !== 'undefined') {
|
|
16
|
+
throw new Error('interceptors.request');
|
|
17
|
+
}
|
|
18
|
+
if (typeof response !== 'function' && typeof response !== 'undefined') {
|
|
19
|
+
throw new Error('interceptors.response');
|
|
20
|
+
}
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (typeof interceptors !== 'undefined') {
|
|
24
|
+
throw new Error('interceptors');
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
exports.validateInterceptors = validateInterceptors;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.validateMockServerConfig = void 0;
|
|
7
|
+
var _validateBaseUrl = require("./validateBaseUrl/validateBaseUrl");
|
|
8
|
+
var _validateCors = require("./validateCors/validateCors");
|
|
9
|
+
var _validateDatabaseConfig = require("./validateDatabaseConfig/validateDatabaseConfig");
|
|
10
|
+
var _validateGraphqlConfig = require("./validateGraphqlConfig/validateGraphqlConfig");
|
|
11
|
+
var _validateInterceptors = require("./validateInterceptors/validateInterceptors");
|
|
12
|
+
var _validatePort = require("./validatePort/validatePort");
|
|
13
|
+
var _validateRestConfig = require("./validateRestConfig/validateRestConfig");
|
|
14
|
+
var _validateStaticPath = require("./validateStaticPath/validateStaticPath");
|
|
15
|
+
const validateMockServerConfig = mockServerConfig => {
|
|
16
|
+
if (!mockServerConfig.rest && !mockServerConfig.graphql && !mockServerConfig.database) {
|
|
17
|
+
throw new Error('configuration should contain at least one of these configs: rest | graphql | database; see our doc (https://www.npmjs.com/package/mock-config-server) for more information');
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
if (mockServerConfig.rest) (0, _validateRestConfig.validateRestConfig)(mockServerConfig.rest);
|
|
21
|
+
if (mockServerConfig.graphql) (0, _validateGraphqlConfig.validateGraphqlConfig)(mockServerConfig.graphql);
|
|
22
|
+
if (mockServerConfig.database) (0, _validateDatabaseConfig.validateDatabaseConfig)(mockServerConfig.database);
|
|
23
|
+
(0, _validateBaseUrl.validateBaseUrl)(mockServerConfig.baseUrl);
|
|
24
|
+
(0, _validatePort.validatePort)(mockServerConfig.port);
|
|
25
|
+
(0, _validateStaticPath.validateStaticPath)(mockServerConfig.staticPath);
|
|
26
|
+
(0, _validateInterceptors.validateInterceptors)(mockServerConfig.interceptors);
|
|
27
|
+
(0, _validateCors.validateCors)(mockServerConfig.cors);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
throw new Error(`Validation Error: configuration.${error.message} does not match the API schema. Click here to see correct type: https://github.com/siberiacancode/mock-config-server`);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
exports.validateMockServerConfig = validateMockServerConfig;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const validatePort: (port: unknown) => void;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.validatePort = void 0;
|
|
7
|
+
const validatePort = port => {
|
|
8
|
+
if (typeof port !== 'number' && typeof port !== 'undefined') {
|
|
9
|
+
throw new Error('port');
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
exports.validatePort = validatePort;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const validateRestConfig: (restConfig: unknown) => void;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.validateRestConfig = void 0;
|
|
7
|
+
var _helpers = require("../../../src/utils/helpers");
|
|
8
|
+
var _validateBaseUrl = require("../validateBaseUrl/validateBaseUrl");
|
|
9
|
+
var _validateInterceptors = require("../validateInterceptors/validateInterceptors");
|
|
10
|
+
var _validateRoutes = require("./validateRoutes/validateRoutes");
|
|
11
|
+
const validateConfigs = configs => {
|
|
12
|
+
const isConfigsArray = Array.isArray(configs);
|
|
13
|
+
if (isConfigsArray) {
|
|
14
|
+
configs.forEach((config, index) => {
|
|
15
|
+
const {
|
|
16
|
+
path,
|
|
17
|
+
method
|
|
18
|
+
} = config;
|
|
19
|
+
const isPathStringWithLeadingSlash = typeof path === 'string' && path.startsWith('/');
|
|
20
|
+
if (!isPathStringWithLeadingSlash && !(path instanceof RegExp)) {
|
|
21
|
+
throw new Error(`configs[${index}].path`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// ✅ important:
|
|
25
|
+
// compare without 'toLowerCase' because Express methods names is case-sensitive
|
|
26
|
+
const allowedMethods = ['get', 'post', 'delete', 'put', 'patch', 'options'];
|
|
27
|
+
const isMethodAllowed = typeof method === 'string' && allowedMethods.includes(method);
|
|
28
|
+
if (!isMethodAllowed) {
|
|
29
|
+
throw new Error(`configs[${index}].method`);
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
(0, _validateRoutes.validateRoutes)(config.routes, method);
|
|
33
|
+
(0, _validateInterceptors.validateInterceptors)(config.interceptors);
|
|
34
|
+
} catch (error) {
|
|
35
|
+
throw new Error(`configs[${index}].${error.message}`);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
throw new Error('configs');
|
|
41
|
+
};
|
|
42
|
+
const validateRestConfig = restConfig => {
|
|
43
|
+
const isRestConfigObject = (0, _helpers.isPlainObject)(restConfig);
|
|
44
|
+
if (isRestConfigObject) {
|
|
45
|
+
try {
|
|
46
|
+
(0, _validateBaseUrl.validateBaseUrl)(restConfig.baseUrl);
|
|
47
|
+
validateConfigs(restConfig.configs);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
throw new Error(`rest.${error.message}`);
|
|
50
|
+
}
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (typeof restConfig !== 'undefined') {
|
|
54
|
+
throw new Error('rest');
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
exports.validateRestConfig = validateRestConfig;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.validateRoutes = void 0;
|
|
7
|
+
var _helpers = require("../../../../src/utils/helpers");
|
|
8
|
+
var _helpers2 = require("../../helpers");
|
|
9
|
+
var _validateInterceptors = require("../../validateInterceptors/validateInterceptors");
|
|
10
|
+
const ALLOWED_ENTITIES_BY_METHOD = {
|
|
11
|
+
get: ['headers', 'cookies', 'query', 'params'],
|
|
12
|
+
delete: ['headers', 'cookies', 'query', 'params'],
|
|
13
|
+
post: ['headers', 'cookies', 'query', 'params', 'body'],
|
|
14
|
+
put: ['headers', 'cookies', 'query', 'params', 'body'],
|
|
15
|
+
patch: ['headers', 'cookies', 'query', 'params', 'body'],
|
|
16
|
+
options: ['headers', 'cookies', 'query', 'params']
|
|
17
|
+
};
|
|
18
|
+
const validateEntity = (entity, entityName) => {
|
|
19
|
+
const {
|
|
20
|
+
checkMode: topLevelCheckMode,
|
|
21
|
+
value: topLevelValue
|
|
22
|
+
} = (0, _helpers.convertToEntityDescriptor)(entity);
|
|
23
|
+
const isBody = entityName === 'body';
|
|
24
|
+
const isTopLevelDescriptor = (0, _helpers.isEntityDescriptor)(entity);
|
|
25
|
+
if (isTopLevelDescriptor && isBody) {
|
|
26
|
+
if (!(0, _helpers2.isCheckModeValid)(topLevelCheckMode, 'body')) {
|
|
27
|
+
throw new Error('body.checkMode');
|
|
28
|
+
}
|
|
29
|
+
if (!(0, _helpers2.isDescriptorValueValid)(topLevelCheckMode, topLevelValue, 'body')) {
|
|
30
|
+
const errorMessage = 'body.value';
|
|
31
|
+
throw new Error(errorMessage);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const isEntityObject = (0, _helpers.isPlainObject)(entity) && !(entity instanceof RegExp);
|
|
35
|
+
const isEntityArray = Array.isArray(entity) && isBody;
|
|
36
|
+
if (isEntityObject || isEntityArray) {
|
|
37
|
+
Object.entries(topLevelValue).forEach(([key, valueOrDescriptor]) => {
|
|
38
|
+
const {
|
|
39
|
+
checkMode,
|
|
40
|
+
value
|
|
41
|
+
} = (0, _helpers.convertToEntityDescriptor)(valueOrDescriptor);
|
|
42
|
+
if (!(0, _helpers2.isCheckModeValid)(checkMode)) {
|
|
43
|
+
throw new Error(`${entityName}.${key}.checkMode`);
|
|
44
|
+
}
|
|
45
|
+
const isDescriptor = (0, _helpers.isEntityDescriptor)(valueOrDescriptor);
|
|
46
|
+
const errorMessage = `${entityName}.${key}${isDescriptor ? '.value' : ''}`;
|
|
47
|
+
const isValueArray = Array.isArray(value);
|
|
48
|
+
if (isValueArray && !isBody) {
|
|
49
|
+
value.forEach((element, index) => {
|
|
50
|
+
if (!(0, _helpers2.isDescriptorValueValid)(checkMode, element)) {
|
|
51
|
+
throw new Error(`${errorMessage}[${index}]`);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (!(0, _helpers2.isDescriptorValueValid)(checkMode, value)) {
|
|
57
|
+
throw new Error(errorMessage);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
throw new Error(entityName);
|
|
63
|
+
};
|
|
64
|
+
const validateEntities = (entities, method) => {
|
|
65
|
+
const isEntitiesObject = (0, _helpers.isPlainObject)(entities);
|
|
66
|
+
if (isEntitiesObject) {
|
|
67
|
+
Object.keys(entities).forEach(entityName => {
|
|
68
|
+
const isEntityAllowed = ALLOWED_ENTITIES_BY_METHOD[method].includes(entityName);
|
|
69
|
+
if (!isEntityAllowed) {
|
|
70
|
+
throw new Error(`entities.${entityName}`);
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
validateEntity(entities[entityName], entityName);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
throw new Error(`entities.${error.message}`);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (typeof entities !== 'undefined') {
|
|
81
|
+
throw new Error('entities');
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
const validateRoutes = (routes, method) => {
|
|
85
|
+
const isRoutesArray = Array.isArray(routes);
|
|
86
|
+
if (isRoutesArray) {
|
|
87
|
+
routes.forEach((route, index) => {
|
|
88
|
+
const isRouteObject = (0, _helpers.isPlainObject)(route);
|
|
89
|
+
if (isRouteObject) {
|
|
90
|
+
const isRouteHasDataProperty = ('data' in route);
|
|
91
|
+
if (!isRouteHasDataProperty) {
|
|
92
|
+
throw new Error(`routes[${index}]`);
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
validateEntities(route.entities, method);
|
|
96
|
+
(0, _validateInterceptors.validateInterceptors)(route.interceptors);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
throw new Error(`routes[${index}].${error.message}`);
|
|
99
|
+
}
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
throw new Error(`routes[${index}]`);
|
|
103
|
+
});
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
throw new Error('routes');
|
|
107
|
+
};
|
|
108
|
+
exports.validateRoutes = validateRoutes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const validateStaticPath: (staticPath: unknown) => void;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.validateStaticPath = void 0;
|
|
7
|
+
var _helpers = require("../../../src/utils/helpers");
|
|
8
|
+
const validateStaticPath = staticPath => {
|
|
9
|
+
const isStaticPathArray = Array.isArray(staticPath);
|
|
10
|
+
if (isStaticPathArray) {
|
|
11
|
+
staticPath.forEach((staticPathElement, index) => {
|
|
12
|
+
const isStaticPathElementObject = (0, _helpers.isPlainObject)(staticPathElement);
|
|
13
|
+
if (isStaticPathElementObject) {
|
|
14
|
+
const {
|
|
15
|
+
prefix,
|
|
16
|
+
path
|
|
17
|
+
} = staticPathElement;
|
|
18
|
+
if (typeof prefix !== 'string' || !prefix.startsWith('/')) {
|
|
19
|
+
throw new Error(`staticPath[${index}].prefix`);
|
|
20
|
+
}
|
|
21
|
+
if (typeof path !== 'string' || !path.startsWith('/')) {
|
|
22
|
+
throw new Error(`staticPath[${index}].path`);
|
|
23
|
+
}
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (typeof staticPathElement !== 'string' || !staticPathElement.startsWith('/')) {
|
|
27
|
+
throw new Error(`staticPath[${index}]`);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const isStaticPathObject = (0, _helpers.isPlainObject)(staticPath);
|
|
33
|
+
if (isStaticPathObject) {
|
|
34
|
+
const {
|
|
35
|
+
prefix,
|
|
36
|
+
path
|
|
37
|
+
} = staticPath;
|
|
38
|
+
if (typeof prefix !== 'string' || !prefix.startsWith('/')) {
|
|
39
|
+
throw new Error('staticPath.prefix');
|
|
40
|
+
}
|
|
41
|
+
if (typeof path !== 'string' || !path.startsWith('/')) {
|
|
42
|
+
throw new Error('staticPath.path');
|
|
43
|
+
}
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (typeof staticPath !== 'string' && typeof staticPath !== 'undefined') {
|
|
47
|
+
throw new Error('staticPath');
|
|
48
|
+
}
|
|
49
|
+
if (typeof staticPath === 'string' && !staticPath.startsWith('/')) {
|
|
50
|
+
throw new Error('staticPath');
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
exports.validateStaticPath = validateStaticPath;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createDatabaseRoutes = void 0;
|
|
7
|
+
var _helpers = require("./helpers");
|
|
8
|
+
var _storages = require("./storages");
|
|
9
|
+
const isVariableJsonFile = variable => typeof variable === 'string' && variable.endsWith('.json');
|
|
10
|
+
const createDatabaseRoutes = (router, {
|
|
11
|
+
data,
|
|
12
|
+
routes
|
|
13
|
+
}) => {
|
|
14
|
+
if (routes) {
|
|
15
|
+
const storage = isVariableJsonFile(routes) ? new _storages.FileStorage(routes) : new _storages.MemoryStorage(routes);
|
|
16
|
+
(0, _helpers.createRewrittenDatabaseRoutes)(router, storage.read());
|
|
17
|
+
router.route('/__routes').get((_request, response) => {
|
|
18
|
+
response.json(storage.read());
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
const storage = isVariableJsonFile(data) ? new _storages.FileStorage(data) : new _storages.MemoryStorage(data);
|
|
22
|
+
const {
|
|
23
|
+
shallowDatabase,
|
|
24
|
+
nestedDatabase
|
|
25
|
+
} = (0, _helpers.splitDatabaseByNesting)(storage.read());
|
|
26
|
+
(0, _helpers.createShallowDatabaseRoutes)(router, shallowDatabase, storage);
|
|
27
|
+
(0, _helpers.createNestedDatabaseRoutes)(router, nestedDatabase, storage);
|
|
28
|
+
router.route('/__db').get((_request, response) => {
|
|
29
|
+
response.json(storage.read());
|
|
30
|
+
});
|
|
31
|
+
return router;
|
|
32
|
+
};
|
|
33
|
+
exports.createDatabaseRoutes = createDatabaseRoutes;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createNewId = void 0;
|
|
7
|
+
const createNewId = array => {
|
|
8
|
+
let maxId = -1;
|
|
9
|
+
for (let i = 0; i < array.length; i += 1) {
|
|
10
|
+
if (typeof array[i].id === 'number' && array[i].id > maxId) {
|
|
11
|
+
maxId = array[i].id;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return maxId + 1;
|
|
15
|
+
};
|
|
16
|
+
exports.createNewId = createNewId;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _createNewId = require("./createNewId/createNewId");
|
|
7
|
+
Object.keys(_createNewId).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _createNewId[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _createNewId[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
var _findIndexById = require("./findIndexById/findIndexById");
|
|
18
|
+
Object.keys(_findIndexById).forEach(function (key) {
|
|
19
|
+
if (key === "default" || key === "__esModule") return;
|
|
20
|
+
if (key in exports && exports[key] === _findIndexById[key]) return;
|
|
21
|
+
Object.defineProperty(exports, key, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function () {
|
|
24
|
+
return _findIndexById[key];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
var _isIndex = require("./isIndex/isIndex");
|
|
29
|
+
Object.keys(_isIndex).forEach(function (key) {
|
|
30
|
+
if (key === "default" || key === "__esModule") return;
|
|
31
|
+
if (key in exports && exports[key] === _isIndex[key]) return;
|
|
32
|
+
Object.defineProperty(exports, key, {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function () {
|
|
35
|
+
return _isIndex[key];
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const isIndex: (value: any) => value is number;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { IRouter } from 'express';
|
|
2
|
+
import type { NestedDatabase } from '../../../../../utils/types';
|
|
3
|
+
import type { MemoryStorage } from '../../storages';
|
|
4
|
+
export declare const createNestedDatabaseRoutes: (router: IRouter, database: NestedDatabase, storage: MemoryStorage<NestedDatabase>) => IRouter;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createNestedDatabaseRoutes = void 0;
|
|
7
|
+
var _array = require("../array");
|
|
8
|
+
const createNestedDatabaseRoutes = (router, database, storage) => {
|
|
9
|
+
Object.keys(database).forEach(key => {
|
|
10
|
+
const collectionPath = `/${key}`;
|
|
11
|
+
const itemPath = `/${key}/:id`;
|
|
12
|
+
router.route(collectionPath).get((_request, response) => {
|
|
13
|
+
// ✅ important:
|
|
14
|
+
// set 'Cache-Control' header for explicit browsers response revalidate
|
|
15
|
+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
|
|
16
|
+
response.set('Cache-control', 'max-age=0, must-revalidate');
|
|
17
|
+
response.json(storage.read(key));
|
|
18
|
+
});
|
|
19
|
+
router.route(collectionPath).post((request, response) => {
|
|
20
|
+
const collection = storage.read(key);
|
|
21
|
+
const newResourceId = (0, _array.createNewId)(collection);
|
|
22
|
+
const newResource = {
|
|
23
|
+
...request.body,
|
|
24
|
+
id: newResourceId
|
|
25
|
+
};
|
|
26
|
+
storage.write([key, collection.length], newResource);
|
|
27
|
+
response.set('Location', `${request.url}/${newResourceId}`);
|
|
28
|
+
response.status(201).json(newResource);
|
|
29
|
+
});
|
|
30
|
+
router.route(itemPath).get((request, response) => {
|
|
31
|
+
const currentResourceCollection = storage.read(key);
|
|
32
|
+
const currentResourceIndex = (0, _array.findIndexById)(currentResourceCollection, request.params.id);
|
|
33
|
+
if (currentResourceIndex === -1) {
|
|
34
|
+
response.status(404).end();
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ✅ important:
|
|
39
|
+
// set 'Cache-Control' header for explicit browsers response revalidate
|
|
40
|
+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
|
|
41
|
+
response.set('Cache-control', 'max-age=0, must-revalidate');
|
|
42
|
+
response.json(storage.read([key, currentResourceIndex]));
|
|
43
|
+
});
|
|
44
|
+
router.route(itemPath).put((request, response) => {
|
|
45
|
+
const currentResourceCollection = storage.read(key);
|
|
46
|
+
const currentResourceIndex = (0, _array.findIndexById)(currentResourceCollection, request.params.id);
|
|
47
|
+
if (currentResourceIndex === -1) {
|
|
48
|
+
response.status(404).end();
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const currentResource = storage.read([key, currentResourceIndex]);
|
|
52
|
+
const updatedResource = {
|
|
53
|
+
...request.body,
|
|
54
|
+
id: currentResource.id
|
|
55
|
+
};
|
|
56
|
+
storage.write([key, currentResourceIndex], updatedResource);
|
|
57
|
+
response.json(updatedResource);
|
|
58
|
+
});
|
|
59
|
+
router.route(itemPath).patch((request, response) => {
|
|
60
|
+
const currentResourceCollection = storage.read(key);
|
|
61
|
+
const currentResourceIndex = (0, _array.findIndexById)(currentResourceCollection, request.params.id);
|
|
62
|
+
if (currentResourceIndex === -1) {
|
|
63
|
+
response.status(404).end();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const currentResource = storage.read([key, currentResourceIndex]);
|
|
67
|
+
const updatedResource = {
|
|
68
|
+
...currentResource,
|
|
69
|
+
...request.body,
|
|
70
|
+
id: currentResource.id
|
|
71
|
+
};
|
|
72
|
+
storage.write([key, currentResourceIndex], updatedResource);
|
|
73
|
+
response.json(updatedResource);
|
|
74
|
+
});
|
|
75
|
+
router.route(itemPath).delete((request, response) => {
|
|
76
|
+
const currentResourceCollection = storage.read(key);
|
|
77
|
+
const currentResourceIndex = (0, _array.findIndexById)(currentResourceCollection, request.params.id);
|
|
78
|
+
if (currentResourceIndex === -1) {
|
|
79
|
+
response.status(404).end();
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
storage.delete([key, currentResourceIndex]);
|
|
83
|
+
response.status(204).end();
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
return router;
|
|
87
|
+
};
|
|
88
|
+
exports.createNestedDatabaseRoutes = createNestedDatabaseRoutes;
|