Code Monkey home page Code Monkey logo

subsonic-api's Introduction

Subsonic-API

A lightweight Subsonic/Opensubsonic Client written in TypeScript. Supports Node, Deno, Bun, and modern Browsers.

Features

  • Supports all Subsonic API methods up to version 1.16.1 / Subsonic 6.1.4.
  • Supports most of OpenSubsonic's new API methods.
  • Supports both GET and POST requests.

Installation

$ npm install subsonic-api  # npm
$ bun add subsonic-api      # bun
<script type="module">
  import { SubsonicAPI } from "https://esm.sh/subsonic-api";
</script>

Example Usage

import { SubsonicAPI } from "subsonic-api";

const api = new SubsonicAPI({
  url: "https://demo.navidrome.org",
  auth: {
    username: "demo",
    password: "demo",
  },
});

const { randomSongs } = await api.getRandomSongs();
console.log(randomSongs);

API

subsonic-api supports all of the Subsonic API methods as documented here, up to API version 1.16.1 / Subsonic 6.1.4. Additionally, most of OpenSubsonic's new API methods are also supported. All methods return a promise that resolves to the JSON response from the server.

Additionally, the following methods are available:

new SubsonicAPI

new SubsonicAPI(config: SubsonicConfig)

Creates a new SubsonicAPI instance.

interface SubsonicConfig {
  // The base URL of the Subsonic server, e.g., https://demo.navidrome.org.
  url: string;

  // The authentication details to use when connecting to the server.
  auth: {
    username: string;
    password: string;
  };

  // A salt to use when hashing the password
  salt?: string;

  // Whether to reuse generated salts. If not provided,
  // a random salt will be generated for each request.
  // Ignored if `salt` is provided.
  reuseSalt?: boolean;

  // Whether to use a POST requests instead of GET requests.
  // Only supported by OpenSubsonic compatible servers with the `formPost` extension.
  post?: boolean;

  // The fetch implementation to use. If not provided, the global fetch will be used.
  fetch?: Fetch;

  // The crypto implementation to use. If not provided, the global WebCrypto object
  // or the Node.js crypto module will be used.
  crypto?: WebCrypto;
}

navidromeSession

subsonicSession(): Promise<SessionInfo>

Creates a new Navidrome session

interface SessionInfo {
  id: string;
  isAdmin: boolean;
  name: string;
  subsonicSalt: string;
  subsonicToken: string;
  token: string;
  username: string;
}

baseURL

baseURL(): string

Returns the base URL of the server. Useful for interacting with other APIs like Navidrome's.

custom

custom(method: string, params: Params): Promise<Response>

Allows you to make a custom request to the server.

customJSON

customJSON<T>(method: string, params: Params): Promise<T>

Allows you to make a custom request to the server and parse the response as JSON.

subsonic-api's People

Contributors

explodingcamera avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

geoxor

subsonic-api's Issues

Dynamic values for `t` and `s` URL params on each request

The authentication token t and salt s parameter values are dynamic and change on every request (as I can see here). I suspect this happens on each request whatever the method. I've witnessed this with the getArtists and getCoverArt methods so far.

This prevents the browser from caching images returned by the getCoverArt method which is definitely not optimal performance wise.

Is there a solution?

Thanks in advance ;)

Is there a way to get all albums from an artistId?

Hello ✌️

I'm currently using Navidrome.

Navidrome seems to have a private API with different endpoints and parameters like we see here:
CleanShot 2023-09-08 at 13 52 56

Is there a way to get all albums from an artistId like Navidrome does? I can only see the getAlbumList method which only has the id (of the album) parameter. This returns a list of albums that all have the artistId. I could get the albums from an artist by using both the getArtists and getAlbumList but it's not practical.

Is there a cleaner/simpler way of doing this?

Thanks ;)

Login method errors with "Unknown encoding: base64url"

The environment is in Electron v24.1.3, Node v19.1.0 (trying to implement in https://github.com/Geoxor/amethyst)

Sample code:

import { SubsonicAPI } from "subsonic-api";

const api = new SubsonicAPI({
  url: "https://demo.navidrome.org",
  type: "navidrome", // or "generic" or "subsonic"
});

api.login({
  username: "demo",
  password: "demo",
}).then(() => {
  api.getTopSongs({count: 50}).then(songs => console.log(songs));
  console.log("logged in");
}).catch(err => {
  console.log("subsonic error:", err);
});

Error in console:

subsonic error: TypeError: Unknown encoding: base64url
    at Uint8Array.slowToString (index.js:544:32)
    at Uint8Array.toString (index.js:606:23)
    at M (index.js:1:9081)
    at n.E_fn (index.js:1:10196)
    at n.G_fn (index.js:1:10257)
    at n.C_fn (index.js:1:11207)
    at n.R_fn (index.js:1:10408)
    at n.ping (index.js:1:11393)
    at n.login (index.js:1:9663)

Scrobble() method doesn't take into account submission param

I'm creating a player component that gets the current song that is playing.

From Subsonic API I see I need to call the following methods in this exact order:

  1. stream(songId)
  2. scrobble(songId)
  3. getNowPlaying

The scrobble method needs to have the submission param set to false to add the a song in the playing queue.

Issue: the scrobble method doesn't seem to take into account the submission param. The id and time params work well though.

When directly using the Subsonic API in Postman with the same params everything works well.

My code:
CleanShot 2023-09-13 at 12 47 14

The request in the browser doesn't send the submission param:
CleanShot 2023-09-13 at 12 48 21

Did I miss something?

Thanks in advance ;)

EDIT
After setting the submission value as a string it works. It may be coming from this line in your package :)
CleanShot 2023-09-13 at 18 45 19
CleanShot 2023-09-13 at 18 42 38

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.