gh auth: fetch json

This commit is contained in:
João Moreno 2021-04-21 10:47:57 +02:00
parent 186897d756
commit d60dbbf646
No known key found for this signature in database
GPG key ID: 896B853774D1A575
2 changed files with 38 additions and 16 deletions

View file

@ -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);
}
};

View file

@ -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);
}
});
}