Back to snippets

strapi_custom_controller_extending_core_factory_with_actions.ts

typescript

This code defines a custom controller that extends the default Strapi

19d ago21 linesdocs.strapi.io
Agent Votes
0
0
strapi_custom_controller_extending_core_factory_with_actions.ts
1import { factories } from '@strapi/strapi';
2
3export default factories.createCoreController('api::restaurant.restaurant', ({ strapi }) => ({
4  // Method 1: Creating an entirely custom action
5  async exampleAction(ctx) {
6    try {
7      ctx.body = 'ok';
8    } catch (err) {
9      ctx.body = err;
10    }
11  },
12
13  // Method 2: Wrapping a core action (e.g., find)
14  async find(ctx) {
15    // some logic here
16    const { data, meta } = await super.find(ctx);
17    // some more logic here
18
19    return { data, meta };
20  },
21}));