@strapi/strapi 4.0.0-beta.2 → 4.0.0-beta.20
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 +12 -12
- package/bin/strapi.js +6 -1
- package/lib/Strapi.js +33 -21
- package/lib/commands/build.js +1 -1
- package/lib/commands/content-types/list.js +3 -5
- package/lib/commands/develop.js +8 -10
- package/lib/commands/generate-template.js +4 -5
- package/lib/commands/hooks/list.js +3 -5
- package/lib/commands/middlewares/list.js +3 -5
- package/lib/commands/new.js +3 -1
- package/lib/commands/policies/list.js +3 -5
- package/lib/commands/services/list.js +22 -0
- package/lib/commands/watchAdmin.js +4 -4
- package/lib/core/app-configuration/index.js +5 -10
- package/lib/core/bootstrap.js +2 -2
- package/lib/core/domain/module/index.js +3 -1
- package/lib/core/domain/module/validation.js +1 -4
- package/lib/core/loaders/admin.js +2 -2
- package/lib/core/loaders/apis.js +3 -1
- package/lib/core/loaders/plugins/get-enabled-plugins.js +25 -9
- package/lib/core/loaders/plugins/index.js +21 -7
- package/lib/core/loaders/src-index.js +1 -0
- package/lib/core/registries/apis.js +2 -16
- package/lib/core/registries/content-types.js +50 -6
- package/lib/core/registries/controllers.d.ts +7 -0
- package/lib/core/registries/controllers.js +74 -3
- package/lib/core/registries/hooks.d.ts +20 -0
- package/lib/core/registries/hooks.js +57 -7
- package/lib/core/registries/middlewares.d.ts +5 -0
- package/lib/core/registries/middlewares.js +61 -2
- package/lib/core/registries/policies.d.ts +9 -0
- package/lib/core/registries/policies.js +57 -6
- package/lib/core/registries/services.d.ts +7 -0
- package/lib/core/registries/services.js +67 -11
- package/lib/core-api/controller/collection-type.js +38 -11
- package/lib/core-api/controller/index.d.ts +25 -0
- package/lib/core-api/controller/index.js +30 -11
- package/lib/core-api/controller/single-type.js +26 -7
- package/lib/core-api/routes/index.js +71 -0
- package/lib/core-api/service/collection-type.js +8 -12
- package/lib/core-api/service/index.d.ts +21 -0
- package/lib/core-api/service/index.js +9 -19
- package/lib/core-api/service/pagination.js +7 -2
- package/lib/core-api/service/single-type.js +12 -11
- package/lib/factories.d.ts +48 -0
- package/lib/factories.js +84 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +5 -1
- package/lib/middlewares/body.js +33 -0
- package/lib/middlewares/compression.js +1 -1
- package/lib/middlewares/cors.js +3 -2
- package/lib/middlewares/errors.js +24 -119
- package/lib/middlewares/favicon.js +3 -3
- package/lib/middlewares/index.d.ts +2 -1
- package/lib/middlewares/index.js +4 -2
- package/lib/middlewares/ip.js +1 -1
- package/lib/middlewares/logger.js +1 -1
- package/lib/middlewares/powered-by.js +4 -2
- package/lib/middlewares/public/index.js +4 -7
- package/lib/middlewares/query.js +46 -0
- package/lib/middlewares/responses.js +2 -2
- package/lib/middlewares/security.js +29 -3
- package/lib/middlewares/session/index.js +1 -1
- package/lib/services/auth/index.js +1 -6
- package/lib/services/entity-service/attributes/index.js +31 -0
- package/lib/services/entity-service/attributes/transforms.js +20 -0
- package/lib/services/entity-service/components.js +2 -3
- package/lib/services/entity-service/index.d.ts +1 -1
- package/lib/services/entity-service/index.js +83 -27
- package/lib/services/entity-service/params.js +37 -87
- package/lib/services/entity-validator/index.js +76 -43
- package/lib/services/entity-validator/validators.js +129 -43
- package/lib/services/errors.js +77 -0
- package/lib/services/metrics/index.js +37 -35
- package/lib/services/server/compose-endpoint.js +39 -11
- package/lib/services/server/content-api.js +1 -1
- package/lib/services/server/index.js +4 -9
- package/lib/services/server/koa.js +64 -0
- package/lib/services/server/middleware.js +8 -1
- package/lib/services/server/policy.js +8 -10
- package/lib/services/server/register-middlewares.js +7 -2
- package/lib/services/server/register-routes.js +1 -3
- package/lib/services/server/routing.js +13 -0
- package/lib/utils/get-dirs.js +1 -0
- package/lib/utils/signals.js +24 -0
- package/package.json +22 -17
- package/lib/core/app-configuration/load-functions.js +0 -28
- package/lib/core-api/index.js +0 -39
- package/lib/middlewares/request.js +0 -74
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const { join } = require('path');
|
|
4
|
-
const
|
|
4
|
+
const fse = require('fs-extra');
|
|
5
5
|
const { defaultsDeep, getOr, get } = require('lodash/fp');
|
|
6
6
|
const { env } = require('@strapi/utils');
|
|
7
7
|
const loadConfigFile = require('../../app-configuration/load-config-file');
|
|
@@ -26,7 +26,7 @@ const defaultPlugin = {
|
|
|
26
26
|
|
|
27
27
|
const applyUserExtension = async plugins => {
|
|
28
28
|
const extensionsDir = strapi.dirs.extensions;
|
|
29
|
-
if (!
|
|
29
|
+
if (!(await fse.pathExists(extensionsDir))) {
|
|
30
30
|
return;
|
|
31
31
|
}
|
|
32
32
|
|
|
@@ -39,7 +39,11 @@ const applyUserExtension = async plugins => {
|
|
|
39
39
|
for (const ctName in plugin.contentTypes) {
|
|
40
40
|
const extendedSchema = get([pluginName, 'content-types', ctName, 'schema'], extendedSchemas);
|
|
41
41
|
if (extendedSchema) {
|
|
42
|
-
plugin.contentTypes[ctName].schema =
|
|
42
|
+
plugin.contentTypes[ctName].schema = Object.assign(
|
|
43
|
+
{},
|
|
44
|
+
plugin.contentTypes[ctName].schema,
|
|
45
|
+
extendedSchema
|
|
46
|
+
);
|
|
43
47
|
}
|
|
44
48
|
}
|
|
45
49
|
// second: execute strapi-server extension
|
|
@@ -61,9 +65,9 @@ const formatContentTypes = plugins => {
|
|
|
61
65
|
}
|
|
62
66
|
};
|
|
63
67
|
|
|
64
|
-
const applyUserConfig = plugins => {
|
|
68
|
+
const applyUserConfig = async plugins => {
|
|
65
69
|
const userPluginConfigPath = join(strapi.dirs.config, 'plugins.js');
|
|
66
|
-
const userPluginsConfig =
|
|
70
|
+
const userPluginsConfig = (await fse.pathExists(userPluginConfigPath))
|
|
67
71
|
? loadConfigFile(userPluginConfigPath)
|
|
68
72
|
: {};
|
|
69
73
|
|
|
@@ -90,14 +94,24 @@ const loadPlugins = async strapi => {
|
|
|
90
94
|
|
|
91
95
|
const enabledPlugins = await getEnabledPlugins(strapi);
|
|
92
96
|
|
|
97
|
+
strapi.config.set('enabledPlugins', enabledPlugins);
|
|
98
|
+
|
|
93
99
|
for (const pluginName in enabledPlugins) {
|
|
94
100
|
const enabledPlugin = enabledPlugins[pluginName];
|
|
95
|
-
|
|
101
|
+
|
|
102
|
+
const serverEntrypointPath = join(enabledPlugin.pathToPlugin, 'strapi-server.js');
|
|
103
|
+
|
|
104
|
+
// only load plugins with a server entrypoint
|
|
105
|
+
if (!(await fse.pathExists(serverEntrypointPath))) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const pluginServer = loadConfigFile(serverEntrypointPath);
|
|
96
110
|
plugins[pluginName] = defaultsDeep(defaultPlugin, pluginServer);
|
|
97
111
|
}
|
|
98
112
|
|
|
99
113
|
// TODO: validate plugin format
|
|
100
|
-
applyUserConfig(plugins);
|
|
114
|
+
await applyUserConfig(plugins);
|
|
101
115
|
await applyUserExtension(plugins);
|
|
102
116
|
formatContentTypes(plugins);
|
|
103
117
|
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const { has } = require('lodash/fp');
|
|
4
|
-
const { createCoreApi } = require('../../core-api');
|
|
5
4
|
|
|
6
5
|
const apisRegistry = strapi => {
|
|
7
6
|
const apis = {};
|
|
@@ -18,22 +17,9 @@ const apisRegistry = strapi => {
|
|
|
18
17
|
throw new Error(`API ${apiName} has already been registered.`);
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
const
|
|
20
|
+
const api = strapi.container.get('modules').add(`api::${apiName}`, apiConfig);
|
|
22
21
|
|
|
23
|
-
|
|
24
|
-
const contentType = apiInstance.contentTypes[ctName];
|
|
25
|
-
|
|
26
|
-
const { service, controller } = createCoreApi({
|
|
27
|
-
model: contentType,
|
|
28
|
-
api: apiInstance,
|
|
29
|
-
strapi,
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
strapi.container.get('services').set(`api::${apiName}.${ctName}`, service);
|
|
33
|
-
strapi.container.get('controllers').set(`api::${apiName}.${ctName}`, controller);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
apis[apiName] = apiInstance;
|
|
22
|
+
apis[apiName] = api;
|
|
37
23
|
|
|
38
24
|
return apis[apiName];
|
|
39
25
|
},
|
|
@@ -20,32 +20,76 @@ const contentTypesRegistry = () => {
|
|
|
20
20
|
const contentTypes = {};
|
|
21
21
|
|
|
22
22
|
return {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Returns this list of registered contentTypes uids
|
|
25
|
+
* @returns {string[]}
|
|
26
|
+
*/
|
|
27
|
+
keys() {
|
|
28
|
+
return Object.keys(contentTypes);
|
|
25
29
|
},
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Returns the instance of a contentType. Instantiate the contentType if not already done
|
|
33
|
+
* @param {string} uid
|
|
34
|
+
* @returns
|
|
35
|
+
*/
|
|
36
|
+
get(uid) {
|
|
37
|
+
return contentTypes[uid];
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Returns a map with all the contentTypes in a namespace
|
|
42
|
+
* @param {string} namespace
|
|
43
|
+
*/
|
|
26
44
|
getAll(namespace) {
|
|
27
45
|
return pickBy((_, uid) => hasNamespace(uid, namespace))(contentTypes);
|
|
28
46
|
},
|
|
29
|
-
add(namespace, rawContentTypes) {
|
|
30
|
-
validateKeySameToSingularName(rawContentTypes);
|
|
31
47
|
|
|
32
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Registers a contentType
|
|
50
|
+
* @param {string} uid
|
|
51
|
+
* @param {Object} contentType
|
|
52
|
+
*/
|
|
53
|
+
set(uid, contentType) {
|
|
54
|
+
contentTypes[uid] = contentType;
|
|
55
|
+
return this;
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Registers a map of contentTypes for a specific namespace
|
|
60
|
+
* @param {string} namespace
|
|
61
|
+
* @param {{ [key: string]: Object }} newContentTypes
|
|
62
|
+
*/
|
|
63
|
+
add(namespace, newContentTypes) {
|
|
64
|
+
validateKeySameToSingularName(newContentTypes);
|
|
65
|
+
|
|
66
|
+
for (const rawCtName in newContentTypes) {
|
|
33
67
|
const uid = addNamespace(rawCtName, namespace);
|
|
34
68
|
|
|
35
69
|
if (has(uid, contentTypes)) {
|
|
36
70
|
throw new Error(`Content-type ${uid} has already been registered.`);
|
|
37
71
|
}
|
|
38
72
|
|
|
39
|
-
contentTypes[uid] = createContentType(uid,
|
|
73
|
+
contentTypes[uid] = createContentType(uid, newContentTypes[rawCtName]);
|
|
40
74
|
}
|
|
41
75
|
},
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Wraps a contentType to extend it
|
|
79
|
+
* @param {string} uid
|
|
80
|
+
* @param {(contentType: Object) => Object} extendFn
|
|
81
|
+
*/
|
|
42
82
|
extend(ctUID, extendFn) {
|
|
43
83
|
const currentContentType = this.get(ctUID);
|
|
84
|
+
|
|
44
85
|
if (!currentContentType) {
|
|
45
86
|
throw new Error(`Content-Type ${ctUID} doesn't exist`);
|
|
46
87
|
}
|
|
88
|
+
|
|
47
89
|
const newContentType = extendFn(currentContentType);
|
|
48
90
|
contentTypes[ctUID] = newContentType;
|
|
91
|
+
|
|
92
|
+
return this;
|
|
49
93
|
},
|
|
50
94
|
};
|
|
51
95
|
};
|
|
@@ -3,20 +3,80 @@
|
|
|
3
3
|
const { pickBy, has } = require('lodash/fp');
|
|
4
4
|
const { addNamespace, hasNamespace } = require('../utils');
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {import('./controllers').Controller} Controller
|
|
8
|
+
* @typedef {import('./controllers').ControllerFactory} ControllerFactory
|
|
9
|
+
*/
|
|
10
|
+
|
|
6
11
|
const controllersRegistry = () => {
|
|
7
12
|
const controllers = {};
|
|
13
|
+
const instances = {};
|
|
8
14
|
|
|
9
15
|
return {
|
|
16
|
+
/**
|
|
17
|
+
* Returns this list of registered controllers uids
|
|
18
|
+
* @returns {string[]}
|
|
19
|
+
*/
|
|
20
|
+
keys() {
|
|
21
|
+
return Object.keys(controllers);
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Returns the instance of a controller. Instantiate the controller if not already done
|
|
26
|
+
* @param {string} uid
|
|
27
|
+
* @returns {Controller}
|
|
28
|
+
*/
|
|
10
29
|
get(uid) {
|
|
11
|
-
|
|
30
|
+
if (instances[uid]) {
|
|
31
|
+
return instances[uid];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const controller = controllers[uid];
|
|
35
|
+
|
|
36
|
+
if (controller) {
|
|
37
|
+
instances[uid] = typeof controller === 'function' ? controller({ strapi }) : controller;
|
|
38
|
+
return instances[uid];
|
|
39
|
+
}
|
|
12
40
|
},
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Returns a map with all the controller in a namespace
|
|
44
|
+
* @param {string} namespace
|
|
45
|
+
* @returns {{ [key: string]: Controller }}
|
|
46
|
+
*/
|
|
13
47
|
getAll(namespace) {
|
|
14
|
-
|
|
48
|
+
const filteredControllers = pickBy((_, uid) => hasNamespace(uid, namespace))(controllers);
|
|
49
|
+
|
|
50
|
+
const map = {};
|
|
51
|
+
for (const uid in filteredControllers) {
|
|
52
|
+
Object.defineProperty(map, uid, {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
get: () => {
|
|
55
|
+
return this.get(uid);
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return map;
|
|
15
61
|
},
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Registers a controller
|
|
65
|
+
* @param {string} uid
|
|
66
|
+
* @param {Controller} controller
|
|
67
|
+
*/
|
|
16
68
|
set(uid, value) {
|
|
17
69
|
controllers[uid] = value;
|
|
70
|
+
delete instances[uid];
|
|
18
71
|
return this;
|
|
19
72
|
},
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Registers a map of controllers for a specific namespace
|
|
76
|
+
* @param {string} namespace
|
|
77
|
+
* @param {{ [key: string]: Controller|ControllerFactory }} newControllers
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
20
80
|
add(namespace, newControllers) {
|
|
21
81
|
for (const controllerName in newControllers) {
|
|
22
82
|
const controller = newControllers[controllerName];
|
|
@@ -27,15 +87,26 @@ const controllersRegistry = () => {
|
|
|
27
87
|
}
|
|
28
88
|
controllers[uid] = controller;
|
|
29
89
|
}
|
|
90
|
+
|
|
30
91
|
return this;
|
|
31
92
|
},
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Wraps a controller to extend it
|
|
96
|
+
* @param {string} uid
|
|
97
|
+
* @param {(controller: Controller) => Controller} extendFn
|
|
98
|
+
*/
|
|
32
99
|
extend(controllerUID, extendFn) {
|
|
33
100
|
const currentController = this.get(controllerUID);
|
|
101
|
+
|
|
34
102
|
if (!currentController) {
|
|
35
103
|
throw new Error(`Controller ${controllerUID} doesn't exist`);
|
|
36
104
|
}
|
|
105
|
+
|
|
37
106
|
const newController = extendFn(currentController);
|
|
38
|
-
|
|
107
|
+
instances[controllerUID] = newController;
|
|
108
|
+
|
|
109
|
+
return this;
|
|
39
110
|
},
|
|
40
111
|
};
|
|
41
112
|
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
|
|
2
|
+
type Handler = (context: any) => any;
|
|
3
|
+
|
|
4
|
+
type AsyncHook = {
|
|
5
|
+
handlers: Handler[];
|
|
6
|
+
register(handler: Handler): this;
|
|
7
|
+
delete(handler: Handler): this;
|
|
8
|
+
call(): Promise<void>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
type SyncHook = {
|
|
13
|
+
get handlers(): Handler[];
|
|
14
|
+
register(handler: Handler): this;
|
|
15
|
+
delete(handler: Handler): this;
|
|
16
|
+
call(): void;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
export type Hook = AsyncHook|SyncHook
|
|
@@ -1,26 +1,58 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const { pickBy
|
|
3
|
+
const { pickBy } = require('lodash/fp');
|
|
4
4
|
const { addNamespace, hasNamespace } = require('../utils');
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {import('./hooks').Hook} Hook
|
|
8
|
+
*/
|
|
9
|
+
|
|
6
10
|
const hooksRegistry = () => {
|
|
7
11
|
const hooks = {};
|
|
8
12
|
|
|
9
13
|
return {
|
|
10
|
-
|
|
11
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Returns this list of registered hooks uids
|
|
16
|
+
* @returns {string[]}
|
|
17
|
+
*/
|
|
18
|
+
keys() {
|
|
19
|
+
return Object.keys(hooks);
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Returns the instance of a hook.
|
|
24
|
+
* @param {string} uid
|
|
25
|
+
* @returns {Hook}
|
|
26
|
+
*/
|
|
27
|
+
get(uid) {
|
|
28
|
+
return hooks[uid];
|
|
12
29
|
},
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Returns a map with all the hooks in a namespace
|
|
33
|
+
* @param {string} namespace
|
|
34
|
+
* @returns {{ [key: string]: Hook }}
|
|
35
|
+
*/
|
|
13
36
|
getAll(namespace) {
|
|
14
37
|
return pickBy((_, uid) => hasNamespace(uid, namespace))(hooks);
|
|
15
38
|
},
|
|
16
|
-
set(uid, hook) {
|
|
17
|
-
if (has(uid, hooks)) {
|
|
18
|
-
throw new Error(`hook ${uid} has already been registered.`);
|
|
19
|
-
}
|
|
20
39
|
|
|
40
|
+
/**
|
|
41
|
+
* Registers a hook
|
|
42
|
+
* @param {string} uid
|
|
43
|
+
* @param {Hook} hook
|
|
44
|
+
*/
|
|
45
|
+
set(uid, hook) {
|
|
21
46
|
hooks[uid] = hook;
|
|
22
47
|
return this;
|
|
23
48
|
},
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Registers a map of hooks for a specific namespace
|
|
52
|
+
* @param {string} namespace
|
|
53
|
+
* @param {{ [key: string]: Hook }} newHooks
|
|
54
|
+
* @returns
|
|
55
|
+
*/
|
|
24
56
|
add(namespace, hooks) {
|
|
25
57
|
for (const hookName in hooks) {
|
|
26
58
|
const hook = hooks[hookName];
|
|
@@ -31,6 +63,24 @@ const hooksRegistry = () => {
|
|
|
31
63
|
|
|
32
64
|
return this;
|
|
33
65
|
},
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Wraps a hook to extend it
|
|
69
|
+
* @param {string} uid
|
|
70
|
+
* @param {(hook: Hook) => Hook} extendFn
|
|
71
|
+
*/
|
|
72
|
+
extend(uid, extendFn) {
|
|
73
|
+
const currentHook = this.get(uid);
|
|
74
|
+
|
|
75
|
+
if (!currentHook) {
|
|
76
|
+
throw new Error(`Hook ${uid} doesn't exist`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const newHook = extendFn(currentHook);
|
|
80
|
+
hooks[uid] = newHook;
|
|
81
|
+
|
|
82
|
+
return this;
|
|
83
|
+
},
|
|
34
84
|
};
|
|
35
85
|
};
|
|
36
86
|
|
|
@@ -3,16 +3,57 @@
|
|
|
3
3
|
const { pickBy, has } = require('lodash/fp');
|
|
4
4
|
const { addNamespace, hasNamespace } = require('../utils');
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {import('./middlewares').Middleware} Middleware
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// TODO: move instantiation part here instead of in the server service
|
|
6
11
|
const middlewaresRegistry = () => {
|
|
7
12
|
const middlewares = {};
|
|
8
13
|
|
|
9
14
|
return {
|
|
10
|
-
|
|
11
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Returns this list of registered middlewares uids
|
|
17
|
+
* @returns {string[]}
|
|
18
|
+
*/
|
|
19
|
+
keys() {
|
|
20
|
+
return Object.keys(middlewares);
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Returns the instance of a middleware. Instantiate the middleware if not already done
|
|
25
|
+
* @param {string} uid
|
|
26
|
+
* @returns {Middleware}
|
|
27
|
+
*/
|
|
28
|
+
get(uid) {
|
|
29
|
+
return middlewares[uid];
|
|
12
30
|
},
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Returns a map with all the middlewares in a namespace
|
|
34
|
+
* @param {string} namespace
|
|
35
|
+
* @returns {{ [key: string]: Middleware }}
|
|
36
|
+
*/
|
|
13
37
|
getAll(namespace) {
|
|
14
38
|
return pickBy((_, uid) => hasNamespace(uid, namespace))(middlewares);
|
|
15
39
|
},
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Registers a middleware
|
|
43
|
+
* @param {string} uid
|
|
44
|
+
* @param {Middleware} middleware
|
|
45
|
+
*/
|
|
46
|
+
set(uid, middleware) {
|
|
47
|
+
middlewares[uid] = middleware;
|
|
48
|
+
return this;
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Registers a map of middlewares for a specific namespace
|
|
53
|
+
* @param {string} namespace
|
|
54
|
+
* @param {{ [key: string]: Middleware }} newMiddlewares
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
16
57
|
add(namespace, rawMiddlewares) {
|
|
17
58
|
for (const middlewareName in rawMiddlewares) {
|
|
18
59
|
const middleware = rawMiddlewares[middlewareName];
|
|
@@ -24,6 +65,24 @@ const middlewaresRegistry = () => {
|
|
|
24
65
|
middlewares[uid] = middleware;
|
|
25
66
|
}
|
|
26
67
|
},
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Wraps a middleware to extend it
|
|
71
|
+
* @param {string} uid
|
|
72
|
+
* @param {(middleware: Middleware) => Middleware} extendFn
|
|
73
|
+
*/
|
|
74
|
+
extend(uid, extendFn) {
|
|
75
|
+
const currentMiddleware = this.get(uid);
|
|
76
|
+
|
|
77
|
+
if (!currentMiddleware) {
|
|
78
|
+
throw new Error(`Middleware ${uid} doesn't exist`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const newMiddleware = extendFn(currentMiddleware);
|
|
82
|
+
middlewares[uid] = newMiddleware;
|
|
83
|
+
|
|
84
|
+
return this;
|
|
85
|
+
},
|
|
27
86
|
};
|
|
28
87
|
};
|
|
29
88
|
|
|
@@ -3,16 +3,57 @@
|
|
|
3
3
|
const { pickBy, has } = require('lodash/fp');
|
|
4
4
|
const { addNamespace, hasNamespace } = require('../utils');
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {import('./policies').Policy} Policy
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// TODO: move instantiation part here instead of in the policy utils
|
|
6
11
|
const policiesRegistry = () => {
|
|
7
12
|
const policies = {};
|
|
8
13
|
|
|
9
14
|
return {
|
|
10
|
-
|
|
11
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Returns this list of registered policies uids
|
|
17
|
+
* @returns {string[]}
|
|
18
|
+
*/
|
|
19
|
+
keys() {
|
|
20
|
+
return Object.keys(policies);
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Returns the instance of a policy. Instantiate the policy if not already done
|
|
25
|
+
* @param {string} uid
|
|
26
|
+
* @returns {Policy}
|
|
27
|
+
*/
|
|
28
|
+
get(uid) {
|
|
29
|
+
return policies[uid];
|
|
12
30
|
},
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Returns a map with all the policies in a namespace
|
|
34
|
+
* @param {string} namespace
|
|
35
|
+
* @returns {{ [key: string]: Policy }}
|
|
36
|
+
*/
|
|
13
37
|
getAll(namespace) {
|
|
14
38
|
return pickBy((_, uid) => hasNamespace(uid, namespace))(policies);
|
|
15
39
|
},
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Registers a policy
|
|
43
|
+
* @param {string} uid
|
|
44
|
+
* @param {Policy} policy
|
|
45
|
+
*/
|
|
46
|
+
set(uid, policy) {
|
|
47
|
+
policies[uid] = policy;
|
|
48
|
+
return this;
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Registers a map of policies for a specific namespace
|
|
53
|
+
* @param {string} namespace
|
|
54
|
+
* @param {{ [key: string]: Policy }} newPolicies
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
16
57
|
add(namespace, newPolicies) {
|
|
17
58
|
for (const policyName in newPolicies) {
|
|
18
59
|
const policy = newPolicies[policyName];
|
|
@@ -24,13 +65,23 @@ const policiesRegistry = () => {
|
|
|
24
65
|
policies[uid] = policy;
|
|
25
66
|
}
|
|
26
67
|
},
|
|
27
|
-
|
|
28
|
-
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Wraps a policy to extend it
|
|
71
|
+
* @param {string} uid
|
|
72
|
+
* @param {(policy: Policy) => Policy} extendFn
|
|
73
|
+
*/
|
|
74
|
+
extend(uid, extendFn) {
|
|
75
|
+
const currentPolicy = this.get(uid);
|
|
76
|
+
|
|
29
77
|
if (!currentPolicy) {
|
|
30
|
-
throw new Error(`Policy ${
|
|
78
|
+
throw new Error(`Policy ${uid} doesn't exist`);
|
|
31
79
|
}
|
|
80
|
+
|
|
32
81
|
const newPolicy = extendFn(currentPolicy);
|
|
33
|
-
policies[
|
|
82
|
+
policies[uid] = newPolicy;
|
|
83
|
+
|
|
84
|
+
return this;
|
|
34
85
|
},
|
|
35
86
|
};
|
|
36
87
|
};
|