0.0.4 • Published 7 years ago

flank v0.0.4

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
7 years ago

flank

NPM Version NPM Downloads Node.js Version apache-version-image

Attribute-oriented programming(@OP) framework for node and typescript.

Installation

$ npm install flank

API

import {flank} from "flank"

flank.IValidator

Implement a simple Validator

class myValidator implements flank.IValidator
{
    constructor()
    {
        this.name = this.constructor.name
    }
    name:string
    validate = function(obj : Object, key : string, descriptor: PropertyDescriptor, args : any[], i : number)
    {
        if(args[i] != "Hello")
          return {isValid:false, message:"You didn't say Hello"};
        else
          return {isValid:true}
    }

}

flank.NotifyPropertyChanged

Implement a property changed notification handler

 @flank.NotifyPropertyChanged(true)
 public Prop : string;

flank.Route

Implement a flank.Route

  @flank.Route('/demo/:message?')
  public Demo(@flank.Validator(myvalidator) message:string = "Hello World"): any
  {
      return message
  } 

Complete implementation example

Prerequisite:

$ npm install body-parser
$ npm install @types/body-parser

Code:

import {flank} from "flank";
import * as bodyParser from "body-parser";
import * as express from "express";
import * as path from "path";

class myValidator implements flank.IValidator
{
    constructor()
    {
        this.name = this.constructor.name
    }
    name:string
    validate = function(obj : Object, key : string, descriptor: PropertyDescriptor, args : any[], i : number)
    {
        if(args[i] != "Hello")
          return {isValid:false, message:"You didn't say Hello World"};
        else
          return {isValid:true}
    }

}

let myvalidator = new myValidator()
export class DemoController extends flank.express.Controller
{
    constructor () {
       super(true)
       var p : any = this
       p.on('Prop', ( args : any[])=>{ console.log(args) })
    }

    @flank.NotifyPropertyChanged(true)
    public Prop : string;
    
    @flank.Validate()
    @flank.Route('/demo/:message')
    public Demo(@flank.Validator(myvalidator) message:string = "Hello World"): any
    {
        this.Prop = message
        return message
    } 
}

export class Server
{

  public app: express.Application;

  constructor() 
  {
    //create expressjs application
    this.app = express();

    //configure application
    this.config();

    //add routes
    this.routes();   
  }

   
  public config() 
  {
    //use json form parser middlware
    this.app.use(bodyParser.json());
  
    //use query string parser middlware
    this.app.use(bodyParser.urlencoded({
      extended: true
    }));

     //catch 404 and forward to error handler
    this.app.use(function(err: any, req: express.Request, res: express.Response, next: express.NextFunction)
    {
      err.status = 404;
      next(err);
    });
  }

  public routes() 
  {
    this.app.use(flank.express.Router({},[DemoController]))
  }
}

Testing

TODO

License

Apache-2.0