]> git.r.bdr.sh - rbdr/forum/blame - src/lib/stores/response_builder.ts
Don't remember what this WIP was about
[rbdr/forum] / src / lib / stores / response_builder.ts
CommitLineData
852ee620
RBR
1/**
2 * Utilities to build responses
3 */
4
5interface AnyError {
6ccc6f60
RBR
6 message: string;
7}
852ee620
RBR
8
9export type StoreState<Type> = {
10 loading: boolean;
11 data: Type | void;
12 error: AnyError | void;
13};
14
6ccc6f60
RBR
15export 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
23export function errorResponse<T>(error: AnyError): StoreState<T> {
24 return {
25 loading: false,
26 data: undefined,
27 error
28 };
852ee620
RBR
29}
30
6ccc6f60
RBR
31export function response<T>(data: T): StoreState<T> {
32 return {
33 loading: false,
34 data,
35 error: undefined
36 };
852ee620 37}