Add middleware generator

This commit is contained in:
Alexandre Bodin 2021-10-26 09:37:33 +02:00
parent ed64650c25
commit 92b3fb7585
5 changed files with 55 additions and 3 deletions

View File

@ -7,6 +7,7 @@ const generateController = require('./plops/controller');
const generateContentType = require('./plops/content-type');
const generatePlugin = require('./plops/plugin');
const generatePolicy = require('./plops/policy');
const generateMiddleware = require('./plops/middleware');
const generateService = require('./plops/service');
module.exports = plop => {
@ -20,5 +21,6 @@ module.exports = plop => {
generateContentType(plop);
generatePlugin(plop);
generatePolicy(plop);
generateMiddleware(plop);
generateService(plop);
};

View File

@ -0,0 +1,36 @@
'use strict';
const getDestinationPrompts = require('./utils/get-destination-prompts');
module.exports = plop => {
// middleware generator
plop.setGenerator('middleware', {
description: 'Generate a middleware for an API',
prompts: [
{
type: 'input',
name: 'name',
message: 'Middleware name',
},
...getDestinationPrompts('middleware', plop.getDestBasePath(), { rootFolder: true }),
],
actions(answers) {
let filePath;
if (answers.destination === 'api') {
filePath = `api/{{ api }}`;
} else if (answers.destination === 'plugin') {
filePath = `plugins/{{ plugin }}`;
} else {
filePath = `./`;
}
return [
{
type: 'add',
path: `${filePath}/middlewares/{{ name }}.js`,
templateFile: 'templates/middleware.js.hbs',
},
];
},
});
};

View File

@ -12,7 +12,7 @@ module.exports = plop => {
name: 'id',
message: 'Policy name',
},
...getDestinationPrompts('policy', plop.getDestBasePath()),
...getDestinationPrompts('policy', plop.getDestBasePath(), { rootFolder: true }),
],
actions(answers) {
let filePath;

View File

@ -2,7 +2,7 @@
const { join } = require('path');
const fs = require('fs-extra');
module.exports = (action, basePath) => {
module.exports = (action, basePath, { rootFolder = false } = {}) => {
return [
{
type: 'list',
@ -10,7 +10,7 @@ module.exports = (action, basePath) => {
message: `Where do you want to add this ${action}?`,
choices: [
{
name: `Add ${action} to ${action === 'policy' ? 'root of project' : 'new API'}`,
name: `Add ${action} to ${rootFolder ? 'root of project' : 'new API'}`,
value: 'new',
},
{ name: `Add ${action} to an existing API`, value: 'api' },

View File

@ -0,0 +1,14 @@
'use strict';
/**
* `{{ name }}` middleware.
*/
module.exports = async (config, { strapi }) => {
// Add your own logic here.
return async (ctx, next) => {
strapi.log.info('In {{ name }} middleware.');
await next();
};
};