UNPKG

lunify.js

Version:

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

50 lines 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PlaylistTracksManager = void 0; const __1 = require("../.."); const cache_1 = require("../cache"); const FETCH_TRACK_CHUNK_SIZE = 100; class PlaylistTracksManager { client; playlist; oauth; cache; constructor(client, playlist, // | Playlist oauth) { this.client = client; this.playlist = playlist; this.oauth = oauth; this.cache = new cache_1.CacheManager(); } async fetchSinglePage(page) { const params = new URLSearchParams(); params.append("limit", FETCH_TRACK_CHUNK_SIZE.toString()); params.append("offset", (page * FETCH_TRACK_CHUNK_SIZE).toString() || "0"); const res = await this.client.rest.get("/playlists/" + this.playlist.id + "/tracks?" + params.toString(), { headers: { Authorization: await this.oauth.getAuthorization() } }); const tracks = res.items.map((track) => track.track.type === "episode" ? track.track : new __1.PlaylistTrack(this.client, track)); for (const track of tracks) this.cache.set(track.id, track); return tracks; } async fetchMany(page) { const res = await this.fetchSinglePage(page); if (res.length >= FETCH_TRACK_CHUNK_SIZE) { return [ ...res, ...(await this.fetchMany(page + 1)) ]; } return res; } fetch() { return this.fetchMany(0); } } exports.PlaylistTracksManager = PlaylistTracksManager; //# sourceMappingURL=Tracks.js.map