feat(federation): Allow parseBody to return a type other than void

This commit is contained in:
Jesse Wierzbinski 2024-05-28 14:14:02 -10:00
parent 16eae048c8
commit 57bd2a5c43
No known key found for this signature in database

View file

@ -2,25 +2,25 @@ import type { EntityValidator } from "../validator/index";
type MaybePromise<T> = T | Promise<T>; type MaybePromise<T> = T | Promise<T>;
type ParserCallbacks = { type ParserCallbacks<T> = {
note: (note: typeof EntityValidator.$Note) => MaybePromise<void>; note: (note: typeof EntityValidator.$Note) => MaybePromise<T>;
follow: (follow: typeof EntityValidator.$Follow) => MaybePromise<void>; follow: (follow: typeof EntityValidator.$Follow) => MaybePromise<T>;
followAccept: ( followAccept: (
followAccept: typeof EntityValidator.$FollowAccept, followAccept: typeof EntityValidator.$FollowAccept,
) => MaybePromise<void>; ) => MaybePromise<T>;
followReject: ( followReject: (
followReject: typeof EntityValidator.$FollowReject, followReject: typeof EntityValidator.$FollowReject,
) => MaybePromise<void>; ) => MaybePromise<T>;
user: (user: typeof EntityValidator.$User) => MaybePromise<void>; user: (user: typeof EntityValidator.$User) => MaybePromise<T>;
like: (like: typeof EntityValidator.$Like) => MaybePromise<void>; like: (like: typeof EntityValidator.$Like) => MaybePromise<T>;
dislike: (dislike: typeof EntityValidator.$Dislike) => MaybePromise<void>; dislike: (dislike: typeof EntityValidator.$Dislike) => MaybePromise<T>;
undo: (undo: typeof EntityValidator.$Undo) => MaybePromise<void>; undo: (undo: typeof EntityValidator.$Undo) => MaybePromise<T>;
serverMetadata: ( serverMetadata: (
serverMetadata: typeof EntityValidator.$ServerMetadata, serverMetadata: typeof EntityValidator.$ServerMetadata,
) => MaybePromise<void>; ) => MaybePromise<T>;
extension: ( extension: (
extension: typeof EntityValidator.$Extension, extension: typeof EntityValidator.$Extension,
) => MaybePromise<void>; ) => MaybePromise<T>;
}; };
export class RequestParserHandler { export class RequestParserHandler {
@ -34,24 +34,27 @@ export class RequestParserHandler {
/** /**
* Parse the body of the request and call the appropriate callback. * Parse the body of the request and call the appropriate callback.
* To change the return type, edit the ReturnType generic parameter.
* @param callbacks The callbacks to call when a specific entity is found. * @param callbacks The callbacks to call when a specific entity is found.
* @returns A promise that resolves when the body has been parsed, and the callbacks have finished executing. * @returns A promise that resolves when the body has been parsed, and the callbacks have finished executing.
*/ */
public async parseBody(callbacks: Partial<ParserCallbacks>): Promise<void> { public async parseBody<ReturnType = void>(
callbacks: Partial<ParserCallbacks<ReturnType>>,
): Promise<ReturnType> {
if (!this.body.type) throw new Error("Missing type field in body"); if (!this.body.type) throw new Error("Missing type field in body");
switch (this.body.type) { switch (this.body.type) {
case "Note": { case "Note": {
const note = await this.validator.Note(this.body); const note = await this.validator.Note(this.body);
if (callbacks.note) await callbacks.note(note); if (callbacks.note) return await callbacks.note(note);
break; break;
} }
case "Follow": { case "Follow": {
const follow = await this.validator.Follow(this.body); const follow = await this.validator.Follow(this.body);
if (callbacks.follow) await callbacks.follow(follow); if (callbacks.follow) return await callbacks.follow(follow);
break; break;
} }
@ -61,7 +64,7 @@ export class RequestParserHandler {
); );
if (callbacks.followAccept) if (callbacks.followAccept)
await callbacks.followAccept(followAccept); return await callbacks.followAccept(followAccept);
break; break;
} }
@ -71,35 +74,35 @@ export class RequestParserHandler {
); );
if (callbacks.followReject) if (callbacks.followReject)
await callbacks.followReject(followReject); return await callbacks.followReject(followReject);
break; break;
} }
case "User": { case "User": {
const user = await this.validator.User(this.body); const user = await this.validator.User(this.body);
if (callbacks.user) await callbacks.user(user); if (callbacks.user) return await callbacks.user(user);
break; break;
} }
case "Like": { case "Like": {
const like = await this.validator.Like(this.body); const like = await this.validator.Like(this.body);
if (callbacks.like) await callbacks.like(like); if (callbacks.like) return await callbacks.like(like);
break; break;
} }
case "Dislike": { case "Dislike": {
const dislike = await this.validator.Dislike(this.body); const dislike = await this.validator.Dislike(this.body);
if (callbacks.dislike) await callbacks.dislike(dislike); if (callbacks.dislike) return await callbacks.dislike(dislike);
break; break;
} }
case "Undo": { case "Undo": {
const undo = await this.validator.Undo(this.body); const undo = await this.validator.Undo(this.body);
if (callbacks.undo) await callbacks.undo(undo); if (callbacks.undo) return await callbacks.undo(undo);
break; break;
} }
@ -109,14 +112,15 @@ export class RequestParserHandler {
); );
if (callbacks.serverMetadata) if (callbacks.serverMetadata)
await callbacks.serverMetadata(serverMetadata); return await callbacks.serverMetadata(serverMetadata);
break; break;
} }
case "Extension": { case "Extension": {
const extension = await this.validator.Extension(this.body); const extension = await this.validator.Extension(this.body);
if (callbacks.extension) await callbacks.extension(extension); if (callbacks.extension)
return await callbacks.extension(extension);
break; break;
} }
@ -125,5 +129,7 @@ export class RequestParserHandler {
`Invalid type field in body: ${this.body.type}`, `Invalid type field in body: ${this.body.type}`,
); );
} }
throw new Error(`Invalid type field in body: ${this.body.type}`);
} }
} }