@strapi/strapi 4.0.0-next.7 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -14
- package/bin/strapi.js +46 -60
- package/lib/Strapi.js +193 -98
- package/lib/commands/build.js +19 -8
- package/lib/commands/console.js +1 -1
- package/lib/commands/content-types/list.js +22 -0
- package/lib/commands/controllers/list.js +22 -0
- package/lib/commands/develop.js +22 -27
- 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 -9
- package/lib/core/app-configuration/index.js +3 -36
- package/lib/core/bootstrap.js +25 -0
- package/lib/core/domain/content-type/index.js +26 -29
- package/lib/core/domain/content-type/validator.js +22 -3
- package/lib/core/domain/module/index.js +42 -11
- package/lib/core/domain/module/validation.js +16 -19
- package/lib/core/loaders/admin.js +16 -0
- package/lib/core/loaders/apis.js +159 -0
- package/lib/core/loaders/{load-components.js → components.js} +5 -6
- package/lib/core/loaders/index.js +11 -0
- package/lib/core/loaders/middlewares.js +36 -0
- package/lib/core/{load-plugins → loaders/plugins}/get-enabled-plugins.js +55 -19
- package/lib/core/loaders/plugins/get-user-plugins-config.js +37 -0
- package/lib/core/{load-plugins → loaders/plugins}/index.js +35 -19
- 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/registries/content-types.js +66 -10
- package/lib/core/registries/controllers.d.ts +7 -0
- package/lib/core/registries/controllers.js +92 -7
- 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 +65 -5
- package/lib/core/registries/modules.js +3 -3
- package/lib/core/registries/plugins.js +2 -2
- package/lib/core/registries/policies.d.ts +9 -0
- package/lib/core/registries/policies.js +65 -5
- package/lib/core/registries/services.d.ts +7 -0
- package/lib/core/registries/services.js +88 -17
- 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 +125 -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 -116
- 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 +72 -77
- 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 +131 -43
- package/lib/services/errors.js +77 -0
- package/lib/{core → services}/fs.js +1 -1
- package/lib/services/metrics/index.js +38 -36
- 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/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/signals.js +24 -0
- package/lib/utils/startup-logger.js +2 -2
- package/lib/utils/update-notifier/index.js +3 -2
- package/package.json +94 -97
- package/lib/commands/generate.js +0 -76
- package/lib/core/loaders/bootstrap.js +0 -176
- package/lib/core/loaders/load-apis.js +0 -20
- package/lib/core/loaders/load-functions.js +0 -21
- package/lib/core/loaders/load-middlewares.js +0 -136
- package/lib/core/loaders/load-modules.js +0 -21
- package/lib/core/loaders/load-policies.js +0 -36
- package/lib/core/loaders/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 -56
- package/lib/middlewares/router/utils/composeEndpoint.js +0 -25
- package/lib/middlewares/router/utils/routerChecker.js +0 -96
- package/lib/utils/get-prefixed-dependencies.js +0 -7
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const _ = require('lodash');
|
|
4
|
-
const { toLower, kebabCase } = require('lodash/fp');
|
|
5
|
-
const { getConfigUrls } = require('@strapi/utils');
|
|
6
|
-
const pluralize = require('pluralize');
|
|
7
|
-
const { createContentType } = require('../domain/content-type');
|
|
8
|
-
|
|
9
|
-
const { createCoreApi } = require('../../core-api');
|
|
10
|
-
|
|
11
|
-
// TODO: function to be moved next to where the api will be loaded
|
|
12
|
-
const validateContentTypesUnicity = schemas => {
|
|
13
|
-
const names = [];
|
|
14
|
-
schemas.forEach(schema => {
|
|
15
|
-
if (schema.info.singularName) {
|
|
16
|
-
const singularName = kebabCase(schema.info.singularName);
|
|
17
|
-
if (names.includes(singularName)) {
|
|
18
|
-
throw new Error(`The singular name "${schema.info.singularName}" should be unique`);
|
|
19
|
-
}
|
|
20
|
-
names.push(singularName);
|
|
21
|
-
}
|
|
22
|
-
if (schema.info.pluralName) {
|
|
23
|
-
const pluralName = kebabCase(schema.info.pluralName);
|
|
24
|
-
if (names.includes(pluralName)) {
|
|
25
|
-
throw new Error(`The plural name "${schema.info.pluralName}" should be unique`);
|
|
26
|
-
}
|
|
27
|
-
names.push(pluralName);
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
module.exports = function(strapi) {
|
|
33
|
-
strapi.contentTypes = {};
|
|
34
|
-
|
|
35
|
-
// validate Content-Types unicity
|
|
36
|
-
const allApisSchemas = Object.values(strapi.api).flatMap(api => Object.values(api.models));
|
|
37
|
-
validateContentTypesUnicity(allApisSchemas);
|
|
38
|
-
|
|
39
|
-
// TODO: to change with new loading system
|
|
40
|
-
// Register api content types
|
|
41
|
-
for (const apiName in strapi.api) {
|
|
42
|
-
const api = strapi.api[apiName];
|
|
43
|
-
|
|
44
|
-
const v4ContentTypes = _.mapValues(api.models, (model, modelName) => {
|
|
45
|
-
model.info.displayName = model.info.displayName || model.info.name;
|
|
46
|
-
model.info.singularName = model.info.singularName || modelName;
|
|
47
|
-
model.info.pluralName = model.info.pluralName || pluralize(modelName);
|
|
48
|
-
|
|
49
|
-
return {
|
|
50
|
-
schema: model,
|
|
51
|
-
actions: {},
|
|
52
|
-
lifecycles: {},
|
|
53
|
-
};
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
strapi.container.get('content-types').add(`api::${apiName}`, v4ContentTypes);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// TODO: remove v3
|
|
60
|
-
// Set models.
|
|
61
|
-
strapi.models = {};
|
|
62
|
-
for (const apiName in strapi.api) {
|
|
63
|
-
const api = strapi.api[apiName];
|
|
64
|
-
for (let modelName in api.models) {
|
|
65
|
-
let model = api.models[modelName];
|
|
66
|
-
const contentType = strapi.container
|
|
67
|
-
.get('content-types')
|
|
68
|
-
.get(`api::${apiName}.${model.info.singularName}`);
|
|
69
|
-
Object.assign(model, contentType.schema);
|
|
70
|
-
strapi.contentTypes[model.uid] = contentType.schema;
|
|
71
|
-
|
|
72
|
-
strapi.models[modelName] = model;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// set default services and default controllers
|
|
77
|
-
for (const apiName in strapi.api) {
|
|
78
|
-
const api = strapi.api[apiName];
|
|
79
|
-
for (const modelName in api.models) {
|
|
80
|
-
const model = api.models[modelName];
|
|
81
|
-
const { service, controller } = createCoreApi({ model, api, strapi });
|
|
82
|
-
_.set(strapi.api[apiName], ['services', modelName], service);
|
|
83
|
-
_.set(strapi.api[apiName], ['controllers', modelName], controller);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Set user's controllers.
|
|
88
|
-
strapi.controllers = Object.keys(strapi.api || []).reduce((acc, apiName) => {
|
|
89
|
-
strapi.container.get('controllers').add(`api::${apiName}`, strapi.api[apiName].controllers);
|
|
90
|
-
for (let controllerName in strapi.api[apiName].controllers) {
|
|
91
|
-
let controller = strapi.api[apiName].controllers[controllerName];
|
|
92
|
-
acc[controllerName] = controller;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return acc;
|
|
96
|
-
}, {});
|
|
97
|
-
|
|
98
|
-
// Set user's services.
|
|
99
|
-
strapi.services = Object.keys(strapi.api || []).reduce((acc, apiName) => {
|
|
100
|
-
strapi.container.get('services').add(`api::${apiName}`, strapi.api[apiName].services);
|
|
101
|
-
for (let serviceName in strapi.api[apiName].services) {
|
|
102
|
-
acc[serviceName] = strapi.api[apiName].services[serviceName];
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return acc;
|
|
106
|
-
}, {});
|
|
107
|
-
|
|
108
|
-
// Set routes.
|
|
109
|
-
strapi.config.routes = Object.keys(strapi.api || []).reduce((acc, key) => {
|
|
110
|
-
return acc.concat(_.get(strapi.api[key], 'config.routes') || {});
|
|
111
|
-
}, []);
|
|
112
|
-
|
|
113
|
-
// Init admin models.
|
|
114
|
-
Object.keys(strapi.admin.models || []).forEach(modelName => {
|
|
115
|
-
let model = strapi.admin.models[modelName];
|
|
116
|
-
// mutate model
|
|
117
|
-
const ct = { schema: model, actions: {}, lifecycles: {} };
|
|
118
|
-
ct.schema.info.displayName = model.info.name;
|
|
119
|
-
ct.schema.info.singularName = modelName;
|
|
120
|
-
ct.schema.info.pluralName = pluralize(modelName);
|
|
121
|
-
|
|
122
|
-
const createdContentType = createContentType(`strapi::${ct.schema.info.singularName}`, ct);
|
|
123
|
-
|
|
124
|
-
Object.assign(model, createdContentType.schema);
|
|
125
|
-
strapi.contentTypes[model.uid] = model;
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
// TODO: delete v3 code
|
|
129
|
-
_.forEach(strapi.plugins, plugin => {
|
|
130
|
-
_.forEach(plugin.contentTypes, (ct, ctUID) => {
|
|
131
|
-
strapi.contentTypes[ctUID] = ct.schema;
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
_.forEach(plugin.middlewares, (middleware, middlewareUID) => {
|
|
135
|
-
const middlewareName = toLower(middlewareUID.split('.')[1]);
|
|
136
|
-
strapi.middleware[middlewareName] = middleware;
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
// Preset config in alphabetical order.
|
|
141
|
-
strapi.config.middleware.settings = Object.keys(strapi.middleware).reduce((acc, current) => {
|
|
142
|
-
// Try to find the settings in the current environment, then in the main configurations.
|
|
143
|
-
const currentSettings = _.merge(
|
|
144
|
-
_.cloneDeep(_.get(strapi.middleware[current], ['defaults', current], {})),
|
|
145
|
-
strapi.config.get(['middleware', 'settings', current], {})
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
acc[current] = !_.isObject(currentSettings) ? {} : currentSettings;
|
|
149
|
-
|
|
150
|
-
// Ensure that enabled key exist by forcing to false.
|
|
151
|
-
_.defaults(acc[current], { enabled: false });
|
|
152
|
-
|
|
153
|
-
return acc;
|
|
154
|
-
}, {});
|
|
155
|
-
|
|
156
|
-
// default settings
|
|
157
|
-
strapi.config.port = strapi.config.get('server.port') || strapi.config.port;
|
|
158
|
-
strapi.config.host = strapi.config.get('server.host') || strapi.config.host;
|
|
159
|
-
|
|
160
|
-
const { serverUrl, adminUrl, adminPath } = getConfigUrls(strapi.config.get('server'));
|
|
161
|
-
|
|
162
|
-
strapi.config.server = strapi.config.server || {};
|
|
163
|
-
strapi.config.server.url = serverUrl;
|
|
164
|
-
strapi.config.admin.url = adminUrl;
|
|
165
|
-
strapi.config.admin.path = adminPath;
|
|
166
|
-
|
|
167
|
-
// check if we should serve admin panel
|
|
168
|
-
const shouldServeAdmin = strapi.config.get(
|
|
169
|
-
'server.admin.serveAdminPanel',
|
|
170
|
-
strapi.config.get('serveAdminPanel')
|
|
171
|
-
);
|
|
172
|
-
|
|
173
|
-
if (!shouldServeAdmin) {
|
|
174
|
-
strapi.config.serveAdminPanel = false;
|
|
175
|
-
}
|
|
176
|
-
};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { join } = require('path');
|
|
4
|
-
const { existsSync } = require('fs-extra');
|
|
5
|
-
const _ = require('lodash');
|
|
6
|
-
const loadFiles = require('../../load/load-files');
|
|
7
|
-
const loadConfig = require('../../load/load-config-files');
|
|
8
|
-
|
|
9
|
-
module.exports = async ({ dir }) => {
|
|
10
|
-
const apiDir = join(dir, 'api');
|
|
11
|
-
|
|
12
|
-
if (!existsSync(apiDir)) {
|
|
13
|
-
throw new Error(`Missing api folder. Please create one in your app root directory`);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const apis = await loadFiles(apiDir, '*/!(config)/**/*.*(js|json)');
|
|
17
|
-
const apiConfigs = await loadConfig(apiDir, '*/config/**/*.*(js|json)');
|
|
18
|
-
|
|
19
|
-
return _.merge(apis, apiConfigs);
|
|
20
|
-
};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const fse = require('fs-extra');
|
|
4
|
-
|
|
5
|
-
const walk = require('./walk');
|
|
6
|
-
|
|
7
|
-
const loadFunctions = dir => {
|
|
8
|
-
if (!fse.existsSync(dir)) return {};
|
|
9
|
-
|
|
10
|
-
return walk(dir, { loader: loadFunction });
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const loadFunction = file => {
|
|
14
|
-
try {
|
|
15
|
-
return require(file);
|
|
16
|
-
} catch (error) {
|
|
17
|
-
throw `Could not load function ${file}: ${error.message}`;
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
module.exports = loadFunctions;
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// Dependencies.
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const fs = require('fs-extra');
|
|
6
|
-
const _ = require('lodash');
|
|
7
|
-
const glob = require('../../load/glob');
|
|
8
|
-
const findPackagePath = require('../../load/package-path');
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Load middlewares
|
|
12
|
-
*/
|
|
13
|
-
module.exports = async function(strapi) {
|
|
14
|
-
const installedMiddlewares = strapi.config.get('installedMiddlewares');
|
|
15
|
-
const appPath = strapi.config.get('appPath');
|
|
16
|
-
|
|
17
|
-
let middlewares = {};
|
|
18
|
-
|
|
19
|
-
const loaders = createLoaders(strapi);
|
|
20
|
-
|
|
21
|
-
await loaders.loadMiddlewareDependencies(installedMiddlewares, middlewares);
|
|
22
|
-
// internal middlewares
|
|
23
|
-
await loaders.loadInternalMiddlewares(middlewares);
|
|
24
|
-
// local middleware
|
|
25
|
-
await loaders.loadLocalMiddlewares(appPath, middlewares);
|
|
26
|
-
// plugins middlewares
|
|
27
|
-
await loaders.loadPluginsMiddlewares(strapi.plugins, middlewares);
|
|
28
|
-
// local plugin middlewares
|
|
29
|
-
await loaders.loadLocalPluginsMiddlewares(appPath, middlewares);
|
|
30
|
-
// load admin middlewares
|
|
31
|
-
await loaders.loadAdminMiddlewares(middlewares);
|
|
32
|
-
|
|
33
|
-
return middlewares;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Build loader functions
|
|
38
|
-
* @param {*} strapi - strapi instance
|
|
39
|
-
*/
|
|
40
|
-
const createLoaders = strapi => {
|
|
41
|
-
const loadMiddlewaresInDir = async (dir, middlewares) => {
|
|
42
|
-
const files = await glob('*/*(index|defaults).*(js|json)', {
|
|
43
|
-
cwd: dir,
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
files.forEach(f => {
|
|
47
|
-
const name = f.split('/')[0];
|
|
48
|
-
mountMiddleware(name, [path.resolve(dir, f)], middlewares);
|
|
49
|
-
});
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const loadInternalMiddlewares = middlewares =>
|
|
53
|
-
loadMiddlewaresInDir(path.resolve(__dirname, '..', '..', 'middlewares'), middlewares);
|
|
54
|
-
|
|
55
|
-
const loadLocalMiddlewares = (appPath, middlewares) =>
|
|
56
|
-
loadMiddlewaresInDir(path.resolve(appPath, 'middlewares'), middlewares);
|
|
57
|
-
|
|
58
|
-
const loadPluginsMiddlewares = async (plugins, middlewares) => {
|
|
59
|
-
for (const pluginName in plugins) {
|
|
60
|
-
const pluginMiddlewares = plugins[pluginName].middlewares;
|
|
61
|
-
for (const middlewareName in pluginMiddlewares) {
|
|
62
|
-
middlewares[middlewareName] = {
|
|
63
|
-
loaded: false,
|
|
64
|
-
...pluginMiddlewares[middlewareName],
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
const loadLocalPluginsMiddlewares = async (appPath, middlewares) => {
|
|
71
|
-
const pluginsDir = path.resolve(appPath, 'plugins');
|
|
72
|
-
if (!fs.existsSync(pluginsDir)) return;
|
|
73
|
-
|
|
74
|
-
const pluginsNames = await fs.readdir(pluginsDir);
|
|
75
|
-
|
|
76
|
-
for (let pluginFolder of pluginsNames) {
|
|
77
|
-
// ignore files
|
|
78
|
-
const stat = await fs.stat(path.resolve(pluginsDir, pluginFolder));
|
|
79
|
-
if (!stat.isDirectory()) continue;
|
|
80
|
-
|
|
81
|
-
const dir = path.resolve(pluginsDir, pluginFolder, 'middlewares');
|
|
82
|
-
await loadMiddlewaresInDir(dir, middlewares);
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
const loadAdminMiddlewares = async middlewares => {
|
|
87
|
-
const middlewaresDir = 'middlewares';
|
|
88
|
-
const dir = path.resolve(findPackagePath(`@strapi/admin`), middlewaresDir);
|
|
89
|
-
await loadMiddlewaresInDir(dir, middlewares);
|
|
90
|
-
|
|
91
|
-
// load ee admin middlewares if they exist
|
|
92
|
-
if (process.env.STRAPI_DISABLE_EE !== 'true' && strapi.EE) {
|
|
93
|
-
await loadMiddlewaresInDir(`${dir}/../ee/${middlewaresDir}`, middlewares);
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const loadMiddlewareDependencies = async (packages, middlewares) => {
|
|
98
|
-
for (let packageName of packages) {
|
|
99
|
-
const baseDir = path.dirname(require.resolve(`@strapi/middleware-${packageName}`));
|
|
100
|
-
const files = await glob('*(index|defaults).*(js|json)', {
|
|
101
|
-
cwd: baseDir,
|
|
102
|
-
absolute: true,
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
mountMiddleware(packageName, files, middlewares);
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
const mountMiddleware = (name, files, middlewares) => {
|
|
110
|
-
files.forEach(file => {
|
|
111
|
-
middlewares[name] = middlewares[name] || { loaded: false };
|
|
112
|
-
|
|
113
|
-
if (_.endsWith(file, 'index.js') && !middlewares[name].load) {
|
|
114
|
-
return Object.defineProperty(middlewares[name], 'load', {
|
|
115
|
-
configurable: false,
|
|
116
|
-
enumerable: true,
|
|
117
|
-
get: () => require(file)(strapi),
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (_.endsWith(file, 'defaults.json')) {
|
|
122
|
-
middlewares[name].defaults = require(file);
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
return {
|
|
129
|
-
loadInternalMiddlewares,
|
|
130
|
-
loadLocalMiddlewares,
|
|
131
|
-
loadPluginsMiddlewares,
|
|
132
|
-
loadLocalPluginsMiddlewares,
|
|
133
|
-
loadMiddlewareDependencies,
|
|
134
|
-
loadAdminMiddlewares,
|
|
135
|
-
};
|
|
136
|
-
};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const loadApis = require('./load-apis');
|
|
4
|
-
const loadMiddlewares = require('./load-middlewares');
|
|
5
|
-
const loadComponents = require('./load-components');
|
|
6
|
-
|
|
7
|
-
module.exports = async strapi => {
|
|
8
|
-
const [api, middlewares, components] = await Promise.all([
|
|
9
|
-
loadApis(strapi),
|
|
10
|
-
loadMiddlewares(strapi), // TODO: load in the middleware registry directly
|
|
11
|
-
loadComponents(strapi),
|
|
12
|
-
]);
|
|
13
|
-
|
|
14
|
-
// TODO: move this into the appropriate loaders
|
|
15
|
-
|
|
16
|
-
return {
|
|
17
|
-
api,
|
|
18
|
-
middlewares,
|
|
19
|
-
components,
|
|
20
|
-
};
|
|
21
|
-
};
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const assert = require('assert');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const _ = require('lodash');
|
|
6
|
-
const fse = require('fs-extra');
|
|
7
|
-
|
|
8
|
-
module.exports = dir => {
|
|
9
|
-
if (!fse.existsSync(dir)) return {};
|
|
10
|
-
|
|
11
|
-
const root = {};
|
|
12
|
-
const paths = fse.readdirSync(dir, { withFileTypes: true }).filter(fd => fd.isFile());
|
|
13
|
-
|
|
14
|
-
for (let fd of paths) {
|
|
15
|
-
const { name } = fd;
|
|
16
|
-
const fullPath = dir + path.sep + name;
|
|
17
|
-
|
|
18
|
-
const ext = path.extname(name);
|
|
19
|
-
const key = path.basename(name, ext);
|
|
20
|
-
root[_.toLower(key)] = loadPolicy(fullPath);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return root;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const loadPolicy = file => {
|
|
27
|
-
try {
|
|
28
|
-
const policy = require(file);
|
|
29
|
-
|
|
30
|
-
assert(typeof policy === 'function', 'Policy must be a function.');
|
|
31
|
-
|
|
32
|
-
return policy;
|
|
33
|
-
} catch (error) {
|
|
34
|
-
throw `Could not load policy ${file}: ${error.message}`;
|
|
35
|
-
}
|
|
36
|
-
};
|
package/lib/core/loaders/walk.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const assert = require('assert');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const fse = require('fs-extra');
|
|
6
|
-
|
|
7
|
-
module.exports = function walk(dir, { loader } = {}) {
|
|
8
|
-
assert(typeof loader === 'function', 'opts.loader must be a function');
|
|
9
|
-
|
|
10
|
-
const root = {};
|
|
11
|
-
const paths = fse.readdirSync(dir, { withFileTypes: true });
|
|
12
|
-
|
|
13
|
-
for (let fd of paths) {
|
|
14
|
-
const { name } = fd;
|
|
15
|
-
const fullPath = dir + path.sep + name;
|
|
16
|
-
|
|
17
|
-
if (fd.isDirectory()) {
|
|
18
|
-
root[name] = walk(fullPath, { loader });
|
|
19
|
-
} else {
|
|
20
|
-
const ext = path.extname(name);
|
|
21
|
-
const key = path.basename(name, ext);
|
|
22
|
-
root[key] = loader(fullPath);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return root;
|
|
27
|
-
};
|
package/lib/core-api/index.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Core API
|
|
3
|
-
*/
|
|
4
|
-
'use strict';
|
|
5
|
-
|
|
6
|
-
const _ = require('lodash');
|
|
7
|
-
|
|
8
|
-
const createController = require('./controller');
|
|
9
|
-
const { createService } = require('./service');
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Returns a service and a controller built based on the content type passed
|
|
13
|
-
*
|
|
14
|
-
* @param {object} opts options
|
|
15
|
-
* @param {object} opts.api api
|
|
16
|
-
* @param {object} opts.model model
|
|
17
|
-
* @param {object} opts.strapi strapi
|
|
18
|
-
* @returns {object} controller & service
|
|
19
|
-
*/
|
|
20
|
-
function createCoreApi({ api, model, strapi }) {
|
|
21
|
-
const { modelName } = model;
|
|
22
|
-
|
|
23
|
-
// find corresponding service and controller
|
|
24
|
-
const userService = _.get(api, ['services', modelName], {});
|
|
25
|
-
const userController = _.get(api, ['controllers', modelName], {});
|
|
26
|
-
|
|
27
|
-
const service = Object.assign(createService({ model, strapi }), userService);
|
|
28
|
-
|
|
29
|
-
const controller = Object.assign(createController({ service, model }), userController);
|
|
30
|
-
|
|
31
|
-
return {
|
|
32
|
-
service,
|
|
33
|
-
controller,
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
module.exports = {
|
|
38
|
-
createCoreApi,
|
|
39
|
-
};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const _ = require('lodash');
|
|
4
|
-
|
|
5
|
-
// files to load with filename key
|
|
6
|
-
const prefixedPaths = ['functions', 'policies', 'locales', 'middleware', 'language', 'layout'];
|
|
7
|
-
|
|
8
|
-
module.exports = function checkReservedFilenames(file) {
|
|
9
|
-
return _.some(prefixedPaths, e => file.indexOf(`config/${e}`) >= 0) ? true : false;
|
|
10
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const loadFiles = require('./load-files');
|
|
4
|
-
const requireFileAndParse = require('./require-file-parse');
|
|
5
|
-
const checkReservedFilename = require('./check-reserved-filename');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @param {string} dir - directory from which to load configs
|
|
9
|
-
* @param {string} pattern - glob pattern to search for config files
|
|
10
|
-
*/
|
|
11
|
-
const loadConfigFiles = (dir, pattern = 'config/**/*.+(js|json)') =>
|
|
12
|
-
loadFiles(dir, pattern, {
|
|
13
|
-
requireFn: requireFileAndParse,
|
|
14
|
-
shouldUseFileNameAsKey: checkReservedFilename,
|
|
15
|
-
globArgs: {
|
|
16
|
-
// used to load .init.json at first startup
|
|
17
|
-
dot: true,
|
|
18
|
-
ignore: ['config/**/*.test.js'],
|
|
19
|
-
},
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
module.exports = loadConfigFiles;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const { templateConfiguration } = require('@strapi/utils');
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Requires a file and eval expression if it is a json
|
|
8
|
-
* @param {string} filePath - file path
|
|
9
|
-
*/
|
|
10
|
-
module.exports = filePath => {
|
|
11
|
-
if (path.extname(filePath) === '.json') {
|
|
12
|
-
return templateConfiguration(require(filePath));
|
|
13
|
-
}
|
|
14
|
-
return require(filePath);
|
|
15
|
-
};
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Boom hook
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// Public node modules.
|
|
8
|
-
const _ = require('lodash');
|
|
9
|
-
const Boom = require('boom');
|
|
10
|
-
const delegate = require('delegates');
|
|
11
|
-
|
|
12
|
-
const boomMethods = [
|
|
13
|
-
'badRequest',
|
|
14
|
-
'unauthorized',
|
|
15
|
-
'paymentRequired',
|
|
16
|
-
'forbidden',
|
|
17
|
-
'notFound',
|
|
18
|
-
'methodNotAllowed',
|
|
19
|
-
'notAcceptable',
|
|
20
|
-
'proxyAuthRequired',
|
|
21
|
-
'clientTimeout',
|
|
22
|
-
'conflict',
|
|
23
|
-
'resourceGone',
|
|
24
|
-
'lengthRequired',
|
|
25
|
-
'preconditionFailed',
|
|
26
|
-
'entityTooLarge',
|
|
27
|
-
'uriTooLong',
|
|
28
|
-
'unsupportedMediaType',
|
|
29
|
-
'rangeNotSatisfiable',
|
|
30
|
-
'expectationFailed',
|
|
31
|
-
'teapot',
|
|
32
|
-
'badData',
|
|
33
|
-
'locked',
|
|
34
|
-
'failedDependency',
|
|
35
|
-
'preconditionRequired',
|
|
36
|
-
'tooManyRequests',
|
|
37
|
-
'illegal',
|
|
38
|
-
'badImplementation',
|
|
39
|
-
'notImplemented',
|
|
40
|
-
'badGateway',
|
|
41
|
-
'serverUnavailable',
|
|
42
|
-
'gatewayTimeout',
|
|
43
|
-
];
|
|
44
|
-
|
|
45
|
-
const formatBoomPayload = boomError => {
|
|
46
|
-
if (!Boom.isBoom(boomError)) {
|
|
47
|
-
boomError = Boom.boomify(boomError, {
|
|
48
|
-
statusCode: boomError.status || 500,
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const { output } = boomError;
|
|
53
|
-
|
|
54
|
-
if (output.statusCode < 500 && !_.isNil(boomError.data)) {
|
|
55
|
-
output.payload.data = boomError.data;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return { status: output.statusCode, body: output.payload };
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
module.exports = strapi => {
|
|
62
|
-
return {
|
|
63
|
-
/**
|
|
64
|
-
* Initialize the hook
|
|
65
|
-
*/
|
|
66
|
-
|
|
67
|
-
initialize() {
|
|
68
|
-
this.delegator = delegate(strapi.app.context, 'response');
|
|
69
|
-
this.createResponses();
|
|
70
|
-
|
|
71
|
-
strapi.errors = Boom;
|
|
72
|
-
strapi.app.use(async (ctx, next) => {
|
|
73
|
-
try {
|
|
74
|
-
// App logic.
|
|
75
|
-
await next();
|
|
76
|
-
} catch (error) {
|
|
77
|
-
// emit error if configured
|
|
78
|
-
if (strapi.config.get('server.emitErrors', false)) {
|
|
79
|
-
strapi.app.emit('error', error, ctx);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Log error.
|
|
83
|
-
|
|
84
|
-
const { status, body } = formatBoomPayload(error);
|
|
85
|
-
|
|
86
|
-
if (status >= 500) {
|
|
87
|
-
strapi.log.error(error);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
ctx.body = body;
|
|
91
|
-
ctx.status = status;
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
strapi.app.use(async (ctx, next) => {
|
|
96
|
-
await next();
|
|
97
|
-
// Empty body is considered as `notFound` response.
|
|
98
|
-
if (_.isNil(ctx.body) && _.isNil(ctx.status)) {
|
|
99
|
-
ctx.notFound();
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
},
|
|
103
|
-
|
|
104
|
-
// Custom function to avoid ctx.body repeat
|
|
105
|
-
createResponses() {
|
|
106
|
-
boomMethods.forEach(method => {
|
|
107
|
-
strapi.app.response[method] = function(msg, ...rest) {
|
|
108
|
-
const boomError = Boom[method](msg, ...rest) || {};
|
|
109
|
-
|
|
110
|
-
const { status, body } = formatBoomPayload(boomError);
|
|
111
|
-
|
|
112
|
-
// keep retro-compatibility for old error formats
|
|
113
|
-
body.message = msg || body.data || body.message;
|
|
114
|
-
|
|
115
|
-
this.body = body;
|
|
116
|
-
this.status = status;
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
this.delegator.method(method);
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
strapi.app.response.send = function(data, status = 200) {
|
|
123
|
-
this.status = status;
|
|
124
|
-
this.body = data;
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
strapi.app.response.created = function(data) {
|
|
128
|
-
this.status = 201;
|
|
129
|
-
this.body = data;
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
strapi.app.response.deleted = function(data) {
|
|
133
|
-
if (_.isNil(data)) {
|
|
134
|
-
this.status = 204;
|
|
135
|
-
} else {
|
|
136
|
-
this.status = 200;
|
|
137
|
-
this.body = data;
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
this.delegator
|
|
142
|
-
.method('send')
|
|
143
|
-
.method('created')
|
|
144
|
-
.method('deleted');
|
|
145
|
-
},
|
|
146
|
-
};
|
|
147
|
-
};
|