chore(lib/patcher): add jsdoc

This commit is contained in:
PalmDevs 2025-07-10 21:24:04 +07:00
parent 78e8a96034
commit 546bbc080a
No known key found for this signature in database
GPG key ID: 6FAE8F8677ACA8BA
3 changed files with 93 additions and 0 deletions

View file

@ -32,6 +32,34 @@ function unpatchAfter<T extends UnknownFunction>(
hookNode.next = undefined
}
/**
* After hooks allow you to modify the return value of the original function, or to perform some action after the original function is called.
*
* ```js
* import { after } from '@revenge-mod/patcher'
*
* const obj = {
* method: (a) => a * 2
* }
*
* after(obj, 'method', (result) => {
* console.log('After method called with result:', result)
* // Modify return value
* return result + 1
* })
*
* console.log(obj.method(2)) // 5
* // CONSOLE OUTPUT:
* // After method called with result: 4
* // 5
* ```
*
* @param parent The parent object containing the method to patch.
* @param key The key of the method to patch.
* @param hook The hook function to execute after the original method.
*
* @returns A function to unpatch.
*/
export function after<
Parent extends Record<Key, UnknownFunction>,
Key extends keyof Parent,

View file

@ -32,6 +32,34 @@ function unpatchBefore<T extends UnknownFunction>(
hookNode.next = undefined
}
/**
* Before hooks allow you to modify the arguments passed to the original function, or to perform some action before the original function is called.
*
* ```js
* import { before } from '@revenge-mod/patcher'
*
* const obj = {
* method: (a) => console.log('Original method called with:', a)
* }
*
* before(obj, 'method', ([a]) => {
* console.log('Before method called with:', a)
* // Modify arguments by returning new array
* return [a + 1]
* })
*
* obj.method(2)
* // CONSOLE OUTPUT:
* // Before method called with: 2
* // Original method called with: 3
* ```
*
* @param parent The parent object containing the method to patch.
* @param key The key of the method to patch.
* @param hook The hook function to execute before the original method.
*
* @returns A function to unpatch.
*/
export function before<
Parent extends Record<Key, UnknownFunction>,
Key extends keyof Parent,

View file

@ -70,6 +70,43 @@ function unpatchInstead<T extends UnknownFunction>(
hookNode.next = undefined
}
/**
* Instead hooks allow you to completely replace the original function with a new one, while still being able to call the original function if needed.
*
* ```js
* import { instead } from '@revenge-mod/patcher'
*
* const obj = {
* method: (a) => {
* console.log('Original method called with:', a)
* return 'original result'
* }
* }
*
* instead(obj, 'method', ([a], original) => {
* console.log('Instead method called with:', a)
* // Call the original function if needed
* const originalResult = original('modified')
* console.log('Original method was called')
*
* // Return a new value
* return 'new value'
* })
*
* console.log(obj.method('test')) // 'new value'
* // CONSOLE OUTPUT:
* // Instead method called with: test
* // Original method called with: modified
* // Original method was called
* // new value
* ```
*
* @param parent The parent object containing the method to patch.
* @param key The key of the method to patch.
* @param hook The hook function to execute instead of the original method.
*
* @return A function to unpatch.
*/
export function instead<
Parent extends Record<Key, UnknownFunction>,
Key extends keyof Parent,