Skip to content

Commit

Permalink
added the manual formating option of response
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalguptaofficial committed Nov 16, 2018
1 parent d550719 commit 4db029c
Show file tree
Hide file tree
Showing 20 changed files with 142 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# fort

Simple & structured MVC web framework for nodejs targeting good code structures, modularity based on the principle of fortification.
Modern, Advanced & structured MVC web framework for nodejs targeting good code structures, modularity based on the principle of fortification.
18 changes: 15 additions & 3 deletions dist/fort.commonjs2.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/fort.commonjs2.js.map

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions dist/ts/abstracts/controller.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ import { CookieManager } from "../model/cookie_manager";
export declare abstract class Controller {
request: IHttpRequest;
response: IHttpResponse;
query: any;
body: any;
query: {
[key: string]: any;
};
body: {
[key: string]: any;
};
session: SessionProvider;
cookies: CookieManager;
params: any;
data: any;
params: {
[key: string]: any;
};
data: {
[key: string]: any;
};
}
16 changes: 12 additions & 4 deletions dist/ts/abstracts/guard.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ import { Controller } from "./controller";
export declare abstract class Guard implements Controller {
request: IHttpRequest;
response: IHttpResponse;
query: object;
body: object;
query: {
[key: string]: any;
};
body: {
[key: string]: any;
};
session: SessionProvider;
cookies: CookieManager;
params: any;
data: any;
params: {
[key: string]: any;
};
data: {
[key: string]: any;
};
abstract check(): Promise<boolean>;
}
16 changes: 12 additions & 4 deletions dist/ts/abstracts/shield.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ import { CookieManager } from "../model/cookie_manager";
export declare abstract class Shield implements Controller {
request: IHttpRequest;
response: IHttpResponse;
query: object;
body: object;
query: {
[key: string]: any;
};
body: {
[key: string]: any;
};
session: SessionProvider;
cookies: CookieManager;
params: any;
data: any;
params: {
[key: string]: any;
};
data: {
[key: string]: any;
};
abstract protect(): Promise<boolean>;
}
16 changes: 12 additions & 4 deletions dist/ts/abstracts/wall.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ import { CookieManager } from "../model/cookie_manager";
export declare abstract class Wall implements Controller {
request: IHttpRequest;
response: IHttpResponse;
query: object;
body: object;
query: {
[key: string]: any;
};
body: {
[key: string]: any;
};
session: SessionProvider;
cookies: CookieManager;
params: any;
data: any;
params: {
[key: string]: any;
};
data: {
[key: string]: any;
};
abstract onIncoming(): Promise<boolean>;
abstract onOutgoing(): Promise<boolean>;
}
3 changes: 3 additions & 0 deletions dist/ts/types/http_result.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ export declare type HttpResult = {
responseData: any;
contentType: MIME_TYPE;
file?: FileResultInfo;
responseFormat?: {
[type: string]: () => any;
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fortjs",
"version": "0.4.0",
"description": "Simple & structured MVC web framework for nodejs targeting good code structures, modularity.",
"description": "Modern, Advanced & structured MVC web framework for nodejs targeting good code structures, modularity based on the principle of fortification.",
"main": "dist/fort.commonjs2.js",
"types": "dist/ts/index.d.ts",
"scripts": {
Expand Down
9 changes: 4 additions & 5 deletions src/code/abstracts/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import { CookieManager } from "../model/cookie_manager";
export abstract class Controller {
request: IHttpRequest;
response: IHttpResponse;
query: any;
body: any;
query: { [key: string]: any };
body: { [key: string]: any };
session: SessionProvider;
cookies: CookieManager;
params: any;

data: any;
params: { [key: string]: any };
data: { [key: string]: any };
}
8 changes: 4 additions & 4 deletions src/code/abstracts/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { Controller } from "./controller";
export abstract class Guard implements Controller {
request: IHttpRequest;
response: IHttpResponse;
query: object;
body: object;
query: { [key: string]: any };
body: { [key: string]: any };
session: SessionProvider;
cookies: CookieManager;
params: any;
data: any;
params: { [key: string]: any };
data: { [key: string]: any };
abstract check(): Promise<boolean>;
}
8 changes: 4 additions & 4 deletions src/code/abstracts/shield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { CookieManager } from "../model/cookie_manager";
export abstract class Shield implements Controller {
request: IHttpRequest;
response: IHttpResponse;
query: object;
body: object;
query: { [key: string]: any };
body: { [key: string]: any };
session: SessionProvider;
cookies: CookieManager;
params: any;
data: any;
params: { [key: string]: any };
data: { [key: string]: any };
abstract protect(): Promise<boolean>;
}

8 changes: 4 additions & 4 deletions src/code/abstracts/wall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { CookieManager } from "../model/cookie_manager";
export abstract class Wall implements Controller {
request: IHttpRequest;
response: IHttpResponse;
query: object;
body: object;
query: { [key: string]: any };
body: { [key: string]: any };
session: SessionProvider;
cookies: CookieManager;
params: any;
data: any;
params: { [key: string]: any };
data: { [key: string]: any };
abstract onIncoming(): Promise<boolean>;
abstract onOutgoing(): Promise<boolean>;
}
3 changes: 3 additions & 0 deletions src/code/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export function create(option: AppOption) {
Global.foldersAllowed = Util.isNull(option.foldersAllowed) ? [] : option.foldersAllowed;
Global.errorHandler = Util.isNull(option.errorHandler) ? ErrorHandler : option.errorHandler;
Global.defaultPath = Util.isNull(option.defaultPath) === true ? "" : "/" + option.defaultPath.toLowerCase();
Global.connectonKeepAliveTimeout = option.connectonKeepAliveTimeout == null ? 5000 : option.connectonKeepAliveTimeout;

}
else {
Global.port = 4000;
Expand All @@ -43,5 +45,6 @@ export function create(option: AppOption) {
throw err;
}
});
app.keepAliveTimeout = Global.connectonKeepAliveTimeout;
}

1 change: 1 addition & 0 deletions src/code/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export class Global {
static walls: typeof GenericWall[] = [];
static errorHandler: typeof ErrorHandler;
static defaultPath?: string;
static connectonKeepAliveTimeout?: number
}
19 changes: 16 additions & 3 deletions src/code/request_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,22 @@ export class RequestHandler extends FileHandler {
const negotiatedMiMeType = this.getContentTypeFromNegotiation(contentType);
if (negotiatedMiMeType != null) {
if (result.file == null) {
this.response.writeHead(result.statusCode || HTTP_STATUS_CODE.Ok,
{ [Content__Type]: negotiatedMiMeType });
this.response.end(getData());
if (result.responseFormat == null) {
this.response.writeHead(result.statusCode || HTTP_STATUS_CODE.Ok,
{ [Content__Type]: negotiatedMiMeType });
this.response.end(getData());
}
else {
const key = Object.keys(result.responseFormat).find(qry => qry === negotiatedMiMeType);
if (key != null) {
this.response.writeHead(result.statusCode || HTTP_STATUS_CODE.Ok,
{ [Content__Type]: negotiatedMiMeType });
this.response.end(result.responseFormat[key]());
}
else {
this.onNotAcceptableRequest();
}
}
}
else {
if (result.file.shouldDownload === true) {
Expand Down
11 changes: 11 additions & 0 deletions src/code/types/app_option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,15 @@ export type AppOption = {
* @memberof IAppOption
*/
defaultPath?: string;

/**
* Timeout in milliseconds. Default: 5000 (5 seconds).
* The number of milliseconds of inactivity a server needs to wait for additional incoming data,
* after it has finished writing the last response, before a socket will be destroyed.
* If the server receives new data before the keep-alive timeout has fired,
* it will reset the regular inactivity timeout
*
* @type {number}
*/
connectonKeepAliveTimeout?: number
}
5 changes: 4 additions & 1 deletion src/code/types/http_result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export type HttpResult = {
statusCode: HTTP_STATUS_CODE;
responseData: any;
contentType: MIME_TYPE;
file?: FileResultInfo
file?: FileResultInfo;
responseFormat?: {
[type: string]: () => any;
}
}

22 changes: 21 additions & 1 deletion test/src/code/controllers/user_controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, action, HTTP_METHOD, declareAsController, htmlResult, textResult, renderView, defaultAction } from "fortjs";
import { Controller, action, HTTP_METHOD, declareAsController, htmlResult, textResult, renderView, defaultAction, HttpResult, MIME_TYPE } from "fortjs";
import * as fs from "fs";

@declareAsController()
Expand Down Expand Up @@ -57,4 +57,24 @@ export class UserController extends Controller {
});
});
}

formatter() {
return new Promise((resolve, reject) => {
resolve({
contentType: MIME_TYPE.Text,
responseData: null,
statusCode: 400,
responseFormat: {
[MIME_TYPE.Text]: function () {
return 'Text';
},
[MIME_TYPE.Json]: function () {
return {
result: 'Text'
}
}
}
} as HttpResult)
});
}
}
2 changes: 1 addition & 1 deletion test/src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4db029c

Please sign in to comment.