UNPKG

lunify.js

Version:

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

58 lines 2.17 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UsersManager = void 0; const __1 = require("../.."); const cache_1 = require("../cache"); __exportStar(require("./Playlists"), exports); class UsersManager { client; cache; 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 }); * ``` */ async fetch(access, options) { if (!options?.force) { const user = this.cache.find((u) => u.oauth.refreshToken === access.refreshToken); if (user) return user; } const res = await this.client.rest.get("/me", { headers: { Authorization: await access.getAuthorization() } }); const user = new __1.User(this.client, access, res); this.cache.set(user.id, user); return user; } } exports.UsersManager = UsersManager; //# sourceMappingURL=index.js.map