@@ -30,4 +30,4 @@
 | 
			
		||||
    "bugs": {
 | 
			
		||||
        "url": "https://github.com/502647092/cc-server-parent/issues"
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
@@ -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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -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 };
 | 
			
		||||
@@ -13,7 +13,7 @@ const TABLE = 'users'
 | 
			
		||||
 | 
			
		||||
class ExampleModel {
 | 
			
		||||
    _id: string;
 | 
			
		||||
    @NotBlank("用户名不得为空!")
 | 
			
		||||
    @NotBlank("username must not be blank!")
 | 
			
		||||
    username: string;
 | 
			
		||||
    password: string;
 | 
			
		||||
    @NotNull()
 | 
			
		||||
@@ -49,8 +49,8 @@ export class Controller {
 | 
			
		||||
    public async create(
 | 
			
		||||
        @Vaild() @requestBody() model: ExampleModel
 | 
			
		||||
    ): Promise<ExampleModel> {
 | 
			
		||||
        return model;
 | 
			
		||||
        //return this.client.insertOne(model);
 | 
			
		||||
        return this.client.findOneById('5d0af7c039179a28de618cb8');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @httpPost('/:id')
 | 
			
		||||
 
 | 
			
		||||
@@ -2,18 +2,21 @@ import 'reflect-metadata';
 | 
			
		||||
import * as express from "express";
 | 
			
		||||
import { InversifyExpressServer, interfaces, getRouteInfo } from 'inversify-express-utils';
 | 
			
		||||
import * as bodyParser from 'body-parser';
 | 
			
		||||
import { buildProviderModule } from 'cc-server-ioc';
 | 
			
		||||
import { rebuildServer } from 'cc-server-binding'
 | 
			
		||||
import { container, buildProviderModule } from 'cc-server-ioc';
 | 
			
		||||
import * as prettyjson from "prettyjson";
 | 
			
		||||
import { Container } from 'inversify';
 | 
			
		||||
 | 
			
		||||
export class CcServerBoot {
 | 
			
		||||
    private server: InversifyExpressServer;
 | 
			
		||||
    private serverInstance: express.Application;
 | 
			
		||||
    constructor() {
 | 
			
		||||
        container.load(buildProviderModule());
 | 
			
		||||
    private _container: Container;
 | 
			
		||||
    private _server: InversifyExpressServer;
 | 
			
		||||
    private _serverInstance: express.Application;
 | 
			
		||||
    constructor(container?: Container) {
 | 
			
		||||
        this._container = container || new Container();
 | 
			
		||||
        this._container.load(buildProviderModule());
 | 
			
		||||
        // start the server
 | 
			
		||||
        this.server = new InversifyExpressServer(container);
 | 
			
		||||
        this.server.setConfig((app) => {
 | 
			
		||||
        this._server = new InversifyExpressServer(this._container);
 | 
			
		||||
        this._server.setConfig((app) => {
 | 
			
		||||
            app.use(bodyParser.urlencoded({
 | 
			
		||||
                extended: true
 | 
			
		||||
            }));
 | 
			
		||||
@@ -23,23 +26,23 @@ export class CcServerBoot {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public setConfig(fn: interfaces.ConfigFunction) {
 | 
			
		||||
        this.server.setConfig(fn)
 | 
			
		||||
        this._server.setConfig(fn)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public setErrorConfig(fn: interfaces.ConfigFunction) {
 | 
			
		||||
        this.server.setErrorConfig(fn)
 | 
			
		||||
        this._server.setErrorConfig(fn)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public build() {
 | 
			
		||||
        this.serverInstance = this.server.build();
 | 
			
		||||
        rebuildServer(container);
 | 
			
		||||
        return this.serverInstance;
 | 
			
		||||
        this._serverInstance = this._server.build();
 | 
			
		||||
        rebuildServer(this._container);
 | 
			
		||||
        return this._serverInstance;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public start() {
 | 
			
		||||
        const routeInfo = getRouteInfo(container);
 | 
			
		||||
    public start(port: number = 80) {
 | 
			
		||||
        const routeInfo = getRouteInfo(this._container);
 | 
			
		||||
        console.log(prettyjson.render({ routes: routeInfo }));
 | 
			
		||||
        this.serverInstance.listen(80);
 | 
			
		||||
        console.log('Server started on port 80 :)');
 | 
			
		||||
        this._serverInstance.listen(port);
 | 
			
		||||
        console.log(`Server started on port ${port} :)`);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,11 +2,6 @@ import "reflect-metadata";
 | 
			
		||||
import { Container, inject, interfaces } from 'inversify';
 | 
			
		||||
import { autoProvide, provide, fluentProvide, buildProviderModule } from 'inversify-binding-decorators';
 | 
			
		||||
 | 
			
		||||
let container = new Container();
 | 
			
		||||
// Reflects all decorators provided by this package and packages them into
 | 
			
		||||
// a module to be loaded by the container
 | 
			
		||||
// container.load(buildProviderModule());
 | 
			
		||||
 | 
			
		||||
const provideNamed = (identifier: interfaces.ServiceIdentifier<any>, name: string) => {
 | 
			
		||||
    return fluentProvide(identifier)
 | 
			
		||||
        .whenTargetNamed(name)
 | 
			
		||||
@@ -19,4 +14,4 @@ const provideSingleton = (identifier: interfaces.ServiceIdentifier<any>) => {
 | 
			
		||||
        .done();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export { container, autoProvide, provide, provideNamed, provideSingleton, inject, buildProviderModule };
 | 
			
		||||
export { autoProvide, provide, provideNamed, provideSingleton, inject, buildProviderModule };
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user