Trying to set up a webhook system where the user can hook into any method of my express server by providing a method and an endpoint to listen to (eg PUT /movies/*
). This would send the edited movie to the user's callback url.
The challenges I'm facing here are:
- Not forcing the user to write a full-on regular expression (something like bash wildcards would be ideal, or the way node
path-to-regexp
library works)
- Retrieving all the hooks matching an endpoint when it is called
I'm really not sure how to deal with the first point, however I was able retrieve my webhooks using the solution provided here MongoDB reverse regex
private static async getWebhooks(endpoint: string, method: string): Promise<any[]> {
return await MongoDb.Instance.Models.Webhook.find({
$where: `"${endpoint}".match(this.endpoint)`,
method: method,
});
}
This doesn't exactly work as expected. Provided GET /movies/*
this matches both the GET /movies
and the GET /movies/:movieId
endpoints when it should only match the latter.