Skip to content

Commit f3caf2c

Browse files
committed
chore(app): replace httpClient with http and refactor UserService for reusability
- Standardize `HttpClient` injection across services by renaming `httpClient` to `http`. - Refactor `UserService` to introduce `extractUser` and `createCacheContext` helper methods.
1 parent e175080 commit f3caf2c

4 files changed

Lines changed: 34 additions & 42 deletions

File tree

src/app/core/services/storage/file.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import type { Observable } from 'rxjs';
66
providedIn: 'root',
77
})
88
export class FileService {
9-
private readonly httpClient = inject(HttpClient);
9+
private readonly http = inject(HttpClient);
1010

1111
getFileAsText(fileUrl: string): Observable<string> {
12-
return this.httpClient.get(fileUrl, { responseType: 'text' });
12+
return this.http.get(fileUrl, { responseType: 'text' });
1313
}
1414
}

src/app/features/authentication/services/authentication.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const REFRESH_TOKEN_KEY = 'refresh-token';
2727
})
2828
export class AuthenticationService {
2929
private readonly endpoints = getEndpoints();
30-
private readonly httpClient = inject(HttpClient);
30+
private readonly http = inject(HttpClient);
3131
private readonly storageService = inject(LOCAL_STORAGE);
3232
private readonly languageService = inject(LanguageService);
3333

@@ -53,7 +53,7 @@ export class AuthenticationService {
5353
};
5454

5555
return this.handleAuthResponse(
56-
this.httpClient.post<RegisterResponse>(this.endpoints.auth.v1.authentication, payload, {
56+
this.http.post<RegisterResponse>(this.endpoints.auth.v1.authentication, payload, {
5757
headers: {
5858
'Accept-Language': this.languageService.convertLocaleToAcceptLanguage(),
5959
},
@@ -68,15 +68,15 @@ export class AuthenticationService {
6868
};
6969

7070
return this.handleAuthResponse(
71-
this.httpClient.post<LoginResponse>(this.endpoints.auth.v1.login, payload),
71+
this.http.post<LoginResponse>(this.endpoints.auth.v1.login, payload),
7272
).pipe(map((data) => data.user));
7373
}
7474

7575
refreshToken(): Observable<RefreshTokenResponseData> {
7676
const refreshToken = this.storageService?.getItem(REFRESH_TOKEN_KEY);
7777

7878
return this.handleAuthResponse(
79-
this.httpClient.post<RefreshTokenResponse>(this.endpoints.auth.v1.refreshToken, {
79+
this.http.post<RefreshTokenResponse>(this.endpoints.auth.v1.refreshToken, {
8080
refreshToken,
8181
}),
8282
);

src/app/features/authentication/services/user.service.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,47 @@ import type { Observable } from 'rxjs';
44
import { map } from 'rxjs';
55
import { CACHING_ENABLED } from '~core/interceptors/caching.interceptor';
66
import type { GetMeResponse } from '~features/authentication/types/get-me-response.type';
7-
import type { User } from '~features/authentication/types/user.type';
87
import type { UpdateUserRequest } from '~features/authentication/types/update-user-request.type';
98
import type { UpdateUserResponse } from '~features/authentication/types/update-user-response.type';
109
import type { CatchPokemonRequest } from '~features/authentication/types/catch-pokemon-request.type';
1110
import type { CatchPokemonResponse } from '~features/authentication/types/catch-pokemon-response.type';
11+
import type { User } from '~features/authentication/types/user.type';
1212
import { getEndpoints } from '~core/constants/endpoints.constants';
1313

1414
@Injectable({
1515
providedIn: 'root',
1616
})
1717
export class UserService {
18+
private readonly http = inject(HttpClient);
1819
private readonly endpoints = getEndpoints();
19-
private readonly httpClient = inject(HttpClient);
2020

21-
getMe(options?: { cache: boolean }): Observable<User> {
22-
const { cache = true } = options ?? {};
23-
return this.httpClient
21+
getMe(options?: { cache?: boolean }): Observable<User> {
22+
const cache = options?.cache ?? true;
23+
24+
return this.http
2425
.get<GetMeResponse>(this.endpoints.user.v1.user, {
25-
context: new HttpContext().set(CACHING_ENABLED, cache),
26+
context: this.createCacheContext(cache),
2627
})
27-
.pipe(
28-
map((response: GetMeResponse) => {
29-
const { data } = response;
30-
return data.user;
31-
}),
32-
);
28+
.pipe(this.extractUser());
3329
}
3430

3531
updateUser(updateUserRequest: UpdateUserRequest): Observable<User> {
36-
return this.httpClient
32+
return this.http
3733
.patch<UpdateUserResponse>(this.endpoints.user.v1.user, updateUserRequest)
38-
.pipe(
39-
map((response: UpdateUserResponse) => {
40-
const { data } = response;
41-
return data.user;
42-
}),
43-
);
34+
.pipe(this.extractUser());
4435
}
4536

4637
catchPokemon(catchPokemonRequest: CatchPokemonRequest): Observable<User> {
47-
return this.httpClient
38+
return this.http
4839
.post<CatchPokemonResponse>(this.endpoints.user.v1.pokemonCatch, catchPokemonRequest)
49-
.pipe(
50-
map((response: CatchPokemonResponse) => {
51-
const { data } = response;
52-
return data.user;
53-
}),
54-
);
40+
.pipe(this.extractUser());
41+
}
42+
43+
private extractUser<T extends { data: { user: User } }>() {
44+
return map((response: T) => response.data.user);
45+
}
46+
47+
private createCacheContext(cache: boolean): HttpContext {
48+
return new HttpContext().set(CACHING_ENABLED, cache);
5549
}
5650
}

src/app/features/pokemon/services/pokemon.service.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import type { LastUpdatedPokemonIdsResponse } from '~features/pokemon/types/last
1313
})
1414
export class PokemonService {
1515
private readonly endpoints = getEndpoints();
16-
private readonly httpClient = inject(HttpClient);
16+
private readonly http = inject(HttpClient);
1717

1818
getPokemon(pokemonIdOrName: string | number): Observable<Pokemon> {
19-
return this.httpClient.get<Pokemon>(this.endpoints.pokemon.v1.pokemon(pokemonIdOrName), {
19+
return this.http.get<Pokemon>(this.endpoints.pokemon.v1.pokemon(pokemonIdOrName), {
2020
params: new HttpParams().set('limit', '1'),
2121
context: new HttpContext().set(CACHING_ENABLED, true),
2222
});
@@ -38,13 +38,11 @@ export class PokemonService {
3838
}
3939

4040
getLastUpdatedPokemonIds(): Observable<string[]> {
41-
return this.httpClient
42-
.get<LastUpdatedPokemonIdsResponse>(this.endpoints.pokemon.v1.lastUpdated)
43-
.pipe(
44-
map((response: LastUpdatedPokemonIdsResponse) => {
45-
const { data } = response;
46-
return data.pokemonIds;
47-
}),
48-
);
41+
return this.http.get<LastUpdatedPokemonIdsResponse>(this.endpoints.pokemon.v1.lastUpdated).pipe(
42+
map((response: LastUpdatedPokemonIdsResponse) => {
43+
const { data } = response;
44+
return data.pokemonIds;
45+
}),
46+
);
4947
}
5048
}

0 commit comments

Comments
 (0)