Comparing version 2.0.0-alpha-1 to 2.0.0-beta-1
@@ -1,18 +0,3 @@ | ||
export declare type MemberType = "Method" | "Getter" | "Setter"; | ||
export declare type Interceptor = (i: Invocation) => any; | ||
export declare abstract class Invocation { | ||
memberName: string; | ||
parameters: IArguments; | ||
memberType: MemberType; | ||
abstract proceed(): any; | ||
} | ||
export declare function getMembers(origin: any): string[]; | ||
export declare class BenaluBuilder<T> { | ||
origin: any; | ||
intercepts: Array<Interceptor>; | ||
constructor(origin: any); | ||
addInterception(interception: Interceptor): this; | ||
private methodInterception(origin, method, interceptors); | ||
build(): T; | ||
} | ||
export declare function fromInstance<T>(instance: T): BenaluBuilder<T>; | ||
import { BenaluBuilder } from "./benalu-builder"; | ||
export declare function fromInstance<T extends object>(instance: T): BenaluBuilder<T>; | ||
export { Invocation, Builder, Interceptor } from "./core"; |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Invocation = /** @class */ (function () { | ||
function Invocation() { | ||
} | ||
return Invocation; | ||
}()); | ||
exports.Invocation = Invocation; | ||
var MethodInvocation = /** @class */ (function (_super) { | ||
__extends(MethodInvocation, _super); | ||
function MethodInvocation(original, memberName, parameters) { | ||
var _this = _super.call(this) || this; | ||
_this.original = original; | ||
_this.memberName = memberName; | ||
_this.parameters = parameters; | ||
_this.memberType = "Method"; | ||
return _this; | ||
} | ||
MethodInvocation.prototype.proceed = function () { | ||
return this.original[this.memberName].apply(this.original, this.parameters); | ||
}; | ||
return MethodInvocation; | ||
}(Invocation)); | ||
var InterceptorInvocation = /** @class */ (function (_super) { | ||
__extends(InterceptorInvocation, _super); | ||
function InterceptorInvocation(next, interceptor) { | ||
var _this = _super.call(this) || this; | ||
_this.next = next; | ||
_this.interceptor = interceptor; | ||
_this.memberName = next.memberName; | ||
_this.memberType = next.memberType; | ||
_this.parameters = next.parameters; | ||
return _this; | ||
} | ||
InterceptorInvocation.prototype.proceed = function () { | ||
return this.interceptor(this.next); | ||
}; | ||
return InterceptorInvocation; | ||
}(Invocation)); | ||
function getMembers(origin) { | ||
var members = Object.keys(origin); | ||
var properties = Object.getOwnPropertyNames(Object.getPrototypeOf(origin)); | ||
members = members.concat(properties); | ||
var conIndex = members.indexOf("constructor"); | ||
members.splice(conIndex, 1); | ||
return members; | ||
} | ||
exports.getMembers = getMembers; | ||
var BenaluBuilder = /** @class */ (function () { | ||
function BenaluBuilder(origin) { | ||
this.intercepts = []; | ||
this.origin = origin; | ||
} | ||
BenaluBuilder.prototype.addInterception = function (interception) { | ||
this.intercepts.push(interception); | ||
return this; | ||
}; | ||
BenaluBuilder.prototype.methodInterception = function (origin, method, interceptors) { | ||
return function () { | ||
var invocation = new MethodInvocation(origin, method, arguments); | ||
interceptors.forEach(function (x) { | ||
invocation = new InterceptorInvocation(invocation, x); | ||
}); | ||
return invocation.proceed(); | ||
}; | ||
}; | ||
BenaluBuilder.prototype.build = function () { | ||
var proxy = {}; | ||
var members = getMembers(this.origin); | ||
for (var _i = 0, members_1 = members; _i < members_1.length; _i++) { | ||
var key = members_1[_i]; | ||
if (typeof this.origin[key] == "function") { | ||
proxy[key] = this.methodInterception(this.origin, key, this.intercepts.reverse()); | ||
} | ||
else { | ||
proxy[key] = this.origin[key]; | ||
} | ||
} | ||
return proxy; | ||
}; | ||
return BenaluBuilder; | ||
}()); | ||
exports.BenaluBuilder = BenaluBuilder; | ||
const benalu_builder_1 = require("./benalu-builder"); | ||
function fromInstance(instance) { | ||
var config = new BenaluBuilder(instance); | ||
return config; | ||
return new benalu_builder_1.BenaluBuilder(instance); | ||
} | ||
exports.fromInstance = fromInstance; | ||
var core_1 = require("./core"); | ||
exports.Invocation = core_1.Invocation; |
{ | ||
"name": "benalu", | ||
"version": "2.0.0-alpha-1", | ||
"description": "A dynamic proxy for javascript", | ||
"version": "2.0.0-beta-1", | ||
"description": "Better ES6 Proxy API for interception", | ||
"main": "dist/benalu.js", | ||
@@ -6,0 +6,0 @@ "typings": "dist/benalu.d.ts", |
@@ -7,20 +7,12 @@ #Benalu | ||
[![NPM](https://nodei.co/npm/benalu.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/benalu/) | ||
Better ES6 Proxy API for interception | ||
A dynamic proxy for javascript. | ||
Provide a light weight [proxy class](https://en.wikipedia.org/wiki/Proxy_pattern) | ||
generator with multiple interception. | ||
Tired of complicated [ES6 Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) class API to do simple interception? Benalu is your friend | ||
###About | ||
The purpose of Benalu is provide a simple way to do a simple AOP in javascript. | ||
## About | ||
The purpose of Benalu is provide a simple API to do interception in JavaScript. | ||
Benalu also useful for IOC container library that hasn't support for interception | ||
###Features | ||
1. Can proxy a Function prototype or an object. | ||
2. Can proxy method & property | ||
3. Can add multiple interceptions | ||
###Installation | ||
## Installation | ||
``` | ||
@@ -30,12 +22,13 @@ npm install benalu | ||
###How To Use It | ||
## How To Use It | ||
Using benalu is very simple. You start building your proxy by using `Benalu` builder | ||
```Javascript | ||
```javascript | ||
var Benalu = require('benalu'); | ||
//declare the class | ||
function MyObject(){} | ||
MyObject.prototype.getNumber = function(){ | ||
return 700; | ||
class MyObject(){ | ||
getNumber() { | ||
return 700; | ||
} | ||
} | ||
@@ -50,7 +43,7 @@ | ||
//filter the invocation | ||
if(i.methodName == "getNumber"){ | ||
if(i.memberName == "getNumber"){ | ||
//call the real method | ||
i.proceed(); | ||
//override the return value | ||
i.returnValue = 300; | ||
let result = i.proceed(); | ||
//return different value | ||
return 300; | ||
} | ||
@@ -64,29 +57,28 @@ }) | ||
###Interception & Invocation | ||
## Interception & Invocation | ||
Interception in Benalu simply a callback function with single parameter of `Invocation`. | ||
Invocation consist of 3 important members: | ||
1. `methodName` name of current invoked method. Usefull when you only want to intercept | ||
specific method of the class | ||
1. `memberName` name of current invoked method or getter. Usefull when you only want to intercept specific method or getter of the class | ||
2. `parameters` arguments passed to the invoked method. Usefull when you want to get | ||
2. `memberType` type of currently invoked member the vlaue could be`method` or `getter` | ||
3. `parameters` arguments passed to the invoked method. Usefull when you want to get | ||
information of the arguments passed to the method. | ||
3. `proceed()` method to proceed current invocation. This method will invoke the | ||
4. `proceed()` method to proceed current invocation. This method will invoke the | ||
method of the real object. | ||
4. `returnValue` return value of the current invoked method. this member filled | ||
automatically after the `proceed()` method called. You can override the return value | ||
of current invocation by suplying a value to the `returnValue` member | ||
###Multiple Interception | ||
## Multiple Interception | ||
Benalu also support multiple interception. | ||
```Javascript | ||
```javascript | ||
var Benalu = require('benalu'); | ||
function MyObject(){} | ||
MyObject.prototype.getNumber = function(){ | ||
console.log("The real method called"); | ||
return 700; | ||
class MyObject(){ | ||
getNumber() { | ||
console.log("The real method called") | ||
return 700; | ||
} | ||
} | ||
@@ -100,5 +92,5 @@ | ||
console.log("First interceptor before proceed"); | ||
i.proceed(); | ||
let result = i.proceed(); | ||
console.log("First interceptor after proceed"); | ||
i.returnValue = 300; | ||
return result + 300 | ||
} | ||
@@ -109,5 +101,5 @@ }) | ||
console.log("Second interceptor before proceed"); | ||
i.proceed(); | ||
let result = i.proceed(); | ||
console.log("Second interceptor after proceed"); | ||
i.returnValue = i.returnValue + 300; | ||
return 300 | ||
} | ||
@@ -124,7 +116,7 @@ }) | ||
``` | ||
First interceptor before proceed | ||
Second interceptor before proceed | ||
First interceptor before proceed | ||
The real method called | ||
Second interceptor after proceed | ||
First interceptor after proceed | ||
Second interceptor after proceed | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
11087
14
160
116