safe-wrapper 2.0.0 → 2.1.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/2.0.1 +0 -0
- package/README.md +71 -13
- package/lib/index.cjs +1 -1
- package/lib/index.d.ts +10 -4
- package/lib/index.mjs +1 -1
- package/package.json +1 -1
package/2.0.1
ADDED
|
File without changes
|
package/README.md
CHANGED
|
@@ -1,22 +1,30 @@
|
|
|
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
|
-
- returns consistent
|
|
14
|
+
- returns a consistent response in `[error, result]` format,
|
|
15
|
+
- `error`: null if successful, else an error (or transformed object).
|
|
16
|
+
- `result`: return value (sync) or resolved value (async) when successful, else null.
|
|
17
|
+
- supports custom error transformation for advanced error handling.
|
|
10
18
|
|
|
11
|
-
|
|
19
|
+
### Installation
|
|
12
20
|
|
|
13
|
-
```
|
|
21
|
+
```sh
|
|
14
22
|
npm install safe-wrapper
|
|
15
23
|
```
|
|
16
24
|
|
|
17
|
-
|
|
25
|
+
### Usage
|
|
18
26
|
|
|
19
|
-
import `safe` from `safe-wrapper` to use it with any function.
|
|
27
|
+
import `safe` from `safe-wrapper` to use it with any function.
|
|
20
28
|
|
|
21
29
|
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
30
|
|
|
@@ -86,6 +94,56 @@ const safeSync = safe(sync, [TypeError, RangeError]);
|
|
|
86
94
|
const [error, result] = safeSync(args);
|
|
87
95
|
```
|
|
88
96
|
|
|
97
|
+
#### using synchronous custom error transformer
|
|
98
|
+
|
|
99
|
+
you can provide a custom error transformer function to modify how errors are handled:
|
|
100
|
+
|
|
101
|
+
```javascript
|
|
102
|
+
import { safe } from 'safe-wrapper';
|
|
103
|
+
|
|
104
|
+
const transformer = (error) => ({
|
|
105
|
+
code: error.name,
|
|
106
|
+
message: error.message,
|
|
107
|
+
timestamp: new Date()
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const safeWithTransform = safe(
|
|
111
|
+
() => { throw new Error('custom sync error'); },
|
|
112
|
+
[Error],
|
|
113
|
+
transformer
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
const [error, result] = safeWithTransform();
|
|
117
|
+
// error will be: { code: 'Error', message: 'custom sync error', timestamp: Date }
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
#### using asynchronous custom error transformer
|
|
121
|
+
|
|
122
|
+
you can provide an asynchronous custom error transformer to modify how errors are handled.
|
|
123
|
+
|
|
124
|
+
```javascript
|
|
125
|
+
import { safe } from 'safe-wrapper';
|
|
126
|
+
|
|
127
|
+
const transformer = async (error) => {
|
|
128
|
+
await report(error);
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
code: error.name,
|
|
132
|
+
message: error.message,
|
|
133
|
+
timestamp: new Date()
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const safeWithTransform = safe(
|
|
138
|
+
async () => { throw new Error('custom async error'); },
|
|
139
|
+
[Error],
|
|
140
|
+
transformer
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const [error, result] = await safeWithTransform();
|
|
144
|
+
// error will be: { code: 'Error', message: 'custom async error', timestamp: Date }
|
|
145
|
+
```
|
|
146
|
+
|
|
89
147
|
#### wrapping built-in functions
|
|
90
148
|
|
|
91
149
|
we can also wrap built-in functions, like `JSON.parse`, `Object.keys`, and more.
|
|
@@ -99,14 +157,14 @@ const [error, result] = safeJsonParse('invalid_json');
|
|
|
99
157
|
const [error, result] = safe(Object.keys, [TypeError])(null);
|
|
100
158
|
```
|
|
101
159
|
|
|
102
|
-
|
|
160
|
+
### API Reference
|
|
161
|
+
|
|
162
|
+
`safe(action, types, transformer)`
|
|
103
163
|
|
|
104
|
-
`safe(action, types)`
|
|
105
164
|
- parameters
|
|
106
165
|
- 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.
|
|
166
|
+
- 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.
|
|
167
|
+
- 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
168
|
- returns `[error, result]`
|
|
109
|
-
- error (error | null): the error object if error occurred, otherwise null.
|
|
110
|
-
- result (
|
|
111
|
-
|
|
112
|
-
this structure keeps it concise, approachable, and clear for all levels of users.
|
|
169
|
+
- error (error | null): the error object if error occurred, otherwise null. if an transformer is provided, this will be the transformed error.
|
|
170
|
+
- result (result | null): the result of the action function if no error occurred, otherwise null.
|
package/lib/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var f=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var a=Object.getOwnPropertyNames;var h=Object.prototype.hasOwnProperty;var l=(n,e)=>{for(var t in e)f(n,t,{get:e[t],enumerable:!0})},m=(n,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let c of a(e))!h.call(n,c)&&c!==t&&f(n,c,{get:()=>e[c],enumerable:!(o=d(e,c))||o.enumerable});return n};var p=n=>m(f({},"__esModule",{value:!0}),n);var y={};l(y,{safe:()=>g});module.exports=p(y);var r=({error:n=null,data:e=null,result:t=void 0})=>[n,t!==void 0?t:e],s=n=>n instanceof Promise||typeof n?.then=="function",b=n=>(...e)=>{try{let t=n(...e);return s(t)?t.then(o=>r({error:o})).catch(o=>r({error:o})):r({error:t})}catch(t){return r({error:t})}},i=(n,e=[],t=void 0)=>{let o=!e.length||e.some(u=>n instanceof u),c=t!==void 0&&typeof t=="function";if(o)return c?b(t)(n):r({error:n});throw n},g=(n,e=[],t=void 0)=>(...o)=>{try{let c=n(...o);return s(c)?c.then(u=>r({data:u})).catch(u=>i(u,e,t)):r({result:c})}catch(c){return i(c,e,t)}};
|
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
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
|
-
*
|
|
4
|
-
* @
|
|
5
|
-
* @
|
|
3
|
+
*
|
|
4
|
+
* @template R,
|
|
5
|
+
* @template [T = Error]
|
|
6
|
+
*
|
|
7
|
+
* @param {(...args: any[]) => R} action - the function to be wrapped.
|
|
8
|
+
* @param {Array<ErrorConstructor>} [types = []] - an optional array of error types to catch.
|
|
9
|
+
* @param {(error: Error) => Promise<T> | T} [transformer] - an optional function to transform the error object.
|
|
10
|
+
*
|
|
11
|
+
* @returns {(...args: Parameters<typeof action>) => R extends Promise<infer U> ? Promise<[T, null] | [null, U]> : [T, null] | [null, R]} - a tuple where the first element is the error object, if available, or the transformed error object, if a transformer function is provided. the second element is the result of the action, if available.
|
|
6
12
|
*/
|
|
7
|
-
export function safe(action:
|
|
13
|
+
export function safe<R, T = Error>(action: (...args: any[]) => R, types?: Array<ErrorConstructor>, transformer?: (error: Error) => Promise<T> | T): (...args: Parameters<typeof action>) => R extends Promise<infer U> ? Promise<[T, null] | [null, U]> : [T, null] | [null, R];
|
package/lib/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var r=({error:t=null,data:e=null,result:n=void 0})=>[t,n!==void 0?n:e],i=t=>t instanceof Promise||typeof t?.then=="function",s=t=>(...e)=>{try{let n=t(...e);return i(n)?n.then(c=>r({error:c})).catch(c=>r({error:c})):r({error:n})}catch(n){return r({error:n})}},f=(t,e=[],n=void 0)=>{let c=!e.length||e.some(u=>t instanceof u),o=n!==void 0&&typeof n=="function";if(c)return o?s(n)(t):r({error:t});throw t},d=(t,e=[],n=void 0)=>(...c)=>{try{let o=t(...c);return i(o)?o.then(u=>r({data:u})).catch(u=>f(u,e,n)):r({result:o})}catch(o){return f(o,e,n)}};export{d as safe};
|
package/package.json
CHANGED