Extending routes into the write api plugin

Plugin Development
  • I've seen in the write api plugin that there is a comment referring to plugins being able to add their own routes into the write api plugin.

    Does this mean that we can extend the v1 api to route to an endpoint within a custom plugin? If so, how would you consume this, or is this feature not fully implemented yet?

    Thanks.

  • @snodejoke This is implemented. The write-api fires off the following hook after it implements its own routes: filter:plugin.write-api.routes

    It passes in router, apiMiddleware, middleware, and errorHandler, so given that you can style your own routes like I do in the write api, e.g.:

    var addRoutes = function(data, callback) {
      data.router.get('/awesome', apiMiddleware.requireUser, function(req, res) {
        console.log('lorem ipsum!');
        res.sendStatus(200);
      });
    
      callback(null, data);
    });
    

    In the above example, I used the requireUser middleware, which, provided by the write-api, means you have to pass in an user/master token just like the other routes. It also sets req.user.uid for you to use.

    Edit: You can see it in use here in the friends plugin

  • Thanks a lot. This should save me loads of time! 🙂


Suggested Topics