From eb66393fe62367d406e905a6c988e1ded3218da2 Mon Sep 17 00:00:00 2001 From: simojenki Date: Sat, 23 Apr 2022 16:54:30 +1000 Subject: [PATCH] tidy --- src/http.ts | 16 +++++----- src/subsonic/index.ts | 14 ++++----- src/subsonic/library.ts | 10 +++---- src/subsonic/subsonic_http.ts | 56 +---------------------------------- 4 files changed, 20 insertions(+), 76 deletions(-) diff --git a/src/http.ts b/src/http.ts index fbc9f66..0a6e561 100644 --- a/src/http.ts +++ b/src/http.ts @@ -1,19 +1,17 @@ import { AxiosPromise, AxiosRequestConfig, ResponseType } from "axios"; -import _ from "underscore"; - -export interface RequestModifier { - (config: AxiosRequestConfig): AxiosRequestConfig; -} - -export const no_op = (config: AxiosRequestConfig) => config; export interface Http { (config: AxiosRequestConfig): AxiosPromise; } -export type RequestParams = { baseURL: string; url: string, params: any, headers: any, responseType: ResponseType } +export type RequestParams = { + baseURL: string; + url: string; + params: any; + headers: any; + responseType: ResponseType; +}; -// todo: rename to http export const http = (base: Http, defaults: Partial): Http => (config: AxiosRequestConfig) => { diff --git a/src/subsonic/index.ts b/src/subsonic/index.ts index efcdbe0..c3e71ca 100644 --- a/src/subsonic/index.ts +++ b/src/subsonic/index.ts @@ -101,7 +101,7 @@ export class Subsonic implements MusicService { // todo: why is this in here? externalImageFetcher: ImageFetcher; - base: Http; + subsonicHttp: Http; constructor( url: string, @@ -111,15 +111,15 @@ export class Subsonic implements MusicService { this.url = url; this.streamClientApplication = streamClientApplication; this.externalImageFetcher = externalImageFetcher; - this.base = http2(axios, { + this.subsonicHttp = http2(axios, { baseURL: this.url, params: { v: "1.16.1", c: DEFAULT_CLIENT_APPLICATION }, headers: { "User-Agent": "bonob" }, }); } - authenticated = (credentials: Credentials, wrap: Http = this.base) => - http2(wrap, { + authenticatedSubsonicHttp = (credentials: Credentials) => + http2(this.subsonicHttp, { params: { u: credentials.username, ...t_and_s(credentials.password), @@ -130,12 +130,12 @@ export class Subsonic implements MusicService { credentials: Credentials, url: string, params: {} = {} - ): Promise => getJSON2(http2(this.authenticated(credentials), { url, params })); + ): Promise => getJSON2(http2(this.authenticatedSubsonicHttp(credentials), { url, params })); generateToken = (credentials: Credentials) => pipe( TE.tryCatch( - () => getJSON2(http2(this.authenticated(credentials), { url: "/rest/ping.view" })), + () => getJSON2(http2(this.authenticatedSubsonicHttp(credentials), { url: "/rest/ping.view" })), (e) => new AuthFailure(e as string) ), TE.chain(({ type }) => @@ -170,7 +170,7 @@ export class Subsonic implements MusicService { ): Promise => { const subsonicGenericLibrary = new SubsonicGenericMusicLibrary( this.streamClientApplication, - this.authenticated(credentials, this.base) + this.authenticatedSubsonicHttp(credentials) ); if (credentials.type == "navidrome") { return Promise.resolve( diff --git a/src/subsonic/library.ts b/src/subsonic/library.ts index 38f240a..e1f2ce7 100644 --- a/src/subsonic/library.ts +++ b/src/subsonic/library.ts @@ -277,19 +277,19 @@ const maybeAsGenre = (genreName: string | undefined): Genre | undefined => export class SubsonicGenericMusicLibrary implements SubsonicMusicLibrary { streamClientApplication: StreamClientApplication; - http: Http; + subsonicHttp: Http; constructor( streamClientApplication: StreamClientApplication, - http: Http + subsonicHttp: Http ) { this.streamClientApplication = streamClientApplication; - this.http = http; + this.subsonicHttp = subsonicHttp; } GET = (query: Partial) => ({ - asRAW: () => getRaw2(http2(this.http, query)), - asJSON: () => getJSON2(http2(this.http, query)), + asRAW: () => getRaw2(http2(this.subsonicHttp, query)), + asJSON: () => getJSON2(http2(this.subsonicHttp, query)), }); flavour = () => "subsonic"; diff --git a/src/subsonic/subsonic_http.ts b/src/subsonic/subsonic_http.ts index 8a9d975..4ed9a81 100644 --- a/src/subsonic/subsonic_http.ts +++ b/src/subsonic/subsonic_http.ts @@ -1,45 +1,9 @@ -import axios, { AxiosPromise, AxiosRequestConfig } from "axios"; import { - DEFAULT_CLIENT_APPLICATION, isError, SubsonicEnvelope, - t_and_s, - USER_AGENT, } from "."; // todo: rename http2 to http import { Http, http as http2 } from "../http"; -import { Credentials } from "../music_service"; -import { asURLSearchParams } from "../utils"; - -export const http = (base: string, credentials: Credentials): HTTP => ({ - get: async ( - path: string, - params: Partial<{ q: {}; config: AxiosRequestConfig | undefined }> - ) => - axios - .get(`${base}${path}`, { - params: asURLSearchParams({ - u: credentials.username, - v: "1.16.1", - c: DEFAULT_CLIENT_APPLICATION, - ...t_and_s(credentials.password), - f: "json", - ...(params.q || {}), - }), - headers: { - "User-Agent": USER_AGENT, - }, - ...(params.config || {}), - }) - .catch((e) => { - throw `Subsonic failed with: ${e}`; - }) - .then((response) => { - if (response.status != 200 && response.status != 206) { - throw `Subsonic failed with a ${response.status || "no!"} status`; - } else return response; - }), -}); export type HttpResponse = { data: any; @@ -47,24 +11,6 @@ export type HttpResponse = { headers: any; }; -export interface HTTP { - get( - path: string, - params: Partial<{ q: {}; config: AxiosRequestConfig | undefined }> - ): Promise; -} - -export const raw = (response: AxiosPromise) => - response - .catch((e) => { - throw `Subsonic failed with: ${e}`; - }) - .then((response) => { - if (response.status != 200 && response.status != 206) { - throw `Subsonic failed with a ${response.status || "no!"} status`; - } else return response; - }); - export const getRaw2 = (http: Http) => http({ method: "get" }) .catch((e) => { @@ -88,4 +34,4 @@ export const asJSON = (response: HttpResponse): T => { else return subsonicResponse as unknown as T; }; -export default http; +