safe-wrapper 2.0.0 → 2.0.1
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/2.0.0 +0 -0
- package/README.md +40 -11
- package/lib/index.cjs +1 -1
- package/lib/index.d.ts +3 -1
- package/lib/index.mjs +1 -1
- package/package.json +1 -1
package/2.0.0
ADDED
|
File without changes
|
package/README.md
CHANGED
|
@@ -1,22 +1,28 @@
|
|
|
1
1
|
## safe-wrapper
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/safe-wrapper)
|
|
4
|
+
[](https://github.com/mcking-07/safe-wrapper/blob/main/LICENSE)
|
|
5
|
+
[](https://github.com/mcking-07/safe-wrapper/actions)
|
|
6
|
+
[](https://www.npmjs.com/package/safe-wrapper)
|
|
7
|
+
|
|
3
8
|
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
9
|
|
|
5
|
-
|
|
10
|
+
### Features
|
|
6
11
|
|
|
7
12
|
- handles synchronous and asynchronous functions.
|
|
8
13
|
- supports specifying error types to control which errors are caught and handled.
|
|
9
14
|
- returns consistent responses in `[error, result]` format where error is null if no error occurred.
|
|
15
|
+
- supports custom error transformation for advanced error handling.
|
|
10
16
|
|
|
11
|
-
|
|
17
|
+
### Installation
|
|
12
18
|
|
|
13
|
-
```
|
|
19
|
+
```sh
|
|
14
20
|
npm install safe-wrapper
|
|
15
21
|
```
|
|
16
22
|
|
|
17
|
-
|
|
23
|
+
### Usage
|
|
18
24
|
|
|
19
|
-
import `safe` from `safe-wrapper` to use it with any function.
|
|
25
|
+
import `safe` from `safe-wrapper` to use it with any function.
|
|
20
26
|
|
|
21
27
|
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.
|
|
22
28
|
|
|
@@ -86,6 +92,29 @@ const safeSync = safe(sync, [TypeError, RangeError]);
|
|
|
86
92
|
const [error, result] = safeSync(args);
|
|
87
93
|
```
|
|
88
94
|
|
|
95
|
+
#### using custom error transformer
|
|
96
|
+
|
|
97
|
+
you can provide a custom error transformer function to modify how errors are handled:
|
|
98
|
+
|
|
99
|
+
```javascript
|
|
100
|
+
import { safe } from 'safe-wrapper';
|
|
101
|
+
|
|
102
|
+
const transformer = (error) => ({
|
|
103
|
+
code: error.name,
|
|
104
|
+
message: error.message,
|
|
105
|
+
timestamp: new Date()
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const safeWithTransform = safe(
|
|
109
|
+
() => { throw new Error('custom error'); },
|
|
110
|
+
[Error],
|
|
111
|
+
transformer
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
const [error, result] = safeWithTransform();
|
|
115
|
+
// error will be: { code: 'Error', message: 'custom error', timestamp: Date }
|
|
116
|
+
```
|
|
117
|
+
|
|
89
118
|
#### wrapping built-in functions
|
|
90
119
|
|
|
91
120
|
we can also wrap built-in functions, like `JSON.parse`, `Object.keys`, and more.
|
|
@@ -99,14 +128,14 @@ const [error, result] = safeJsonParse('invalid_json');
|
|
|
99
128
|
const [error, result] = safe(Object.keys, [TypeError])(null);
|
|
100
129
|
```
|
|
101
130
|
|
|
102
|
-
|
|
131
|
+
### API Reference
|
|
132
|
+
|
|
133
|
+
`safe(action, types, transformer)`
|
|
103
134
|
|
|
104
|
-
`safe(action, types)`
|
|
105
135
|
- parameters
|
|
106
136
|
- 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.
|
|
137
|
+
- types (array, optional): an array of error types to catch. if no types are specified, all errors are caught. each element must be a valid error constructor.
|
|
138
|
+
- transformer (function, optional): a function that takes an error object and returns a transformed version of it. if not provided, the original error is used.
|
|
108
139
|
- returns `[error, result]`
|
|
109
|
-
- error (error | null): the error object if error occurred, otherwise null.
|
|
140
|
+
- error (error | null): the error object if error occurred, otherwise null. if an transformer is provided, this will be the transformed error.
|
|
110
141
|
- 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 s=Object.defineProperty;var i=Object.getOwnPropertyDescriptor;var a=Object.getOwnPropertyNames;var l=Object.prototype.hasOwnProperty;var h=(e,n)=>{for(var t in n)s(e,t,{get:n[t],enumerable:!0})},d=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let c of a(n))!l.call(e,c)&&c!==t&&s(e,c,{get:()=>n[c],enumerable:!(r=i(n,c))||r.enumerable});return e};var m=e=>d(s({},"__esModule",{value:!0}),e);var P={};h(P,{safe:()=>g});module.exports=m(P);var o=({error:e=null,data:n=null,result:t=null})=>[e,t||n],b=e=>(...n)=>{try{let t=e(...n);return t instanceof Promise?t.then(r=>o({error:r})).catch(r=>o({error:r})):o({error:t})}catch(t){return o({error:t})}},f=(e,n=[],t=void 0)=>{let r=!n.length||n.some(u=>e instanceof u),c=t!==void 0&&typeof t=="function";if(r)return c?b(t)(e):o({error:e});throw e},g=(e,n=[],t=void 0)=>(...r)=>{try{let c=e(...r);return c instanceof Promise?c.then(u=>o({data:u})).catch(u=>f(u,n,t)):o({result:c})}catch(c){return f(c,n,t)}};
|
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* safely executes a synchronous or asynchronous action, handles any errors that occur and returns the result in a consistent response structure.
|
|
3
|
+
*
|
|
3
4
|
* @param {Function} action - the function to be wrapped, which can either be synchronous or asynchronous.
|
|
4
5
|
* @param {Array<ErrorConstructor>} [types = []] - an optional array of error types to check against.
|
|
6
|
+
* @param {Function} [transformer = undefined] - an optional function to transform the error object.
|
|
5
7
|
* @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.
|
|
6
8
|
*/
|
|
7
|
-
export function safe(action: Function, types?: ErrorConstructor
|
|
9
|
+
export function safe(action: Function, types?: Array<ErrorConstructor>, transformer?: Function): Promise<[Error, null] | [null, any]> | [Error, null] | [null, any];
|
package/lib/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var o=({error:
|
|
1
|
+
var o=({error:t=null,data:e=null,result:n=null})=>[t,n||e],f=t=>(...e)=>{try{let n=t(...e);return n instanceof Promise?n.then(c=>o({error:c})).catch(c=>o({error:c})):o({error:n})}catch(n){return o({error:n})}},s=(t,e=[],n=void 0)=>{let c=!e.length||e.some(u=>t instanceof u),r=n!==void 0&&typeof n=="function";if(c)return r?f(n)(t):o({error:t});throw t},i=(t,e=[],n=void 0)=>(...c)=>{try{let r=t(...c);return r instanceof Promise?r.then(u=>o({data:u})).catch(u=>s(u,e,n)):o({result:r})}catch(r){return s(r,e,n)}};export{i as safe};
|
package/package.json
CHANGED