proxy-handler-decorators
v1.0.4
Published
A template for creating npm packages using TypeScript and VSCode
Downloads
20
Maintainers
Readme
proxy-handler-decorators
Class decorators for proxy handlers.
Example
A proxy to transform an array, prepending a '!' character to each element.
import {
getTarget,
AllowGetTarget,
Mapped,
DefaultToPrimitive,
DefaultToStringTag,
TargetAsThis,
} from 'proxy-handler-decorators';
import { DefaultProxyHandler } from 'default-proxy-handler';
let counter = 0;
function addBang(words: string[]): string[] {
counter++;
return words.map((word) => `${word}!`);
}
@AllowGetTarget
@Mapped<string[]>(addBang)
@DefaultToPrimitive
@DefaultToStringTag
@TargetAsThis
class MappedProxyHandler extends DefaultProxyHandler<string[]> {}
const mappedProxyHandler = new MappedProxyHandler();
const proxy = new Proxy(['hello', 'world'], mappedProxyHandler);
expect(counter).toBe(0);
expect(proxy).toEqual(['hello!', 'world!']);
expect(counter).toBeGreaterThan(0);
counter = 0;
expect(proxy).toEqual(['hello!', 'world!']);
expect(counter).toBeGreaterThan(0);
expect(getTarget(proxy)).toEqual(['hello', 'world']);
Functions
AllowGetTarget
▸ AllowGetTarget<T
, SuperClass
>(superClass
): SuperClass
The decorator to enable getTarget function.
Type parameters
| Name | Type |
| :------ | :------ |
| T
| extends object
|
| SuperClass
| extends (...args
: any
[]) => ProxyHandler
<T
> |
Parameters
| Name | Type |
| :------ | :------ |
| superClass
| SuperClass
|
Returns
SuperClass
Defined in
DefaultToPrimitive
▸ DefaultToPrimitive<T
, SuperClass
>(superClass
): SuperClass
A decorator to add a default implementation of [Symbol.toPrimitive]
method
to the proxy.
Type parameters
| Name | Type |
| :------ | :------ |
| T
| extends object
|
| SuperClass
| extends (...args
: any
[]) => ProxyHandler
<T
> |
Parameters
| Name | Type |
| :------ | :------ |
| superClass
| SuperClass
|
Returns
SuperClass
Defined in
DefaultToStringTag
▸ DefaultToStringTag<T
, SuperClass
>(superClass
): SuperClass
A decorator to add a default implementation of [Symbol.toStringTag]
method
to the proxy.
Type parameters
| Name | Type |
| :------ | :------ |
| T
| extends object
|
| SuperClass
| extends (...args
: any
[]) => ProxyHandler
<T
> |
Parameters
| Name | Type |
| :------ | :------ |
| superClass
| SuperClass
|
Returns
SuperClass
Defined in
Mapped
▸ Mapped<T
>(mapper
): (proxyHandlerConstructor
: (...args
: any
[]) => ProxyHandler
<T
>) => void
Type parameters
| Name | Type |
| :------ | :------ |
| T
| extends object
|
Parameters
| Name | Type |
| :------ | :------ |
| mapper
| (from
: T
) => T
|
Returns
fn
▸ (proxyHandlerConstructor
): void
Parameters
| Name | Type |
| :------ | :------ |
| proxyHandlerConstructor
| (...args
: any
[]) => ProxyHandler
<T
> |
Returns
void
Defined in
TargetAsThis
▸ TargetAsThis<T
, SuperClass
>(superClass
): SuperClass
A decorator to bind this
to the proxy target for all instance methods on
the proxy.
Type parameters
| Name | Type |
| :------ | :------ |
| T
| extends object
|
| SuperClass
| extends (...args
: any
[]) => ProxyHandler
<T
> |
Parameters
| Name | Type |
| :------ | :------ |
| superClass
| SuperClass
|
Returns
SuperClass
Defined in
getTarget
▸ getTarget<T
>(maybeProxy
): T
| undefined
Returns the proxy target if proxy
is a proxy and its proxy handler has the
AllowGetTarget decorator, or undefined
otherwise.
Type parameters
| Name |
| :------ |
| T
|
Parameters
| Name | Type |
| :------ | :------ |
| maybeProxy
| T
|
Returns
T
| undefined