@strapi/strapi 4.0.0-next.6 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -12
- package/bin/strapi.js +41 -60
- package/lib/Strapi.js +234 -114
- package/lib/commands/build.js +16 -6
- package/lib/commands/console.js +1 -1
- package/lib/commands/content-types/list.js +22 -0
- package/lib/commands/develop.js +17 -18
- package/lib/commands/generate-template.js +4 -5
- package/lib/commands/hooks/list.js +22 -0
- package/lib/commands/middlewares/list.js +22 -0
- package/lib/commands/new.js +3 -1
- package/lib/commands/policies/list.js +22 -0
- package/lib/commands/routes/list.js +28 -0
- package/lib/commands/services/list.js +22 -0
- package/lib/commands/watchAdmin.js +18 -8
- package/lib/container.js +6 -6
- package/lib/core/app-configuration/config-loader.js +1 -37
- package/lib/core/app-configuration/index.js +6 -46
- package/lib/core/app-configuration/load-config-file.js +43 -0
- package/lib/core/bootstrap.js +5 -117
- package/lib/core/domain/component/index.js +24 -0
- package/lib/core/domain/component/validator.js +29 -0
- package/lib/core/domain/content-type/index.js +140 -0
- package/lib/core/domain/content-type/validator.js +64 -0
- package/lib/core/domain/module/index.js +108 -0
- package/lib/core/domain/module/validation.js +33 -0
- package/lib/core/loaders/admin.js +16 -0
- package/lib/core/loaders/apis.js +159 -0
- package/lib/core/{load-components.js → loaders/components.js} +5 -7
- package/lib/core/loaders/index.js +11 -0
- package/lib/core/loaders/middlewares.js +36 -0
- package/lib/core/loaders/plugins/get-enabled-plugins.js +116 -0
- package/lib/core/loaders/plugins/index.js +123 -0
- package/lib/core/loaders/policies.js +28 -0
- package/lib/core/loaders/src-index.js +39 -0
- package/lib/core/registries/apis.js +29 -0
- package/lib/core/{app-configuration/config-provider.js → registries/config.js} +4 -11
- package/lib/core/registries/content-types.js +97 -0
- package/lib/core/registries/controllers.d.ts +7 -0
- package/lib/core/registries/controllers.js +114 -0
- package/lib/core/registries/hooks.d.ts +20 -0
- package/lib/core/registries/hooks.js +87 -0
- package/lib/core/registries/middlewares.d.ts +5 -0
- package/lib/core/registries/middlewares.js +89 -0
- package/lib/core/registries/modules.js +44 -0
- package/lib/core/registries/plugins.js +28 -0
- package/lib/core/registries/policies.d.ts +9 -0
- package/lib/core/registries/policies.js +89 -0
- package/lib/core/registries/services.d.ts +7 -0
- package/lib/core/registries/services.js +114 -0
- package/lib/core/utils.js +35 -0
- package/lib/core-api/controller/collection-type.js +45 -26
- package/lib/core-api/controller/index.d.ts +25 -0
- package/lib/core-api/controller/index.js +33 -11
- package/lib/core-api/controller/single-type.js +29 -15
- package/lib/core-api/controller/transform.js +62 -6
- package/lib/core-api/routes/index.js +71 -0
- package/lib/core-api/service/collection-type.js +43 -21
- package/lib/core-api/service/index.d.ts +21 -0
- package/lib/core-api/service/index.js +8 -67
- package/lib/core-api/service/pagination.js +130 -0
- package/lib/core-api/service/single-type.js +17 -19
- package/lib/factories.d.ts +48 -0
- package/lib/factories.js +84 -0
- package/lib/index.d.ts +10 -31
- package/lib/index.js +5 -1
- package/lib/middlewares/body.js +33 -0
- package/lib/middlewares/compression.js +8 -0
- package/lib/middlewares/cors.js +58 -0
- package/lib/middlewares/errors.js +40 -0
- package/lib/middlewares/favicon.js +19 -0
- package/lib/middlewares/index.d.ts +5 -0
- package/lib/middlewares/index.js +30 -117
- package/lib/middlewares/ip.js +8 -0
- package/lib/middlewares/logger.js +27 -0
- package/lib/middlewares/powered-by.js +20 -0
- package/lib/middlewares/public/index.js +98 -73
- package/lib/middlewares/query.js +46 -0
- package/lib/middlewares/response-time.js +15 -0
- package/lib/middlewares/responses.js +19 -0
- package/lib/middlewares/security.js +51 -0
- package/lib/middlewares/session/index.js +6 -6
- package/lib/migrations/draft-publish.js +57 -0
- package/lib/services/auth/index.js +87 -0
- package/lib/services/core-store.js +64 -51
- package/lib/services/cron.js +54 -0
- 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 +39 -15
- package/lib/services/entity-service/index.d.ts +91 -0
- package/lib/services/entity-service/index.js +120 -59
- package/lib/services/entity-service/params.js +52 -94
- 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/{core → services}/fs.js +10 -2
- package/lib/services/metrics/index.js +41 -38
- package/lib/services/metrics/sender.js +2 -2
- package/lib/services/server/admin-api.js +14 -0
- package/lib/services/server/api.js +36 -0
- package/lib/services/server/compose-endpoint.js +141 -0
- package/lib/services/server/content-api.js +16 -0
- package/lib/{server.js → services/server/http-server.js} +0 -0
- package/lib/services/server/index.js +127 -0
- package/lib/services/server/koa.js +64 -0
- package/lib/services/server/middleware.js +122 -0
- package/lib/services/server/policy.js +32 -0
- package/lib/services/server/register-middlewares.js +110 -0
- package/lib/services/server/register-routes.js +106 -0
- package/lib/services/server/routing.js +120 -0
- package/lib/services/utils/upload-files.js +1 -1
- package/lib/services/webhook-runner.js +1 -1
- package/lib/utils/ee.js +3 -3
- package/lib/utils/get-dirs.js +17 -0
- package/lib/utils/index.js +2 -0
- package/lib/utils/is-initialized.js +1 -1
- package/lib/utils/run-checks.js +0 -15
- package/lib/utils/signals.js +24 -0
- package/lib/utils/startup-logger.js +2 -2
- package/lib/utils/update-notifier/index.js +3 -2
- package/package.json +93 -96
- package/lib/commands/generate.js +0 -76
- package/lib/core/index.js +0 -17
- package/lib/core/load-apis.js +0 -20
- package/lib/core/load-extensions.js +0 -71
- package/lib/core/load-functions.js +0 -21
- package/lib/core/load-middlewares.js +0 -130
- package/lib/core/load-modules.js +0 -55
- package/lib/core/load-plugins.js +0 -68
- package/lib/core/load-policies.js +0 -36
- package/lib/core/walk.js +0 -27
- package/lib/core-api/index.js +0 -39
- package/lib/load/check-reserved-filename.js +0 -10
- package/lib/load/load-config-files.js +0 -22
- package/lib/load/require-file-parse.js +0 -15
- package/lib/middlewares/boom/defaults.json +0 -5
- package/lib/middlewares/boom/index.js +0 -147
- package/lib/middlewares/cors/index.js +0 -66
- package/lib/middlewares/cron/defaults.json +0 -5
- package/lib/middlewares/cron/index.js +0 -43
- package/lib/middlewares/favicon/defaults.json +0 -7
- package/lib/middlewares/favicon/index.js +0 -32
- package/lib/middlewares/gzip/defaults.json +0 -6
- package/lib/middlewares/gzip/index.js +0 -19
- package/lib/middlewares/helmet/defaults.json +0 -18
- package/lib/middlewares/helmet/index.js +0 -9
- package/lib/middlewares/ip/defaults.json +0 -7
- package/lib/middlewares/ip/index.js +0 -25
- package/lib/middlewares/language/defaults.json +0 -9
- package/lib/middlewares/language/index.js +0 -40
- package/lib/middlewares/logger/defaults.json +0 -5
- package/lib/middlewares/logger/index.js +0 -37
- package/lib/middlewares/parser/defaults.json +0 -11
- package/lib/middlewares/parser/index.js +0 -71
- package/lib/middlewares/poweredBy/defaults.json +0 -5
- package/lib/middlewares/poweredBy/index.js +0 -16
- package/lib/middlewares/public/defaults.json +0 -8
- package/lib/middlewares/responseTime/defaults.json +0 -5
- package/lib/middlewares/responseTime/index.js +0 -25
- package/lib/middlewares/responses/defaults.json +0 -5
- package/lib/middlewares/responses/index.js +0 -18
- package/lib/middlewares/router/defaults.json +0 -7
- package/lib/middlewares/router/index.js +0 -64
- package/lib/middlewares/router/utils/composeEndpoint.js +0 -25
- package/lib/middlewares/router/utils/routerChecker.js +0 -92
- package/lib/utils/get-prefixed-dependencies.js +0 -7
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { pickBy, has } = require('lodash/fp');
|
|
4
|
+
const { addNamespace, hasNamespace } = require('../utils');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {import('./middlewares').Middleware} Middleware
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// TODO: move instantiation part here instead of in the server service
|
|
11
|
+
const middlewaresRegistry = () => {
|
|
12
|
+
const middlewares = {};
|
|
13
|
+
|
|
14
|
+
return {
|
|
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];
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Returns a map with all the middlewares in a namespace
|
|
34
|
+
* @param {string} namespace
|
|
35
|
+
* @returns {{ [key: string]: Middleware }}
|
|
36
|
+
*/
|
|
37
|
+
getAll(namespace) {
|
|
38
|
+
return pickBy((_, uid) => hasNamespace(uid, namespace))(middlewares);
|
|
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
|
+
*/
|
|
57
|
+
add(namespace, rawMiddlewares) {
|
|
58
|
+
for (const middlewareName in rawMiddlewares) {
|
|
59
|
+
const middleware = rawMiddlewares[middlewareName];
|
|
60
|
+
const uid = addNamespace(middlewareName, namespace);
|
|
61
|
+
|
|
62
|
+
if (has(uid, middlewares)) {
|
|
63
|
+
throw new Error(`Middleware ${uid} has already been registered.`);
|
|
64
|
+
}
|
|
65
|
+
middlewares[uid] = middleware;
|
|
66
|
+
}
|
|
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
|
+
},
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
module.exports = middlewaresRegistry;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { pickBy, has } = require('lodash/fp');
|
|
4
|
+
const { createModule } = require('../domain/module');
|
|
5
|
+
|
|
6
|
+
const modulesRegistry = strapi => {
|
|
7
|
+
const modules = {};
|
|
8
|
+
|
|
9
|
+
return {
|
|
10
|
+
get(namespace) {
|
|
11
|
+
return modules[namespace];
|
|
12
|
+
},
|
|
13
|
+
getAll(prefix = '') {
|
|
14
|
+
return pickBy((mod, namespace) => namespace.startsWith(prefix))(modules);
|
|
15
|
+
},
|
|
16
|
+
add(namespace, rawModule) {
|
|
17
|
+
if (has(namespace, modules)) {
|
|
18
|
+
throw new Error(`Module ${namespace} has already been registered.`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
modules[namespace] = createModule(namespace, rawModule, strapi);
|
|
22
|
+
modules[namespace].load();
|
|
23
|
+
|
|
24
|
+
return modules[namespace];
|
|
25
|
+
},
|
|
26
|
+
async bootstrap() {
|
|
27
|
+
for (const mod of Object.values(modules)) {
|
|
28
|
+
await mod.bootstrap();
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
async register() {
|
|
32
|
+
for (const mod of Object.values(modules)) {
|
|
33
|
+
await mod.register();
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
async destroy() {
|
|
37
|
+
for (const mod of Object.values(modules)) {
|
|
38
|
+
await mod.destroy();
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
module.exports = modulesRegistry;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { has } = require('lodash/fp');
|
|
4
|
+
|
|
5
|
+
const pluginsRegistry = strapi => {
|
|
6
|
+
const plugins = {};
|
|
7
|
+
|
|
8
|
+
return {
|
|
9
|
+
get(name) {
|
|
10
|
+
return plugins[name];
|
|
11
|
+
},
|
|
12
|
+
getAll() {
|
|
13
|
+
return plugins;
|
|
14
|
+
},
|
|
15
|
+
add(name, pluginConfig) {
|
|
16
|
+
if (has(name, plugins)) {
|
|
17
|
+
throw new Error(`Plugin ${name} has already been registered.`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const pluginModule = strapi.container.get('modules').add(`plugin::${name}`, pluginConfig);
|
|
21
|
+
plugins[name] = pluginModule;
|
|
22
|
+
|
|
23
|
+
return plugins[name];
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
module.exports = pluginsRegistry;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { pickBy, has } = require('lodash/fp');
|
|
4
|
+
const { addNamespace, hasNamespace } = require('../utils');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {import('./policies').Policy} Policy
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// TODO: move instantiation part here instead of in the policy utils
|
|
11
|
+
const policiesRegistry = () => {
|
|
12
|
+
const policies = {};
|
|
13
|
+
|
|
14
|
+
return {
|
|
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];
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Returns a map with all the policies in a namespace
|
|
34
|
+
* @param {string} namespace
|
|
35
|
+
* @returns {{ [key: string]: Policy }}
|
|
36
|
+
*/
|
|
37
|
+
getAll(namespace) {
|
|
38
|
+
return pickBy((_, uid) => hasNamespace(uid, namespace))(policies);
|
|
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
|
+
*/
|
|
57
|
+
add(namespace, newPolicies) {
|
|
58
|
+
for (const policyName in newPolicies) {
|
|
59
|
+
const policy = newPolicies[policyName];
|
|
60
|
+
const uid = addNamespace(policyName, namespace);
|
|
61
|
+
|
|
62
|
+
if (has(uid, policies)) {
|
|
63
|
+
throw new Error(`Policy ${uid} has already been registered.`);
|
|
64
|
+
}
|
|
65
|
+
policies[uid] = policy;
|
|
66
|
+
}
|
|
67
|
+
},
|
|
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
|
+
|
|
77
|
+
if (!currentPolicy) {
|
|
78
|
+
throw new Error(`Policy ${uid} doesn't exist`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const newPolicy = extendFn(currentPolicy);
|
|
82
|
+
policies[uid] = newPolicy;
|
|
83
|
+
|
|
84
|
+
return this;
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
module.exports = policiesRegistry;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { pickBy, has } = require('lodash/fp');
|
|
4
|
+
const { addNamespace, hasNamespace } = require('../utils');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {import('./services').Service} Service
|
|
8
|
+
* @typedef {import('./services').ServiceFactory} ServiceFactory
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const servicesRegistry = strapi => {
|
|
12
|
+
const services = {};
|
|
13
|
+
const instantiatedServices = {};
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
/**
|
|
17
|
+
* Returns this list of registered services uids
|
|
18
|
+
* @returns {string[]}
|
|
19
|
+
*/
|
|
20
|
+
keys() {
|
|
21
|
+
return Object.keys(services);
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Returns the instance of a service. Instantiate the service if not already done
|
|
26
|
+
* @param {string} uid
|
|
27
|
+
* @returns {Service}
|
|
28
|
+
*/
|
|
29
|
+
get(uid) {
|
|
30
|
+
if (instantiatedServices[uid]) {
|
|
31
|
+
return instantiatedServices[uid];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const service = services[uid];
|
|
35
|
+
if (service) {
|
|
36
|
+
instantiatedServices[uid] = typeof service === 'function' ? service({ strapi }) : service;
|
|
37
|
+
return instantiatedServices[uid];
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Returns a map with all the services in a namespace
|
|
43
|
+
* @param {string} namespace
|
|
44
|
+
* @returns {{ [key: string]: Service }}
|
|
45
|
+
*/
|
|
46
|
+
getAll(namespace) {
|
|
47
|
+
const filteredServices = pickBy((_, uid) => hasNamespace(uid, namespace))(services);
|
|
48
|
+
|
|
49
|
+
// create lazy accessor to avoid instantiating the services;
|
|
50
|
+
const map = {};
|
|
51
|
+
for (const uid in filteredServices) {
|
|
52
|
+
Object.defineProperty(map, uid, {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
get: () => {
|
|
55
|
+
return this.get(uid);
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return map;
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Registers a service
|
|
65
|
+
* @param {string} uid
|
|
66
|
+
* @param {Service} service
|
|
67
|
+
*/
|
|
68
|
+
set(uid, service) {
|
|
69
|
+
services[uid] = service;
|
|
70
|
+
delete instantiatedServices[uid];
|
|
71
|
+
return this;
|
|
72
|
+
},
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Registers a map of services for a specific namespace
|
|
76
|
+
* @param {string} namespace
|
|
77
|
+
* @param {{ [key: string]: Service|ServiceFactory }} newServices
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
80
|
+
add(namespace, newServices) {
|
|
81
|
+
for (const serviceName in newServices) {
|
|
82
|
+
const service = newServices[serviceName];
|
|
83
|
+
const uid = addNamespace(serviceName, namespace);
|
|
84
|
+
|
|
85
|
+
if (has(uid, services)) {
|
|
86
|
+
throw new Error(`Service ${uid} has already been registered.`);
|
|
87
|
+
}
|
|
88
|
+
services[uid] = service;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return this;
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Wraps a service to extend it
|
|
96
|
+
* @param {string} uid
|
|
97
|
+
* @param {(service: Service) => Service} extendFn
|
|
98
|
+
*/
|
|
99
|
+
extend(uid, extendFn) {
|
|
100
|
+
const currentService = this.get(uid);
|
|
101
|
+
|
|
102
|
+
if (!currentService) {
|
|
103
|
+
throw new Error(`Service ${uid} doesn't exist`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const newService = extendFn(currentService);
|
|
107
|
+
instantiatedServices[uid] = newService;
|
|
108
|
+
|
|
109
|
+
return this;
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
module.exports = servicesRegistry;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const hasNamespace = (name, namespace) => {
|
|
4
|
+
if (!namespace) {
|
|
5
|
+
return true;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (namespace.endsWith('::')) {
|
|
9
|
+
return name.startsWith(namespace);
|
|
10
|
+
} else {
|
|
11
|
+
return name.startsWith(`${namespace}.`);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const addNamespace = (name, namespace) => {
|
|
16
|
+
if (namespace.endsWith('::')) {
|
|
17
|
+
return `${namespace}${name}`;
|
|
18
|
+
} else {
|
|
19
|
+
return `${namespace}.${name}`;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const removeNamespace = (name, namespace) => {
|
|
24
|
+
if (namespace.endsWith('::')) {
|
|
25
|
+
return name.replace(namespace, '');
|
|
26
|
+
} else {
|
|
27
|
+
return name.replace(`${namespace}.`, '');
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
module.exports = {
|
|
32
|
+
addNamespace,
|
|
33
|
+
removeNamespace,
|
|
34
|
+
hasNamespace,
|
|
35
|
+
};
|
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const { isObject } = require('lodash/fp');
|
|
4
|
+
const { ValidationError } = require('@strapi/utils').errors;
|
|
5
|
+
|
|
6
|
+
const { parseBody } = require('./transform');
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
9
|
*
|
|
7
10
|
* Returns a collection type controller to handle default core-api actions
|
|
8
11
|
*/
|
|
9
|
-
const createCollectionTypeController = ({
|
|
12
|
+
const createCollectionTypeController = ({ contentType }) => {
|
|
13
|
+
const { uid } = contentType;
|
|
14
|
+
|
|
10
15
|
return {
|
|
11
16
|
/**
|
|
12
17
|
* Retrieve records.
|
|
@@ -16,9 +21,10 @@ const createCollectionTypeController = ({ service, sanitize, parseMultipartData
|
|
|
16
21
|
async find(ctx) {
|
|
17
22
|
const { query } = ctx;
|
|
18
23
|
|
|
19
|
-
const { results, pagination } = await service.find(
|
|
24
|
+
const { results, pagination } = await strapi.service(uid).find(query);
|
|
25
|
+
const sanitizedResults = await this.sanitizeOutput(results, ctx);
|
|
20
26
|
|
|
21
|
-
return transformResponse(
|
|
27
|
+
return this.transformResponse(sanitizedResults, { pagination });
|
|
22
28
|
},
|
|
23
29
|
|
|
24
30
|
/**
|
|
@@ -30,9 +36,10 @@ const createCollectionTypeController = ({ service, sanitize, parseMultipartData
|
|
|
30
36
|
const { id } = ctx.params;
|
|
31
37
|
const { query } = ctx;
|
|
32
38
|
|
|
33
|
-
const entity = await service.findOne(id,
|
|
39
|
+
const entity = await strapi.service(uid).findOne(id, query);
|
|
40
|
+
const sanitizedEntity = await this.sanitizeOutput(entity, ctx);
|
|
34
41
|
|
|
35
|
-
return transformResponse(
|
|
42
|
+
return this.transformResponse(sanitizedEntity);
|
|
36
43
|
},
|
|
37
44
|
|
|
38
45
|
/**
|
|
@@ -41,17 +48,22 @@ const createCollectionTypeController = ({ service, sanitize, parseMultipartData
|
|
|
41
48
|
* @return {Object}
|
|
42
49
|
*/
|
|
43
50
|
async create(ctx) {
|
|
44
|
-
const {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
} else {
|
|
51
|
-
entity = await service.create({ params: query, data: body });
|
|
51
|
+
const { query } = ctx.request;
|
|
52
|
+
|
|
53
|
+
const { data, files } = parseBody(ctx);
|
|
54
|
+
|
|
55
|
+
if (!isObject(data)) {
|
|
56
|
+
throw new ValidationError('Missing "data" payload in the request body');
|
|
52
57
|
}
|
|
53
58
|
|
|
54
|
-
|
|
59
|
+
const sanitizedInputData = await this.sanitizeInput(data, ctx);
|
|
60
|
+
|
|
61
|
+
const entity = await strapi
|
|
62
|
+
.service(uid)
|
|
63
|
+
.create({ ...query, data: sanitizedInputData, files });
|
|
64
|
+
const sanitizedEntity = await this.sanitizeOutput(entity, ctx);
|
|
65
|
+
|
|
66
|
+
return this.transformResponse(sanitizedEntity);
|
|
55
67
|
},
|
|
56
68
|
|
|
57
69
|
/**
|
|
@@ -61,17 +73,22 @@ const createCollectionTypeController = ({ service, sanitize, parseMultipartData
|
|
|
61
73
|
*/
|
|
62
74
|
async update(ctx) {
|
|
63
75
|
const { id } = ctx.params;
|
|
64
|
-
const {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
} else {
|
|
71
|
-
entity = await service.update(id, { params: query, data: body });
|
|
76
|
+
const { query } = ctx.request;
|
|
77
|
+
|
|
78
|
+
const { data, files } = parseBody(ctx);
|
|
79
|
+
|
|
80
|
+
if (!isObject(data)) {
|
|
81
|
+
throw new ValidationError('Missing "data" payload in the request body');
|
|
72
82
|
}
|
|
73
83
|
|
|
74
|
-
|
|
84
|
+
const sanitizedInputData = await this.sanitizeInput(data, ctx);
|
|
85
|
+
|
|
86
|
+
const entity = await strapi
|
|
87
|
+
.service(uid)
|
|
88
|
+
.update(id, { ...query, data: sanitizedInputData, files });
|
|
89
|
+
const sanitizedEntity = await this.sanitizeOutput(entity, ctx);
|
|
90
|
+
|
|
91
|
+
return this.transformResponse(sanitizedEntity);
|
|
75
92
|
},
|
|
76
93
|
|
|
77
94
|
/**
|
|
@@ -83,8 +100,10 @@ const createCollectionTypeController = ({ service, sanitize, parseMultipartData
|
|
|
83
100
|
const { id } = ctx.params;
|
|
84
101
|
const { query } = ctx;
|
|
85
102
|
|
|
86
|
-
const entity = await service.delete(id,
|
|
87
|
-
|
|
103
|
+
const entity = await strapi.service(uid).delete(id, query);
|
|
104
|
+
const sanitizedEntity = await this.sanitizeOutput(entity, ctx);
|
|
105
|
+
|
|
106
|
+
return this.transformResponse(sanitizedEntity);
|
|
88
107
|
},
|
|
89
108
|
};
|
|
90
109
|
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Context } from 'koa';
|
|
2
|
+
|
|
3
|
+
type Response = object;
|
|
4
|
+
|
|
5
|
+
interface BaseController {
|
|
6
|
+
transformResponse(data: object, meta: object): object;
|
|
7
|
+
sanitizeOutput(data: object, ctx: Context): Promise<object>;
|
|
8
|
+
sanitizeInput(data: object, ctx: Context): Promise<object>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface SingleTypeController extends BaseController {
|
|
12
|
+
find(ctx: Context): Promise<Response>;
|
|
13
|
+
update(ctx: Context): Promise<Response>;
|
|
14
|
+
delete(ctx: Context): Promise<Response>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface CollectionTypeController extends BaseController {
|
|
18
|
+
find(ctx: Context): Promise<Response>;
|
|
19
|
+
findOne(ctx: Context): Promise<Response>;
|
|
20
|
+
create(ctx: Context): Promise<Response>;
|
|
21
|
+
update(ctx: Context): Promise<Response>;
|
|
22
|
+
delete(ctx: Context): Promise<Response>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type Controller = SingleTypeController | CollectionTypeController;
|
|
@@ -1,23 +1,45 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const { getOr } = require('lodash/fp');
|
|
4
4
|
|
|
5
|
+
const { contentTypes, sanitize } = require('@strapi/utils');
|
|
6
|
+
|
|
7
|
+
const { transformResponse } = require('./transform');
|
|
5
8
|
const createSingleTypeController = require('./single-type');
|
|
6
9
|
const createCollectionTypeController = require('./collection-type');
|
|
7
10
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
const getAuthFromKoaContext = getOr({}, 'state.auth');
|
|
12
|
+
|
|
13
|
+
const createController = ({ contentType }) => {
|
|
14
|
+
const ctx = { contentType };
|
|
15
|
+
|
|
16
|
+
const proto = {
|
|
17
|
+
transformResponse(data, meta) {
|
|
18
|
+
return transformResponse(data, meta, { contentType });
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
sanitizeOutput(data, ctx) {
|
|
22
|
+
const auth = getAuthFromKoaContext(ctx);
|
|
23
|
+
|
|
24
|
+
return sanitize.contentAPI.output(data, contentType, { auth });
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
sanitizeInput(data, ctx) {
|
|
28
|
+
const auth = getAuthFromKoaContext(ctx);
|
|
29
|
+
|
|
30
|
+
return sanitize.contentAPI.input(data, contentType, { auth });
|
|
15
31
|
},
|
|
16
32
|
};
|
|
17
33
|
|
|
18
|
-
|
|
19
|
-
|
|
34
|
+
let ctrl;
|
|
35
|
+
|
|
36
|
+
if (contentTypes.isSingleType(contentType)) {
|
|
37
|
+
ctrl = createSingleTypeController(ctx);
|
|
38
|
+
} else {
|
|
39
|
+
ctrl = createCollectionTypeController(ctx);
|
|
20
40
|
}
|
|
21
41
|
|
|
22
|
-
return
|
|
42
|
+
return Object.assign(Object.create(proto), ctrl);
|
|
23
43
|
};
|
|
44
|
+
|
|
45
|
+
module.exports = { createController };
|