safe-wrapper 1.0.7 → 2.0.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.
- package/README.md +60 -55
- package/lib/index.cjs +1 -1
- package/lib/index.d.ts +5 -6
- package/lib/index.mjs +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
## safe-wrapper
|
|
2
2
|
|
|
3
|
-
safe-wrapper is a lightweight utility for javascript that simplifies error handling for both synchronous and asynchronous functions. inspired by the [safe assignment operator proposal](https://github.com/arthurfiorette/proposal-safe-assignment-operator), this utility allows for graceful error management by
|
|
3
|
+
safe-wrapper is a lightweight utility for javascript that simplifies error handling for both synchronous and asynchronous functions. inspired by the [safe assignment operator proposal](https://github.com/arthurfiorette/proposal-safe-assignment-operator), this utility allows for graceful error management by wrapping functions in a way that enables error handling without the need for explicit `try-catch` blocks.
|
|
4
|
+
|
|
5
|
+
#### Features
|
|
6
|
+
|
|
7
|
+
- handles synchronous and asynchronous functions.
|
|
8
|
+
- supports specifying error types to control which errors are caught and handled.
|
|
9
|
+
- returns consistent responses in `[error, result]` format where error is null if no error occurred.
|
|
4
10
|
|
|
5
11
|
#### Installation
|
|
6
12
|
|
|
@@ -10,98 +16,97 @@ npm install safe-wrapper
|
|
|
10
16
|
|
|
11
17
|
#### Usage
|
|
12
18
|
|
|
13
|
-
import `safe` from `safe-wrapper` to use it with any function.
|
|
19
|
+
import `safe` from `safe-wrapper` to use it with any function.
|
|
14
20
|
|
|
15
|
-
|
|
21
|
+
the `safe` function takes a target function (synchronous or asynchronous) and returns a function which handles errors and returns a response in a consistent way. the function returns an array `[error, result]`, where `error` is an instance of the specified error type or `null` if successful, and `result` is the result of the function when there is no error.
|
|
16
22
|
|
|
17
|
-
|
|
23
|
+
#### directly wrapping functions
|
|
24
|
+
|
|
25
|
+
we can directly wrap any function definition with safe.
|
|
18
26
|
|
|
19
27
|
```javascript
|
|
20
28
|
import { safe } from 'safe-wrapper';
|
|
21
29
|
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
}
|
|
30
|
+
const safeSync = safe((args) => {
|
|
31
|
+
throw new Error('sync error occurred');
|
|
32
|
+
});
|
|
25
33
|
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return handle(error);
|
|
30
|
-
}
|
|
34
|
+
const safeAsync = safe(async (args) => {
|
|
35
|
+
throw new Error('async error occurred');
|
|
36
|
+
});
|
|
31
37
|
|
|
32
|
-
|
|
38
|
+
const [error, result] = await safeAsync(args);
|
|
33
39
|
```
|
|
34
40
|
|
|
35
|
-
####
|
|
41
|
+
#### wrapping existing functions
|
|
36
42
|
|
|
37
|
-
|
|
43
|
+
safe can be applied to pre-existing functions, including ones from third-party libraries.
|
|
38
44
|
|
|
39
45
|
```javascript
|
|
40
46
|
import { safe } from 'safe-wrapper';
|
|
41
47
|
|
|
42
|
-
const sync = () => {
|
|
43
|
-
|
|
48
|
+
const sync = (args) => {
|
|
49
|
+
throw new Error('sync error occurred');
|
|
44
50
|
}
|
|
45
51
|
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
if (error) {
|
|
49
|
-
return handle(error);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return handle(result);
|
|
52
|
+
const safeSync = safe(sync);
|
|
53
|
+
const [error, result] = safeSync(args);
|
|
53
54
|
```
|
|
54
55
|
|
|
55
|
-
####
|
|
56
|
+
#### handling specific error types
|
|
56
57
|
|
|
57
|
-
|
|
58
|
+
we can specify error types to catch, allowing other errors to throw.
|
|
58
59
|
|
|
59
60
|
```javascript
|
|
60
61
|
import { safe } from 'safe-wrapper';
|
|
61
62
|
|
|
62
|
-
const
|
|
63
|
-
throw new
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const [error, result] = await safe(async());
|
|
67
|
-
|
|
68
|
-
if (error) {
|
|
69
|
-
return handle(error);
|
|
70
|
-
}
|
|
63
|
+
const safeAsync = safe(async (args) => {
|
|
64
|
+
throw new TypeError('async type error occurred');
|
|
65
|
+
}, [TypeError]);
|
|
71
66
|
|
|
72
|
-
|
|
67
|
+
const [error, result] = await safeAsync(args);
|
|
73
68
|
```
|
|
74
69
|
|
|
75
|
-
####
|
|
70
|
+
#### handling multiple error types
|
|
76
71
|
|
|
77
|
-
we can specify error types
|
|
72
|
+
we can specify multiple error types when wrapping a function, enabling safe to catch any of the specified errors.
|
|
78
73
|
|
|
79
74
|
```javascript
|
|
80
75
|
import { safe } from 'safe-wrapper';
|
|
81
76
|
|
|
82
|
-
const
|
|
83
|
-
|
|
77
|
+
const sync = (args) => {
|
|
78
|
+
if (args) {
|
|
79
|
+
throw new TypeError('sync type error occurred');
|
|
80
|
+
} else {
|
|
81
|
+
throw new RangeError('sync range error occurred');
|
|
82
|
+
}
|
|
84
83
|
}
|
|
85
84
|
|
|
86
|
-
const
|
|
85
|
+
const safeSync = safe(sync, [TypeError, RangeError]);
|
|
86
|
+
const [error, result] = safeSync(args);
|
|
87
|
+
```
|
|
87
88
|
|
|
88
|
-
|
|
89
|
-
return handle(error);
|
|
90
|
-
}
|
|
89
|
+
#### wrapping built-in functions
|
|
91
90
|
|
|
92
|
-
|
|
93
|
-
```
|
|
91
|
+
we can also wrap built-in functions, like `JSON.parse`, `Object.keys`, and more.
|
|
94
92
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
93
|
+
```javascript
|
|
94
|
+
import { safe } from 'safe-wrapper';
|
|
95
|
+
|
|
96
|
+
const safeJsonParse = safe(JSON.parse);
|
|
97
|
+
const [error, result] = safeJsonParse('invalid_json');
|
|
98
98
|
|
|
99
|
-
|
|
99
|
+
const [error, result] = safe(Object.keys, [TypeError])(null);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
#### API Reference
|
|
100
103
|
|
|
101
|
-
|
|
104
|
+
`safe(action, types)`
|
|
102
105
|
- parameters
|
|
103
|
-
-
|
|
104
|
-
- types (array, optional): an array of error types to catch
|
|
106
|
+
- action (function): function to be wrapped. it can either be synchronous or asynchronous.
|
|
107
|
+
- types (array, optional): an array of error types to catch. if no types are specified, all errors are caught.
|
|
105
108
|
- returns `[error, result]`
|
|
106
|
-
- error (error | null): the error
|
|
107
|
-
- result (any): the result of the function
|
|
109
|
+
- error (error | null): the error object if error occurred, otherwise null.
|
|
110
|
+
- result (any): the result of the action function if no error occurred, otherwise null.
|
|
111
|
+
|
|
112
|
+
this structure keeps it concise, approachable, and clear for all levels of users.
|
package/lib/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var o=Object.defineProperty;var l=Object.getOwnPropertyDescriptor;var a=Object.getOwnPropertyNames;var h=Object.prototype.hasOwnProperty;var f=(t,n)=>{for(var r in n)o(t,r,{get:n[r],enumerable:!0})},i=(t,n,r,e)=>{if(n&&typeof n=="object"||typeof n=="function")for(let c of a(n))!h.call(t,c)&&c!==r&&o(t,c,{get:()=>n[c],enumerable:!(e=l(n,c))||e.enumerable});return t};var b=t=>i(o({},"__esModule",{value:!0}),t);var m={};f(m,{safe:()=>g});module.exports=b(m);var s=({error:t=null,data:n=null,result:r=null})=>[t,r||n],u=(t,n=[])=>{if(!n.length||n.some(e=>t instanceof e))return s({error:t});throw t},g=(t,n=[])=>(...r)=>{try{let e=t(...r);return e instanceof Promise?e.then(c=>s({data:c})).catch(c=>u(c,n)):s({result:e})}catch(e){return u(e,n)}};
|
package/lib/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* safely executes
|
|
3
|
-
*
|
|
4
|
-
* @param {
|
|
5
|
-
* @
|
|
6
|
-
* @returns {Promise<[null, any]|[Error, null]>|[null, any]|[Error, null]} - returns an array where the first element is null if successful, or an error if it fails. if the input is a promise, it resolves to that array; otherwise, it returns the array directly.
|
|
2
|
+
* safely executes a synchronous or asynchronous action, handles any errors that occur and returns the result in a consistent response structure.
|
|
3
|
+
* @param {Function} action - the function to be wrapped, which can either be synchronous or asynchronous.
|
|
4
|
+
* @param {Array<ErrorConstructor>} [types = []] - an optional array of error types to check against.
|
|
5
|
+
* @returns {Promise<[Error, null] | [null, any]> | [Error, null] | [null, any]} - a tuple where the first element is null, if the execution was successful, or an error object if an error occurred. the second element is the result of the action, if available.
|
|
7
6
|
*/
|
|
8
|
-
export function safe(
|
|
7
|
+
export function safe(action: Function, types?: ErrorConstructor[] | undefined): Promise<[Error, null] | [null, any]> | [Error, null] | [null, any];
|
package/lib/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var o=({error:n=null,data:t=null,result:r=null})=>[n,r||t],s=(n,t=[])=>{if(!t.length||t.some(e=>n instanceof e))return o({error:n});throw n},u=(n,t=[])=>(...r)=>{try{let e=n(...r);return e instanceof Promise?e.then(c=>o({data:c})).catch(c=>s(c,t)):o({result:e})}catch(e){return s(e,t)}};export{u as safe};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "safe-wrapper",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "a js-util for safely wrapping synchronous and asynchronous functions to handle errors based on specified types.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.cjs",
|
|
@@ -40,4 +40,4 @@
|
|
|
40
40
|
"esbuild": "^0.24.0",
|
|
41
41
|
"typescript": "^5.6.3"
|
|
42
42
|
}
|
|
43
|
-
}
|
|
43
|
+
}
|