refactor: optimize framework

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2019-06-21 12:30:30 +08:00
parent b6443f805d
commit dc46ab904a
7 changed files with 57 additions and 55 deletions

View File

@@ -30,4 +30,4 @@
"bugs": {
"url": "https://github.com/502647092/cc-server-parent/issues"
}
}
}

View File

@@ -1,7 +1,7 @@
import { Container, interfaces as inversify_interfaces } from 'inversify'
import { TYPE, interfaces as express_interfaces } from 'inversify-express-utils'
import { METADATA_KEY, VAILD_TYPE } from './constants'
import { interfaces } from './interfaces'
import { METADATA_KEY } from './constants'
import { VaildError } from './interfaces'
import { getVaildMethodMetadata, getVaildControllerMetadata, getVaildModelMetadata } from './utils'
let handler = {
@@ -12,32 +12,24 @@ let handler = {
// loop @Valid params
for (const param of methodParams) {
// get function argument value
let origin = argumentsList[param.index]
let origin = argumentsList[param.index];
let props = getVaildModelMetadata(param.type);
for (const prop of props) {
let propValue = origin[prop.name];
switch (prop.type) {
case VAILD_TYPE.NOT_BLANK:
if (!propValue) {
throw new Error(prop.message);
}
break;
case VAILD_TYPE.NOT_NULL:
if (propValue == undefined) {
throw new Error(prop.message);
}
break;
default:
throw new Error('Unkonw Vaild Type!')
if (!prop.handle(origin[prop.name])) {
throw new VaildError(prop.message);
}
}
}
return target.apply(thisArgument, argumentsList);
} catch (ex) {
res.status(400).json({
status: 400,
message: ex.message
})
if (ex instanceof VaildError) {
res.status(400).json({
status: 400,
message: ex.message
})
return;
}
throw ex;
}
}
}

View File

@@ -1,7 +1,7 @@
import 'reflect-metadata'
import { METADATA_KEY, VAILD_TYPE } from './constants';
import { getVaildControllerMetadata, getVaildModelMetadata } from './utils'
import { interfaces } from './interfaces'
import { VaildFunction, interfaces } from './interfaces'
/**
* ParameterVaild
*/
@@ -20,18 +20,26 @@ export function Vaild(): ParameterDecorator {
}
}
let vaildFunctions: {
[methodName: string]: VaildFunction;
} = {};
/**
* Vaild Blank String
* @param message Error Message
*/
export const NotBlank: (message?: string) => PropertyDecorator = vaildDecoratorFactory(VAILD_TYPE.NOT_BLANK);
export const NotBlank: (message?: string) => PropertyDecorator = vaildDecoratorFactory(
VAILD_TYPE.NOT_BLANK, (param) => !!param);
/**
* Vaild Null Param
* @param message Error Message
*/
export const NotNull: (message?: string) => PropertyDecorator = vaildDecoratorFactory(VAILD_TYPE.NOT_NULL);
export const NotNull: (message?: string) => PropertyDecorator = vaildDecoratorFactory(
VAILD_TYPE.NOT_NULL, (param) => param !== undefined
);
function vaildDecoratorFactory(type: VAILD_TYPE): () => PropertyDecorator {
function vaildDecoratorFactory(type: VAILD_TYPE, handle: VaildFunction): () => PropertyDecorator {
vaildFunctions[type] = handle;
return function(message?: string): PropertyDecorator {
return vaildProperty(type, message);
};
@@ -41,9 +49,10 @@ function vaildProperty(type: VAILD_TYPE, message?: string): PropertyDecorator {
return (model: Object, propertyKey: string) => {
let metadataList: interfaces.PropertyMetadata[] = getVaildModelMetadata(model.constructor);
metadataList.push({
type: type,
name: propertyKey,
message: message || `model ${model.constructor.name} property ${propertyKey} vaild failed => ${VAILD_TYPE[type]}`,
type: type
handle: vaildFunctions[type]
})
Reflect.defineMetadata(METADATA_KEY.vaildModel, metadataList, model.constructor);
}

View File

@@ -1,6 +1,10 @@
import { VAILD_TYPE } from './constants'
namespace interfaces {
export class VaildError extends Error { }
export declare type VaildFunction = (param: object) => boolean;
export namespace interfaces {
export interface MethodMetadata {
[methodName: string]: ParameterMetadata[];
}
@@ -11,10 +15,9 @@ namespace interfaces {
}
export interface PropertyMetadata {
type: VAILD_TYPE;
name: string;
message: string;
type: VAILD_TYPE;
handle: VaildFunction;
}
}
export { interfaces };