Skip to content

Commit 77639a7

Browse files
committed
feat(app): add last updated pokemon to build script
1 parent bf541da commit 77639a7

File tree

4 files changed

+57
-26
lines changed

4 files changed

+57
-26
lines changed

src/app/app.routes.server.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { ServerRoute } from '@angular/ssr';
22
import { RenderMode } from '@angular/ssr';
3+
import { inject } from '@angular/core';
4+
import { PokemonService } from '~features/pokemon/services/pokemon.service';
5+
import { firstValueFrom } from 'rxjs';
36

47
export const serverRoutes: ServerRoute[] = [
58
{
@@ -9,9 +12,10 @@ export const serverRoutes: ServerRoute[] = [
912
{
1013
path: 'pokemon/:pokemonId',
1114
renderMode: RenderMode.Prerender,
12-
// eslint-disable-next-line @typescript-eslint/require-await
1315
async getPrerenderParams() {
14-
return [{ pokemonId: 'pikachu' }];
16+
const pokemonService = inject(PokemonService);
17+
const pokemonIds = await firstValueFrom(pokemonService.getLastUpdatedPokemonIds());
18+
return pokemonIds.map((pokemonId) => ({ pokemonId }));
1519
},
1620
},
1721
{

src/app/core/constants/endpoints.constants.ts

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,41 @@ import { inject } from '@angular/core';
22
import type { Environment } from '~core/tokens/environment.token';
33
import { ENVIRONMENT } from '~core/tokens/environment.token';
44

5+
const getAuthEndpoints = (baseUrl: string) => ({
6+
v1: {
7+
authentication: `${baseUrl}/v1/authentication`,
8+
login: `${baseUrl}/v1/authentication/login`,
9+
refreshToken: `${baseUrl}/v1/authentication/token/refresh`,
10+
},
11+
});
12+
13+
const getUserEndpoints = (baseUrl: string) => ({
14+
v1: {
15+
user: `${baseUrl}/v1/user`,
16+
pokemonCatch: `${baseUrl}/v1/user/pokemon/catch`,
17+
},
18+
});
19+
20+
const getPokemonEndpoints = (baseUrl: string, host: string) => ({
21+
v1: {
22+
pokemon: (pokemonIdOrName: string | number) => `${host}/v2/pokemon/${pokemonIdOrName}`,
23+
lastUpdated: `${baseUrl}/v1/pokemon/last-updated`,
24+
},
25+
});
26+
27+
const getAnalyticsEndpoints = (baseUrl: string) => ({
28+
v1: {
29+
realtimeUsers: `${baseUrl}/v1/analytics/realtime-users`,
30+
},
31+
});
32+
533
export const getEndpoints = () => {
634
const environment = inject<Environment>(ENVIRONMENT);
735
const POKEMON_API_HOST = 'https://pokeapi.co/api';
836
return {
9-
auth: {
10-
v1: {
11-
authentication: `${environment.apiBaseUrl}/v1/authentication`,
12-
login: `${environment.apiBaseUrl}/v1/authentication/login`,
13-
refreshToken: `${environment.apiBaseUrl}/v1/authentication/token/refresh`,
14-
},
15-
},
16-
user: {
17-
v1: {
18-
user: `${environment.apiBaseUrl}/v1/user`,
19-
pokemonCatch: `${environment.apiBaseUrl}/v1/user/pokemon/catch`,
20-
},
21-
},
22-
pokemon: {
23-
v1: {
24-
pokemon: (pokemonIdOrName: string | number) =>
25-
`${POKEMON_API_HOST}/v2/pokemon/${pokemonIdOrName}`,
26-
},
27-
},
28-
analytics: {
29-
v1: {
30-
realtimeUsers: `${environment.apiBaseUrl}/v1/analytics/realtime-users`,
31-
},
32-
},
37+
auth: getAuthEndpoints(environment.apiBaseUrl),
38+
user: getUserEndpoints(environment.apiBaseUrl),
39+
pokemon: getPokemonEndpoints(environment.apiBaseUrl, POKEMON_API_HOST),
40+
analytics: getAnalyticsEndpoints(environment.apiBaseUrl),
3341
} as const;
3442
};

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { HttpClient, HttpContext, HttpParams, httpResource } from '@angular/comm
66
import { CACHING_ENABLED } from '~core/interceptors/caching.interceptor';
77
import type { Pokemon } from '~features/pokemon/types/pokemon.type';
88
import { getEndpoints } from '~core/constants/endpoints.constants';
9+
import type { LastUpdatedPokemonIdsResponse } from '~features/pokemon/types/last-updated-pokemon-ids-response.type';
910

1011
@Injectable({
1112
providedIn: 'root',
@@ -35,4 +36,15 @@ export class PokemonService {
3536
),
3637
);
3738
}
39+
40+
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+
);
49+
}
3850
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { ApiResponse } from '~core/types/api-response.types';
2+
3+
export type LastUpdatedPokemonIdsResponseData = {
4+
pokemonIds: string[];
5+
};
6+
7+
export type LastUpdatedPokemonIdsResponse = ApiResponse<LastUpdatedPokemonIdsResponseData>;

0 commit comments

Comments
 (0)