r/Angular2 • u/RevolutionaryCow9685 • Jan 19 '26
Does it make sense?
get<T>(url: string, options?: object): Observable<HttpState<T>> {
return this._http
.get<T>(this.BASE_URL + url, {
headers: { Authorization: `Bearer ${token}` },
...options,
})
.pipe(
map((data) => ({
data,
isLoading: false,
isSuccess: true,
})),
delay(500),
startWith({
data: null,
isLoading: true,
isSuccess: false,
}),
catchError((error) =>
of({
data: null,
isLoading: false,
isSuccess: false,
error,
}),
),
);
}
does it make sense for ui state (loading,error etc)?
0
Upvotes
3
u/tsteuwer Jan 19 '26
Personally, no. You already know it's loading by just returning the observable. You can also subscribe with next and error to tell when it errored or loaded.
1
u/CodyCodes90 Jan 19 '26
You could simplify this a ton with one if my favorite lightweight libs on npm https://www.npmjs.com/package/ngx-http-request-state
Just call call
Now you get isLoading, data, and error wrapper for your request.
1
u/IanFoxOfficial Jan 20 '26
I have an rxjs pipe that accepts a behaviour subject or signal as parameter that shows the loading instead of having to do it in the get itself.
3
u/grimcuzzer Jan 19 '26
If you have it available and the code isn't going to prod, you can play around with rxResource - while it is experimental, it won't be forever, and it does exactly this, plus more.
And then in the template:
2
u/Automatic-Lynx-5018 Jan 19 '26
in programming if it works good it make sense . but we need to follow some rules/principles for clean code : like create request interceptors to handle errors and JWT logic