lunify.js
Version:
A basic api wrapper for the spotify api covering the oauth routes.
125 lines • 5.88 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TracksManager = void 0;
const track_1 = require("../../structures/track");
const cache_1 = require("../cache");
class TracksManager {
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");
* ```
*/
fetch(trackId, options) {
return __awaiter(this, void 0, void 0, function* () {
let track;
if (!(options === null || options === void 0 ? void 0 : options.force)) {
track = this.cache.get(trackId);
if (track)
return track;
}
const res = yield 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"]);
* ```
*/
list(trackIds, options) {
var _a, trackIds_1, trackIds_1_1;
var _b, e_1, _c, _d, _e, e_2, _f, _g;
return __awaiter(this, void 0, void 0, function* () {
let tracks = [];
if (!(options === null || options === void 0 ? void 0 : options.force)) {
try {
for (_a = true, trackIds_1 = __asyncValues(trackIds); trackIds_1_1 = yield trackIds_1.next(), _b = trackIds_1_1.done, !_b;) {
_d = trackIds_1_1.value;
_a = false;
try {
const trackId = _d;
tracks.push(this.cache.get(trackId));
}
finally {
_a = true;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_a && !_b && (_c = trackIds_1.return)) yield _c.call(trackIds_1);
}
finally { if (e_1) throw e_1.error; }
}
if (tracks.filter((track) => track).length === trackIds.length)
return tracks;
}
tracks = [];
const res = yield this.client.rest.get('/tracks', {
query: {
ids: trackIds.join(',')
},
advancedAuthRequired: true
});
try {
for (var _h = true, _j = __asyncValues((res === null || res === void 0 ? void 0 : res.tracks) || []), _k; _k = yield _j.next(), _e = _k.done, !_e;) {
_g = _k.value;
_h = false;
try {
const r = _g;
const track = new track_1.Track(this.client, r);
this.cache.set(track.id, track);
tracks.push(track);
}
finally {
_h = true;
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (!_h && !_e && (_f = _j.return)) yield _f.call(_j);
}
finally { if (e_2) throw e_2.error; }
}
return tracks;
});
}
}
exports.TracksManager = TracksManager;
//# sourceMappingURL=index.js.map