2017-02-06 13:40:28 -05:00
|
|
|
// Imports
|
|
|
|
import {IncomingMessage} from 'http';
|
|
|
|
import * as https from 'https';
|
2017-02-27 14:04:43 -05:00
|
|
|
import {assertNotMissingOrEmpty} from './utils';
|
2017-02-06 13:40:28 -05:00
|
|
|
|
|
|
|
// Constants
|
|
|
|
const GITHUB_HOSTNAME = 'api.github.com';
|
|
|
|
|
|
|
|
// Interfaces - Types
|
|
|
|
interface RequestParams {
|
|
|
|
[key: string]: string | number;
|
|
|
|
}
|
|
|
|
|
|
|
|
type RequestParamsOrNull = RequestParams | null;
|
|
|
|
|
|
|
|
// Classes
|
|
|
|
export class GithubApi {
|
|
|
|
protected requestHeaders: {[key: string]: string};
|
|
|
|
|
|
|
|
// Constructor
|
2017-02-28 07:17:20 -05:00
|
|
|
constructor(githubToken: string) {
|
2017-02-27 15:40:13 -05:00
|
|
|
assertNotMissingOrEmpty('githubToken', githubToken);
|
2017-02-06 13:40:28 -05:00
|
|
|
|
2017-02-27 15:40:13 -05:00
|
|
|
this.requestHeaders = {
|
|
|
|
'Authorization': `token ${githubToken}`,
|
|
|
|
'User-Agent': `Node/${process.versions.node}`,
|
|
|
|
};
|
2017-02-06 13:40:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Methods - Public
|
2018-05-10 08:56:07 -04:00
|
|
|
public get<T = any>(pathname: string, params?: RequestParamsOrNull): Promise<T> {
|
2017-02-06 13:40:28 -05:00
|
|
|
const path = this.buildPath(pathname, params);
|
|
|
|
return this.request<T>('get', path);
|
|
|
|
}
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
public post<T = any>(pathname: string, params?: RequestParamsOrNull, data?: any): Promise<T> {
|
2017-02-06 13:40:28 -05:00
|
|
|
const path = this.buildPath(pathname, params);
|
|
|
|
return this.request<T>('post', path, data);
|
|
|
|
}
|
|
|
|
|
2018-09-15 08:44:38 -04:00
|
|
|
// In GitHub API paginated requests, page numbering is 1-based. (https://developer.github.com/v3/#pagination)
|
|
|
|
public getPaginated<T>(pathname: string, baseParams: RequestParams = {}, currentPage: number = 1): Promise<T[]> {
|
2017-02-28 07:01:20 -05:00
|
|
|
const perPage = 100;
|
|
|
|
const params = {
|
|
|
|
...baseParams,
|
|
|
|
page: currentPage,
|
|
|
|
per_page: perPage,
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.get<T[]>(pathname, params).then(items => {
|
|
|
|
if (items.length < perPage) {
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2017-06-17 14:03:10 -04:00
|
|
|
return this.getPaginated<T>(pathname, baseParams, currentPage + 1).then(moreItems => [...items, ...moreItems]);
|
2017-02-28 07:01:20 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
// Methods - Protected
|
2018-08-10 06:10:22 -04:00
|
|
|
protected buildPath(pathname: string, params?: RequestParamsOrNull): string {
|
2018-05-10 08:56:07 -04:00
|
|
|
if (params == null) {
|
|
|
|
return pathname;
|
|
|
|
}
|
|
|
|
|
|
|
|
const search = (params === null) ? '' : this.serializeSearchParams(params);
|
|
|
|
const joiner = search && '?';
|
|
|
|
|
|
|
|
return `${pathname}${joiner}${search}`;
|
|
|
|
}
|
|
|
|
|
2018-08-10 06:10:22 -04:00
|
|
|
protected request<T>(method: string, path: string, data: any = null): Promise<T> {
|
2017-02-06 13:40:28 -05:00
|
|
|
return new Promise<T>((resolve, reject) => {
|
|
|
|
const options = {
|
|
|
|
headers: {...this.requestHeaders},
|
|
|
|
host: GITHUB_HOSTNAME,
|
|
|
|
method,
|
|
|
|
path,
|
|
|
|
};
|
|
|
|
|
|
|
|
const onError = (statusCode: number, responseText: string) => {
|
|
|
|
const url = `https://${GITHUB_HOSTNAME}${path}`;
|
|
|
|
reject(`Request to '${url}' failed (status: ${statusCode}): ${responseText}`);
|
|
|
|
};
|
|
|
|
const onSuccess = (responseText: string) => {
|
2018-05-10 08:56:07 -04:00
|
|
|
try { resolve(responseText && JSON.parse(responseText)); } catch (err) { reject(err); }
|
2017-02-06 13:40:28 -05:00
|
|
|
};
|
|
|
|
const onResponse = (res: IncomingMessage) => {
|
|
|
|
const statusCode = res.statusCode || -1;
|
|
|
|
const isSuccess = (200 <= statusCode) && (statusCode < 400);
|
|
|
|
let responseText = '';
|
|
|
|
|
|
|
|
res.
|
|
|
|
on('data', d => responseText += d).
|
|
|
|
on('end', () => isSuccess ? onSuccess(responseText) : onError(statusCode, responseText)).
|
|
|
|
on('error', reject);
|
|
|
|
};
|
|
|
|
|
|
|
|
https.
|
|
|
|
request(options, onResponse).
|
|
|
|
on('error', reject).
|
|
|
|
end(data && JSON.stringify(data));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-10 06:10:22 -04:00
|
|
|
protected serializeSearchParams(params: RequestParams): string {
|
2017-02-06 13:40:28 -05:00
|
|
|
return Object.keys(params).
|
|
|
|
filter(key => params[key] != null).
|
|
|
|
map(key => `${key}=${encodeURIComponent(String(params[key]))}`).
|
|
|
|
join('&');
|
|
|
|
}
|
|
|
|
}
|