Skip to content

Commit

Permalink
Consolidate the getOAuthToken function and GITHUB_OAUTH_TOKEN variable (
Browse files Browse the repository at this point in the history
  • Loading branch information
xcv58 authored Apr 12, 2021
1 parent a253db0 commit b7a8f69
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
11 changes: 5 additions & 6 deletions extensions/github1s/src/commands/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
*/

import * as vscode from 'vscode';
import { getExtensionContext } from '@/helpers/context';
import { getExtensionContext, getOAuthToken } from '@/helpers/context';
import { validateToken } from '@/interfaces/github-api-rest';
import { GITHUB_OAUTH_TOKEN } from '@/helpers/constants';

export const commandValidateToken = (silent: boolean = false) => {
const context = getExtensionContext();
const oAuthToken =
(context.globalState.get('github-oauth-token') as string) || '';
const oAuthToken = getOAuthToken();
return validateToken(oAuthToken).then((tokenStatus) => {
if (!silent) {
const remaining = tokenStatus.remaining;
Expand Down Expand Up @@ -52,7 +51,7 @@ export const commandUpdateToken = (silent: boolean = false) => {
return;
}
return getExtensionContext()!
.globalState.update('github-oauth-token', token || '')
.globalState.update(GITHUB_OAUTH_TOKEN, token || '')
.then(() => {
// we don't need wait validate, so we don't `return`
validateToken(token).then((tokenStatus) => {
Expand Down Expand Up @@ -91,7 +90,7 @@ export const commandClearToken = (silent: boolean = false) => {
.then((choose) => {
if (choose === 'Confirm') {
return getExtensionContext()!
.globalState.update('github-oauth-token', '')
.globalState.update(GITHUB_OAUTH_TOKEN, '')
.then(() => {
!silent &&
vscode.window.showInformationMessage(
Expand Down
6 changes: 6 additions & 0 deletions extensions/github1s/src/helpers/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @file extension constants variables
* @author xcv58
*/

export const GITHUB_OAUTH_TOKEN = 'github-oauth-token';
3 changes: 2 additions & 1 deletion extensions/github1s/src/helpers/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import * as vscode from 'vscode';
import { GITHUB_OAUTH_TOKEN } from './constants';

let extensionContext: vscode.ExtensionContext | null = null;

Expand All @@ -23,7 +24,7 @@ export const getExtensionContext = (): vscode.ExtensionContext => {

export const getOAuthToken = () => {
const context = getExtensionContext();
return (context.globalState.get('github-oauth-token') as string) || '';
return (context.globalState.get(GITHUB_OAUTH_TOKEN) as string) || '';
};

export const hasValidToken = () => getOAuthToken() !== '';
9 changes: 2 additions & 7 deletions extensions/github1s/src/helpers/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@

import * as vscode from 'vscode';
import { reuseable, throttle } from './func';
import { getExtensionContext } from './context';
import { getOAuthToken } from './context';
import { noop } from './util';

const getGitHubAuthToken = (): string => {
const context = getExtensionContext();
return context?.globalState.get('github-oauth-token') || '';
};

export class RequestError extends Error {
constructor(message: string, public token: string) {
super(message);
Expand Down Expand Up @@ -65,7 +60,7 @@ export const getFetchOptions = (forceUpdate?: boolean): RequestInit => {
const cache = new Map();

export const fetch = reuseable(async (url: string, options?: RequestInit) => {
const token = getGitHubAuthToken();
const token = getOAuthToken();
const authHeaders = token ? { Authorization: `token ${token}` } : {};
const customHeaders = options && 'headers' in options ? options.headers : {};
/**
Expand Down
5 changes: 3 additions & 2 deletions extensions/github1s/src/views/settings-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as vscode from 'vscode';
import { getExtensionContext } from '@/helpers/context';
import { getNonce, getWebviewOptions } from '@/helpers/util';
import { validateToken } from '@/interfaces/github-api-rest';
import { GITHUB_OAUTH_TOKEN } from '@/helpers/constants';

interface WebviewState {
token?: string;
Expand Down Expand Up @@ -62,7 +63,7 @@ export class SettingsView implements vscode.WebviewViewProvider {
default:
const oauthToken =
(this._extensionContext.globalState.get(
'github-oauth-token'
GITHUB_OAUTH_TOKEN
) as string) || '';
(oauthToken
? validateToken(oauthToken).then(
Expand Down Expand Up @@ -136,7 +137,7 @@ export class SettingsView implements vscode.WebviewViewProvider {
validating: false,
});
this._extensionContext.globalState
.update('github-oauth-token', token || '')
.update(GITHUB_OAUTH_TOKEN, token || '')
.then(() => {
vscode.commands.executeCommand(
'workbench.files.action.refreshFilesExplorer'
Expand Down

0 comments on commit b7a8f69

Please sign in to comment.