TypeScript that scales


Keywords
agent, aop, typescript, es6, dependency injection, proxy, interceptor, ioc, codegen, agent-oriented-programming, aspect-oriented-framework, dependency-injection, metadata, reflection, serverless
License
Apache-2.0
Install
npm install agentframework@2.0.2

Documentation

Agent Framework for TypeScript 2.2+

Build Status Coverage Status

What's this?

  • AOP for TypeScript
  • Elegant design pattern to decorate your class with interceptors
  • 100% TypeScript implementation! No dependencies!!!
  • Require ES6 and TypeScript 2.2+

Install and usage

  npm install --save agentframework

or

  yarn add agentframework

Examples

  • @prerequisite() Do not run the method body and throw an error when prerequistite() not met

  • @conditional() Likes prerequistite() but not throw error

  • @normalize() Capture throw in the method and modify the return value to this object { ok: 1|0, result?: any = return object, results?: any = return array, message?: string = err.message }

  • @success() Change the specified class property value when this method run success (without throw)

  • @failure() Always return specified value if any throw happen in the intercepted method.

  • @timestamp() Update timestamp field after changes the field value

  • @cache() Simple but useful memory cache implementation

Development

Run tests with coverage report

npm test

Start development workflow - monitoring .ts file changes and run unit test against the changes

npm start

Without 'Agent Framework'

class Kernel {
  
  private _initialized: boolean;
  private _root: Directory;
  
  public static getInstance(): Kernel {
    return new Kernel();
  }
  
  public init(configDir: string = process.cwd()): void {
    if(!this._initialized) {
    	throw new TypeError('Kernel already initialized')
    }
    this._root = Directory.withReadPermission(configDir);
    this._initialized = true
  }
  
  public resolve(relativePath): Directory {
    if(!this._initialized) {
    	throw new TypeError('Kernel already initialized')
    }
    return this._root.resolve(relativePath);
  }
  
  public getRoot(): string {
    try {
      return this._root.path;
    }
    catch(err) {
      return null;
    }
  }
}

Implement with 'Agent Framework - State Machine'

import { agent, prerequisite, success, failure } from 'agentframework'

@agent('OneStack')
class Kernel {
  
  private _root: Directory;
  
  public static getInstance(): Kernel {
    return Domain.createAgentFromType(Kernel);
  }
  
  @prerequisite('initialized', false, 'OneStack already initialized')
  @success('initialized', true)
  public init(configDir: string = process.cwd()): void {
    this._root = Directory.withReadPermission(configDir);
  }
  
  @prerequisite('initialized', true, 'OneStack not initialized. Please call init() first!')
  public resolve(relativePath): Directory {
    return this._root.resolve(relativePath);
  }
  
  @failure(null)
  public getRoot(): string {
    return this._root.path;
  }
}