handlers.js/src/handler.ts

44 lines
1.1 KiB
TypeScript

import { method, request, responder, response } from "./interface/index";
export class handler<RequestCustomType, ResponseCustomType> {
public responders: responder<RequestCustomType, ResponseCustomType>[];
public method: method;
constructor(
method: method,
responders: responder<RequestCustomType, ResponseCustomType>[]
) {
this.responders = responders;
this.method = method;
}
add(responder: responder<RequestCustomType, ResponseCustomType>) {
this.responders.push(responder);
}
async respond(
request: request<RequestCustomType>,
responseMessage: response<ResponseCustomType> = new response<ResponseCustomType>(
""
)
): Promise<response<ResponseCustomType> | void> {
switch (this.responders.length) {
case 0:
Promise.reject("No responders found in this handler.");
break;
case 1:
return this.responders[0](request, responseMessage);
default:
for (let responder of this.responders) {
let thisResponse = await responder(request, responseMessage);
if (thisResponse instanceof response) {
responseMessage = thisResponse;
}
}
return responseMessage;
}
}
}
export default handler;