Skip to content

Commit d892fa1

Browse files
authored
Merge pull request #21 from daiscog/typing-improvements
Adjust typing for improved type guard ergonomics.
2 parents d17b0f6 + 0b5acf2 commit d892fa1

File tree

5 files changed

+25
-38
lines changed

5 files changed

+25
-38
lines changed

apps/examples/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"outputPath": "dist/apps/examples",
1414
"index": "apps/examples/src/index.html",
1515
"main": "apps/examples/src/main.ts",
16-
"polyfills": ["zone.js"],
16+
"polyfills": [],
1717
"tsConfig": "apps/examples/tsconfig.app.json",
1818
"assets": ["apps/examples/src/favicon.ico", "apps/examples/src/assets"],
1919
"styles": ["apps/examples/src/styles.css"],

apps/examples/src/app/app.config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { ApplicationConfig } from '@angular/core';
1+
import {
2+
ApplicationConfig,
3+
provideExperimentalZonelessChangeDetection,
4+
} from '@angular/core';
25
import {
36
provideRouter,
47
withEnabledBlockingInitialNavigation,
@@ -10,5 +13,6 @@ export const appConfig: ApplicationConfig = {
1013
providers: [
1114
provideRouter(appRoutes, withEnabledBlockingInitialNavigation()),
1215
provideHttpClient(),
16+
provideExperimentalZonelessChangeDetection(),
1317
],
1418
};

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.3.0",
12+
"version": "3.4.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"

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

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
11
import { HttpErrorResponse } from '@angular/common/http';
22

3-
/**
4-
* Representation of the state of a data-loading operation.
5-
*
6-
* The various fields are readonly as this is meant to be used to represent an
7-
* immutable snapshot of the current state in a stream of state change events.
8-
*/
9-
export interface HttpRequestState<T> {
10-
/**
11-
* Whether a request is currently in-flight. true for a "loading" state,
12-
* false otherwise.
13-
*/
14-
readonly isLoading: boolean;
15-
/**
16-
* The response data for a "loaded" state, or optionally the last-known data
17-
* (if any) for a "loading" or "error" state.
18-
*/
19-
readonly value?: T;
20-
/**
21-
* The response error (if any) for an "error" state.
22-
*/
23-
readonly error?: HttpErrorResponse | Error;
24-
}
25-
263
/**
274
* Represents an in-flight HTTP request to load some data.
285
*
@@ -32,7 +9,7 @@ export interface HttpRequestState<T> {
329
* UI).
3310
*
3411
*/
35-
export interface LoadingState<T> extends HttpRequestState<T> {
12+
export interface LoadingState<T> {
3613
readonly isLoading: true;
3714
readonly value?: T;
3815
readonly error: undefined;
@@ -56,7 +33,7 @@ export interface LoadingStateWithValue<T> extends LoadingState<T> {
5633
* A <code>value</code> may be omitted if there is no data to display and such
5734
* a scenario is not considered an error condition.
5835
*/
59-
export interface LoadedState<T> extends HttpRequestState<T> {
36+
export interface LoadedState<T> {
6037
readonly isLoading: false;
6138
readonly value: T;
6239
readonly error: undefined;
@@ -68,7 +45,7 @@ export interface LoadedState<T> extends HttpRequestState<T> {
6845
*
6946
* A <code>value</code> may be set to represent a last-known value, or similar.
7047
*/
71-
export interface ErrorState<T> extends HttpRequestState<T> {
48+
export interface ErrorState<T> {
7249
readonly isLoading: false;
7350
readonly value?: T;
7451
readonly error: HttpErrorResponse | Error;
@@ -85,3 +62,14 @@ export interface ErrorState<T> extends HttpRequestState<T> {
8562
export interface ErrorStateWithValue<T> extends ErrorState<T> {
8663
readonly value: T;
8764
}
65+
66+
/**
67+
* Representation of the state of a data-loading operation.
68+
*
69+
* The various fields are readonly as this is meant to be used to represent an
70+
* immutable snapshot of the current state in a stream of state change events.
71+
*/
72+
export type HttpRequestState<T> =
73+
| LoadingState<T>
74+
| LoadedState<T>
75+
| ErrorState<T>;
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
import {
2-
ErrorState,
3-
HttpRequestState,
4-
LoadedState,
5-
LoadingState,
6-
} from './model';
1+
import { ErrorState, LoadedState, LoadingState } from './model';
72

83
export function isLoadingState<T>(
9-
state?: HttpRequestState<T>
4+
state?: LoadingState<T> | ErrorState<unknown> | LoadedState<unknown>
105
): state is LoadingState<T> {
116
return !!state && state.isLoading;
127
}
138

149
export function isLoadedState<T>(
15-
state?: HttpRequestState<T>
10+
state?: LoadedState<T> | LoadingState<unknown> | ErrorState<unknown>
1611
): state is LoadedState<T> {
1712
return !!state && !state.isLoading && !state.error;
1813
}
1914

2015
export function isErrorState<T>(
21-
state?: HttpRequestState<T>
16+
state?: ErrorState<T> | LoadedState<unknown> | LoadingState<unknown>
2217
): state is ErrorState<T> {
2318
return !!state && !state.isLoading && !!state.error;
2419
}

0 commit comments

Comments
 (0)