aboutsummaryrefslogtreecommitdiff
path: root/src/lib/stores/response_builder.ts
blob: 91e8d0f36f7ec76cee2f245552e378f1330d798e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
 * Utilities to build responses
 */

interface AnyError {
	message: string;
}

export type StoreState<Type> = {
	loading: boolean;
	data: Type | void;
	error: AnyError | void;
};

export function initialResponse<T>(initialState: T): StoreState<T> {
	return {
		loading: true,
		data: initialState,
		error: undefined
	};
}

export function errorResponse<T>(error: AnyError): StoreState<T> {
	return {
		loading: false,
		data: undefined,
		error
	};
}

export function response<T>(data: T): StoreState<T> {
	return {
		loading: false,
		data,
		error: undefined
	};
}