|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +/* |
| 3 | + We redefine Proxy because the native Proxy type treats the `target` and |
| 4 | + `receiver` as the same type incorrectly. |
| 5 | +
|
| 6 | + We ported this from Typescript's own Proxy types on 3/10/2024. |
| 7 | +*/ |
| 8 | +interface ProxyHandler<T extends object> { |
| 9 | + /** |
| 10 | + * A trap method for a function call. |
| 11 | + * @param target The original callable object which is being proxied. |
| 12 | + * @internal |
| 13 | + */ |
| 14 | + apply?(target: T, thisArg: any, argArray: any[]): any; |
| 15 | + |
| 16 | + /** |
| 17 | + * A trap for the `new` operator. |
| 18 | + * @param target The original object which is being proxied. |
| 19 | + * @param newTarget The constructor that was originally called. |
| 20 | + * @internal |
| 21 | + */ |
| 22 | + // eslint-disable-next-line @typescript-eslint/ban-types |
| 23 | + construct?(target: T, argArray: any[], newTarget: Function): object; |
| 24 | + |
| 25 | + /** |
| 26 | + * A trap for `Object.defineProperty()`. |
| 27 | + * @param target The original object which is being proxied. |
| 28 | + * @returns A `Boolean` indicating whether or not the property has been defined. |
| 29 | + * @internal |
| 30 | + */ |
| 31 | + defineProperty?(target: T, property: string | symbol, attributes: PropertyDescriptor): boolean; |
| 32 | + |
| 33 | + /** |
| 34 | + * A trap for the `delete` operator. |
| 35 | + * @param target The original object which is being proxied. |
| 36 | + * @param p The name or `Symbol` of the property to delete. |
| 37 | + * @returns A `Boolean` indicating whether or not the property was deleted. |
| 38 | + * @internal |
| 39 | + */ |
| 40 | + deleteProperty?(target: T, p: string | symbol): boolean; |
| 41 | + |
| 42 | + /** |
| 43 | + * A trap for getting a property value. |
| 44 | + * @param target The original object which is being proxied. |
| 45 | + * @param p The name or `Symbol` of the property to get. |
| 46 | + * @param receiver The proxy or an object that inherits from the proxy. |
| 47 | + * @internal |
| 48 | + */ |
| 49 | + get?(target: T, p: string | symbol, receiver: any): any; |
| 50 | + |
| 51 | + /** |
| 52 | + * A trap for `Object.getOwnPropertyDescriptor()`. |
| 53 | + * @param target The original object which is being proxied. |
| 54 | + * @param p The name of the property whose description should be retrieved. |
| 55 | + * @internal |
| 56 | + */ |
| 57 | + getOwnPropertyDescriptor?(target: T, p: string | symbol): PropertyDescriptor | undefined; |
| 58 | + |
| 59 | + /** |
| 60 | + * A trap for the `[[GetPrototypeOf]]` internal method. |
| 61 | + * @param target The original object which is being proxied. |
| 62 | + * @internal |
| 63 | + */ |
| 64 | + getPrototypeOf?(target: T): object | null; |
| 65 | + |
| 66 | + /** |
| 67 | + * A trap for the `in` operator. |
| 68 | + * @param target The original object which is being proxied. |
| 69 | + * @param p The name or `Symbol` of the property to check for existence. |
| 70 | + * @internal |
| 71 | + */ |
| 72 | + has?(target: T, p: string | symbol): boolean; |
| 73 | + |
| 74 | + /** |
| 75 | + * A trap for `Object.isExtensible()`. |
| 76 | + * @param target The original object which is being proxied. |
| 77 | + * @internal |
| 78 | + */ |
| 79 | + isExtensible?(target: T): boolean; |
| 80 | + |
| 81 | + /** |
| 82 | + * A trap for `Reflect.ownKeys()`. |
| 83 | + * @param target The original object which is being proxied. |
| 84 | + * @internal |
| 85 | + */ |
| 86 | + ownKeys?(target: T): ArrayLike<string | symbol>; |
| 87 | + |
| 88 | + /** |
| 89 | + * A trap for `Object.preventExtensions()`. |
| 90 | + * @param target The original object which is being proxied. |
| 91 | + * @internal |
| 92 | + */ |
| 93 | + preventExtensions?(target: T): boolean; |
| 94 | + |
| 95 | + /** |
| 96 | + * A trap for setting a property value. |
| 97 | + * @param target The original object which is being proxied. |
| 98 | + * @param p The name or `Symbol` of the property to set. |
| 99 | + * @param receiver The object to which the assignment was originally directed. |
| 100 | + * @returns A `Boolean` indicating whether or not the property was set. |
| 101 | + * @internal |
| 102 | + */ |
| 103 | + set?(target: T, p: string | symbol, newValue: any, receiver: any): boolean; |
| 104 | + |
| 105 | + /** |
| 106 | + * A trap for `Object.setPrototypeOf()`. |
| 107 | + * @param target The original object which is being proxied. |
| 108 | + * @param newPrototype The object's new prototype or `null`. |
| 109 | + * @internal |
| 110 | + */ |
| 111 | + setPrototypeOf?(target: T, v: object | null): boolean; |
| 112 | +} |
| 113 | + |
| 114 | +interface ProxyConstructor { |
| 115 | + /** |
| 116 | + * Creates a revocable Proxy object. |
| 117 | + * @param target A target object to wrap with Proxy. |
| 118 | + * @param handler An object whose properties define the behavior of Proxy when an operation is attempted on it. |
| 119 | + * @internal |
| 120 | + */ |
| 121 | + revocable<T extends object>(target: T, handler: ProxyHandler<T>): { proxy: T; revoke: () => void }; |
| 122 | + |
| 123 | + /** |
| 124 | + * Creates a Proxy object. The Proxy object allows you to create an object that can be used in place of the |
| 125 | + * original object, but which may redefine fundamental Object operations like getting, setting, and defining |
| 126 | + * properties. Proxy objects are commonly used to log property accesses, validate, format, or sanitize inputs. |
| 127 | + * @param target A target object to wrap with Proxy. |
| 128 | + * @param handler An object whose properties define the behavior of Proxy when an operation is attempted on it. |
| 129 | + * @internal |
| 130 | + */ |
| 131 | + new <TSource extends object, TTarget extends object>(target: TSource, handler: ProxyHandler<TSource>): TTarget; |
| 132 | +} |
| 133 | + |
| 134 | +export const NativeProxy: ProxyConstructor = Proxy as unknown as ProxyConstructor; |
0 commit comments