p-safe 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Shahrad Elahi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # p-safe
2
+
3
+ > Safely handle promise rejections
4
+
5
+ [![codecov](https://codecov.io/gh/shahradelahi/p-safe/branch/master/graph/badge.svg)](https://codecov.io/gh/shahradelahi/p-safe)
6
+ [![npm](https://img.shields.io/npm/v/p-safe)](https://www.npmjs.com/package/p-safe)
7
+ [![npm bundle size](https://packagephobia.now.sh/badge?p=p-safe)](https://packagephobia.now.sh/result?p=p-safe)
8
+
9
+ ### Install
10
+
11
+ ```bash
12
+ npm install p-safe
13
+ ```
14
+
15
+ ### Usage
16
+
17
+ ```typescript
18
+ import { trySafe, type SafeReturn } from 'p-safe';
19
+
20
+ const { error } = trySafe(async (): SafeReturn<never, Error> => {
21
+ await promiseThatMightReject();
22
+ await fetch('...');
23
+ });
24
+
25
+ if (error) {
26
+ // Handle error
27
+ console.error(error);
28
+ return;
29
+ }
30
+
31
+ console.log(error); // undefined
32
+ ```
33
+
34
+ Or implement your function and only use `SafeReturn` type:
35
+
36
+ ```typescript
37
+ import type { SafeReturn } from 'p-safe';
38
+
39
+ async function foo(): Promise<SafeReturn<object, Error>> {
40
+ const resp = await fetch('...');
41
+ if (!resp.ok) {
42
+ return { error: new Error('Request failed for a silly reason') };
43
+ }
44
+ return { data: await resp.json() };
45
+ }
46
+
47
+ const { data, error } = await foo();
48
+
49
+ if (error) {
50
+ // Handle error
51
+ console.error(error);
52
+ return;
53
+ }
54
+
55
+ console.log(error); // undefined
56
+ console.log(data); // { ... }
57
+ ```
58
+
59
+ Are you still confused? Let me explain it in a simple way. `p-safe` is a simple utility that helps you to safely handle
60
+ promise rejections. It's a simple and clean way to handle promise rejections without using `try/catch` blocks.
61
+
62
+ Check out the [tests](/index.test.ts) for more examples.
63
+
64
+ ## Related
65
+
66
+ - [p-catch-if](https://github.com/sindresorhus/p-catch-if) - Conditional promise catch handler
67
+ - [p-if](https://github.com/sindresorhus/p-if) - Conditional promise chains
68
+ - [p-tap](https://github.com/sindresorhus/p-tap) - Tap into a promise chain without affecting its value or state
69
+ - [p-log](https://github.com/sindresorhus/p-log) - Log the value/error of a promise
70
+ - [More…](https://github.com/sindresorhus/promise-fun)
71
+
72
+ ### License
73
+
74
+ [MIT](/LICENSE) © [Shahrad Elahi](https://github.com/shahradelahi)
@@ -0,0 +1,14 @@
1
+ type SafeReturn<T, K = Error> = Partial<{
2
+ data: T;
3
+ error: K;
4
+ }> & ({
5
+ data: T;
6
+ error?: never;
7
+ } | {
8
+ data?: never;
9
+ error: K;
10
+ });
11
+ type TrySafeFn<T, K> = () => SafeReturn<T, K> | Promise<SafeReturn<T, K>> | void | Promise<void>;
12
+ declare function trySafe<T = void, E = Error>(fn: TrySafeFn<T, E>): Promise<SafeReturn<T, E>>;
13
+
14
+ export { type SafeReturn, type TrySafeFn, trySafe };
@@ -0,0 +1,14 @@
1
+ type SafeReturn<T, K = Error> = Partial<{
2
+ data: T;
3
+ error: K;
4
+ }> & ({
5
+ data: T;
6
+ error?: never;
7
+ } | {
8
+ data?: never;
9
+ error: K;
10
+ });
11
+ type TrySafeFn<T, K> = () => SafeReturn<T, K> | Promise<SafeReturn<T, K>> | void | Promise<void>;
12
+ declare function trySafe<T = void, E = Error>(fn: TrySafeFn<T, E>): Promise<SafeReturn<T, E>>;
13
+
14
+ export { type SafeReturn, type TrySafeFn, trySafe };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";var o=Object.defineProperty;var i=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var d=(e,r)=>{for(var a in r)o(e,a,{get:r[a],enumerable:!0})},u=(e,r,a,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of T(r))!f.call(e,t)&&t!==a&&o(e,t,{get:()=>r[t],enumerable:!(n=i(r,t))||n.enumerable});return e};var s=e=>u(o({},"__esModule",{value:!0}),e);var S={};d(S,{trySafe:()=>y});module.exports=s(S);async function y(e){try{let r=await e();return typeof r=="object"?r:{data:void 0,error:void 0}}catch(r){return{error:r}}}0&&(module.exports={trySafe});
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ async function t(e){try{let r=await e();return typeof r=="object"?r:{data:void 0,error:void 0}}catch(r){return{error:r}}}export{t as trySafe};
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "p-safe",
3
+ "version": "0.1.0",
4
+ "main": "dist/index.js",
5
+ "module": "dist/index.mjs",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist/**"
9
+ ],
10
+ "exports": {
11
+ ".": {
12
+ "import": {
13
+ "types": "./dist/index.d.mts",
14
+ "default": "./dist/index.mjs"
15
+ },
16
+ "require": {
17
+ "types": "./dist/index.d.ts",
18
+ "default": "./dist/index.js"
19
+ }
20
+ }
21
+ },
22
+ "scripts": {
23
+ "dev": "tsup --watch",
24
+ "build": "tsup",
25
+ "test": "mocha \"**/*.test.ts\"",
26
+ "typecheck": "tsc --noEmit",
27
+ "format:check": "prettier --check \"**/*.{ts,md}\"",
28
+ "format": "prettier --write .",
29
+ "prepublishOnly": "pnpm test && pnpm run format:check && pnpm run typecheck && pnpm build"
30
+ },
31
+ "packageManager": "pnpm@8.15.0",
32
+ "devDependencies": {
33
+ "@types/chai": "^4.3.12",
34
+ "@types/mocha": "^10.0.6",
35
+ "@types/node": "^20.11.28",
36
+ "chai": "^5.1.0",
37
+ "mocha": "^10.3.0",
38
+ "prettier": "^3.2.5",
39
+ "tsd": "^0.30.7",
40
+ "tsup": "^8.0.2",
41
+ "tsx": "^4.7.1",
42
+ "typescript": "^5.4.2"
43
+ },
44
+ "license": "MIT",
45
+ "author": "Shahrad Elahi <shahrad@litehex.com> (https://github.com/shahradelahi)",
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "https://github.com/shahradelahi/p-safe.git"
49
+ },
50
+ "publishConfig": {
51
+ "access": "public",
52
+ "registry": "https://registry.npmjs.org/"
53
+ },
54
+ "keywords": [
55
+ "promise",
56
+ "safe",
57
+ "try"
58
+ ]
59
+ }