I know that under the src/api
folder are the entity folders each allowing 4 folders content-types, controllers, routes, and services. If I want to extend or change the behavior of an API then I intervene in the file under the controllers folder, possibly services as well. But this is only good for external calls.
For example if I want to do it when I use an external API I do the following under the foder /controllers
=> my-entity-name.js
:
"use strict";
/**
* my-entity-name controller
*/
const { createCoreController } = require("@strapi/strapi").factories;
module.exports = createCoreController("api::my-entity-name.my-entity-name", ({ strapi }) => ({
async create(ctx) {
//Do my stuff here
//create manually the entity or super.create(ctx)
}
}))
I need to intercept the creation of an entity when the latter is done from the backoffice. Basically I need to invoke an external service before creating it. I noticed that it does not go through the same endpoint as the external API but it uses an internal one made like this: /content-manager/collection-types/api::my-entity.my-entity
What should I do to intercept it?
you have to do model lifecycle:
src/api/contenType/models/contentType/lifecycles.js
module.exports = {
async beforeCreate(event) {
…
}
}
If you need to stop creation due to error you can throw new ApplicationError(‘message’)