@dynamic-labs/utils 0.0.0-exp20240808.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (238) hide show
  1. package/CHANGELOG.md +3746 -0
  2. package/LICENSE +21 -0
  3. package/README.md +0 -0
  4. package/_virtual/_tslib.cjs +51 -0
  5. package/_virtual/_tslib.js +45 -0
  6. package/package.json +37 -0
  7. package/src/CancellablePromise/CancellablePromise.cjs +136 -0
  8. package/src/CancellablePromise/CancellablePromise.d.ts +54 -0
  9. package/src/CancellablePromise/CancellablePromise.js +132 -0
  10. package/src/CancellablePromise/index.d.ts +1 -0
  11. package/src/DeferredPromise/DeferredPromise.cjs +22 -0
  12. package/src/DeferredPromise/DeferredPromise.d.ts +13 -0
  13. package/src/DeferredPromise/DeferredPromise.js +18 -0
  14. package/src/DeferredPromise/index.d.ts +1 -0
  15. package/src/bufferPolyfill.cjs +15 -0
  16. package/src/bufferPolyfill.d.ts +1 -0
  17. package/src/bufferPolyfill.js +13 -0
  18. package/src/bufferToBase64.cjs +15 -0
  19. package/src/bufferToBase64.d.ts +2 -0
  20. package/src/bufferToBase64.js +11 -0
  21. package/src/ceil/ceil.cjs +11 -0
  22. package/src/ceil/ceil.d.ts +1 -0
  23. package/src/ceil/ceil.js +7 -0
  24. package/src/ceil/index.d.ts +1 -0
  25. package/src/eip6963/eip6963Provider.cjs +40 -0
  26. package/src/eip6963/eip6963Provider.d.ts +34 -0
  27. package/src/eip6963/eip6963Provider.js +35 -0
  28. package/src/eip6963/index.d.ts +1 -0
  29. package/src/errors/AccessBlockedError.cjs +11 -0
  30. package/src/errors/AccessBlockedError.d.ts +3 -0
  31. package/src/errors/AccessBlockedError.js +7 -0
  32. package/src/errors/AccountExistsError.cjs +16 -0
  33. package/src/errors/AccountExistsError.d.ts +14 -0
  34. package/src/errors/AccountExistsError.js +12 -0
  35. package/src/errors/ChainalysisError.cjs +15 -0
  36. package/src/errors/ChainalysisError.d.ts +5 -0
  37. package/src/errors/ChainalysisError.js +11 -0
  38. package/src/errors/CookieInvalidError.cjs +14 -0
  39. package/src/errors/CookieInvalidError.d.ts +4 -0
  40. package/src/errors/CookieInvalidError.js +10 -0
  41. package/src/errors/CustomError.cjs +24 -0
  42. package/src/errors/CustomError.d.ts +11 -0
  43. package/src/errors/CustomError.js +20 -0
  44. package/src/errors/CustomFieldNotUniqueError.cjs +14 -0
  45. package/src/errors/CustomFieldNotUniqueError.d.ts +4 -0
  46. package/src/errors/CustomFieldNotUniqueError.js +10 -0
  47. package/src/errors/DynamicError.cjs +11 -0
  48. package/src/errors/DynamicError.d.ts +3 -0
  49. package/src/errors/DynamicError.js +7 -0
  50. package/src/errors/EmailAlreadyExistsError.cjs +14 -0
  51. package/src/errors/EmailAlreadyExistsError.d.ts +4 -0
  52. package/src/errors/EmailAlreadyExistsError.js +10 -0
  53. package/src/errors/EmailProviderError.cjs +14 -0
  54. package/src/errors/EmailProviderError.d.ts +4 -0
  55. package/src/errors/EmailProviderError.js +10 -0
  56. package/src/errors/EmailVerificationError.cjs +14 -0
  57. package/src/errors/EmailVerificationError.d.ts +4 -0
  58. package/src/errors/EmailVerificationError.js +10 -0
  59. package/src/errors/EmbeddedWalletException.cjs +14 -0
  60. package/src/errors/EmbeddedWalletException.d.ts +4 -0
  61. package/src/errors/EmbeddedWalletException.js +10 -0
  62. package/src/errors/ExternalAuthError.cjs +14 -0
  63. package/src/errors/ExternalAuthError.d.ts +4 -0
  64. package/src/errors/ExternalAuthError.js +10 -0
  65. package/src/errors/GateBlockedError.cjs +15 -0
  66. package/src/errors/GateBlockedError.d.ts +5 -0
  67. package/src/errors/GateBlockedError.js +11 -0
  68. package/src/errors/InsufficientFundsError.cjs +25 -0
  69. package/src/errors/InsufficientFundsError.d.ts +9 -0
  70. package/src/errors/InsufficientFundsError.js +21 -0
  71. package/src/errors/InvalidPhoneNumberError.cjs +14 -0
  72. package/src/errors/InvalidPhoneNumberError.d.ts +4 -0
  73. package/src/errors/InvalidPhoneNumberError.js +10 -0
  74. package/src/errors/MergeAccountsConfirmationError.cjs +16 -0
  75. package/src/errors/MergeAccountsConfirmationError.d.ts +12 -0
  76. package/src/errors/MergeAccountsConfirmationError.js +12 -0
  77. package/src/errors/MfaInvalidOtpError.cjs +14 -0
  78. package/src/errors/MfaInvalidOtpError.d.ts +4 -0
  79. package/src/errors/MfaInvalidOtpError.js +10 -0
  80. package/src/errors/MissingEnvironmentIdError.cjs +14 -0
  81. package/src/errors/MissingEnvironmentIdError.d.ts +4 -0
  82. package/src/errors/MissingEnvironmentIdError.js +10 -0
  83. package/src/errors/MissingPublicAddressError.cjs +14 -0
  84. package/src/errors/MissingPublicAddressError.d.ts +4 -0
  85. package/src/errors/MissingPublicAddressError.js +10 -0
  86. package/src/errors/NoAccessError.cjs +19 -0
  87. package/src/errors/NoAccessError.d.ts +15 -0
  88. package/src/errors/NoAccessError.js +15 -0
  89. package/src/errors/NotSupportedError.cjs +11 -0
  90. package/src/errors/NotSupportedError.d.ts +3 -0
  91. package/src/errors/NotSupportedError.js +7 -0
  92. package/src/errors/PasskeyError.d.ts +8 -0
  93. package/src/errors/SmsVerificationError.cjs +11 -0
  94. package/src/errors/SmsVerificationError.d.ts +3 -0
  95. package/src/errors/SmsVerificationError.js +7 -0
  96. package/src/errors/SocialAccountAlreadyExistsError.cjs +14 -0
  97. package/src/errors/SocialAccountAlreadyExistsError.d.ts +4 -0
  98. package/src/errors/SocialAccountAlreadyExistsError.js +10 -0
  99. package/src/errors/TooManyEmailVerificationsError.cjs +14 -0
  100. package/src/errors/TooManyEmailVerificationsError.d.ts +4 -0
  101. package/src/errors/TooManyEmailVerificationsError.js +10 -0
  102. package/src/errors/TransactionGasCannotBeSponsoredError.cjs +25 -0
  103. package/src/errors/TransactionGasCannotBeSponsoredError.d.ts +9 -0
  104. package/src/errors/TransactionGasCannotBeSponsoredError.js +21 -0
  105. package/src/errors/UserHasAccountWithEmailError.cjs +15 -0
  106. package/src/errors/UserHasAccountWithEmailError.d.ts +5 -0
  107. package/src/errors/UserHasAccountWithEmailError.js +11 -0
  108. package/src/errors/UserRejectedTransactionError.cjs +14 -0
  109. package/src/errors/UserRejectedTransactionError.d.ts +4 -0
  110. package/src/errors/UserRejectedTransactionError.js +10 -0
  111. package/src/errors/UsernameAlreadyExistsError.cjs +14 -0
  112. package/src/errors/UsernameAlreadyExistsError.d.ts +4 -0
  113. package/src/errors/UsernameAlreadyExistsError.js +10 -0
  114. package/src/errors/VerificationDataCollectionError.cjs +15 -0
  115. package/src/errors/VerificationDataCollectionError.d.ts +5 -0
  116. package/src/errors/VerificationDataCollectionError.js +11 -0
  117. package/src/errors/WalletAddressMismatchError.cjs +17 -0
  118. package/src/errors/WalletAddressMismatchError.d.ts +11 -0
  119. package/src/errors/WalletAddressMismatchError.js +13 -0
  120. package/src/errors/WalletNotDeployedError.cjs +14 -0
  121. package/src/errors/WalletNotDeployedError.d.ts +4 -0
  122. package/src/errors/WalletNotDeployedError.js +10 -0
  123. package/src/errors/WalletUsedError.cjs +14 -0
  124. package/src/errors/WalletUsedError.d.ts +4 -0
  125. package/src/errors/WalletUsedError.js +10 -0
  126. package/src/errors/index.d.ts +33 -0
  127. package/src/formatNumberText/formatNumberText.cjs +20 -0
  128. package/src/formatNumberText/formatNumberText.d.ts +5 -0
  129. package/src/formatNumberText/formatNumberText.js +16 -0
  130. package/src/formatNumberText/index.d.ts +1 -0
  131. package/src/get/get.cjs +24 -0
  132. package/src/get/get.d.ts +7 -0
  133. package/src/get/get.js +20 -0
  134. package/src/get/index.d.ts +1 -0
  135. package/src/getProvidersFromWindow.cjs +39 -0
  136. package/src/getProvidersFromWindow.d.ts +7 -0
  137. package/src/getProvidersFromWindow.js +35 -0
  138. package/src/getTLD/getTLD.cjs +12 -0
  139. package/src/getTLD/getTLD.d.ts +1 -0
  140. package/src/getTLD/getTLD.js +8 -0
  141. package/src/getTLD/index.d.ts +1 -0
  142. package/src/handleMobileWalletRedirect/handleMobileWalletRedirect.cjs +20 -0
  143. package/src/handleMobileWalletRedirect/handleMobileWalletRedirect.d.ts +4 -0
  144. package/src/handleMobileWalletRedirect/handleMobileWalletRedirect.js +16 -0
  145. package/src/handleMobileWalletRedirect/index.d.ts +1 -0
  146. package/src/hexToString/hexToString.cjs +32 -0
  147. package/src/hexToString/hexToString.d.ts +12 -0
  148. package/src/hexToString/hexToString.js +28 -0
  149. package/src/hexToString/index.d.ts +1 -0
  150. package/src/index.cjs +151 -0
  151. package/src/index.d.ts +32 -0
  152. package/src/index.js +66 -0
  153. package/src/isFunction/index.d.ts +1 -0
  154. package/src/isFunction/isFunction.cjs +9 -0
  155. package/src/isFunction/isFunction.d.ts +1 -0
  156. package/src/isFunction/isFunction.js +5 -0
  157. package/src/isHex/index.d.ts +1 -0
  158. package/src/isHex/isHex.cjs +20 -0
  159. package/src/isHex/isHex.d.ts +6 -0
  160. package/src/isHex/isHex.js +16 -0
  161. package/src/isLedgerAddressViaVerifiedCredentials.cjs +13 -0
  162. package/src/isLedgerAddressViaVerifiedCredentials.d.ts +2 -0
  163. package/src/isLedgerAddressViaVerifiedCredentials.js +9 -0
  164. package/src/isMobile.cjs +146 -0
  165. package/src/isMobile.d.ts +18 -0
  166. package/src/isMobile.js +133 -0
  167. package/src/last.cjs +22 -0
  168. package/src/last.d.ts +15 -0
  169. package/src/last.js +18 -0
  170. package/src/localStorageAsync.cjs +38 -0
  171. package/src/localStorageAsync.d.ts +3 -0
  172. package/src/localStorageAsync.js +32 -0
  173. package/src/logger/index.d.ts +1 -0
  174. package/src/logger/logger.cjs +10 -0
  175. package/src/logger/logger.d.ts +2 -0
  176. package/src/logger/logger.js +6 -0
  177. package/src/nativeMobileOauthStateParam.cjs +13 -0
  178. package/src/nativeMobileOauthStateParam.d.ts +14 -0
  179. package/src/nativeMobileOauthStateParam.js +9 -0
  180. package/src/parseChainId.cjs +8 -0
  181. package/src/parseChainId.d.ts +1 -0
  182. package/src/parseChainId.js +4 -0
  183. package/src/parseEvmNetworks.cjs +17 -0
  184. package/src/parseEvmNetworks.d.ts +2 -0
  185. package/src/parseEvmNetworks.js +13 -0
  186. package/src/pipe/index.d.ts +1 -0
  187. package/src/pipe/pipe.cjs +40 -0
  188. package/src/pipe/pipe.d.ts +27 -0
  189. package/src/pipe/pipe.js +36 -0
  190. package/src/retryableFn.cjs +73 -0
  191. package/src/retryableFn.d.ts +19 -0
  192. package/src/retryableFn.js +68 -0
  193. package/src/runSafe/index.d.ts +1 -0
  194. package/src/runSafe/runSafe.cjs +25 -0
  195. package/src/runSafe/runSafe.d.ts +8 -0
  196. package/src/runSafe/runSafe.js +21 -0
  197. package/src/services/FetchService/FetchService.cjs +29 -0
  198. package/src/services/FetchService/FetchService.d.ts +10 -0
  199. package/src/services/FetchService/FetchService.js +25 -0
  200. package/src/services/FetchService/index.d.ts +2 -0
  201. package/src/services/FetchService/types.d.ts +3 -0
  202. package/src/services/Oauth2Service/Oauth2Service.cjs +38 -0
  203. package/src/services/Oauth2Service/Oauth2Service.d.ts +34 -0
  204. package/src/services/Oauth2Service/Oauth2Service.js +34 -0
  205. package/src/services/Oauth2Service/createWindowOauth2Service/createWindowOauth2Service.cjs +192 -0
  206. package/src/services/Oauth2Service/createWindowOauth2Service/createWindowOauth2Service.d.ts +2 -0
  207. package/src/services/Oauth2Service/createWindowOauth2Service/createWindowOauth2Service.js +188 -0
  208. package/src/services/Oauth2Service/createWindowOauth2Service/index.d.ts +1 -0
  209. package/src/services/Oauth2Service/index.d.ts +2 -0
  210. package/src/services/PlatformService/PlatformService.cjs +40 -0
  211. package/src/services/PlatformService/PlatformService.d.ts +11 -0
  212. package/src/services/PlatformService/PlatformService.js +36 -0
  213. package/src/services/PlatformService/createBrowserPlatformService/createBrowserPlatformService.cjs +30 -0
  214. package/src/services/PlatformService/createBrowserPlatformService/createBrowserPlatformService.d.ts +2 -0
  215. package/src/services/PlatformService/createBrowserPlatformService/createBrowserPlatformService.js +26 -0
  216. package/src/services/PlatformService/createBrowserPlatformService/index.d.ts +1 -0
  217. package/src/services/PlatformService/index.d.ts +3 -0
  218. package/src/services/PlatformService/types.d.ts +25 -0
  219. package/src/sleep/index.d.ts +1 -0
  220. package/src/sleep/sleep.cjs +13 -0
  221. package/src/sleep/sleep.d.ts +11 -0
  222. package/src/sleep/sleep.js +9 -0
  223. package/src/template/index.d.ts +1 -0
  224. package/src/template/template.cjs +25 -0
  225. package/src/template/template.d.ts +18 -0
  226. package/src/template/template.js +21 -0
  227. package/src/trimEnd/index.d.ts +1 -0
  228. package/src/trimEnd/trimEnd.cjs +14 -0
  229. package/src/trimEnd/trimEnd.d.ts +1 -0
  230. package/src/trimEnd/trimEnd.js +10 -0
  231. package/src/uniq/index.d.ts +1 -0
  232. package/src/uniq/uniq.cjs +33 -0
  233. package/src/uniq/uniq.d.ts +17 -0
  234. package/src/uniq/uniq.js +29 -0
  235. package/src/wrapMethodWithCallback/index.d.ts +1 -0
  236. package/src/wrapMethodWithCallback/wrapMethodWithCallback.cjs +47 -0
  237. package/src/wrapMethodWithCallback/wrapMethodWithCallback.d.ts +36 -0
  238. package/src/wrapMethodWithCallback/wrapMethodWithCallback.js +43 -0
