Skip to content

Commit

Permalink
fix(core): revert proxyDelegation
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Jun 26, 2023
1 parent d0f181c commit 1c1a774
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export * from "./interfaces/DecoratorParameters";
export * from "./interfaces/MetadataTypes";
export * from "./interfaces/ValueOf";
export * from "./utils/catchError";
export * from "./utils/proxyDelegation";
export * from "./utils/uniq";
export * from "./utils/decorators/decorateMethodsOf";
export * from "./utils/decorators/decoratorArgs";
Expand Down
91 changes: 91 additions & 0 deletions packages/core/src/utils/proxyDelegation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {uniq} from "./uniq";

/**
* @ignore
*/
export type ProxyDelegationGetter<T = any> = (target: T, property: PropertyKey) => any | undefined;
/**
* @ignore
*/
export type ProxyDelegationRemove<T = any> = (target: T, property: PropertyKey) => any;
/**
* @ignore
*/
export type ProxyDelegationSetter<T = any> = (target: T, property: PropertyKey, value: any, receiver: any) => any;
/**
* @ignore
*/
export type ProxyDelegationOwnKeys<T = any> = (target: T) => (string | symbol)[];

/**
* @ignore
*/
export interface ProxyDelegation<T extends object> {
handlers?: ProxyHandler<T>;
getter?: ProxyDelegationGetter<T>;
setter?: ProxyDelegationSetter<T>;
remove?: ProxyDelegationRemove<T>;
ownKeys?: ProxyDelegationOwnKeys<T>;
}

/**
* Create a complete and iterable trap.
* @param self
* @param options
* @ignore
*/
export function proxyDelegation<T extends object = any>(self: any, options: ProxyDelegation<T> = {}) {
const {handlers = {}, remove, ownKeys} = options;
const get: ProxyDelegationGetter = options.getter || ((target: any, propertyKey: PropertyKey) => target.get(propertyKey));
const set: ProxyDelegationSetter =
options.setter || ((target: any, propertyKey: PropertyKey, value: any) => !!target.set(propertyKey, value));

const itsOwnProp = (target: any, p: PropertyKey) => Reflect.has(target, p) || typeof p === "symbol";

return new Proxy<T>(self, {
getOwnPropertyDescriptor(target: any, p: PropertyKey): PropertyDescriptor | undefined {
return Reflect.getOwnPropertyDescriptor(target, p);
},

has(target: any, p: PropertyKey): boolean {
if (itsOwnProp(target, p)) {
return Reflect.has(target, p);
}

return get(target, p) !== undefined;
},

get(target: any, p: PropertyKey, receiver: any): any {
if (itsOwnProp(target, p)) {
return Reflect.get(target, p, receiver);
}

return get(target, p);
},

set(target: any, p: PropertyKey, value: any, receiver: any): boolean {
if (itsOwnProp(target, p)) {
return Reflect.set(target, p, value, receiver);
}

return !!set(target, p as any, value, receiver);
},

deleteProperty(target: any, p: PropertyKey): boolean {
if (itsOwnProp(target, p) || !remove) {
return Reflect.deleteProperty(target, p);
}

return remove(target, p);
},

defineProperty(target: any, p: PropertyKey, attributes: PropertyDescriptor): boolean {
return Reflect.defineProperty(target, p, attributes);
},

ownKeys(target: any) {
return uniq(Reflect.ownKeys(target).concat((ownKeys && ownKeys(target)) || []));
},
...handlers
});
}
11 changes: 8 additions & 3 deletions packages/di/src/common/services/DIConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {Env, getValue, setValue} from "@tsed/core";
import {Env, getValue, proxyDelegation, setValue} from "@tsed/core";
import type {ProviderScope} from "../domain/ProviderScope";
import type {DILoggerOptions} from "../interfaces/DILoggerOptions";
import type {DIResolver} from "../interfaces/DIResolver";
import type {TokenProvider} from "../interfaces/TokenProvider";
import type {TokenProviderOpts} from "../interfaces/TokenProvider";
import type {TokenProvider, TokenProviderOpts} from "../interfaces/TokenProvider";
import type {TokenRoute} from "../interfaces/TokenRoute";

export class DIConfiguration {
Expand All @@ -21,6 +20,12 @@ export class DIConfiguration {
}).forEach(([key, value]) => {
this.default.set(key, value);
});

return proxyDelegation<DIConfiguration>(this, {
ownKeys(target) {
return [...target.default.keys(), ...target.map.keys()];
}
});
}

get version() {
Expand Down

0 comments on commit 1c1a774

Please sign in to comment.