@fancode/react-native-codepush-joystick
Version:
A flexible CodePush Joystick for React Native apps
61 lines • 2.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GitHubProvider = void 0;
class GitHubProvider {
config;
constructor(config) {
this.config = config;
}
async makeRequest(endpoint, options = {}, expectedStatus = 200, timeoutMs) {
const url = `https://api.github.com/repos/${this.config.owner}/${this.config.repo}${endpoint}`;
let controller;
let signal;
if (timeoutMs) {
controller = new AbortController();
signal = controller.signal;
setTimeout(() => controller.abort(), timeoutMs);
}
const response = await fetch(url, {
...options,
signal,
headers: {
Authorization: `Bearer ${this.config.token}`,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
"Content-Type": "application/json",
...options.headers,
},
});
if (response.status !== expectedStatus) {
throw new Error(`GitHub API error: ${response.status} ${response.statusText}`);
}
if (response.status === 204 || response.status === 202) {
return null;
}
const contentType = response.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
return response.json();
}
else {
return response.text();
}
}
async fetchPullRequests(options = {}) {
const params = new URLSearchParams();
if (options.state)
params.append("state", options.state);
if (options.per_page)
params.append("per_page", options.per_page.toString());
if (options.page)
params.append("page", options.page.toString());
if (options.sort)
params.append("sort", options.sort);
if (options.direction)
params.append("direction", options.direction);
const queryString = params.toString();
const endpoint = `/pulls${queryString ? `?${queryString}` : ""}`;
return this.makeRequest(endpoint);
}
}
exports.GitHubProvider = GitHubProvider;
//# sourceMappingURL=github-provider.js.map