import {ParamType, EParamType} from "./types"; import {UserInputHelper} from "./input-helper.user"; import {FileInputHelper} from "./input-helper.upload"; import {BodyInputHelper} from "./input-helper.body"; import {ParamInputHelper, SafeParamInputHelper} from "./input-helper.param"; import bodyParser = require("body-parser"); export interface ArgumentHandlerCallbackType { (name: string): InputHelperBuilder; } export abstract class InputHelperBuilder { protected state: ParamType; protected handlerCallback: ArgumentHandlerCallbackType; constructor(parent: {state: ParamType; handlerCallback?: ArgumentHandlerCallbackType;}, from?: EParamType) { this.state = parent.state; this.handlerCallback = parent.handlerCallback; if (from !== undefined) { this.state.from = from; } } _finish() { Object.freeze(this); Object.freeze(this.state); } _copy() { return { state: this.state, handlerCallback: this.handlerCallback, } } _toJSON() { return this.state; } from(from: EParamType.TYPE_LOGIN): UserInputHelper; from(from: EParamType.TYPE_BODY): BodyInputHelper; from(from: EParamType.TYPE_REWRITE_PATH): ParamInputHelper; from(from: EParamType.TYPE_GET): ParamInputHelper; from(from: EParamType.TYPE_POST): ParamInputHelper; from(from: EParamType.TYPE_UPLOAD): FileInputHelper; from(from: EParamType.TYPE_COOKIE): ParamInputHelper; from(from: EParamType.TYPE_SAFE_COOKIE): SafeParamInputHelper; from(from: EParamType.TYPE_SESSION): SafeParamInputHelper; from(from: EParamType): InputHelper; from(from: EParamType): InputHelper { let copy; return inputHelperFactory(from, this); } fromLogin(): UserInputHelper { return inputHelperFactory(EParamType.TYPE_LOGIN, this); } fromBody(): BodyInputHelper { return inputHelperFactory(EParamType.TYPE_BODY, this); } fromPath(): ParamInputHelper { return inputHelperFactory(EParamType.TYPE_REWRITE_PATH, this); } fromGet(): ParamInputHelper { return inputHelperFactory(EParamType.TYPE_GET, this); } fromPost(): ParamInputHelper { return inputHelperFactory(EParamType.TYPE_POST, this); } fromUpload(): FileInputHelper { return inputHelperFactory(EParamType.TYPE_UPLOAD, this); } fromCookie(): ParamInputHelper { return inputHelperFactory(EParamType.TYPE_COOKIE, this); } fromSafeCookie(): SafeParamInputHelper { return inputHelperFactory(EParamType.TYPE_SAFE_COOKIE, this); } fromSession(): SafeParamInputHelper { return inputHelperFactory(EParamType.TYPE_SESSION, this); } assumeType(type: string) { this.state.tsGenerateType = type; return this; } handleArgument(name: string): InputHelperBuilder { return this.handlerCallback(name); } } export abstract class InputHelper extends InputHelperBuilder { constructor(builder: InputHelperBuilder, from?: EParamType) { super(builder._copy(), from); } } const {inputHelperFactory} = require("./input-helper.factory");