UNPKG

lunify.js

Version:

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

53 lines 1.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CredentialsManager = void 0; const __1 = require("../.."); class CredentialsManager { client; accessToken; tokenType; scope; expiresIn; expiresTimestamp; createdTimestamp; constructor(client) { this.client = client; } /** * Fetches client credentials from the spotify api */ async fetch() { const params = new URLSearchParams(); params.append("grant_type", "client_credentials"); params.append("client_id", this.client.options.clientId); params.append("client_secret", this.client.options.clientSecret); const res = await 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(); this.accessToken = res.access_token; this.tokenType = res.token_type; this.expiresIn = res.expires_in * 1_000; this.expiresTimestamp = res.created_timestamp + res.expires_in * 1_000; this.createdTimestamp = res.created_timestamp; return this; } /** * Generates a authorization token header */ async getAuthorization() { if (!this.accessToken || !this.expiresTimestamp || this.expiresTimestamp < Date.now()) { this.accessToken = (await this.fetch()).accessToken; } return this.tokenType + " " + this.accessToken; } } exports.CredentialsManager = CredentialsManager; //# sourceMappingURL=index.js.map