import { NextFunction, RequestHandler, Request, Response } from 'express'; import { EnvoyEntryEventRequest, EnvoyEventRequest, EnvoyInviteEventRequest, EnvoyLocationEventRequest, EnvoyMigrationRouteRequest, EnvoyNotificationEventRequest, EnvoyOptionsRouteRequest, EnvoyRemoteValueRouteRequest, EnvoySelectedValuesRouteRequest, EnvoyTakeoverEventRequest, EnvoyValidationRouteRequest, } from './EnvoyRequest'; import EnvoyResponse, { EnvoyOptionsRouteResponse, EnvoyRemoteValueRouteResponse, EnvoySelectedValuesRouteResponse, EnvoyValidationRouteResponse, } from './EnvoyResponse'; type SomeObject = Record; type Result = Promise | void; /** * Handle an entry event, such as `entry_sign_in`. * @category Handler */ export type EntryEventHandler = ( req: EnvoyEntryEventRequest & Additions, res: EnvoyResponse, ) => Result; /** * Handle an invite event, such as `invite_created`. * @category Handler */ export type InviteEventHandler = ( req: EnvoyInviteEventRequest & Additions, res: EnvoyResponse, ) => Result; /** * Handle an location event, such as `location_capacity_updated`. * @category Handler */ export type LocationEventHandler = ( req: EnvoyLocationEventRequest & Additions, res: EnvoyResponse, ) => Result; /** * Handle an notification event. * @category Handler */ export type NotificationEventHandler = ( req: EnvoyNotificationEventRequest & Additions, res: EnvoyResponse, ) => Result; /** * Handle a takeover event. * @category Handler */ export type TakeoverEventHandler = ( req: EnvoyTakeoverEventRequest & Additions, res: EnvoyResponse, ) => Result; /** * Handle a `plugin_uninstalled` event for cleaning up. * @category Handler */ export type PluginUninstalledEventHandler = ( req: EnvoyEventRequest<'plugin_uninstalled', never, Config> & Additions, res: EnvoyResponse, ) => Result; /** * Handle a "migration" route. * @category Handler */ export type MigrationRouteHandler = ( req: EnvoyMigrationRouteRequest & Additions, res: EnvoyResponse, ) => Result; /** * Handle an "options" route. * @category Handler */ export type OptionsRouteHandler = ( req: EnvoyOptionsRouteRequest & Additions, res: EnvoyOptionsRouteResponse, ) => Result; /** * Handle a "remote value" route. * @category Handler */ export type RemoteValueRouteHandler = ( req: EnvoyRemoteValueRouteRequest & Additions, res: EnvoyRemoteValueRouteResponse, ) => Result; /** * Handle a "selected values" route. * @category Handler */ export type SelectedValuesRouteHandler = ( req: EnvoySelectedValuesRouteRequest & Additions, res: EnvoySelectedValuesRouteResponse, ) => Result; /** * Handle a "validation" route. * @category Handler */ export type ValidationRouteHandler = ( req: EnvoyValidationRouteRequest & Additions, res: EnvoyValidationRouteResponse, ) => Result; /** * Wraps any express.js-based handlers * to catch Promise-based errors. * * @category Handler */ // eslint-disable-next-line max-len export function asyncHandler( handler: (req: Req, res: Res) => Result, ): RequestHandler { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore return (req: Req, res: Res, next: NextFunction): void => { void Promise.resolve() .then(() => handler(req, res)) .catch(next); }; } /** * Handler for entry events. * * @category Handler */ export function entryEventHandler( handler: EntryEventHandler, ) { return asyncHandler(handler); } /** * Handler for invite events. * * @category Handler */ export function inviteEventHandler( handler: InviteEventHandler, ) { return asyncHandler(handler); } /** * Handler for location events. * * @category Handler */ export function locationEventHandler( handler: LocationEventHandler, ) { return asyncHandler(handler); } /** * Handler for a migration route. * * @category Handler */ export function migrationRouteHandler( handler: MigrationRouteHandler, ) { return asyncHandler(handler); } /** * Handler for notification events. * * @category Handler */ export function notificationEventHandler( handler: NotificationEventHandler, ) { return asyncHandler(handler); } /** * Handler for takeover events. * * @category Handler */ export function takeoverEventHandler( handler: TakeoverEventHandler, ) { return asyncHandler(handler); } /** * Handler for options URL routes. * * @category Handler */ export function optionsRouteHandler( handler: OptionsRouteHandler, ) { return asyncHandler(handler); } /** * Handler for `plugin_uninstalled` events. * * @category Handler */ export function pluginUninstalledEventHandler( handler: PluginUninstalledEventHandler, ) { return asyncHandler(handler); } /** * Handler for remote value URL routes. * * @category Handler */ export function remoteValueRouteHandler( handler: RemoteValueRouteHandler, ) { return asyncHandler(handler); } /** * Handler for selected values URL routes. * * @category Handler */ export function selectedValuesRouteHandler( handler: SelectedValuesRouteHandler, ) { return asyncHandler(handler); } /** * Handler for validation URL routes. * * @category Handler */ export function validationRouteHandler( handler: ValidationRouteHandler, ) { return asyncHandler(handler); }