copykitku/src/vendor/github/vendormgr.ts
2020-08-21 11:53:55 +02:00

60 lines
1.8 KiB
TypeScript

/*
* Copycat. Copyright (C) 2020 selfisekai <laura@selfisekai.rocks> and other contributors.
*
* This is free software, and you are welcome to redistribute it
* under the GNU General Public License 3.0 or later; see the LICENSE file for details,
* or, if the file is unavailable, visit <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*/
import got from 'got';
import { VendorManager, VENDOR_TYPE, Vendor, RepoManager } from '../../types';
import { GHQuery } from './api-types';
import GitHubRepoManager from './repomgr';
export interface GitHubConfig {
token: string;
domain?: string | null;
}
export default class GitHubVendorManager implements VendorManager<GitHubConfig> {
vendor: Vendor;
config: GitHubConfig;
gqlEndpoint: string;
constructor(config: GitHubConfig) {
this.vendor = {
display:
config.domain && config.domain !== 'github.com'
? `Github Enterprise @ ${config.domain}`
: 'Microsoft GitHub',
type: VENDOR_TYPE.GITHUB,
domain: config.domain || 'github.com',
};
this.config = config;
this.gqlEndpoint = `https://${
this.vendor.domain === 'github.com'
? 'api.github.com'
: /* github enterprise server */ `${this.vendor.domain}/api`
}/graphql`;
}
public async initialize() {
return this;
}
public async getRepo(path: string) {
return new GitHubRepoManager(this, path).initialize() as Promise<RepoManager>;
}
/** internal and for RepoManager */
public async _doRequest(query: string, variables: any) {
return got
.post(this.gqlEndpoint, {
body: JSON.stringify({ query, variables }),
headers: { Authorization: `Bearer ${this.config.token}` },
})
.then((res) => JSON.parse(res.body))
.then((res) => res.data) as Promise<GHQuery>;
}
}