@@ -0,0 +1,18 @@
1
+ type ExtractPlaceholders<T extends string> = T extends `${infer _Start}{{${infer Placeholder}}}${infer Rest}` ? Placeholder | ExtractPlaceholders<Rest> : never;
2
+ /**
3
+ * Creates a template function that replaces placeholders with corresponding values from a data object.
4
+ * @param {string} templateText - The template string containing placeholders in the form {{placeholder}}.
5
+ * @returns {(data) => string} - A function that replaces the placeholders with the values from the data object.
6
+ *
7
+ * @example
8
+ * // Basic usage
9
+ * const compiled = template('Test text {{placeholder}} value');
10
+ * console.log(compiled({ placeholder: 'test' })); // Output: 'Test text test value'
11
+ *
12
+ * @example
13
+ * // Multiple placeholders
14
+ * const compiled = template('Test text {{placeholder}} value {{placeholder2}}');
15
+ * console.log(compiled({ placeholder: 'test', placeholder2: 'test2' })); // Output: 'Test text test value test2'
16
+ */
17
+ export declare const template: <T extends string>(templateText: T) => (data: { [key in ExtractPlaceholders<T>]: string; }) => string;
18
+ export {};
@@ -0,0 +1,21 @@
1
+ 'use client'
2
+ /**
3
+ * Creates a template function that replaces placeholders with corresponding values from a data object.
4
+ * @param {string} templateText - The template string containing placeholders in the form {{placeholder}}.
5
+ * @returns {(data) => string} - A function that replaces the placeholders with the values from the data object.
6
+ *
7
+ * @example
8
+ * // Basic usage
9
+ * const compiled = template('Test text {{placeholder}} value');
10
+ * console.log(compiled({ placeholder: 'test' })); // Output: 'Test text test value'
11
+ *
12
+ * @example
13
+ * // Multiple placeholders
14
+ * const compiled = template('Test text {{placeholder}} value {{placeholder2}}');
15
+ * console.log(compiled({ placeholder: 'test', placeholder2: 'test2' })); // Output: 'Test text test value test2'
16
+ */
17
+ const template = (templateText) => {
18
+ return (data) => templateText.replace(/{{(\w+?)}}/g, (match, key) => key in data ? data[key] : match);
19
+ };
20
+
21
+ export { template };
@@ -0,0 +1 @@
1
+ export { trimEnd } from './trimEnd';
@@ -0,0 +1,14 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ const trimEnd = (text, char = '') => {
7
+ let endIndex = text.length - 1;
8
+ while (endIndex >= 0 && text[endIndex] === char) {
9
+ endIndex--;
10
+ }
11
+ return text.slice(0, endIndex + 1);
12
+ };
13
+
14
+ exports.trimEnd = trimEnd;
@@ -0,0 +1 @@
1
+ export declare const trimEnd: (text: string, char?: string) => string;
@@ -0,0 +1,10 @@
1
+ 'use client'
2
+ const trimEnd = (text, char = '') => {
3
+ let endIndex = text.length - 1;
4
+ while (endIndex >= 0 && text[endIndex] === char) {
5
+ endIndex--;
6
+ }
7
+ return text.slice(0, endIndex + 1);
8
+ };
9
+
10
+ export { trimEnd };
@@ -0,0 +1 @@
1
+ export { uniq } from './uniq';
@@ -0,0 +1,33 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ /**
7
+ * Removes duplicate elements from an array.
8
+ *
9
+ * @param {T[]} array - The array to filter for unique elements.
10
+ * @template T - The type of elements in the array.
11
+ * @returns {T[]} A new array containing only the unique elements of the given array.
12
+ *
13
+ * @example
14
+ * // returns [1, 2, 3, 4, 5]
15
+ * uniq([1, 2, 2, 3, 4, 4, 5]);
16
+ *
17
+ * @example
18
+ * // returns ['apple', 'banana', 'orange']
19
+ * uniq(['apple', 'banana', 'apple', 'orange']);
20
+ *
21
+ */
22
+ const uniq = (array) => {
23
+ const seen = new Set();
24
+ return array.filter((item) => {
25
+ if (seen.has(item)) {
26
+ return false;
27
+ }
28
+ seen.add(item);
29
+ return true;
30
+ });
31
+ };
32
+
33
+ exports.uniq = uniq;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Removes duplicate elements from an array.
3
+ *
4
+ * @param {T[]} array - The array to filter for unique elements.
5
+ * @template T - The type of elements in the array.
6
+ * @returns {T[]} A new array containing only the unique elements of the given array.
7
+ *
8
+ * @example
9
+ * // returns [1, 2, 3, 4, 5]
10
+ * uniq([1, 2, 2, 3, 4, 4, 5]);
11
+ *
12
+ * @example
13
+ * // returns ['apple', 'banana', 'orange']
14
+ * uniq(['apple', 'banana', 'apple', 'orange']);
15
+ *
16
+ */
17
+ export declare const uniq: <T>(array: T[]) => T[];
@@ -0,0 +1,29 @@
1
+ 'use client'
2
+ /**
3
+ * Removes duplicate elements from an array.
4
+ *
5
+ * @param {T[]} array - The array to filter for unique elements.
6
+ * @template T - The type of elements in the array.
7
+ * @returns {T[]} A new array containing only the unique elements of the given array.
8
+ *
9
+ * @example
10
+ * // returns [1, 2, 3, 4, 5]
11
+ * uniq([1, 2, 2, 3, 4, 4, 5]);
12
+ *
13
+ * @example
14
+ * // returns ['apple', 'banana', 'orange']
15
+ * uniq(['apple', 'banana', 'apple', 'orange']);
16
+ *
17
+ */
18
+ const uniq = (array) => {
19
+ const seen = new Set();
20
+ return array.filter((item) => {
21
+ if (seen.has(item)) {
22
+ return false;
23
+ }
24
+ seen.add(item);
25
+ return true;
26
+ });
27
+ };
28
+
29
+ export { uniq };
@@ -0,0 +1 @@
1
+ export { wrapMethodWithCallback } from './wrapMethodWithCallback';
@@ -0,0 +1,47 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ /**
7
+ * Wraps an existing method of an object with a callback function. This allows for additional
8
+ * processing or behavior to be executed around the original method. The original method is
9
+ * called within the callback, and its parameters and return value can be accessed and/or modified.
10
+ *
11
+ * @template T - The type of the object whose method is being wrapped.
12
+ * @template K - The key type of the method within the object.
13
+ * @template F - The type of the function, inferred from the method of the object.
14
+ *
15
+ * @param {T} obj - The object containing the method to be wrapped.
16
+ * @param {K} key - The key of the method in the object to be wrapped.
17
+ * @param {(original: F, ...args: Parameters<F>) => ReturnType<F>} callback - The callback
18
+ * function to wrap around the original method. It receive1`s the original method and its
19
+ * parameters as arguments.
20
+ *
21
+ * @returns {void} - The function does not return a value; it modifies the method of the object in place.
22
+ *
23
+ * @example
24
+ * // Example usage
25
+ * const myObject = {
26
+ * greet(name) {
27
+ * return `Hello, ${name}!`;
28
+ * }
29
+ * };
30
+ *
31
+ * wrapMethodWithCallback(myObject, 'greet', (original, ...args) => {
32
+ * console.log('Greet method called with args:', args);
33
+ * return original(...args).toUpperCase();
34
+ * });
35
+ *
36
+ * console.log(myObject.greet('Alice')); // Logs: 'Greet method called with args: ["Alice"]'
37
+ * // Returns: 'HELLO, ALICE!'
38
+ */
39
+ const wrapMethodWithCallback = (obj, key, callback) => {
40
+ const original = obj[key];
41
+ const boundOriginal = original.bind(obj);
42
+ Object.assign(obj, {
43
+ [key]: (...args) => callback(boundOriginal, ...args),
44
+ });
45
+ };
46
+
47
+ exports.wrapMethodWithCallback = wrapMethodWithCallback;
@@ -0,0 +1,36 @@
1
+ type Method = (...args: any[]) => any;
2
+ /**
3
+ * Wraps an existing method of an object with a callback function. This allows for additional
4
+ * processing or behavior to be executed around the original method. The original method is
5
+ * called within the callback, and its parameters and return value can be accessed and/or modified.
6
+ *
7
+ * @template T - The type of the object whose method is being wrapped.
8
+ * @template K - The key type of the method within the object.
9
+ * @template F - The type of the function, inferred from the method of the object.
10
+ *
11
+ * @param {T} obj - The object containing the method to be wrapped.
12
+ * @param {K} key - The key of the method in the object to be wrapped.
13
+ * @param {(original: F, ...args: Parameters<F>) => ReturnType<F>} callback - The callback
14
+ * function to wrap around the original method. It receive1`s the original method and its
15
+ * parameters as arguments.
16
+ *
17
+ * @returns {void} - The function does not return a value; it modifies the method of the object in place.
18
+ *
19
+ * @example
20
+ * // Example usage
21
+ * const myObject = {
22
+ * greet(name) {
23
+ * return `Hello, ${name}!`;
24
+ * }
25
+ * };
26
+ *
27
+ * wrapMethodWithCallback(myObject, 'greet', (original, ...args) => {
28
+ * console.log('Greet method called with args:', args);
29
+ * return original(...args).toUpperCase();
30
+ * });
31
+ *
32
+ * console.log(myObject.greet('Alice')); // Logs: 'Greet method called with args: ["Alice"]'
33
+ * // Returns: 'HELLO, ALICE!'
34
+ */
35
+ export declare const wrapMethodWithCallback: <T extends object, K extends { [P in keyof T]: T[P] extends Method ? P : never; }[keyof T], F extends Method = T[K] extends Method ? T[K] : never>(obj: T, key: K, callback: (original: F, ...args: Parameters<F>) => ReturnType<F>) => void;
36
+ export {};
@@ -0,0 +1,43 @@
1
+ 'use client'
2
+ /**
3
+ * Wraps an existing method of an object with a callback function. This allows for additional
4
+ * processing or behavior to be executed around the original method. The original method is
5
+ * called within the callback, and its parameters and return value can be accessed and/or modified.
6
+ *
7
+ * @template T - The type of the object whose method is being wrapped.
8
+ * @template K - The key type of the method within the object.
9
+ * @template F - The type of the function, inferred from the method of the object.
10
+ *
11
+ * @param {T} obj - The object containing the method to be wrapped.
12
+ * @param {K} key - The key of the method in the object to be wrapped.
13
+ * @param {(original: F, ...args: Parameters<F>) => ReturnType<F>} callback - The callback
14
+ * function to wrap around the original method. It receive1`s the original method and its
15
+ * parameters as arguments.
16
+ *
17
+ * @returns {void} - The function does not return a value; it modifies the method of the object in place.
18
+ *
19
+ * @example
20
+ * // Example usage
21
+ * const myObject = {
22
+ * greet(name) {
23
+ * return `Hello, ${name}!`;
24
+ * }
25
+ * };
26
+ *
27
+ * wrapMethodWithCallback(myObject, 'greet', (original, ...args) => {
28
+ * console.log('Greet method called with args:', args);
29
+ * return original(...args).toUpperCase();
30
+ * });
31
+ *
32
+ * console.log(myObject.greet('Alice')); // Logs: 'Greet method called with args: ["Alice"]'
33
+ * // Returns: 'HELLO, ALICE!'
34
+ */
35
+ const wrapMethodWithCallback = (obj, key, callback) => {
36
+ const original = obj[key];
37
+ const boundOriginal = original.bind(obj);
38
+ Object.assign(obj, {
39
+ [key]: (...args) => callback(boundOriginal, ...args),
40
+ });
41
+ };
42
+
43
+ export { wrapMethodWithCallback };