主题
发请求获取github仓库代码

代码实现
js
const { request } = require("urllib");
const downLoadGitRepo = require("download-git-repo");
const { promisify } = require("node:util");
const downLoadGit = promisify(downLoadGitRepo);
const baseURL = "https://api.github.com";
async function curl(url, options) {
try {
if (baseURL.endsWith("/")) {
url = baseURL + url.slice(1);
} else {
url = baseURL + url;
}
options = {
dataType: "json",
timeout: 30000,
...options,
};
const { data } = await request(url, options);
return data;
} catch (error) {
throw error;
}
}
/**
* 获取组织下的仓库列表
* @param {String} orgName 组织名称
* @returns Promise
*/
const getRepoList = async (orgName) => {
return await curl(`/orgs/${orgName}/repos`);
};
/**
* 获取仓库下的版本列表
* @param {String} org 组织名称
* @param {String} repo 仓库名称
* @returns Promise
*/
const getTagList = async (org, repo) => {
return await curl(`/repos/${org}/${repo}/tags`);
};
const downloadGiuhubZip = async (targetDir, { org, repo, tag }) => {
let repository = `/repos/${org}/${repo}/zipball/refs/tags/4${tag}`;
if (baseURL.endsWith("/")) {
repository = baseURL + repository.slice(1);
} else {
repository = baseURL + repository;
}
return await downLoadGit(repository, targetDir);
};
module.exports = {
getRepoList,
getTagList,
downloadGiuhubZip,
};