twitch-oauth-authorization-code-express
Advanced tools
Comparing version
@@ -9,6 +9,11 @@ import { Application } from "express"; | ||
}; | ||
export declare class InvalidStateTokenError extends Error { | ||
constructor(); | ||
} | ||
declare type OAuthTokenCallback = (req: express.Request, res: express.Response, info: TokenInfo) => void; | ||
declare type ErrorCallback = (e: Error) => string | Promise<string>; | ||
declare type TwitchOAuthPathOptions = { | ||
app: Application; | ||
callback: OAuthTokenCallback; | ||
errorHandler?: ErrorCallback; | ||
redirect_uri: string; | ||
@@ -15,0 +20,0 @@ scopes?: string[]; |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -43,2 +56,28 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
var url_1 = require("url"); | ||
var InvalidStateTokenError = /** @class */ (function (_super) { | ||
__extends(InvalidStateTokenError, _super); | ||
function InvalidStateTokenError() { | ||
return _super.call(this, "Invalid state token returned from Twitch.") || this; | ||
} | ||
return InvalidStateTokenError; | ||
}(Error)); | ||
exports.InvalidStateTokenError = InvalidStateTokenError; | ||
function handleError(res, e, errorHandler) { | ||
if (errorHandler) { | ||
var errRetText = errorHandler(e); | ||
if (errRetText.then !== undefined) { | ||
errRetText.then(function (str) { | ||
res.send(str); | ||
res.end(); | ||
}); | ||
} | ||
else { | ||
res.send(errRetText); | ||
res.end(); | ||
} | ||
} | ||
else { | ||
res.end(e); | ||
} | ||
} | ||
// Assumes that the express application has the session middleware installed | ||
@@ -58,3 +97,4 @@ function setupTwitchOAuthPath(options) { | ||
//TODO better error handling? | ||
res.end('Invalid state token returned from twitch.'); | ||
var e = new InvalidStateTokenError(); | ||
handleError(res, e, options.errorHandler); | ||
return; | ||
@@ -93,5 +133,3 @@ } | ||
http_request.on("error", function (e) { | ||
//TODO better error handling? | ||
res.send('Got error'); | ||
res.end(e); | ||
handleError(res, e, options.errorHandler); | ||
}); | ||
@@ -98,0 +136,0 @@ http_request.end(); |
{ | ||
"name": "twitch-oauth-authorization-code-express", | ||
"version": "2.1.2", | ||
"version": "2.2.0", | ||
"description": "Small library to get a Twitch OAuth code using the OAuth authorization code flow.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
14031
12.52%241
21.72%