/ src / helpers / patch / patchMethod.ts
patchMethod.ts
 1  import type { ConditionalKeys } from 'type-fest';
 2  
 3  import type { Logger } from '@/utils/logger';
 4  
 5  import { saveOriginalMethod } from './saveOriginalMethod';
 6  
 7  export const patchMethod = <
 8  	Obj extends object,
 9  	Key extends ConditionalKeys<NoInfer<Obj>, (...args: any[]) => any>,
10  >(
11  	logger: Logger,
12  	obj: Obj,
13  	key: Key,
14  	origSym: Extract<ConditionalKeys<NoInfer<Required<Obj>>, NoInfer<Obj[Key]>>, symbol>,
15  	patchedMethod: (
16  		this: ThisParameterType<NoInfer<Obj[Key]>>,
17  		origMethod: NoInfer<Obj[Key]>,
18  		...args: Parameters<NoInfer<Obj[Key]>>
19  	) => ReturnType<NoInfer<Obj[Key]>>,
20  ) => {
21  	const origMethod = saveOriginalMethod(logger, obj, key, origSym);
22  
23  	const patchedWrapper = function patchedWrapper(
24  		this: ThisParameterType<Obj[Key]>,
25  		...args: Parameters<Obj[Key]>
26  	) {
27  		return Reflect.apply(patchedMethod, this, [origMethod, ...args]);
28  	};
29  
30  	exportFunction(patchedWrapper, obj, { defineAs: key });
31  };