@strapi/strapi 4.0.0-next.9 → 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 +32 -5
- package/lib/Strapi.js +140 -72
- 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 +14 -15
- 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 -19
- package/lib/core/bootstrap.js +3 -34
- package/lib/core/domain/content-type/index.js +3 -7
- package/lib/core/domain/module/index.js +8 -6
- package/lib/core/domain/module/validation.js +1 -4
- package/lib/core/loaders/admin.js +2 -2
- package/lib/core/loaders/apis.js +7 -7
- package/lib/core/loaders/components.js +3 -5
- package/lib/core/loaders/index.js +1 -0
- package/lib/core/loaders/middlewares.js +23 -123
- package/lib/core/loaders/plugins/get-enabled-plugins.js +48 -14
- package/lib/core/loaders/plugins/index.js +30 -14
- package/lib/core/loaders/policies.js +1 -1
- package/lib/core/loaders/src-index.js +39 -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 +87 -0
- package/lib/core/registries/middlewares.d.ts +5 -0
- package/lib/core/registries/middlewares.js +61 -2
- 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 +57 -6
- package/lib/core/registries/services.d.ts +7 -0
- package/lib/core/registries/services.js +71 -15
- 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/controller/transform.js +28 -3
- package/lib/core-api/routes/index.js +71 -0
- package/lib/core-api/service/collection-type.js +22 -27
- 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 +17 -20
- 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 +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 -49
- 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 +118 -60
- package/lib/services/entity-service/params.js +48 -81
- 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/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/signals.js +24 -0
- package/lib/utils/update-notifier/index.js +2 -1
- package/package.json +93 -93
- package/lib/core/app-configuration/load-functions.js +0 -28
- package/lib/core-api/index.js +0 -39
- 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 -72
- 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 -72
- package/lib/middlewares/router/utils/compose-endpoint.js +0 -169
- package/lib/utils/get-prefixed-dependencies.js +0 -7
|
@@ -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
|
-
};
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Module dependencies
|
|
5
|
-
*/
|
|
6
|
-
const cors = require('@koa/cors');
|
|
7
|
-
|
|
8
|
-
const defaults = {
|
|
9
|
-
origin: '*',
|
|
10
|
-
maxAge: 31536000,
|
|
11
|
-
credentials: true,
|
|
12
|
-
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
|
|
13
|
-
headers: ['Content-Type', 'Authorization', 'Origin', 'Accept'],
|
|
14
|
-
keepHeadersOnError: false,
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
module.exports = strapi => {
|
|
18
|
-
return {
|
|
19
|
-
/**
|
|
20
|
-
* Initialize the hook
|
|
21
|
-
*/
|
|
22
|
-
initialize() {
|
|
23
|
-
const {
|
|
24
|
-
origin,
|
|
25
|
-
expose,
|
|
26
|
-
maxAge,
|
|
27
|
-
credentials,
|
|
28
|
-
methods,
|
|
29
|
-
headers,
|
|
30
|
-
keepHeadersOnError,
|
|
31
|
-
} = Object.assign({}, defaults, strapi.config.get('middleware.settings.cors'));
|
|
32
|
-
|
|
33
|
-
strapi.app.use(
|
|
34
|
-
cors({
|
|
35
|
-
origin: async function(ctx) {
|
|
36
|
-
let originList;
|
|
37
|
-
|
|
38
|
-
if (typeof origin === 'function') {
|
|
39
|
-
originList = await origin(ctx);
|
|
40
|
-
} else {
|
|
41
|
-
originList = origin;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const whitelist = Array.isArray(originList) ? originList : originList.split(/\s*,\s*/);
|
|
45
|
-
|
|
46
|
-
const requestOrigin = ctx.accept.headers.origin;
|
|
47
|
-
if (whitelist.includes('*')) {
|
|
48
|
-
return '*';
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (!whitelist.includes(requestOrigin)) {
|
|
52
|
-
return ctx.throw(`${requestOrigin} is not a valid origin`);
|
|
53
|
-
}
|
|
54
|
-
return requestOrigin;
|
|
55
|
-
},
|
|
56
|
-
exposeHeaders: expose,
|
|
57
|
-
maxAge,
|
|
58
|
-
credentials,
|
|
59
|
-
allowMethods: methods,
|
|
60
|
-
allowHeaders: headers,
|
|
61
|
-
keepHeadersOnError,
|
|
62
|
-
})
|
|
63
|
-
);
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
};
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Module dependencies
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// Public node modules.
|
|
8
|
-
const _ = require('lodash');
|
|
9
|
-
const cron = require('node-schedule');
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* CRON hook
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
module.exports = strapi => {
|
|
16
|
-
return {
|
|
17
|
-
/**
|
|
18
|
-
* Initialize the hook
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
initialize() {
|
|
22
|
-
if (strapi.config.get('server.cron.enabled', false) === true) {
|
|
23
|
-
_.forEach(_.keys(strapi.config.get('functions.cron', {})), taskExpression => {
|
|
24
|
-
const taskValue = strapi.config.functions.cron[taskExpression];
|
|
25
|
-
|
|
26
|
-
if (_.isFunction(taskValue)) {
|
|
27
|
-
return cron.scheduleJob(taskExpression, taskValue);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const options = _.get(taskValue, 'options', {});
|
|
31
|
-
|
|
32
|
-
cron.scheduleJob(
|
|
33
|
-
{
|
|
34
|
-
rule: taskExpression,
|
|
35
|
-
...options,
|
|
36
|
-
},
|
|
37
|
-
taskValue.task
|
|
38
|
-
);
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
};
|
|
43
|
-
};
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Module dependencies
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// Node.js core.
|
|
8
|
-
const { resolve } = require('path');
|
|
9
|
-
const favicon = require('koa-favicon');
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Favicon hook
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
module.exports = strapi => {
|
|
16
|
-
return {
|
|
17
|
-
/**
|
|
18
|
-
* Initialize the hook
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
initialize() {
|
|
22
|
-
const { dir } = strapi;
|
|
23
|
-
const { maxAge, path: faviconPath } = strapi.config.middleware.settings.favicon;
|
|
24
|
-
|
|
25
|
-
strapi.app.use(
|
|
26
|
-
favicon(resolve(dir, faviconPath), {
|
|
27
|
-
maxAge,
|
|
28
|
-
})
|
|
29
|
-
);
|
|
30
|
-
},
|
|
31
|
-
};
|
|
32
|
-
};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Gzip hook
|
|
5
|
-
*/
|
|
6
|
-
const compress = require('koa-compress');
|
|
7
|
-
|
|
8
|
-
module.exports = strapi => {
|
|
9
|
-
return {
|
|
10
|
-
/**
|
|
11
|
-
* Initialize the hook
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
initialize() {
|
|
15
|
-
const { options = {} } = strapi.config.middleware.settings.gzip;
|
|
16
|
-
strapi.app.use(compress(options));
|
|
17
|
-
},
|
|
18
|
-
};
|
|
19
|
-
};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"helmet": {
|
|
3
|
-
"enabled": true,
|
|
4
|
-
"crossOriginEmbedderPolicy": false,
|
|
5
|
-
"crossOriginOpenerPolicy": false,
|
|
6
|
-
"crossOriginResourcePolicy": false,
|
|
7
|
-
"originAgentCluster": false,
|
|
8
|
-
"contentSecurityPolicy": false,
|
|
9
|
-
"xssFilter": false,
|
|
10
|
-
"hsts": {
|
|
11
|
-
"maxAge": 31536000,
|
|
12
|
-
"includeSubDomains": true
|
|
13
|
-
},
|
|
14
|
-
"frameguard": {
|
|
15
|
-
"action": "sameorigin"
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const ip = require('koa-ip');
|
|
4
|
-
/**
|
|
5
|
-
* IP filter hook
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
module.exports = strapi => {
|
|
9
|
-
return {
|
|
10
|
-
/**
|
|
11
|
-
* Initialize the hook
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
initialize() {
|
|
15
|
-
const { whiteList, blackList } = strapi.config.middleware.settings.ip;
|
|
16
|
-
|
|
17
|
-
strapi.app.use(
|
|
18
|
-
ip({
|
|
19
|
-
whitelist: whiteList,
|
|
20
|
-
blacklist: blackList,
|
|
21
|
-
})
|
|
22
|
-
);
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
};
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Module dependencies
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// Node.js core.
|
|
8
|
-
const { resolve } = require('path');
|
|
9
|
-
const locale = require('koa-locale');
|
|
10
|
-
const i18n = require('koa-i18n');
|
|
11
|
-
/**
|
|
12
|
-
* Language hook
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
module.exports = strapi => {
|
|
16
|
-
return {
|
|
17
|
-
/**
|
|
18
|
-
* Initialize the hook
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
initialize() {
|
|
22
|
-
locale(strapi.app);
|
|
23
|
-
|
|
24
|
-
const { defaultLocale, modes, cookieName } = strapi.config.middleware.settings.language;
|
|
25
|
-
|
|
26
|
-
const directory = resolve(strapi.config.appPath, strapi.config.paths.config, 'locales');
|
|
27
|
-
|
|
28
|
-
strapi.app.use(
|
|
29
|
-
i18n(strapi.app, {
|
|
30
|
-
directory,
|
|
31
|
-
locales: strapi.config.get('middleware.settings.language.locales', []),
|
|
32
|
-
defaultLocale,
|
|
33
|
-
modes,
|
|
34
|
-
cookieName,
|
|
35
|
-
extension: '.json',
|
|
36
|
-
})
|
|
37
|
-
);
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
};
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
const chalk = require('chalk');
|
|
3
|
-
|
|
4
|
-
const codeToColor = code => {
|
|
5
|
-
return code >= 500
|
|
6
|
-
? chalk.red(code)
|
|
7
|
-
: code >= 400
|
|
8
|
-
? chalk.yellow(code)
|
|
9
|
-
: code >= 300
|
|
10
|
-
? chalk.cyan(code)
|
|
11
|
-
: code >= 200
|
|
12
|
-
? chalk.green(code)
|
|
13
|
-
: code;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Logger hook
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
module.exports = strapi => {
|
|
21
|
-
return {
|
|
22
|
-
/**
|
|
23
|
-
* Initialize the hook
|
|
24
|
-
*/
|
|
25
|
-
initialize() {
|
|
26
|
-
strapi.app.context.log = strapi.log;
|
|
27
|
-
|
|
28
|
-
strapi.app.use(async (ctx, next) => {
|
|
29
|
-
const start = Date.now();
|
|
30
|
-
await next();
|
|
31
|
-
const delta = Math.ceil(Date.now() - start);
|
|
32
|
-
|
|
33
|
-
strapi.log.http(`${ctx.method} ${ctx.url} (${delta} ms) ${codeToColor(ctx.status)}`);
|
|
34
|
-
});
|
|
35
|
-
},
|
|
36
|
-
};
|
|
37
|
-
};
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const body = require('koa-body');
|
|
4
|
-
const qs = require('qs');
|
|
5
|
-
const { omit } = require('lodash');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Body parser hook
|
|
9
|
-
*/
|
|
10
|
-
const addQsParser = (app, settings) => {
|
|
11
|
-
Object.defineProperty(app.request, 'query', {
|
|
12
|
-
configurable: false,
|
|
13
|
-
enumerable: true,
|
|
14
|
-
/*
|
|
15
|
-
* Get parsed query-string.
|
|
16
|
-
*/
|
|
17
|
-
get() {
|
|
18
|
-
const qstr = this.querystring;
|
|
19
|
-
const cache = (this._querycache = this._querycache || {});
|
|
20
|
-
return cache[qstr] || (cache[qstr] = qs.parse(qstr, settings));
|
|
21
|
-
},
|
|
22
|
-
|
|
23
|
-
/*
|
|
24
|
-
* Set query-string as an object.
|
|
25
|
-
*/
|
|
26
|
-
set(obj) {
|
|
27
|
-
this.querystring = qs.stringify(obj);
|
|
28
|
-
},
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
return app;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
module.exports = strapi => {
|
|
35
|
-
return {
|
|
36
|
-
/**
|
|
37
|
-
* Initialize the hook
|
|
38
|
-
*/
|
|
39
|
-
initialize() {
|
|
40
|
-
strapi.app.use(async (ctx, next) => {
|
|
41
|
-
// disable for graphql
|
|
42
|
-
// TODO: find a better way later
|
|
43
|
-
if (ctx.url === '/graphql') {
|
|
44
|
-
return next();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
try {
|
|
48
|
-
const res = await body({
|
|
49
|
-
patchKoa: true,
|
|
50
|
-
...omit(strapi.config.middleware.settings.parser, 'queryStringParser'),
|
|
51
|
-
})(ctx, next);
|
|
52
|
-
return res;
|
|
53
|
-
} catch (e) {
|
|
54
|
-
if ((e || {}).message && e.message.includes('maxFileSize exceeded')) {
|
|
55
|
-
throw strapi.errors.entityTooLarge('FileTooBig', {
|
|
56
|
-
errors: [
|
|
57
|
-
{
|
|
58
|
-
id: 'Upload.status.sizeLimit',
|
|
59
|
-
message: `file is bigger than the limit size!`,
|
|
60
|
-
},
|
|
61
|
-
],
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
throw e;
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
addQsParser(strapi.app, strapi.config.get('middleware.settings.parser.queryStringParser'));
|
|
70
|
-
},
|
|
71
|
-
};
|
|
72
|
-
};
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
module.exports = strapi => {
|
|
4
|
-
return {
|
|
5
|
-
initialize() {
|
|
6
|
-
strapi.app.use(async (ctx, next) => {
|
|
7
|
-
await next();
|
|
8
|
-
|
|
9
|
-
ctx.set(
|
|
10
|
-
'X-Powered-By',
|
|
11
|
-
strapi.config.get('middleware.settings.poweredBy.value', 'Strapi <strapi.io>')
|
|
12
|
-
);
|
|
13
|
-
});
|
|
14
|
-
},
|
|
15
|
-
};
|
|
16
|
-
};
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* X-Response-Time hook
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
module.exports = strapi => {
|
|
8
|
-
return {
|
|
9
|
-
/**
|
|
10
|
-
* Initialize the hook
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
initialize() {
|
|
14
|
-
strapi.app.use(async (ctx, next) => {
|
|
15
|
-
const start = Date.now();
|
|
16
|
-
|
|
17
|
-
await next();
|
|
18
|
-
|
|
19
|
-
const delta = Math.ceil(Date.now() - start);
|
|
20
|
-
|
|
21
|
-
ctx.set('X-Response-Time', delta + 'ms'); // eslint-disable-line prefer-template
|
|
22
|
-
});
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const _ = require('lodash');
|
|
4
|
-
|
|
5
|
-
module.exports = strapi => {
|
|
6
|
-
return {
|
|
7
|
-
initialize() {
|
|
8
|
-
strapi.app.use(async (ctx, next) => {
|
|
9
|
-
await next();
|
|
10
|
-
|
|
11
|
-
const responseFn = strapi.config.get(['functions', 'responses', ctx.status]);
|
|
12
|
-
if (_.isFunction(responseFn)) {
|
|
13
|
-
await responseFn(ctx);
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
},
|
|
17
|
-
};
|
|
18
|
-
};
|