spotify-api.js
Version:
A complete node js wrapper of spotify api with oauth support
73 lines (72 loc) • 2.88 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlbumManager = void 0;
const Cache_1 = require("../Cache");
/**
* A manager to perform actions which belongs to the spotify album web api.
*/
class AlbumManager {
/**
* A manager to perform actions which belongs to the spotify album web api.
*
* @param client The spotify api client.
* @example const albums = new AlbumManager(client);
*/
constructor(client) {
this.client = client;
}
/**
* Search for spotify albums with query.
*
* @param query The query to search.
* @param options Some search options to make the search more efficient.
* @example const results = await client.albums.search('some search');
*/
async search(query, options = {}) {
const fetchedData = await this.client.fetch('/search', {
params: {
q: query,
type: 'album',
market: options.market,
limit: options.limit,
offset: options.offset,
include_external: options.includeExternalAudio ? 'audio' : undefined
}
});
return fetchedData ? (0, Cache_1.createCacheStructArray)('albums', this.client, fetchedData.albums.items) : [];
}
/**
* Get an album's information.
*
* @param id The spotify album id.
* @param force When true, will directly fetch else will search for the cache first!
* @example const album = await client.albums.get('id');
*/
async get(id, force = !this.client.cacheSettings.albums) {
if (!force && Cache_1.Cache.albums.has(id))
return Cache_1.Cache.albums.get(id);
const fetchedData = await this.client.fetch(`/albums/${id}`);
return fetchedData ? (0, Cache_1.createCacheStruct)('albums', this.client, fetchedData) : null;
}
/**
* Get the information of multiple albums in one fetch.
*
* @param ids An array of spotify ids.
* @example const albums = await client.albums.getMultiple(['id1', 'id2']);
*/
async getMultiple(ids) {
const fetchedData = await this.client.fetch('/albums', { params: { ids: ids.join(',') } });
return fetchedData ? (0, Cache_1.createCacheStructArray)('albums', this.client, fetchedData.albums) : [];
}
/**
* Get the information about the album's tracks.
*
* @param id The spotify album id.
* @example const tracks = await client.albums.getTracks('id');
*/
async getTracks(id) {
const fetchedData = await this.client.fetch(`/albums/${id}/tracks`);
return fetchedData ? (0, Cache_1.createCacheStructArray)('tracks', this.client, fetchedData.items) : [];
}
}
exports.AlbumManager = AlbumManager;
;