From b7a8f69a819fa63e3982a3b7fbd9c8982247f8c2 Mon Sep 17 00:00:00 2001 From: xcv58 Date: Mon, 12 Apr 2021 01:50:05 -0700 Subject: [PATCH] Consolidate the getOAuthToken function and GITHUB_OAUTH_TOKEN variable (#282) --- extensions/github1s/src/commands/token.ts | 11 +++++------ extensions/github1s/src/helpers/constants.ts | 6 ++++++ extensions/github1s/src/helpers/context.ts | 3 ++- extensions/github1s/src/helpers/fetch.ts | 9 ++------- extensions/github1s/src/views/settings-view.ts | 5 +++-- 5 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 extensions/github1s/src/helpers/constants.ts diff --git a/extensions/github1s/src/commands/token.ts b/extensions/github1s/src/commands/token.ts index f716c9213..d788274cd 100644 --- a/extensions/github1s/src/commands/token.ts +++ b/extensions/github1s/src/commands/token.ts @@ -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; @@ -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) => { @@ -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( diff --git a/extensions/github1s/src/helpers/constants.ts b/extensions/github1s/src/helpers/constants.ts new file mode 100644 index 000000000..1c18980e7 --- /dev/null +++ b/extensions/github1s/src/helpers/constants.ts @@ -0,0 +1,6 @@ +/** + * @file extension constants variables + * @author xcv58 + */ + +export const GITHUB_OAUTH_TOKEN = 'github-oauth-token'; diff --git a/extensions/github1s/src/helpers/context.ts b/extensions/github1s/src/helpers/context.ts index 5dc9e88cb..3761b146d 100644 --- a/extensions/github1s/src/helpers/context.ts +++ b/extensions/github1s/src/helpers/context.ts @@ -4,6 +4,7 @@ */ import * as vscode from 'vscode'; +import { GITHUB_OAUTH_TOKEN } from './constants'; let extensionContext: vscode.ExtensionContext | null = null; @@ -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() !== ''; diff --git a/extensions/github1s/src/helpers/fetch.ts b/extensions/github1s/src/helpers/fetch.ts index cb53e719a..1d75d7d02 100644 --- a/extensions/github1s/src/helpers/fetch.ts +++ b/extensions/github1s/src/helpers/fetch.ts @@ -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); @@ -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 : {}; /** diff --git a/extensions/github1s/src/views/settings-view.ts b/extensions/github1s/src/views/settings-view.ts index d7fab51ce..14bc2a502 100644 --- a/extensions/github1s/src/views/settings-view.ts +++ b/extensions/github1s/src/views/settings-view.ts @@ -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; @@ -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( @@ -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'