diff --git a/extensions/github-authentication/src/githubServer.ts b/extensions/github-authentication/src/githubServer.ts index bc27d5e1646..5851c2d8daf 100644 --- a/extensions/github-authentication/src/githubServer.ts +++ b/extensions/github-authentication/src/githubServer.ts @@ -126,25 +126,34 @@ export class GitHubServer { return; } - const authServerHost = query.staging ? AUTH_RELAY_STAGING_SERVER : AUTH_RELAY_SERVER; - - try { - const result = await fetch(`https://${authServerHost}/token?code=${code}&state=${query.state}`, { - method: 'POST', - headers: { - Accept: 'application/json' - } - }); - - if (result.ok) { - const json = await result.json(); + // TODO@joao: remove + if (query.staging) { + try { + const json: any = await vscode.commands.executeCommand('_workbench.fetchJSON', `https://${AUTH_RELAY_STAGING_SERVER}/token?code=${code}&state=${query.state}`, 'POST'); Logger.info('Token exchange success!'); resolve(json.access_token); - } else { - reject(result.statusText); + } catch (err) { + reject(err); + } + } else { + try { + const result = await fetch(`https://${AUTH_RELAY_SERVER}/token?code=${code}&state=${query.state}`, { + method: 'POST', + headers: { + Accept: 'application/json' + } + }); + + if (result.ok) { + const json = await result.json(); + Logger.info('Token exchange success!'); + resolve(json.access_token); + } else { + reject(result.statusText); + } + } catch (ex) { + reject(ex); } - } catch (ex) { - reject(ex); } }; diff --git a/src/vs/workbench/api/common/apiCommands.ts b/src/vs/workbench/api/common/apiCommands.ts index da918ae31e8..141c6bb03a4 100644 --- a/src/vs/workbench/api/common/apiCommands.ts +++ b/src/vs/workbench/api/common/apiCommands.ts @@ -12,6 +12,7 @@ import { IWorkspacesService, IRecent } from 'vs/platform/workspaces/common/works import { ILogService } from 'vs/platform/log/common/log'; import { IEnvironmentService } from 'vs/platform/environment/common/environment'; import { IViewDescriptorService, IViewsService, ViewVisibilityState } from 'vs/workbench/common/views'; +import { isWeb } from 'vs/base/common/platform'; // ----------------------------------------------------------------- // The following commands are registered on both sides separately. @@ -206,3 +207,15 @@ class DiffAPICommand { } } CommandsRegistry.registerCommand(DiffAPICommand.ID, adjustHandler(DiffAPICommand.execute)); + +if (isWeb) { + CommandsRegistry.registerCommand('_workbench.fetchJSON', async function (accessor: ServicesAccessor, url: string, method: string) { + const result = await fetch(url, { method, headers: { Accept: 'application/json' } }); + + if (result.ok) { + return result.json(); + } else { + throw new Error(result.statusText); + } + }); +}