From 0d7312d0c2bcabd63409d9759c602876f920bc7a Mon Sep 17 00:00:00 2001 From: Ealhad Date: Mon, 12 Nov 2018 15:54:49 +0100 Subject: [PATCH] Make some small style improvements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - let data = await InvidiousAPI.getVideo(id); + const data = await InvidiousAPI.getVideo(id); I try to use `const` whenever I don't have to use `let`. While it's not a guarantee as strong as what e.g. Rust can provide, I think it helps reducing cognitive load — we know that the symbol will always reference the same piece of data. - if (prefs.redirectYoutube === 'Auto') { + if (prefs.redirectYoutube == RedirectType.Auto) { Me nitpicking again. The string value of the RedirectYoutube enum is mainly there for the radio buttons in the options page; in the code, it's better to use the actual RedirectType.Auto symbol. - return {}; + throw new Error('No results.'); Under the hood, an `async` function returns a promise, which resolves on return and is rejected on throw. --- src/background.ts | 14 +++++++------- src/invidious-api.ts | 3 ++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/background.ts b/src/background.ts index d939e4a..2fc52e0 100644 --- a/src/background.ts +++ b/src/background.ts @@ -17,7 +17,7 @@ import * as _ from 'lodash/fp'; import * as browser from 'webextension-polyfill'; -import { MessageKind } from './types'; +import { MessageKind, RedirectType } from './types'; import constants from './constants'; import Preferences from './preferences'; import InvidiousAPI from './invidious-api'; @@ -27,7 +27,7 @@ import { getPeertubeVideoURL } from './util'; * Retrieve the title of the video using Invidious API. */ const getTitle = async (id: string) => { - let data = await InvidiousAPI.getVideo(id); + const data = await InvidiousAPI.getVideo(id); return data.title; } @@ -38,19 +38,19 @@ const getTitle = async (id: string) => { const preventYoutube = async (r) => { const prefs = await Preferences.getPreferences(); - if (prefs.redirectYoutube === 'Auto') { + if (prefs.redirectYoutube == RedirectType.Auto) { const query = new URLSearchParams(r.url.substring(r.url.indexOf('?') + 1)); - let title = await getTitle(query.get('v')); - let video = await searchByName(title); - let url = getPeertubeVideoURL(video, prefs); + const title = await getTitle(query.get('v')); + const video = await searchByName(title); + const url = getPeertubeVideoURL(video, prefs); return { redirectUrl: url }; } - return {}; + throw new Error('No results.'); }; const buildSearchByNameURL = (instance: string, query: string): string => `https://${instance}/api/v1/search/videos?search=${encodeURIComponent(query)}`; diff --git a/src/invidious-api.ts b/src/invidious-api.ts index 9049d07..c9a482b 100644 --- a/src/invidious-api.ts +++ b/src/invidious-api.ts @@ -3,7 +3,7 @@ import constants from './constants'; export default class InvidiousAPI { static async _fetchAPI(action: string, params: any) { - let paramString = typeof params == 'string' + const paramString = typeof params == 'string' ? params : Object.keys(params).map(function(key) { return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]); @@ -16,6 +16,7 @@ export default class InvidiousAPI { + e.message )); } + static async getVideo(id: string) { return this._fetchAPI(constants.invidiousAPI.videos, id); }