UNPKG

@evolvejs/core

Version:

An advanced Discord API wrapper with TS and JS support

73 lines (72 loc) 3.11 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Oauth2 = void 0; const __1 = require(".."); const node_fetch_1 = __importDefault(require("node-fetch")); class Oauth2 { constructor(client) { this.client = client; if (!this.client.secret) throw this.client.transformer.error("No Client Secret Provided in EvolveBuilder"); } requestOauth2Token(options) { return __awaiter(this, void 0, void 0, function* () { let string = ""; for (const [key, value] of Object.entries({ client_id: this.client.user.id, client_secret: this.client.secret, grant_type: "authorization_code", code: options.code, redirect_uri: options.redirectUri, scope: options.scopes, })) { if (!value) continue; string += `&${encodeURIComponent(key)}=${encodeURIComponent(value)}`; } const fetched = yield node_fetch_1.default(`${__1.CONSTANTS.Api}/oauth2/token`, { headers: { "Content-Type": "application/x-www-form-urlencoded", }, method: "POST", body: string.substring(1), }); return yield fetched.json(); }); } requestTokenExchange(refreshToken, redirectURI, scopes) { return __awaiter(this, void 0, void 0, function* () { let string = ""; for (const [k, v] of Object.entries({ client_id: this.client.user.id, client_secret: this.client.secret, grant_type: "refresh_token", refresh_token: refreshToken, redirect_uri: redirectURI, scope: scopes, })) { if (!v) continue; string += `&${encodeURIComponent(k)}=${encodeURIComponent(v)}`; } const fetched = yield node_fetch_1.default(`${__1.CONSTANTS.Api}/oauth2/token`, { method: "POST", body: string.substring(1), }); return fetched.json(); }); } } exports.Oauth2 = Oauth2;