refactor: ♻️ improve and export types
This commit is contained in:
37
src/try.d.ts
vendored
37
src/try.d.ts
vendored
@@ -1,6 +1,24 @@
|
||||
|
||||
/**
|
||||
* The WrappedResult type returned by the wrapped function in {@link trySync}
|
||||
* and {@link tryAsync}.
|
||||
*
|
||||
* **If a error occurred, the result is undefined**.
|
||||
* If there's not a error, error will be null and the result will be defined.
|
||||
*/
|
||||
type WrappedResult<R> = [Error, undefined] | [null, R];
|
||||
|
||||
/**
|
||||
* The returned function from {@link tryAsync}.
|
||||
*
|
||||
* @param args - The arguments of the function.
|
||||
* - The arguments of the wrapped function.
|
||||
* @returns
|
||||
* - The final tuple containing the Error object (if one occured) and the resulting value.
|
||||
*/
|
||||
type WrappedAsyncFunction<F> = (...args: Parameters<F>) =>
|
||||
Promise<WrappedResult<Awaited<ReturnType<F>>>>;
|
||||
|
||||
/**
|
||||
* Function-sugar/Syntax-sugar for handling functions that can throw errors. Wrapping then
|
||||
* into a try-catch block / "curried function" that returns a "tuple as array" of error and
|
||||
@@ -27,8 +45,18 @@ function tryAsync<
|
||||
? ReturnType<F>
|
||||
: Promise<ReturnType<F>>
|
||||
),
|
||||
>(func: F): (...args: Parameters<F>) =>
|
||||
Promise<WrappedResult<Awaited<ReturnType<F>>>>;
|
||||
>(func: F): WrappedAsyncFunction<F>;
|
||||
|
||||
/**
|
||||
* The returned function from {@link trySync}.
|
||||
*
|
||||
* @param args
|
||||
* - The arguments of the wrapped function.
|
||||
* @returns
|
||||
* - The final tuple containing the Error object (if one occured) and the resulting value.
|
||||
*/
|
||||
type WrappedFunction<F> = (...args: Parameters<F>) =>
|
||||
WrappedResult<ReturnType<F>>;
|
||||
|
||||
/**
|
||||
* Function-sugar/Syntax-sugar for handling functions that can throw errors. Wrapping then
|
||||
@@ -53,9 +81,12 @@ Promise<WrappedResult<Awaited<ReturnType<F>>>>;
|
||||
*/
|
||||
function trySync<
|
||||
F extends (...args: Parameters<F>) => ReturnType<F>,
|
||||
>(func: F): (...args: Parameters<F>) => WrappedResult<ReturnType<F>>;
|
||||
>(func: F): WrappedFunction<F>;
|
||||
|
||||
export {
|
||||
type WrappedAsyncFunction,
|
||||
type WrappedFunction,
|
||||
type WrappedResult,
|
||||
tryAsync as tryA,
|
||||
tryAsync,
|
||||
trySync as tryS,
|
||||
|
||||
25
src/try.js
25
src/try.js
@@ -1,10 +1,24 @@
|
||||
/* eslint-disable no-secrets/no-secrets */
|
||||
|
||||
/**
|
||||
* The WrappedResult type returned by the wrapped function in {@link trySync}
|
||||
* and {@link tryAsync}.
|
||||
*
|
||||
* **If a error occurred, the result is undefined**.
|
||||
* If there's not a error, error will be null and the result will be defined.
|
||||
*
|
||||
* @typedef {[Error, undefined] | [null, R]} WrappedResult<R>
|
||||
* @template R
|
||||
*/
|
||||
|
||||
/**
|
||||
* The returned function from {@link tryAsync}.
|
||||
*
|
||||
* @typedef {(...args: Parameters<F>) => Promise<WrappedResult<Awaited<ReturnType<F>>>>}
|
||||
* WrappedAsyncFunction
|
||||
* @template {(...args: Parameters<F>) => ReturnType<F>} F
|
||||
*/
|
||||
|
||||
/**
|
||||
* Function-sugar/Syntax-sugar for handling functions that can throw errors. Wrapping then
|
||||
* into a try-catch block / "curried function" that returns a "tuple as array" of error and
|
||||
@@ -17,7 +31,7 @@
|
||||
* @template {(...args: Parameters<F>) => Promise<Awaited<ReturnType<F>>>} F
|
||||
* @param {F} func
|
||||
* - The function to be executed.
|
||||
* @returns {(...args: Parameters<F>) => Promise<WrappedResult<Awaited<ReturnType<F>>>>}
|
||||
* @returns {WrappedAsyncFunction<F>}
|
||||
* - The function to be immediately called with the wrapped function's arguments.
|
||||
* @example
|
||||
* const [error, res] = await tryAsync(fetch)("https://example.com");
|
||||
@@ -54,6 +68,13 @@ function tryAsync(func) {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The returned function from {@link trySync}.
|
||||
*
|
||||
* @typedef {(...args: Parameters<F>) => WrappedResult<ReturnType<F>>} WrappedFunction
|
||||
* @template {(...args: Parameters<F>) => ReturnType<F>} F
|
||||
*/
|
||||
|
||||
/**
|
||||
* Function-sugar/Syntax-sugar for handling functions that can throw errors. Wrapping then
|
||||
* into a try-catch block / "curried function" that returns a "tuple as array" of error and
|
||||
@@ -66,7 +87,7 @@ function tryAsync(func) {
|
||||
* @template {(...args: Parameters<F>) => ReturnType<F>} F
|
||||
* @param {F} func
|
||||
* - The function to be executed.
|
||||
* @returns {(...args: Parameters<F>) => WrappedResult<ReturnType<F>>}
|
||||
* @returns {WrappedFunction<F>}
|
||||
* - The function to be immediately called with the wrapped function's arguments.
|
||||
* @example
|
||||
* const [error, json] = trySync(JSON.parse)('{ "hello": "world" }');
|
||||
|
||||
Reference in New Issue
Block a user