Provides a convenient way to intercept method- property- and accessor-calls of an object.
Go to file
Manuel Thalmann a2ea17afca
All checks were successful
ci/woodpecker/push/check Pipeline was successful
Merge branch 'release/v3.0.1'
2023-09-26 14:05:02 +02:00
.vscode Update the development environment 2023-09-26 08:29:34 +02:00
.woodpecker Update the development environment 2023-09-26 08:29:34 +02:00
scripts Update the development environment 2023-09-26 08:29:34 +02:00
src Assert the existence of the Get and the Set methods respectively 2023-09-26 14:04:00 +02:00
.eslintrc.cjs Update the development environment 2023-09-26 08:29:34 +02:00
.gitignore Update the development environment 2023-09-26 08:29:34 +02:00
.mocharc.jsonc Update the development environment 2023-09-26 08:29:34 +02:00
.npmignore Update the development environment 2023-09-26 08:29:34 +02:00
CHANGELOG.md Update the changelog 2023-09-26 14:04:39 +02:00
LICENSE Initial commit 2020-09-26 16:29:40 +02:00
package-lock.json Bump the version number to v3.0.1 2023-09-26 14:04:21 +02:00
package.json Bump the version number to v3.0.1 2023-09-26 14:04:21 +02:00
README.md Add a status badge 2023-09-26 13:53:47 +02:00
tsconfig.app.json Update the development environment 2023-09-26 08:29:34 +02:00
tsconfig.base.json Update the development environment 2023-09-26 08:29:34 +02:00
tsconfig.build.json Update the development environment 2023-09-26 08:29:34 +02:00
tsconfig.editor.json Update the development environment 2023-09-26 08:29:34 +02:00
tsconfig.eslint.json Update the development environment 2023-09-26 08:29:34 +02:00
tsconfig.json Update the development environment 2023-09-26 08:29:34 +02:00

Interceptor

Provides a convenient way to intercept method- property- and accessor-calls of an object.

status-badge

Installing the package

This package can be added to your dependencies by invoking:

npm install @manuth/interceptor

Creating an Interceptor

An interceptor can be initialized by passing an object to intercept. The constructor accepts a second, optional argument which specifies whether the object should be freezed on creation. This allows you to ignore future updates of the interceptor-target.

import { Interceptor } from "@manuth/interceptor";

let target = {
    a: 10
};

let interceptor = new Interceptor(target);
let proxy = interceptor.Proxy;
target.a = 20;
console.log(proxy.a); // Logs `20`
import { Interceptor } from "@manuth/interceptor";

let target = {
    a: 10
};

let interceptor = new Interceptor(target, true);
let proxy = interceptor.Proxy;
target.a = 20;
console.log(proxy.a); // Logs `10`

Adding a Property-Interception

Fields and accessors can be intercepted by adding a property-interception.

You can add property-interceptions by invoking the Interceptor#AddProperty-method.

A property-interception allows you to specify a getter, a setter and a method for checking the existence of the property.

let target = {
    a: 10
};

let interceptor = new Interceptor(target);
let proxy = interceptor.Proxy;
interceptor.AddProperty(
    "a",
    {
        Get: (target, key) =>
        {
            console.log(key); // Logs `"a"`
            return target[key] + 2;
        }
    });

console.log(proxy.a); // Logs `12`
target.a = 20;
console.log(proxy.a); // Logs `22`
proxy.a = 22; // Throws an error
interceptor.AddProperty(
    "a",
    {
        Get: (target, key) => target[key],
        Set: (target, key, value) => target[key] = value,
        Has: () => false
    });

console.log("a" in proxy); // Logs `false`
proxy.a = 27;
console.log(proxy.a); // Logs `27`

Adding a Method-Interception

Method-interceptions take at least two arguments: The target of the interception, the name of the method.

All the other arguments represent the arguments of the original method:

let target = {
    Sum(x: number, y: number): number
    {
        return x + y;
    }
};

let interceptor = new Interceptor(target);
let proxy = interceptor.Proxy;

interceptor.AddMethod(
    "Sum",
    (target, key, x, y) =>
    {
        target[key](x++, y++);
    });

console.log(proxy.Sum(1, 1)); // Logs `4`

Hiding Methods or Properties

The AddProperty-method also allows you to hide methods or properties ba neither declaring a Getter nor a Setter:

let target = {
    a: 10,
    Sum(x, y)
    {
        return x + y;
    }
};

let interceptor = new Interceptor(target);
let proxy = interceptor.Proxy;

interceptor.AddProperty("a", {});
interceptor.AddProperty("Sum", null);
console.log(proxy.a); // Throws an error
console.log(proxy.Sum(1, 1)); // Throws an error

Deleting Interceptions

Interceptions can be removed using the Interception#Delete-method:

let target = {
    a: 10
};

let interceptor = new Interceptor(target);
let proxy = interceptor.Proxy;

interceptor.AddProperty(
    "a",
    {
        Get: () => 20
    });

console.log(proxy.a); // Logs `20`
interceptor.Delete("a");
console.log(proxy.a); // Logs `10`

Deleting all Interceptions

The Interception#Clear-method allows you to remove all interceptions.

let target = {};
let interceptor = new Interceptor(target);
let proxy = interceptor.Proxy;

interceptor.AddProperty(
    "a",
    {
        Get: () => 10
    });

interceptor.AddProperty(
    "b",
    {
        Get: () => 20
    });

console.log(proxy.a); // Logs `10`
console.log(proxy.b); // Logs `20`
interceptor.Clear();
console.log(proxy.a); // Logs `undefined`
console.log(proxy.b); // Logs `undefined`

Disposing the Interceptor

The Interceptor can be disposed. This causes all interceptions to be deactivated. Instead all method-, property- and field-calls are redirected to the target directly.

This allows you to permanently disable your interceptions.

interceptor.Dispose();