Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update/Fix Hooks Docs and Examples #9913

Closed
benhaynes opened this issue Nov 18, 2021 · 2 comments · Fixed by #10033
Closed

Update/Fix Hooks Docs and Examples #9913

benhaynes opened this issue Nov 18, 2021 · 2 comments · Fixed by #10033

Comments

@benhaynes
Copy link
Sponsor Member

We need to improve the documentation and examples for Event Hooks. I've heard many users complaining about this, with the comment below being the most recent. From a Discord User:

I would love to point out parts I feel could be better. I am a newcomer to coding, so a more experienced might find it a lot easier. But a lot more examples with different use cases of the types of hooks create, update, delete, etc.. would make it an easier start. Strapi does it here with their controllers: https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#controllers

Also, I still have a lot of troubles understanding the events e.g. schema, collection, input, accountability, payload, context and when to use which. They seem essential to creating hooks, but very little information about them is disclosed in the docs.

I understand that a Filter event executes before an event and Action does after. But I think it would make sense to add more human understandable context as to when it makes sense to use which over the other. So I can conclude on to which type of event I should use.

@paulboudewijn
Copy link
Contributor

The action hook section doesn't contain an example at the moment. It took me quite some time to figure out how to execute a custom query after an action. Perhaps it's useful to add this snippet:

https://docs.directus.io/extensions/hooks/#action

module.exports = function registerHook({ action }) {
	action('<collection name>.items.create', (input, { database }) => {
		database.raw('exec [dbo].[<storedproc name>]').then(() => {
			console.log("stored procedure executed");
		})
		.catch((e) => {
			console.log("error executing stored procedure. see error log for details.");
			console.error(e);
		});
	});
};

@WayneEld
Copy link
Sponsor

WayneEld commented Nov 22, 2021

Here is a working filter example if anyone is looking for it:

const axios = require('axios');
const logger = require('pino')()

module.exports = function registerHook({ filter }, { exceptions, database }) {
	const { InvalidPayloadException } = exceptions
	filter('<collection name>items.create', async (input) => {
		try {
                       // Make call to server
			var response = await axios.post('http://127.0.0.1:7000/invoke', { payload: input });
			
			// Handle response object
			input.client_code = response.data.client_code;
			return input;
		} catch {
			logger.info(error)
			throw new InvalidPayloadException(error);
		}
	});
};

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants