UNPKG

lunify.js

Version:

A basic api wrapper for the spotify api covering the oauth routes.

73 lines 2.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TracksManager = void 0; const track_1 = require("../../structures/track"); const cache_1 = require("../cache"); class TracksManager { client; cache; constructor(client) { this.client = client; this.cache = new cache_1.CacheManager(); } /** * Fetch a track with a track id * @param {string} trackId - spotify track id: https://open.spotify.com/track/4cOdK2wGLETKBW3PvgPWqT <-- last part after /track/ * @param {StructureFetchOptions?} options * @example ```ts * // will try to get cached track to avoid requesting the spotify api * const track = await lunify.tracks.fetch("4cOdK2wGLETKBW3PvgPWqT"); * ``` */ async fetch(trackId, options) { let track; if (!options?.force) { track = this.cache.get(trackId); if (track) return track; } const res = await this.client.rest.get("/tracks/" + trackId, { advancedAuthRequired: true }); track = new track_1.Track(this.client, res); this.cache.set(track.id, track); return track; } /** * Fetch multiple tracks with a list track of id, the api will only be requested once no matter how many tracks are provided * @param {string[]} trackIds - list of spotify track ids: https://open.spotify.com/track/4cOdK2wGLETKBW3PvgPWqT <-- last part after /track/ * @param {StructureFetchOptions?} options * @example ```ts * // will try to get cached tracks* to avoid requesting the spotify api, * // *only works if all tracks were cached already * const tracks = await lunify.tracks.list(["4cOdK2wGLETKBW3PvgPWqT", "1jcp5qEaDLT4gsIUjDPJo9"]); * ``` */ async list(trackIds, options) { let tracks = []; if (!options?.force) { for (const trackId of trackIds) { const item = this.cache.get(trackId); if (item) tracks.push(item); } if (tracks.length === trackIds.length) return tracks; } tracks = []; const res = await this.client.rest.get("/tracks", { query: { ids: trackIds.join(",") }, advancedAuthRequired: true }); for await (const r of res?.tracks || []) { const track = new track_1.Track(this.client, r); this.cache.set(track.id, track); tracks.push(track); } return tracks; } } exports.TracksManager = TracksManager; //# sourceMappingURL=index.js.map