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



我在用Typescript编写的探针上编写自定义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");
});
}

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

我设法使它工作:

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

你可以用axios

例如:

import axios from "axios"
export class GithubServices {
public async writeCommentPR(repositoryName: string, numberPR: number, comment: string) {
try {
const url = `https://api.github.com/repos/${repositoryName}/issues/${numberPR}/comments`

const options = {
headers: {
Authorization: process.env.GITHUB_PAT || "",
Accept: 'application/vnd.github.v3+json',
ContentType: "application/json"
}
}
const payload = {
"body": comment
};
const response = await axios.post(
url,
payload,
options
)
} catch (error) {
console.log(error)
}
}

相关内容

  • 没有找到相关文章

最新更新