typescriptprobot

How add Probot HTTP routes in Typescript?


I'm having a hard time writing custom HTTP routes on my probot written in Typescript.

The only example in documentation is pure JS but I couldn't figure out how to translate it to TS.

 module.exports = (app, { getRouter }) => {
  // Get an express router to expose new HTTP endpoints
  const router = getRouter("/my-app");

  // Use any middleware
  router.use(require("express").static("public"));

  // Add a new route
  router.get("/hello-world", (req, res) => {
    res.send("Hello World");
  });
};

https://probot.github.io/docs/http/


Solution

  • This should do the trick:

    import { ApplicationFunctionOptions, Probot, } from "probot"
    
    export default (app: Probot, { getRouter }: ApplicationFunctionOptions) => {
    
      if (!getRouter) return
    
      const router = getRouter("/my-app");
    
      router.use(require("express").static("public"));
    
      // Add a new route
      router.get("/hello-world", (req, res) => {
        res.send("Hello World");
      });
    }
    

    In ApplicationFunctionOptions getRouter is optional. That's why i added a quick check in the beginning of the function.