@typed/async-data 1.0.0-beta.0 → 1.0.0-beta.10
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 +21 -0
- package/README.md +32 -23
- package/dist/index.d.ts +664 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +501 -35
- package/package.json +25 -12
- package/src/__tests__/assert.type-test.ts +4 -0
- package/src/__tests__/flat-map.type-test.ts +29 -0
- package/src/index.ts +807 -41
- package/src/AsyncData.test.ts +0 -444
- package/src/index.test.ts +0 -497
- package/tsconfig.json +0 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present The Contributors
|
|
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
CHANGED
|
@@ -39,6 +39,14 @@ AsyncData(A, E): Schema.Codec<AsyncData<A, E>, ...>
|
|
|
39
39
|
|
|
40
40
|
Builds an Effect Schema codec for `AsyncData` given value schema `A` and error schema `E`.
|
|
41
41
|
|
|
42
|
+
`Failure.cause` uses the full Effect `Cause` model. Its encoded form is an ordered JSON array of
|
|
43
|
+
`Fail`, `Die`, and `Interrupt` reasons; defects are normalized with `Schema.Defect()`. For example,
|
|
44
|
+
`failure(Cause.fail("offline"))` encodes its cause as
|
|
45
|
+
`[{ _tag: "Fail", error: "offline" }]` and decodes back to a `Cause.Cause<string>`.
|
|
46
|
+
|
|
47
|
+
Progress codecs accept finite numbers, including negative and fractional values. They reject
|
|
48
|
+
`NaN`, `Infinity`, and `-Infinity` without imposing count or percentage semantics.
|
|
49
|
+
|
|
42
50
|
### Constructors
|
|
43
51
|
|
|
44
52
|
```ts
|
|
@@ -65,15 +73,13 @@ isPending<A, E>(asyncData: AsyncData<A, E>): asyncData is Loading | Refreshing<A
|
|
|
65
73
|
### Pattern matching
|
|
66
74
|
|
|
67
75
|
```ts
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
})
|
|
76
|
-
)
|
|
76
|
+
const label = AsyncData.match(AsyncData.success(1), {
|
|
77
|
+
NoData: () => "no data",
|
|
78
|
+
Loading: () => "loading",
|
|
79
|
+
Failure: () => "failure",
|
|
80
|
+
Success: (value) => `success: ${value}`,
|
|
81
|
+
Optimistic: (value) => `optimistic: ${value}`,
|
|
82
|
+
});
|
|
77
83
|
```
|
|
78
84
|
|
|
79
85
|
Returns a unified result type from the matching branch. Callbacks receive the variant payload (and full variant for `Failure`/`Success`/`Optimistic`).
|
|
@@ -103,21 +109,26 @@ Extract the success value, failure cause, or first error from the variant (or `O
|
|
|
103
109
|
### Transformers
|
|
104
110
|
|
|
105
111
|
```ts
|
|
106
|
-
|
|
112
|
+
const mapped = AsyncData.map(AsyncData.success(1), (value) => value + 1);
|
|
107
113
|
```
|
|
108
114
|
|
|
109
115
|
Maps the success/optimistic value; NoData, Loading, and Failure are unchanged.
|
|
110
116
|
|
|
111
117
|
```ts
|
|
112
|
-
|
|
113
|
-
AsyncData.
|
|
114
|
-
)
|
|
118
|
+
const chained = AsyncData.flatMap(AsyncData.success(1), (value) =>
|
|
119
|
+
AsyncData.success(String(value)),
|
|
120
|
+
);
|
|
115
121
|
```
|
|
116
122
|
|
|
117
123
|
Chains on success or optimistic; NoData, Loading, and Failure are unchanged.
|
|
118
124
|
|
|
119
125
|
```ts
|
|
120
|
-
|
|
126
|
+
import * as Cause from "effect/Cause";
|
|
127
|
+
|
|
128
|
+
const mappedFailure = AsyncData.mapError(
|
|
129
|
+
AsyncData.failure(Cause.fail("offline")),
|
|
130
|
+
(error) => new Error(error),
|
|
131
|
+
);
|
|
121
132
|
```
|
|
122
133
|
|
|
123
134
|
Maps the failure error type; Success, NoData, Loading unchanged; Optimistic recurs on `previous`.
|
|
@@ -137,15 +148,13 @@ import * as AsyncData from "@typed/async-data";
|
|
|
137
148
|
|
|
138
149
|
// Success and match
|
|
139
150
|
const data = AsyncData.success({ id: 1, name: "Alice" });
|
|
140
|
-
const message =
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
})
|
|
148
|
-
);
|
|
151
|
+
const message = AsyncData.match(data, {
|
|
152
|
+
NoData: () => "no data",
|
|
153
|
+
Loading: () => "loading...",
|
|
154
|
+
Failure: (cause) => `error: ${cause}`,
|
|
155
|
+
Success: (user) => `user: ${user.name}`,
|
|
156
|
+
Optimistic: (user) => `optimistic: ${user.name}`,
|
|
157
|
+
});
|
|
149
158
|
// message === "user: Alice"
|
|
150
159
|
|
|
151
160
|
// From Result and getSuccess
|