proxyable
v26.5.22
Published
Dynamic JavaScript proxy creation with isolated context-based interception
Readme
Proxyable
Dynamic JavaScript proxy creation with isolated context-based interception.
Proxyable simplifies creating JavaScript Proxies by providing a clean, type-safe API for registering multiple interceptors per trap with built-in context isolation using unctx.
Features
- Multiple Interceptors - Register multiple handlers for the same proxy trap
- All 8 Proxy Traps -
get,set,has,deleteProperty,ownKeys,getOwnPropertyDescriptor,apply,construct - Isolated Contexts - Automatic context isolation for each proxy instance
- Type Safe - Full TypeScript support
- Specialized APIs - Convenience methods for common patterns
- Zero Config - Works out of the box
Usage
Install package:
# ✨ Auto-detect
npx nypm install proxyable
# npm
npm install proxyable
# yarn
yarn add proxyable
# pnpm
pnpm install proxyable
# bun
bun install proxyable
# deno
deno install proxyableImport:
ESM (Node.js, Bun, Deno)
import {} from "proxyable";CommonJS (Legacy Node.js)
const {} = require("proxyable");CDN (Deno, Bun and Browsers)
import {} from "https://esm.sh/proxyable";Quick Start
import { createProxy } from "proxyable";
const target = { name: "John", age: 30 };
const { proxy, defineGetInterceptor, defineSetInterceptor } = createProxy(target);
// Add a get interceptor to provide computed properties
defineGetInterceptor((target, prop) => {
if (prop === "fullName") {
return `${target.firstName} ${target.lastName}`;
}
return target[prop];
});
// Add a set interceptor to validate age
defineSetInterceptor((target, prop, value) => {
if (prop === "age" && typeof value !== "number") {
throw new TypeError("Age must be a number");
}
target[prop] = value;
return true;
});
console.log(proxy.name); // "John"
proxy.age = 31; // ValidSee the full documentation for more examples and API reference.
Development
- Clone this repository
- Install latest LTS version of Node.js
- Enable Corepack using
corepack enable - Install dependencies using
pnpm install - Run interactive tests using
pnpm dev
License
Published under the MIT license. Made by community 💛
🤖 auto updated with automd
