Migrate middlwares to programmatic for plugin that do not need to export them

This commit is contained in:
Alexandre Bodin 2021-09-29 10:47:10 +02:00
parent 43c7e47f2b
commit c7a94ed081
11 changed files with 76 additions and 57 deletions

View File

@ -14,6 +14,7 @@
"dependencies": {
"@strapi/admin": "3.6.8",
"@strapi/plugin-documentation": "3.6.8",
"@strapi/plugin-sentry": "3.6.8",
"@strapi/plugin-graphql": "3.6.8",
"@strapi/plugin-i18n": "3.6.8",
"@strapi/plugin-users-permissions": "3.6.8",

View File

@ -10,6 +10,9 @@ const DELETE_FILE_MUTATION_NAME = 'removeFile';
const FILE_INFO_INPUT_TYPE_NAME = 'FileInfoInput';
/**
* @param {{ strapi: import('@strapi/strapi').Strapi }}
*/
module.exports = ({ strapi }) => {
const { service: getGraphQLService, config: graphQLConfig } = strapi.plugin('graphql');
const { service: getUploadService } = strapi.plugin('upload');

View File

@ -4,11 +4,16 @@ const { resolve } = require('path');
const range = require('koa-range');
const koaStatic = require('koa-static');
module.exports = () => {
/**
* Programmatic upload middleware. We do not want to export it
* @param {{ strapi: import('@strapi/strapi').Strapi }}
*/
module.exports = ({ strapi }) => {
const configPublicPath = strapi.config.get(
'middleware.settings.public.path',
strapi.config.paths.static
);
const staticDir = resolve(strapi.dirs.root, configPublicPath);
strapi.server.app.on('error', err => {

View File

@ -1,9 +1,13 @@
'use strict';
const registerUploadMiddlware = require('./controllers/upload-middleware');
const registerUploadMiddlware = require('./middlewares/upload');
/**
* Register upload plugin
* @param {{ strapi: import('@strapi/strapi').Strapi }}
*/
module.exports = async ({ strapi }) => {
await registerUploadMiddlware();
await registerUploadMiddlware({ strapi });
if (strapi.plugin('graphql')) {
require('./graphql')({ strapi });

View File

@ -6,6 +6,7 @@ const koaStatic = require('koa-static');
const initialRoutes = [];
// TODO: delete when refactoring documentation plugin for v4
module.exports = {
defaults: { documentation: { enabled: true } },
load: {

View File

@ -0,0 +1,13 @@
'use strict';
const register = require('./register');
const bootstrap = require('./bootstrap');
const services = require('./services');
const config = require('./config');
module.exports = () => ({
register,
bootstrap,
config,
services,
});

View File

@ -1,7 +0,0 @@
'use strict';
const sentry = require('./sentry');
module.exports = {
sentry,
};

View File

@ -0,0 +1,34 @@
'use strict';
/**
* Programmatic upload middleware. We do not want to export it
* @param {{ strapi: import('@strapi/strapi').Strapi }}
*/
module.exports = ({ strapi }) => {
const sentry = strapi.plugin('sentry').service('sentry');
sentry.init();
strapi.server.use(async (ctx, next) => {
try {
await next();
} catch (error) {
sentry.sendError(error, (scope, sentryInstance) => {
scope.addEventProcessor(event => {
// Parse Koa context to add error metadata
return sentryInstance.Handlers.parseRequest(event, ctx.request, {
// Don't parse the transaction name, we'll do it manually
transaction: false,
});
});
// Manually add transaction name
scope.setTag('transaction', `${ctx.method} ${ctx.request.url}`);
// Manually add Strapi version
scope.setTag('strapi_version', strapi.config.info.strapi);
scope.setTag('method', ctx.method);
});
throw error;
}
});
};

View File

@ -1,36 +0,0 @@
'use strict';
module.exports = {
defaults: { sentry: { enabled: true } },
load: {
beforeInitialize() {
strapi.config.middleware.load.after.unshift('sentry');
},
initialize() {
const sentry = strapi.plugin('sentry').service('sentry');
sentry.init();
strapi.server.use(async (ctx, next) => {
try {
await next();
} catch (error) {
sentry.sendError(error, (scope, sentryInstance) => {
scope.addEventProcessor(event => {
// Parse Koa context to add error metadata
return sentryInstance.Handlers.parseRequest(event, ctx.request, {
// Don't parse the transaction name, we'll do it manually
transaction: false,
});
});
// Manually add transaction name
scope.setTag('transaction', `${ctx.method} ${ctx.request.url}`);
// Manually add Strapi version
scope.setTag('strapi_version', strapi.config.info.strapi);
scope.setTag('method', ctx.method);
});
throw error;
}
});
},
},
};

View File

@ -0,0 +1,11 @@
'use strict';
const registerSentryMiddleware = require('./middlewares/sentry');
/**
* Register sentry plugin
* @param {{ strapi: import('@strapi/strapi').Strapi }}
*/
module.exports = ({ strapi }) => {
registerSentryMiddleware({ strapi });
};

View File

@ -1,13 +1,3 @@
'use strict';
const bootstrap = require('./server/bootstrap');
const services = require('./server/services');
const middlewares = require('./server/middlewares');
const config = require('./server/config');
module.exports = () => ({
bootstrap,
config,
middlewares,
services,
});
module.exports = require('./server');