如何在 Typescript 中添加 Probot HTTP 路由?

How add Probot HTTP routes in Typescript?

我很难在用 Typescript 编写的 probot 上编写自定义 HTTP 路由。

文档中唯一的示例是纯 JS,但我不知道如何将其转换为 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/

这应该可以解决问题:

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");
  });
}

ApplicationFunctionOptionsgetRouter是可选的。这就是为什么我在函数的开头添加了一个快速检查。

我设法让它工作于:

import {ApplicationFunctionOptions, Probot} from "probot";


export = (app: Probot, { getRouter }: ApplicationFunctionOptions) => {

  // Get an express router to expose new HTTP endpoints
  if (getRouter) {
    const router = getRouter()
    // Use any middleware
    router.use(require("express").static("public"));

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

  app.on("issues.opened", async (context) => {
    const issueComment = context.issue({
      body: "Thanks for opening this issue!",
    });
    await context.octokit.issues.createComment(issueComment);
  });
...