UNPKG

lunify.js

Version:

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

102 lines 4.73 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.OauthManager = void 0; const stream_1 = require("stream"); const __1 = require("../.."); const user_1 = require("../../structures/user"); class OauthManager extends stream_1.EventEmitter { constructor(client) { super(); this.client = client; } /** * Create a oAuth url for users to authorize * @param {Scopes[]} scopes - A list of spotify scopes {@link https://developer.spotify.com/documentation/web-api/concepts/scopes} * @param {?string} state - If you want to use your own state use this */ generateUrl(scopes, state) { if (!this.client.options.oauth.redirectUri) throw Error(__1.LunifyErrors.NoRedirectUri); const params = new URLSearchParams(); params.append('response_type', 'code'); params.append('client_id', this.client.options.clientId); params.append('redirect_uri', this.client.options.oauth.redirectUri); params.append('scope', scopes.join(' ')); params.append('state', state || crypto.randomUUID()); return 'https://accounts.spotify.com/authorize?' + params.toString(); } /** * Get a spotify access token from a oAuth code * @param {string} code - oauth response query code * @example ```ts * const code = req.query.code; * const access = await api.oauth.fetchToken(code); * ``` */ fetchToken(code) { return __awaiter(this, void 0, void 0, function* () { if (!this.client.options.oauth.redirectUri) throw Error(__1.LunifyErrors.NoRedirectUri); const params = new URLSearchParams(); params.append('grant_type', 'authorization_code'); params.append('redirect_uri', this.client.options.oauth.redirectUri); params.append('code', code); const res = yield this.client.rest.post('/token', { domain: __1.RequestDomain.Accounts, authRequired: true, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: params }); res.created_timestamp = Date.now(); return new user_1.UserOauth(this.client, res); }); } /** * Refresh a spotify access token * @param {string} refreshToken - oauth refresh token * @example ```ts * const refreshToken = ...; * await api.oauth.refreshToken(refreshToken); * ``` */ refreshToken(refreshToken) { return __awaiter(this, void 0, void 0, function* () { const params = new URLSearchParams(); params.append('grant_type', 'refresh_token'); params.append('refresh_token', refreshToken); const res = yield this.client.rest.post('/token', { domain: __1.RequestDomain.Accounts, authRequired: true, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: params }); if ('message' in res) { throw Error(res.message); } // Sometimes, <UserOauth>.refreshToken is null for some reason. // Spotify claims they return a new refresh token, but I don't trust them. // I am just testing things and see what sticks, will be removed if this doesn't fix it. // It only happens sometimes, it seems to only appear after a few days without restarting the node process. // Contact me: luna@waya.one - https://discord.gg/yYd6YKHQZH // https://developer.spotify.com/documentation/web-api/tutorials/refreshing-tokens res.refresh_token || (res.refresh_token = refreshToken); res.created_timestamp = Date.now(); return new user_1.UserOauth(this.client, res); }); } } exports.OauthManager = OauthManager; //# sourceMappingURL=index.js.map