diff --git a/src/background.ts b/src/background.ts index 76b5b10..2b8d99e 100644 --- a/src/background.ts +++ b/src/background.ts @@ -20,8 +20,8 @@ import * as browser from 'webextension-polyfill'; import { MessageKind, RedirectType } from './types'; import constants from './constants'; import Preferences from './preferences'; -import InvidiousAPI from './invidious-api'; -import PeertubeAPI from './peertube-api'; +import * as InvidiousAPI from './invidious-api'; +import * as PeertubeAPI from './peertube-api'; import { getPeertubeVideoURL } from './util'; /** diff --git a/src/invidious-api.ts b/src/invidious-api.ts index 77637d3..266be82 100644 --- a/src/invidious-api.ts +++ b/src/invidious-api.ts @@ -1,27 +1,26 @@ import * as _ from 'lodash/fp'; import constants from './constants'; -export default class InvidiousAPI { - private static async fetchAPI(action: string, params: any) { - const paramString = typeof params == 'string' - ? params - : Object.keys(params).map(function(key) { - return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]); - }).join('&'); +async function fetchAPI(action: string, params: any) { + const paramString = typeof params == 'string' + ? params + : Object.keys(params).map(function(key) { + return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]); + }).join('&'); - return fetch(`${constants.invidiousAPI.url}/${action}/${paramString}`) - .then(res => res.json()) - .catch(e => console.error( - 'An error occured while trying to fetch Invidious API used by PeerTubeify: ' - + e.message - )); - } - - static async getVideo(id: string) { - return this.fetchAPI(constants.invidiousAPI.videos, id); - } - - static async getChannel(ucid: string) { - return this.fetchAPI(constants.invidiousAPI.channels, ucid); - } + return fetch(`${constants.invidiousAPI.url}/${action}/${paramString}`) + .then(res => res.json()) + .catch(e => console.error( + 'An error occured while trying to fetch Invidious API used by PeerTubeify: ' + + e.message + )); +} + + +export async function getVideo(id: string) { + return fetchAPI(constants.invidiousAPI.videos, id); +} + +export async function getChannel(ucid: string) { + return fetchAPI(constants.invidiousAPI.channels, ucid); } diff --git a/src/peertube-api.ts b/src/peertube-api.ts index 2666e60..5e3f457 100644 --- a/src/peertube-api.ts +++ b/src/peertube-api.ts @@ -1,29 +1,27 @@ 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(); +async function fetchAPI(path: string, query?: Object) { + const instance = _.getOr(constants.peertubeAPI.defaultInstance, 'searchInstance', await browser.storage.local.get()).toString(); - let url = `https://${instance}/${constants.peertubeAPI.endpoint}/${path}`; + let url = `https://${instance}/${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 - )); + if (query) { + url = url + '?' + Object.keys(query).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(query[key])).join('&'); } - static async getVideo(id: string) { - return this.fetchAPI('videos/' + id); - } - - static async searchVideo(query: Object) { - return this.fetchAPI('search/videos', query); - } + 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 + )); +} + +export async function getVideo(id: string) { + return fetchAPI('videos/' + id); +} + +export async function searchVideo(query: Object) { + return fetchAPI('search/videos', query); }