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

spotify-react-query

v0.12.2

Published

React Query hooks for the Spotify Web API

Downloads

27

Readme

Spotify React Query

Latest Size

Simple React Query hooks for the Spotify Web API. With the power of React Query, requests for Spotify resources are automatically cached, and by leveraging dataloader under the hood, we can batch calls for similar resources to avoid using up your Spotify API quota.

This package is used by the musictaste.space beta.

Install

Install this package by running the following command in your project:

yarn add spotify-react-query

Usage

In order to use the hooks, you must wrap dependent components in a SpotifyQueryProvider and pass in a React Query QueryClient. The client can be customized to suit your use case, or you can pass in the default client and it will work out of the box.

You must also provide a Spotify Client instance from spotify-web-api-node. The library will not perform any requests until an access token is set on the client. You will need to manage the lifecycle of token refreshes outside of <SpotifyQueryProvider> context.

SpotifyQueryProvider

import { QueryClient } from "@tanstack/react-query"
import SpotifyClient from "spotify-web-api-node"

const query = new QueryClient()
const spotify = new SpotifyClient()
// this is usually managed inside your application
spotify.setAccessToken("<ACCESS_TOKEN>")

const App = () => {
  return (
    <SpotifyQueryProvider query={query} spotify={spotify}>
      <DependentComponents />
    </SpotifyQueryProvider>
  )
}

Quickstart Example

import { useSimplifiedTrack } from "spotify-react-query"

function TrackComponent({ uri }: { uri: string }) {
  const { data: track, isLoading } = useSimplifiedTrack(uri)

  if (isLoading) {
    return <div>Loading...</div>
  }

  if (!track) {
    return null
  }

  return (
    <div>
      {track.name} by ${track.artists[0].name}
    </div>
  )
}

Simplified and Full Entities

For many Spotify API entities, there are two subtypes which are returned depending on your query - simplified and full. Please refer to the Spotify API documentation to differentiate between the two given the entity. In the majority of cases, the simplified result may be enough.

Under the hood, when a query fetches simplified data about related entities (eg. when you query for an album and it returns simplified artist and album tracks), the library will prime the cache with those entities. This means that if you first used the useFullAlbum hook to fetch an album, and then use a component leveraging the useSimplifiedTrack hook to render the tracks based on the album track URIs, the data will already be in the cache and an additional network request will not be made.

For this reason, it's recommended that you use the simplified entities wherever possible.

Hooks

Tracks

function useSimplifiedTrack(id: string, options?: ReactQueryOptions)
function useFullTrack(id: string, options?: ReactQueryOptions)
function useFullTracks(ids: string[], options?: ReactQueryOptions)

Albums

function useSimplifiedAlbum(id: string, options?: ReactQueryOptions)
function useFullAlbum(id: string, options?: ReactQueryOptions)

Artists

function useSimplifiedArtist(id: string, options?: ReactQueryOptions)
function useFullArtist(id: string, options?: ReactQueryOptions)
function useFullArtists(ids: string[], options?: ReactQueryOptions)

Playlists

function usePlaylist(id: string, options?: ReactQueryOptions)
function usePlaylistTracks({
  variables?: { id: string; fields?: string; limit?: number; offset?: number; market?: string } },
  options?: ReactQueryOptions
)

Statistics

function useUserTopTracks(
  variables: { limit?: number; offset?: number; time_range: "short_term" | "medium_term" | "long_term" },
  options?: ReactQueryOptions
)
function useUserTopArtists(
  variables: { limit?: number; offset?: number; time_range: "short_term" | "medium_term" | "long_term" },
  options?: ReactQueryOptions
)
function useRecentlyPlayedTracks(
  variables: { after?: number; before?: number; limit?: number },
  options?: ReactQueryOptions
)

Spotify Client

You can also use the Spotify client directly to leverage all the methods available via spotify-web-api-node

import { useSpotifyClient } from "spotify-react-query"

const client = useSpotifyClient()

client.getMe().then((res) => console.log(res.body))