]>
Commit | Line | Data |
---|---|---|
852ee620 RBR |
1 | /** |
2 | * Utilities to build responses | |
3 | */ | |
4 | ||
5 | interface AnyError { | |
6ccc6f60 RBR |
6 | message: string; |
7 | } | |
852ee620 RBR |
8 | |
9 | export type StoreState<Type> = { | |
10 | loading: boolean; | |
11 | data: Type | void; | |
12 | error: AnyError | void; | |
13 | }; | |
14 | ||
6ccc6f60 RBR |
15 | export function initialResponse<T>(initialState: T): StoreState<T> { |
16 | return { | |
17 | loading: true, | |
18 | data: initialState, | |
19 | error: undefined | |
20 | }; | |
852ee620 RBR |
21 | } |
22 | ||
6ccc6f60 RBR |
23 | export function errorResponse<T>(error: AnyError): StoreState<T> { |
24 | return { | |
25 | loading: false, | |
26 | data: undefined, | |
27 | error | |
28 | }; | |
852ee620 RBR |
29 | } |
30 | ||
6ccc6f60 RBR |
31 | export function response<T>(data: T): StoreState<T> { |
32 | return { | |
33 | loading: false, | |
34 | data, | |
35 | error: undefined | |
36 | }; | |
852ee620 | 37 | } |