Home:ALL Converter>Mongodb reverse match API endpoint path

Mongodb reverse match API endpoint path

Ask Time:2019-08-07T17:33:27         Author:Marius

Json Formatter

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:

  1. 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)
  2. 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.

Author:Marius,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/57391258/mongodb-reverse-match-api-endpoint-path
yy