control-freak-ide/server/nodejs/back/lib/agentframework
plastic-hub-dev-node-saturn 538369cff7 latest
2021-05-12 18:35:18 +02:00
..
src/lib latest 2021-05-12 18:35:18 +02:00
test latest 2021-05-12 18:35:18 +02:00
tools latest 2021-05-12 18:35:18 +02:00
.editorconfig latest 2021-05-12 18:35:18 +02:00
.gitignore latest 2021-05-12 18:35:18 +02:00
.npmignore latest 2021-05-12 18:35:18 +02:00
.travis.yml latest 2021-05-12 18:35:18 +02:00
LICENSE latest 2021-05-12 18:35:18 +02:00
package.json latest 2021-05-12 18:35:18 +02:00
package.release.json latest 2021-05-12 18:35:18 +02:00
README.md latest 2021-05-12 18:35:18 +02:00
TODO.md latest 2021-05-12 18:35:18 +02:00
tsconfig.json latest 2021-05-12 18:35:18 +02:00
tsconfig.release.json latest 2021-05-12 18:35:18 +02:00
yarn.lock latest 2021-05-12 18:35:18 +02:00

Agent Framework for TypeScript 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+

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;
  }
}