|
| 1 | +import * as vscode from 'vscode'; |
| 2 | + |
| 3 | +export interface IHostConfiguration { |
| 4 | + host: string; |
| 5 | + username: string | undefined; |
| 6 | + token: string | undefined; |
| 7 | +} |
| 8 | + |
| 9 | +export const HostHelper = class { |
| 10 | + public static getApiHost(host: IHostConfiguration | vscode.Uri): vscode.Uri { |
| 11 | + const hostUri: vscode.Uri = host instanceof vscode.Uri ? host : vscode.Uri.parse(host.host); |
| 12 | + if (hostUri.authority === 'github.com') { |
| 13 | + return vscode.Uri.parse('https://api.github.com'); |
| 14 | + } else { |
| 15 | + return vscode.Uri.parse(`${hostUri.scheme}://${hostUri.authority}`); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + public static getApiPath(host: IHostConfiguration | vscode.Uri, path: string): string { |
| 20 | + const hostUri: vscode.Uri = host instanceof vscode.Uri ? host : vscode.Uri.parse(host.host); |
| 21 | + if (hostUri.authority === 'github.com') { |
| 22 | + return path; |
| 23 | + } else { |
| 24 | + return `/api/v3${path}`; |
| 25 | + } |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +export interface IConfiguration extends IHostConfiguration { |
| 30 | + onDidChange: vscode.Event<IConfiguration>; |
| 31 | +} |
| 32 | + |
| 33 | +export class Configuration implements IConfiguration { |
| 34 | + public username: string | undefined; |
| 35 | + public token: string | undefined; |
| 36 | + public onDidChange: vscode.Event<IConfiguration>; |
| 37 | + private _emitter: vscode.EventEmitter<IConfiguration>; |
| 38 | + |
| 39 | + constructor(public host: string) { |
| 40 | + this._emitter = new vscode.EventEmitter<IConfiguration>(); |
| 41 | + this.onDidChange = this._emitter.event; |
| 42 | + } |
| 43 | + |
| 44 | + public update(username: string | undefined, token: string | undefined, raiseEvent: boolean = true): void { |
| 45 | + if (username !== this.username || token !== this.token) { |
| 46 | + this.username = username; |
| 47 | + this.token = token; |
| 48 | + if (raiseEvent) { |
| 49 | + this._emitter.fire(this); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments