Make some small style improvements

-    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.
This commit is contained in:
Ealhad 2018-11-12 15:54:49 +01:00
parent 633fa729df
commit 0d7312d0c2
2 changed files with 9 additions and 8 deletions

View File

@ -17,7 +17,7 @@
import * as _ from 'lodash/fp'; import * as _ from 'lodash/fp';
import * as browser from 'webextension-polyfill'; import * as browser from 'webextension-polyfill';
import { MessageKind } from './types'; import { MessageKind, RedirectType } from './types';
import constants from './constants'; import constants from './constants';
import Preferences from './preferences'; import Preferences from './preferences';
import InvidiousAPI from './invidious-api'; import InvidiousAPI from './invidious-api';
@ -27,7 +27,7 @@ import { getPeertubeVideoURL } from './util';
* Retrieve the title of the video using Invidious API. * Retrieve the title of the video using Invidious API.
*/ */
const getTitle = async (id: string) => { const getTitle = async (id: string) => {
let data = await InvidiousAPI.getVideo(id); const data = await InvidiousAPI.getVideo(id);
return data.title; return data.title;
} }
@ -38,19 +38,19 @@ const getTitle = async (id: string) => {
const preventYoutube = async (r) => { const preventYoutube = async (r) => {
const prefs = await Preferences.getPreferences(); 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)); const query = new URLSearchParams(r.url.substring(r.url.indexOf('?') + 1));
let title = await getTitle(query.get('v')); const title = await getTitle(query.get('v'));
let video = await searchByName(title); const video = await searchByName(title);
let url = getPeertubeVideoURL(video, prefs); const url = getPeertubeVideoURL(video, prefs);
return { return {
redirectUrl: url redirectUrl: url
}; };
} }
return {}; throw new Error('No results.');
}; };
const buildSearchByNameURL = (instance: string, query: string): string => `https://${instance}/api/v1/search/videos?search=${encodeURIComponent(query)}`; const buildSearchByNameURL = (instance: string, query: string): string => `https://${instance}/api/v1/search/videos?search=${encodeURIComponent(query)}`;

View File

@ -3,7 +3,7 @@ import constants from './constants';
export default class InvidiousAPI { export default class InvidiousAPI {
static async _fetchAPI(action: string, params: any) { static async _fetchAPI(action: string, params: any) {
let paramString = typeof params == 'string' const paramString = typeof params == 'string'
? params ? params
: Object.keys(params).map(function(key) { : Object.keys(params).map(function(key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]); return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
@ -16,6 +16,7 @@ export default class InvidiousAPI {
+ e.message + e.message
)); ));
} }
static async getVideo(id: string) { static async getVideo(id: string) {
return this._fetchAPI(constants.invidiousAPI.videos, id); return this._fetchAPI(constants.invidiousAPI.videos, id);
} }