I'm trying create a plugin that serve to strapi community to have ready backend post request function of the content type.
src/server/code-generation/generateBackendFunction.js:
'use strict';
// Import required modules
const Mustache = require('mustache');
// Define the generateBackendFunction function
async function generateBackendCode(model, method) {
// Sample model schema
const modelSchema = {
kind: 'collectionType',
collectionName: 'blogs',
attributes: {
Title: { type: 'string', required: true, maxLength: 20 },
Description: { type: 'string', required: true },
image: { type: 'string', required: true },
Sujet: { type: 'string' },
},
};
// Sample HTTP request type
const requestType = 'POST';
// Define a template for the backend function
const template = `
module.exports = {
async handle{{RequestType}}Request(ctx) {
const { {{Attributes}} } = ctx.request.body;
try {
// Add your business logic here
// e.g., create a new blog entry in the database
const newBlog = await strapi.services.blog.create({ {{Attributes}} });
ctx.send(newBlog);
} catch (error) {
ctx.throw(500, 'Error handling {{RequestType}} request', { error });
}
},
};
`;
// Extract attribute names from the model schema
const attributes = Object.keys(modelSchema.attributes);
// Render the template with the extracted attributes and request type
const renderedCode = Mustache.render(template, {
RequestType: requestType.toUpperCase(),
Attributes: attributes.join(', '),
});
// Return the rendered code
return renderedCode;
}
// Export the generateBackendFunction function`
module.exports = generateBackendCode;
src/server/controllers/GenerateBackendFuntion.js:
'use strict';
const generateBackendCode = require('…/code-generationgenerateBackendFunction'); // Corrected import
module.exports = {
async generateBackendFunction(ctx) {
try {
const { model, method } = ctx.request.body;
const backendCode = await generateBackendCode(model, method);
ctx.send({ backendCode });
} catch (error) {
console.error('Error generating backend code:', error);
ctx.throw(500, 'Internal server error');
}
}
};
src/server/routes/index.js:
{
method: “POST”,
path: “/api/generate-backend-function”,
handler: “GenerateBackendFunction.generateBackendFunction”,
config: {
auth: false
}`
}
But when I run npm run develop I get this error:
⠦ Loading Strapi[ERROR] There seems to be an unexpected error, try again with --debug for more information
TypeError: Error creating endpoint POST /api/generate-backend-function: Cannot read properties of undefined (reading 'generateBackendFunction') at getAction (C:\Users\USER\Downloads\StrapiGen=master 1\StrapiGen=master\StrapiGEN\node_mo dules@strapi\strapi\dist\services\server\compose-endpoint.js:104:24) at C:\Users\USER\Downloads\StrapiGen=master 1\StrapiGen=master\StrapiGEN\node_modules@stra pi\strapi\dist\services\server\compose-endpoint.js:56:22 at createRoute (C:\Users\USER\Downloads\StrapiGen=master 1\StrapiGen=master\StrapiGEN\node_ modules@strapi\strapi\dist\services\server\routing.js:71:5) at C:\Users\USER\Downloads\StrapiGen=master 1\StrapiGen=master\StrapiGEN\node_modules@stra pi\strapi\dist\services\server\routing.js:80:9 at Array.forEach () at Object.addRoutes (C:\Users\USER\Downloads\StrapiGen=master 1\StrapiGen=master\StrapiGEN\ node_modules@strapi\strapi\dist\services\server\routing.js:78:21) at Object.routes (C:\Users\USER\Downloads\StrapiGen=master 1\StrapiGen=master\StrapiGEN\node_modules@strapi\strapi\dist\services\server\api.js:20:20) at Object.routes (C:\Users\USER\Downloads\StrapiGen=master 1\StrapiGen=master\StrapiGEN\nod e_modules@strapi\strapi\dist\services\server\index.js:52:27) at registerPluginRoutes (C:\Users\USER\Downloads\StrapiGen=master 1\StrapiGen=master\Strapi GEN\node_modules@strapi\strapi\dist\services\server\register-routes.js:43:21) at registerAllRoutes (C:\Users\USER\Downloads\StrapiGen=master 1\StrapiGen=master\StrapiGEN \node_modules@strapi\strapi\dist\services\server\register-routes.js:20:3) `