npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lastfm-typed

v2.1.0

Published

Typed API wrapper for Last.FM using promises

Downloads

271

Readme

lastfm-typed

Codacy Badge

Introduction

lastfm-typed is a fully typed library for interaction with the Last.FM API. Uses promises.

For a full documentation of this library, please visit the documentation website, complete with interactive examples.

IMPORTANT: Breaking changes in 2.0.0

Version 2.0.0 of lastfm-typed brings with it many breaking changes, mainly related to the typing of responses. Typescript should tell you where you need to make changes if you choose to upgrade. It is highly recommended that you update, as this comes in response to changes to last.fm's API causing issues with the previous versions. Version 2.0.0 comes with a website to document it.

Changes: 2.1.0

2.1.0 Adds the artistCount, albumCount, and trackCount properties to user.getInfo. This comes as last.fm updated its api to add these properties recently.

Usage

The library will call the appropriate endpoint (helper functions may call multiple and do additional processing), and format data to more appropriate types and structures than the default api. The structure output by lastfm-typed can be seen and tested in this documentation, and can also be seen from the typings in your IDE. In general, #text, @attr attributes are both renamed everywhere, and some groupings may change. Some deprecated properties may also not be shown. You can view the result of endpoints in the documentation website.

In general, required parameters are given as separate parameters, while optional parameters are added to an object as the final parameter. However, you can also run endpoints with only one argument, being an object with both required and any optional parameters specified.

Structure

The library exports a single class. This class, in turn, creates instances of a series of sub-classes, allowing using endpoints through their exact Last.FM API names. The object keeps track of your api key and api secret, so you don't have to reenter those. To initialize:

import LastFMTyped from "lastfm-typed";

const lastfm = new LastFMTyped(apiKey:string, options?:{apiSecret?:string, userAgent?:string, secureConnection?:boolean}); //insert key, secret, user agent, and whether to use https here

api_key is the only required parameter.

Without api_secret, auth commands will not work. This includes usage of session key in place of username for typically non-auth functions.

For user agent, please initialize this with an easily identifiable name (preferably one that would lead to your program if googled). You can choose to not set one, in which case lastfm-typed-npm will be set as the user agent. This is probably the best idea if your program is not public. Please note that in browser user agent header cannot be set and whatever is here will be ignored.

secureConnection determines whether to use https or http. By default, this is true, which uses https. Set to false to use http.

Then we can call methods as needed.

Examples

A simple authentication example:

const token = await lastfm.auth.getToken();

//replace this with whatever method you would use to show the url to the user
sendToUser(`https://www.last.fm/api/auth?api_key=${config.lastfm.key}&token=${token}`);

//replace this with whatever method you use to determine that the user has accepted integration.
await userInput;
	
const session = await lastfm.auth.getSession(token);

Getting info about a user:

const userInfo = await lastfm.user.getInfo("Mexdeep");

Helper Methods

In addition to the default Last.FM methods, lastfm-typed offers a couple additional helper methods. The first group of helper methods are designed to help you with method parameters. These are ArtistFromMBID, AlbumFromMBID, TrackFromMBID, ArtistFromName, AlbumFromName, and TrackFromName. The MBID functions takes a single MBID parameter, ArtistFromName takes a single artist name parameter, while TrackFromName and AlbumFromName take two parameters, the artist name and the name of the track/album. These methods then returns a ArtistInput, AlbumInput, or TrackInput that you can use with the getter functions.

const album = await lastfm.album.getInfo(lastfm.helper.AlbumFromName("KITANO REM", "RAINSICK/オレンジ"), {username:"Mexdeep"});

There are also some helper methods that add some basic functionality. These helper methods are documented in the documentation website.