docs(federation): 📝 Add some docs to new request parser classes

This commit is contained in:
Jesse Wierzbinski 2024-05-28 14:22:30 -10:00
parent 57bd2a5c43
commit 3ed54a7c21
No known key found for this signature in database

View file

@ -23,6 +23,21 @@ type ParserCallbacks<T> = {
) => MaybePromise<T>; ) => MaybePromise<T>;
}; };
/**
* A class to parse the body of a request and call the appropriate callback.
* @example
* const parser = new RequestParserHandler(body, validator);
*
* await parser.parseBody({
* note: (note) => {
* console.log(note);
* },
* follow: (follow) => {
* console.log(follow);
* },
* ...
* });
*/
export class RequestParserHandler { export class RequestParserHandler {
constructor( constructor(
private readonly body: Record< private readonly body: Record<
@ -35,8 +50,20 @@ 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. * To change the return type, edit the ReturnType generic parameter.
* const parser = new RequestParserHandler(body, validator);
* @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.
* @throws If the type field is missing or invalid
* @example
* await parser.parseBody({
* note: (note) => {
* console.log(note);
* },
* follow: (follow) => {
* console.log(follow);
* },
* ...
* });
*/ */
public async parseBody<ReturnType = void>( public async parseBody<ReturnType = void>(
callbacks: Partial<ParserCallbacks<ReturnType>>, callbacks: Partial<ParserCallbacks<ReturnType>>,