UNPKG

lunify.js

Version:

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

54 lines 2.34 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UsersManager = void 0; const __1 = require("../.."); const cache_1 = require("../cache"); class UsersManager { constructor(client) { this.client = client; this.cache = new cache_1.CacheManager(); } /** * Fetch a user accociated with its access token * @example ```ts * // will try to get cached user to avoid requesting the spotify api * const access = await lunify.oauth.fetchToken(code); * const user = await lunify.users.fetch(access); * ``` * @example ```ts * // will fetch from the spotify api regardless if it's cached or not * const access = await lunify.oauth.fetchToken(code); * const user = await lunify.users.fetch(access, { force: true }); * ``` */ fetch(access, options) { return __awaiter(this, void 0, void 0, function* () { let user; if (!(options === null || options === void 0 ? void 0 : options.force)) { // not sure if there is a better way user = this.cache.find((u) => u.oauth.refreshToken === access.refreshToken); if (user) return user; } const res = yield this.client.rest.get('/me', { headers: { Authorization: yield access.getAuthorization() } }); user = new __1.User(this.client, access, res); this.cache.set(user.id, user); return user; }); } } exports.UsersManager = UsersManager; //# sourceMappingURL=index.js.map