fix(interactions): add async responses

This commit is contained in:
41666 2021-08-07 18:00:20 -04:00
parent 3601d435b2
commit 26bc74bcbc
11 changed files with 220 additions and 149 deletions

View file

@ -66,5 +66,5 @@ router.addFallback('root', () => {
});
addEventListener('fetch', (event: FetchEvent) => {
event.respondWith(router.handle(event.request));
event.respondWith(router.handle(event));
});

View file

@ -4,7 +4,7 @@ import {
permissions as Permissions,
} from '@roleypoly/misc-utils/hasPermission';
import { SessionData, UserGuildPermissions } from '@roleypoly/types';
import { Handler, WrappedKVNamespace } from '@roleypoly/worker-utils';
import { Handler, HandlerTools, WrappedKVNamespace } from '@roleypoly/worker-utils';
import KSUID from 'ksuid';
import {
allowedCallbackHosts,
@ -107,7 +107,7 @@ const NotAuthenticated = (extra?: string) =>
export const withSession =
(wrappedHandler: (session: SessionData) => Handler): Handler =>
async (request: Request): Promise<Response> => {
async (request: Request, tools: HandlerTools): Promise<Response> => {
const sessionID = getSessionID(request);
if (!sessionID) {
return NotAuthenticated('missing authentication');
@ -118,7 +118,7 @@ export const withSession =
return NotAuthenticated('authentication expired or not found');
}
return await wrappedHandler(session)(request);
return await wrappedHandler(session)(request, tools);
};
export const setupStateSession = async <T>(data: T): Promise<string> => {
@ -138,9 +138,9 @@ export const getStateSession = async <T>(stateID: string): Promise<T | undefined
export const isRoot = (userID: string): boolean => rootUsers.includes(userID);
export const onlyRootUsers = (handler: Handler): Handler =>
withSession((session) => (request: Request) => {
withSession((session) => (request: Request, tools: HandlerTools) => {
if (isRoot(session.user.id)) {
return handler(request);
return handler(request, tools);
}
return respond(
@ -166,11 +166,11 @@ export const isAllowedCallbackHost = (host: string): boolean => {
export const interactionsEndpoint =
(handler: Handler): Handler =>
async (request: Request): Promise<Response> => {
async (request: Request, tools: HandlerTools): Promise<Response> => {
const authHeader = request.headers.get('authorization') || '';
if (authHeader !== `Shared ${interactionsSharedKey}`) {
return notAuthenticated();
}
return handler(request);
return handler(request, tools);
};