I'm trying map out a dynamic interface for an Angular project. I was able to get some help on here previously with using a dynamic interface, but using it in Angular seems to be giving me some problems.
I'm trying to get back some video game data using an API and the top root level key changes, based on the platform name...
// Possible platforms the user can search
enum Platforms {
'ps3',
'ps4',
'ps5',
'xbox-360',
'xbox-one',
'xbox-series-x',
'3ds',
'switch',
'pc'
}
// This interface will map out the games being returned by platform
export type GamesByPlatform = {
[key in Platforms]: {
data: Game[];
}
}
In my home component, I try to access those games by subscribing to the observable returned from a game service method. I want to take that interface and access the Game array that it carries. Also, 'ps4' is being hard-coded in the game service method for testing purposes. I'm mainly concerned with working with the interface I created.
Game Service...
getGameList(): Observable<GamesByPlatform> {
let params = new HttpParams().set('platform', 'ps4')
.set('count', '16');
return this.http.get<GamesByPlatform>(`${env.BASE_URL}`, {
params: params
});
}
Home Component
import { Component, OnInit } from '@angular/core';
import { GamesService } from '../games.service';
import { Game, GamesByPlatform } from '../shared/models/Game';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
games: Game[] = [];
constructor(private gameService: GamesService) { }
ngOnInit(): void {
this.gameService.getGameList()
.subscribe((results: GamesByPlatform) => {
this.games = results
})
}
}
I'm not sure how to work with that dynamic interface in the home component. If I use dot notation, I get the option to use results[0], results[1], results[2] etc. Using any of these options yields a result of undefined. I most likely need to take a deep dive into Typescript, but I'm not quite sure how to use that dynamic interface in my home component.