2
2
3
3
## Description
4
4
5
- A package to handle errors as a result and not throw errors
5
+ A package to handle results. Success, errors or partial success as results and not throw errors
6
6
7
7
## Installation
8
8
@@ -19,48 +19,57 @@ pnpm install @dancastillo/nothrow
19
19
20
20
## Usage
21
21
22
+ #### TypeScript
23
+
22
24
``` typescript
23
- import { Result } from ' @dancastillo/nothrow'
25
+ import {
26
+ type Result ,
27
+ createSuccessfulResult ,
28
+ createFailureResult ,
29
+ createPartialSuccessfulResult ,
30
+ } from ' @dancastillo/nothrow'
24
31
```
25
32
26
- ## API Documentation
33
+ ## Method Documentation
27
34
28
- ### ` successful `
35
+ ### ` createSuccessfulResult `
29
36
30
37
Constructs a successful result with no errors
31
38
32
39
``` typescript
33
- const result = Result . successful ({ success: true })
40
+ const result = createSuccessfulResult ({ success: true })
34
41
// result.data = { success: true }
35
- // result.errors = undefined
42
+ // result.errors = []
36
43
```
37
44
38
- ### ` failure `
45
+ ### ` createFailureResult `
39
46
40
- Constructs a failed result with no data
47
+ Constructs a failure result with no data
41
48
42
49
``` typescript
43
- const result = Result . failure ({ success: false } )
44
- // result.data = undefined
45
- // result.errors = { success: false }
50
+ const result = createFailureResult ([{ error: true }] )
51
+ // result.data = null
52
+ // result.errors = [ { success: false }]
46
53
```
47
54
48
- ### ` partialSuccess `
55
+ ### ` createPartialSuccessResult `
49
56
50
57
Constructs a result with data and errors
51
58
52
59
``` typescript
53
- const result = Result . partialSuccess ({ success: true }, [{ error: ' Missing input' }, { error: ' Not found' }])
60
+ const result = createPartialSuccessReuslt ({ success: true }, [{ error: ' Missing input' }, { error: ' Not found' }])
54
61
// result.data ={ success: true }
55
62
// result.errors = [{ error: 'Missing input' }, { error: 'Not found' }]
56
63
```
57
64
58
- ### ` map `
65
+ ## API Documentation
66
+
67
+ ### ` mapTo `
59
68
60
69
Map the data result and errors result to the wanted type. This method takes in two functions as its arguments
61
70
62
71
``` typescript
63
- const mappedResult = result .map (
72
+ const mappedResult = result .mapTo (
64
73
(data : DataType ) => {
65
74
return <MappedDataType >{ id: data .id }
66
75
},
@@ -96,35 +105,15 @@ const mappedResult = result.mapErrors((errors: ErrorType) => {
96
105
97
106
### ` isSuccessful `
98
107
99
- Returns ` TRUE ` if the result was successful and no errors
108
+ Returns ` boolean `
100
109
101
110
### ` isFailure `
102
111
103
- Returns ` TRUE ` if the result was N error reults and no data
112
+ Returns ` boolean `
104
113
105
114
### ` isPartialSuccess `
106
115
107
- Returns ` FALSE ` if the result returned both a data result and error result
108
-
109
- ### ` hasData `
110
-
111
- Helper method to determine if the ` Result ` has data value set
112
-
113
- ### ` getData `
114
-
115
- Helper method to retrieve the ` Result ` data value set
116
-
117
- ### ` hasErrors `
118
-
119
- Helper method to determine if the ` Result ` has error values set
120
-
121
- ### ` getErrors `
122
-
123
- Helper method to retrieve the ` Result ` error values set
124
-
125
- ### ` errorCount `
126
-
127
- Helper method to retrieve the ` Result ` error values count
116
+ Returns ` boolean `
128
117
129
118
### License
130
119
0 commit comments