mirror of
https://github.com/strapi/strapi.git
synced 2025-12-27 23:24:03 +00:00
Add middleware generator
This commit is contained in:
parent
ed64650c25
commit
92b3fb7585
@ -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);
|
||||
};
|
||||
|
||||
36
packages/generators/generators/lib/plops/middleware.js
Normal file
36
packages/generators/generators/lib/plops/middleware.js
Normal 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',
|
||||
},
|
||||
];
|
||||
},
|
||||
});
|
||||
};
|
||||
@ -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;
|
||||
|
||||
@ -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' },
|
||||
|
||||
@ -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();
|
||||
};
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user