From 5c35ce031d5b6c7754e5661f374d6c1b552a1f5f Mon Sep 17 00:00:00 2001 From: Booteille Date: Mon, 12 Nov 2018 21:34:43 +0100 Subject: [PATCH] Refactor PeertubeAPI usage --- src/background.ts | 17 +++++------------ src/constants.ts | 5 ++++- src/invidious-api.ts | 2 +- src/peertube-api.ts | 29 +++++++++++++++++++++++++++++ src/preferences.ts | 4 ++-- 5 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 src/peertube-api.ts diff --git a/src/background.ts b/src/background.ts index 62fe611..04e07c8 100644 --- a/src/background.ts +++ b/src/background.ts @@ -21,6 +21,7 @@ import { MessageKind, RedirectType } from './types'; import constants from './constants'; import Preferences from './preferences'; import InvidiousAPI from './invidious-api'; +import PeertubeAPI from './peertube-api'; import { getPeertubeVideoURL } from './util'; /** @@ -53,13 +54,8 @@ const redirectYoutube = async (r) => { throw new Error('No results.'); }; -const buildSearchByNameURL = (instance: string, query: string): string => `https://${instance}/api/v1/search/videos?search=${encodeURIComponent(query)}`; - const searchByName = query => new Promise(async (resolve, reject) => { - const instance = _.getOr(constants.defaultInstance, 'searchInstance', await browser.storage.local.get()).toString(); - - fetch(buildSearchByNameURL(instance, query)) - .then(res => res.json()) + PeertubeAPI.searchVideo({ search: query }) .then(function(data) { if (data.total > 0) { const video = data.data[0] @@ -71,15 +67,12 @@ const searchByName = query => new Promise(async (resolve, reject) => { }); }); -const buildSearchByIDURL = (instance: string, id: string): string => `https://${instance}/api/v1/videos/${id}`; - const searchByID = id => new Promise(async (resolve, reject) => { - const instance = _.getOr(constants.defaultInstance, 'searchInstance', await browser.storage.local.get()).toString(); + const prefs = await Preferences.getPreferences(); - fetch(buildSearchByIDURL(instance, id)) - .then(res => res.json()) + PeertubeAPI.getVideo(id) .then(function(video) { - resolve({ url: `https://${instance}/videos/watch/${id}`, video }); + resolve({ url: getPeertubeVideoURL(video, prefs), video }); }) }) diff --git a/src/constants.ts b/src/constants.ts index 7614a01..ab040e2 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,5 +1,8 @@ export default { - defaultInstance: 'peertube.social', + peertubeAPI: { + defaultInstance: 'peertube.social', + endpoint: 'api/v1', + }, invidiousAPI: { url: 'https://invidio.us/api/v1', videos: 'videos', diff --git a/src/invidious-api.ts b/src/invidious-api.ts index d44863f..77637d3 100644 --- a/src/invidious-api.ts +++ b/src/invidious-api.ts @@ -12,7 +12,7 @@ export default class InvidiousAPI { return fetch(`${constants.invidiousAPI.url}/${action}/${paramString}`) .then(res => res.json()) .catch(e => console.error( - 'An error occured while trying to fetch API used by PeerTubeify: ' + 'An error occured while trying to fetch Invidious API used by PeerTubeify: ' + e.message )); } diff --git a/src/peertube-api.ts b/src/peertube-api.ts new file mode 100644 index 0000000..412abcd --- /dev/null +++ b/src/peertube-api.ts @@ -0,0 +1,29 @@ +import * as _ from 'lodash/fp'; +import constants from './constants'; + +export default class PeertubeAPI { + private static async fetchAPI(path: string, query?: Object) { + const instance = _.getOr(constants.peertubeAPI.defaultInstance, 'searchInstance', await browser.storage.local.get()).toString(); + + let url = `https://${constants.peertubeAPI.defaultInstance}/${constants.peertubeAPI.endpoint}/${path}`; + + if (query) { + url = url + '?' + Object.keys(query).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(query[key])).join('&'); + } + + return fetch(url) + .then(res => res.json()) + .catch(e => console.error( + `An error occured while trying to fetch ${instance} API used by PeerTubeify: ` + + e.message + )); + } + + static async getVideo(id: string) { + return this.fetchAPI('videos/' + id); + } + + static async searchVideo(query: Object) { + return this.fetchAPI('search/videos', query); + } +} diff --git a/src/preferences.ts b/src/preferences.ts index fd41297..ada960a 100644 --- a/src/preferences.ts +++ b/src/preferences.ts @@ -28,7 +28,7 @@ export default class Preferences { redirectPeertube: RedirectType; constructor(localStorage) { - this.searchInstance = _.defaultTo(constants.defaultInstance, localStorage.searchInstance as string); + this.searchInstance = _.defaultTo(constants.peertubeAPI.defaultInstance, localStorage.searchInstance as string); this.openInOriginalInstance = _.defaultTo(true, localStorage.openInOriginalInstance as boolean); this.redirectYoutube = _.defaultTo(RedirectType.Show, localStorage.redirectYoutube) this.redirectPeertube = _.defaultTo(RedirectType.None, localStorage.redirectPeertube) @@ -54,6 +54,6 @@ export default class Preferences { } set searchInstance(instance) { - this._searchInstance = _.isEmpty(instance) ? constants.defaultInstance : stripProtocol(instance) + this._searchInstance = _.isEmpty(instance) ? constants.peertubeAPI.defaultInstance : stripProtocol(instance) } }