UNPKG

lunify.js

Version:

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

95 lines 3.64 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.UserOauth = void 0; const __1 = require("../.."); class UserOauth { constructor(client, data) { this.client = client; this.refreshToken = null; if ('refresh_token' in data) this.refreshToken = data.refresh_token; this.accessToken = data.access_token; this.tokenType = data.token_type; this.scope = this.convertScopesToStringArray(data.scope); this.expiresIn = data.expires_in * 1000; this.expiresTimestamp = data.created_timestamp + data.expires_in * 1000; this.createdTimestamp = data.created_timestamp; this.revoked = false; } convertScopesToStringArray(scopesString) { const scopesArray = scopesString.split(' '); const enumValues = Object.values(__1.Scopes); return scopesArray .filter((scope) => enumValues.includes(scope)); } /** * Refresh a spotify access token * @param {string} refreshToken - oauth refresh token * @example ```ts * await access.refresh(); * ``` * Or if you want to use a specific refresh token, like from a database * @example ```ts * const refreshToken = ...; // from the database for example * await access.refresh(refreshToken); * ``` */ refresh(refreshToken) { return __awaiter(this, void 0, void 0, function* () { if (refreshToken) this.refreshToken = refreshToken; if (this.revoked) throw Error(__1.LunifyErrors.TokenRevoked); if (!this.refreshToken) throw Error(__1.LunifyErrors.NoRefreshToken); const res = yield this.client.oauth.refreshToken(this.refreshToken); if (!res) { this.revoked = true; throw Error(__1.LunifyErrors.TokenRevoked); } this.accessToken = res.accessToken; return res.accessToken; }); } /** * Generates a authorization token header */ getAuthorization() { return __awaiter(this, void 0, void 0, function* () { if (this.expiresTimestamp < Date.now()) { yield this.refresh(); } return this.tokenType + ' ' + this.accessToken; }); } /** * Check if the access token is still valid (expires after 1 hour of creating, usually) */ isValid() { if (this.expiresTimestamp < Date.now()) return false; return true; } /** * Fetch the user accociated with this access token * @example ```ts * const user = await access.fetchUser(); * ``` */ fetchUser() { return __awaiter(this, void 0, void 0, function* () { return yield this.client.users.fetch(this); }); } } exports.UserOauth = UserOauth; //# sourceMappingURL=Oauth.js.map