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:
parent
633fa729df
commit
0d7312d0c2
|
@ -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)}`;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue