From 0db7b3c88dab724ebf034c46c6ce5a3c69cf5147 Mon Sep 17 00:00:00 2001 From: Nathan Smith Date: Thu, 28 Mar 2024 16:53:50 -0400 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=94=A8=20refactor:=20#61=20Make=20log?= =?UTF-8?q?ger=20and=20plugin=20settings=20constants?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/github/api.ts | 16 ++++++++-------- src/github/github.ts | 2 +- src/logger.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/plugin.ts | 10 +++++----- src/settings.ts | 2 +- src/util.ts | 35 ----------------------------------- 6 files changed, 59 insertions(+), 50 deletions(-) create mode 100644 src/logger.ts diff --git a/src/github/api.ts b/src/github/api.ts index 13a78bd..f223142 100644 --- a/src/github/api.ts +++ b/src/github/api.ts @@ -14,7 +14,7 @@ import type { RequestUrlParam, RequestUrlResponse } from "obsidian"; import { RequestError, promiseWithResolvers } from "src/util"; import { Notice, requestUrl } from "obsidian"; -import { Logger } from "src/plugin"; +import { logger } from "src/plugin"; import Queue from "queue"; const baseApi = "https://api.github.com"; @@ -41,7 +41,7 @@ export async function queueRequest(config: RequestUrlParam, token?: string): Pro async function githubRequest(config: RequestUrlParam, token?: string): Promise { if (rateLimitReset !== null && rateLimitReset > new Date()) { - Logger.warn( + logger.warn( `GitHub rate limit exceeded. No more requests will be made until ${rateLimitReset.toLocaleTimeString()}`, ); throw new Error("GitHub rate limit exceeded."); @@ -59,18 +59,18 @@ async function githubRequest(config: RequestUrlParam, token?: string): Promise { const account = getAccount(org); - if (!account || !account.token) { + if (!account?.token) { return []; } const _params = sanitizeObject(params, { diff --git a/src/logger.ts b/src/logger.ts new file mode 100644 index 0000000..f67dd89 --- /dev/null +++ b/src/logger.ts @@ -0,0 +1,44 @@ +export enum LogLevel { + Error = 0, + Warn = 1, + Info = 2, + Debug = 3, +} + +export class Logger { + public logLevel: LogLevel = LogLevel.Error; + public log(message: unknown, level: LogLevel) { + if (level <= this.logLevel) { + switch (level) { + case LogLevel.Error: + console.error(message); + break; + case LogLevel.Warn: + console.warn(message); + break; + case LogLevel.Info: + console.info(message); + break; + case LogLevel.Debug: + console.debug(message); + break; + } + } + } + + public error(message: unknown) { + this.log(message, LogLevel.Error); + } + + public warn(message: unknown) { + this.log(message, LogLevel.Warn); + } + + public info(message: unknown) { + this.log(message, LogLevel.Info); + } + + public debug(message: unknown) { + this.log(message, LogLevel.Debug); + } +} diff --git a/src/plugin.ts b/src/plugin.ts index 9e5036d..5488dad 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,5 +1,5 @@ import { DEFAULT_SETTINGS, GithubLinkPluginSettingsTab } from "./settings"; -import { LogLevel, verboseFactory } from "./util"; +import { Logger } from "./logger"; import type { GithubLinkPluginSettings } from "./settings"; import { InlineRenderer } from "./inline/inline"; @@ -7,13 +7,13 @@ import { Plugin } from "obsidian"; import { QueryProcessor } from "./query/processor"; import { createInlineViewPlugin } from "./inline/view-plugin"; -export let PluginSettings: GithubLinkPluginSettings = { ...DEFAULT_SETTINGS }; -export let Logger = verboseFactory(LogLevel.Error); +export const PluginSettings: GithubLinkPluginSettings = { ...DEFAULT_SETTINGS }; +export const logger = new Logger(); export class GithubLinkPlugin extends Plugin { async onload() { - PluginSettings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); - Logger = verboseFactory(PluginSettings.logLevel); + Object.assign(PluginSettings, await this.loadData()); + logger.logLevel = PluginSettings.logLevel; this.addSettingTab(new GithubLinkPluginSettingsTab(this.app, this)); this.registerMarkdownPostProcessor(InlineRenderer); diff --git a/src/settings.ts b/src/settings.ts index 22fb79b..6997ee9 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -4,7 +4,7 @@ import { PluginSettingTab, Setting } from "obsidian"; import type { App } from "obsidian"; import { AuthModal } from "./auth-modal"; import type { GithubLinkPlugin } from "./plugin"; -import { LogLevel } from "./util"; +import { LogLevel } from "./logger"; import { PluginSettings } from "./plugin"; import type { Verification } from "@octokit/auth-oauth-device/dist-types/types"; import { auth } from "./github/auth"; diff --git a/src/util.ts b/src/util.ts index f1b3c6e..ba0f810 100644 --- a/src/util.ts +++ b/src/util.ts @@ -2,41 +2,6 @@ export type RemoveIndexSignature = { [K in keyof T as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: T[K]; }; -export enum LogLevel { - Error = 0, - Warn = 1, - Info = 2, - Debug = 3, -} - -export function verboseFactory(logLevel: LogLevel) { - const log = (message: unknown, level: LogLevel) => { - if (level <= logLevel) { - switch (level) { - case LogLevel.Error: - console.error(message); - break; - case LogLevel.Warn: - console.warn(message); - break; - case LogLevel.Info: - console.info(message); - break; - case LogLevel.Debug: - console.debug(message); - break; - } - } - }; - return { - log, - error: (message: unknown) => log(message, LogLevel.Error), - warn: (message: unknown) => log(message, LogLevel.Warn), - info: (message: unknown) => log(message, LogLevel.Info), - debug: (message: unknown) => log(message, LogLevel.Debug), - }; -} - export function titleCase(value: string): string { const words = value.split(/[-_]/); return words.map((w) => w.charAt(0)?.toUpperCase() + w.slice(1)).join(" "); From ffbded9df982bd255805265586a787414d5943a1 Mon Sep 17 00:00:00 2001 From: Nathan Smith Date: Thu, 28 Mar 2024 17:02:26 -0400 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=A8=20refactor:=20#61=20Reduce=20c?= =?UTF-8?q?omplexity=20of=20url=20parse=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes: #61 --- src/github/url-parse.ts | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/src/github/url-parse.ts b/src/github/url-parse.ts index bd20148..8486490 100644 --- a/src/github/url-parse.ts +++ b/src/github/url-parse.ts @@ -32,45 +32,36 @@ export function parseUrl(urlString: string): ParsedUrl { const parsedUrl: ParsedUrl = { url: urlString, host: url.hostname }; const urlParts = url.pathname.split("/"); - if (urlParts.length >= 4) { + if (urlParts.length > 4) { + const issueNumber = parseInt(urlParts[4], 10); switch (urlParts[3].toLowerCase()) { case "issues": - if (urlParts[4]) { - const issueNumber = parseInt(urlParts[4], 10); - if (!isNaN(issueNumber)) { - parsedUrl.issue = issueNumber; - } + if (!isNaN(issueNumber)) { + parsedUrl.issue = issueNumber; } break; case "pull": - if (urlParts[4]) { - const prNumber = parseInt(urlParts[4], 10); - if (!isNaN(prNumber)) { - parsedUrl.pr = prNumber; - } + if (!isNaN(issueNumber)) { + parsedUrl.pr = issueNumber; } break; case "blob": parsedUrl.code = {}; - if (urlParts[4]) { - parsedUrl.code.branch = urlParts[4]; - } + parsedUrl.code.branch = urlParts[4]; if (urlParts[5]) { const pathParts = urlParts.slice(5); parsedUrl.code.path = pathParts.join("/"); } break; case "commit": - if (urlParts[4]) { - parsedUrl.commit = urlParts.slice(4).join("/"); - } + parsedUrl.commit = urlParts.slice(4).join("/"); break; } } - if (urlParts.length >= 3) { + if (urlParts.length > 2) { parsedUrl.repo = urlParts[2]; } - if (urlParts.length >= 2) { + if (urlParts.length > 1) { parsedUrl.org = urlParts[1]; }