Skip to content

Commit

Permalink
rm controller result handler and move logic to request handler helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalguptaofficial committed Nov 29, 2023
1 parent f263ec4 commit 516e02a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 84 deletions.
73 changes: 0 additions & 73 deletions src/handlers/controller_result_handler.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export * from "./route_handler";
export * from "./request_handler";
export * from "./file_handler";
export * from "./request_handler_helper";
export * from "./controller_result_handler";
export * from './injector_handler';
11 changes: 4 additions & 7 deletions src/handlers/request_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import { TGuard } from "../types";
import { HTTP_METHOD } from "../enums";
import { InjectorHandler } from "./injector_handler";
import { IHttpResult, IRouteMatch } from "../interfaces";
import { ControllerResultHandler } from "./controller_result_handler";
import { FileHandler } from "./file_handler";
import { RequestHandlerHelper } from "./request_handler_helper";


export class RequestHandler extends ControllerResultHandler {
export class RequestHandler extends RequestHandlerHelper {

private routeMatchInfo_: IRouteMatch;
private wallInstances: Wall[] = [];
Expand Down Expand Up @@ -164,8 +163,8 @@ export class RequestHandler extends ControllerResultHandler {
try {
const wallResult = await this.executeWallIncoming_();
if (wallResult) {
await this.onTerminationFromWall(wallResult);
return;
this.controllerResult = wallResult;
return this.handleFinalResult_();
}
const pathUrl = urlDetail.pathname;

Expand All @@ -186,8 +185,6 @@ export class RequestHandler extends ControllerResultHandler {
}
}



handle(request: http.IncomingMessage, response: http.ServerResponse) {
this.componentProps = {
request,
Expand Down
64 changes: 61 additions & 3 deletions src/handlers/request_handler_helper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { HTTP_STATUS_CODE, MIME_TYPE, HTTP_METHOD, HTTP_RESULT_TYPE } from "../enums";
import { CONTENT_TYPE, SET_COOKIE } from "../constants";
import { FORT_GLOBAL } from "../constants/fort_global";
import { CONTENT_TYPE, SET_COOKIE, FORT_GLOBAL } from "../constants";
import * as Negotiator from "negotiator";
import { IComponentProp, IException, IHttpResult } from "../interfaces";
import { IComponentProp, IException, IFileResultInfo, IHttpResult } from "../interfaces";
import { textResult, getResultBasedOnMiMe } from "../helpers";
import { HttpFormatResult } from "../types";
import { parse } from "path";
import { FileHandler } from "./file_handler";

export class RequestHandlerHelper {
protected componentProps: IComponentProp;
Expand Down Expand Up @@ -178,4 +179,61 @@ export class RequestHandlerHelper {
this.response.end(data);
}

private handleRedirectResult_() {
this.response.writeHead(this.controllerResult.statusCode || HTTP_STATUS_CODE.Ok,
{ 'Location': (this.controllerResult as IHttpResult).responseData });
this.response.end();
return null;
}

private handleFileResult_() {
const result = this.controllerResult as IHttpResult;
const fileResult = result.responseData as IFileResultInfo;
const parsedPath = parse(fileResult.filePath);
if (fileResult.shouldDownload === true) {
const fileName = fileResult.alias == null ? parsedPath.name : fileResult.alias;
this.response.setHeader(
"content-disposition",
`attachment;filename=${fileName}${parsedPath.ext}`
);
}
const fileHandler = new FileHandler(this as any);
return fileHandler.handleFileRequestFromAbsolutePath(
fileResult.filePath, parsedPath.ext
);
}

protected handleFinalResult_() {
const result: IHttpResult = this.controllerResult;
this.setCookie();

switch (result.type) {
case HTTP_RESULT_TYPE.Default:
{
const contentType = result.contentType || MIME_TYPE.Text;
const negotiateMimeType = this.getContentTypeFromNegotiation(contentType) as MIME_TYPE;
if (negotiateMimeType != null) {
this.endResponse_(negotiateMimeType);
}
else {
return this.onNotAcceptableRequest();
}
}
break;
case HTTP_RESULT_TYPE.Redirect:
return this.handleRedirectResult_();
case HTTP_RESULT_TYPE.File:
return this.handleFileResult_();
case HTTP_RESULT_TYPE.FormattedResult:
return this.handleFormatResult_();
}
}

onResultFromComponent(result: IHttpResult) {
this.controllerResult = result || textResult("");
// return () => {
return this.handleFinalResult_;
// }
}

}

0 comments on commit 516e02a

Please sign in to comment.