Understanding the different types/categories of Strapi Hooks

Introduction

Goal

Prerequisites

  • Basic knowledge of JavaScript for Node.js
  • Basic understanding of Strapi — get started here.
  • Downloaded and installed latest Node.js v14.

Content-Type Hooks

  • create: Creates an entry in the database and returns the entry.
  • createMany: Creates multiple entries in the database returns the entry.
  • update: Updates an entry in the database and returns the entry.
  • updateMany: Update multuple entries in the database and returns the entry.
  • delete: Deletes an entry (or multiple entries at once) and return its value.
  • deleteMany: Deletes multiple entries at once and returns it value.
  • count: Returns the count of entries matching Strapi filters.
  • findOne: Returns the first entry matching some basic parameters.
  • findMany: Returns multiple entries matching some basic parameters
module.exports = {
lifecycles: {
afterCreate(event) {
const { result, params } = event;
await strapi.plugins["email"].services.email.send({
to: "customers@my-store.io",
from: "noreply@my-store.io",
subject: "New Product Available!",
text: "Check out this amazing new product :)",
});
},
},
};

Server Hooks

module.exports = strapi => {
const hook = {
/**
* Default options
*/
defaults: {
// config object
},

/**
* Initialize the hook
*/
async initialize() {
// Merge defaults and config/hook.json
const settings = {...this.defaults, ...strapi.config.hook.settings.**};

// await someAsyncCode()
},
};

return hook;
};
module.exports = {
settings: {
'hook-name': {
enabled: true,
applicationId: 'ABCDEFGHIJ',
apiKey: 'secure api_key',
debug: true, // default: false
prefix: 'my_own_prefix', // default: Strapi environment (stra pi.config.environment)
},
},
};
/strapi-hook-[...]
└─── lib
- index.js
- LICENSE.md
- package.json
- README.md
/project
└─── admin
└─── api
└─── config
│ - hook.js
└─── hooks
│ └─── strapi-my-local-hook
│ - index.js
└─── public
- favicon.ico
- package.json
- server.js

Webhooks

  • Name
  • URL
  • Request headers
  • Trigger events
  • create: When an entry is created.
  • update: When an entry is updated.
  • delete: When an entry is deleted.
  • publish: When an entry is published (available when draft and publish is enabled on the Content-Type).
  • unpublish: When an entry is unpublished (available when draft and publish is enabled on the Content-Type).
  • create: When a media asset is created.
  • update: When a media asset is updated.
  • delete: When a media asset is deleted.
{
"event": "entry.create",
"created_at": "2020-01-10T08:47:36.649Z",
"model": "address",
"entry": {
"id": 1,
"geolocation": {},
"city": "Paris",
"postal_code": null,
"category": null,
"full_name": "Paris",
"created_at": "2020-01-10T08:47:36.264Z",
"updated_at": "2020-01-10T08:47:36.264Z",
"cover": null,
"images": []
}
}
module.exports = {
webhooks: {
defaultHeaders: {
'Custom-Header': 'my-custom-header',
},
},
};

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Strapi

Strapi

2.1K Followers

The open source Headless CMS Front-End Developers love.