Skip to content

Commit d17b0f6

Browse files
authored
Merge pull request #20 from daiscog/improve-typing
Added overloads to the loadingState and errorState builder functions …
2 parents 1a0aa05 + 56feea4 commit d17b0f6

File tree

3 files changed

+63
-20
lines changed

3 files changed

+63
-20
lines changed

libs/ngx-http-request-state/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"loading",
1010
"error"
1111
],
12-
"version": "3.2.0",
12+
"version": "3.3.0",
1313
"peerDependencies": {
1414
"rxjs": "^6.2.0 || ^7.4.0",
1515
"@angular/common": "^14.2.10 || ^15.0.1 || ^16.0.0 || ^17.0.0 || ^18.0.0"
Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,60 @@
1-
import { ErrorState, LoadedState, LoadingState } from './model';
1+
import {
2+
ErrorState,
3+
ErrorStateWithValue,
4+
LoadedState,
5+
LoadingState,
6+
LoadingStateWithValue,
7+
} from './model';
28
import { HttpErrorResponse } from '@angular/common/http';
39

410
/**
511
* Returns a new LoadingState instance with an optional last-known value.
612
*
713
* @param value may be provided to indicate the previously-loaded, or last-known, state
814
*/
9-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
10-
export const loadingState = <T = any>(value?: T): LoadingState<T> => ({
11-
isLoading: true,
12-
value,
13-
error: undefined,
14-
});
15+
export function loadingState<T>(value: T): LoadingStateWithValue<T>;
16+
export function loadingState<T = never>(): LoadingState<T>;
17+
export function loadingState<T = never>(value?: T): LoadingState<T> {
18+
return {
19+
isLoading: true,
20+
value,
21+
error: undefined,
22+
};
23+
}
1524

1625
/**
1726
* Returns a new LoadedState instance, with the optional value as the loaded data.
1827
*
1928
* @param value
2029
*/
21-
export const loadedState = <T>(value: T): LoadedState<T> => ({
22-
isLoading: false,
23-
error: undefined,
24-
value,
25-
});
30+
export function loadedState<T>(value: T): LoadedState<T> {
31+
return {
32+
isLoading: false,
33+
error: undefined,
34+
value,
35+
};
36+
}
2637

2738
/**
2839
* Returns a new ErrorState instance with the given error and optional last-known value.
2940
*
3041
* @param error
3142
* @param value may be provided to indicate the previously-loaded, or last-known, state
3243
*/
33-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
34-
export const errorState = <T = any>(
44+
export function errorState<T>(
45+
error: HttpErrorResponse | Error,
46+
value: T
47+
): ErrorStateWithValue<T>;
48+
export function errorState<T = never>(
49+
error: HttpErrorResponse | Error
50+
): ErrorState<T>;
51+
export function errorState<T = never>(
3552
error: HttpErrorResponse | Error,
3653
value?: T
37-
): ErrorState<T> => ({
38-
isLoading: false,
39-
error,
40-
value,
41-
});
54+
): ErrorState<T> {
55+
return {
56+
isLoading: false,
57+
error,
58+
value,
59+
};
60+
}

libs/ngx-http-request-state/src/lib/model.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ export interface LoadingState<T> extends HttpRequestState<T> {
3838
readonly error: undefined;
3939
}
4040

41+
/**
42+
* Represents an in-flight HTTP request to load some data, with some existing
43+
* known data already present.
44+
*
45+
* Useful for loading states with a default value to render while waiting, or
46+
* to represent a loading state with previously-loaded data when using a
47+
* 'load more' (e.g., infinite scroll) pattern.
48+
*/
49+
export interface LoadingStateWithValue<T> extends LoadingState<T> {
50+
readonly value: T;
51+
}
52+
4153
/**
4254
* Represents a successfully-completed HTTP request, with the given data.
4355
*
@@ -61,3 +73,15 @@ export interface ErrorState<T> extends HttpRequestState<T> {
6173
readonly value?: T;
6274
readonly error: HttpErrorResponse | Error;
6375
}
76+
77+
/**
78+
* Represents an unsuccessful HTTP request with the failure details given in the
79+
* <code>error</code> property, with some pre-existing known data already present.
80+
*
81+
* Useful for error states with a fallback value to render, or to represent an
82+
* error state with previously-loaded data when using a 'load more' (e.g.,
83+
* infinite scroll) pattern.
84+
*/
85+
export interface ErrorStateWithValue<T> extends ErrorState<T> {
86+
readonly value: T;
87+
}

0 commit comments

Comments
 (0)