@twurple/eventsub-http
Version:
Listen to events on Twitch via their EventSub API using a HTTP/WebHook server.
82 lines (81 loc) • 2.68 kB
JavaScript
import { __decorate } from "tslib";
import { rtfm } from '@twurple/common';
import { checkHostName } from './checks.js';
import { EventSubHttpBase } from './EventSubHttpBase.js';
/**
* An Express middleware for the Twitch EventSub event distribution mechanism.
*
* You can find an extensive example on how to use this class in the [documentation](/docs/getting-data/eventsub/express).
*
* @hideProtected
* @inheritDoc
*
* @meta category main
*/
let EventSubMiddleware = class EventSubMiddleware extends EventSubHttpBase {
_hostName;
_pathPrefix;
_usePathPrefixInHandlers;
/**
* Creates a new EventSub middleware wrapper.
*
* @param config
*
* @expandParams
*/
constructor(config) {
super(config);
checkHostName(config.hostName);
this._hostName = config.hostName;
this._pathPrefix = config.pathPrefix;
this._usePathPrefixInHandlers = config.usePathPrefixInHandlers ?? true;
}
/**
* Applies middleware that handles EventSub notifications to an Express app/router.
*
* @param router The app or router the middleware should be applied to.
*/
apply(router) {
let requestPathPrefix = undefined;
if (this._usePathPrefixInHandlers) {
requestPathPrefix = this._pathPrefix;
requestPathPrefix &&= `/${requestPathPrefix.replace(/^\/|\/$/g, '')}`;
}
const requestHandler = this._createHandleRequest();
const dropLegacyHandler = this._createDropLegacyRequest();
const healthHandler = this._createHandleHealthRequest();
if (requestPathPrefix) {
router.post(`${requestPathPrefix}/event/:id`, requestHandler);
router.post(`${requestPathPrefix}/:id`, dropLegacyHandler);
if (this._helperRoutes) {
router.get(`${requestPathPrefix}`, healthHandler);
}
}
else {
router.post('event/:id', requestHandler);
router.post(':id', dropLegacyHandler);
if (this._helperRoutes) {
router.get('/', healthHandler);
}
}
}
/**
* Marks the middleware as ready to receive events.
*
* The express app should be started before this.
*/
async markAsReady() {
await this._resumeExistingSubscriptions();
this._readyToSubscribe = true;
}
async getHostName() {
return this._hostName;
}
async getPathPrefix() {
return this._pathPrefix;
}
};
EventSubMiddleware = __decorate([
rtfm('eventsub-http', 'EventSubMiddleware')
], EventSubMiddleware);
export { EventSubMiddleware };