Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/dreancaste/TriviaPP/llms.txt

Use this file to discover all available pages before exploring further.

SwapiService is a lightweight HTTP client that converts Angular’s Observable-based HttpClient calls into Promises via RxJS firstValueFrom. It targets the community-hosted SWAPI mirror at https://swapi.py4e.com/api and exposes methods for fetching paginated people and planet lists, individual resource records by ID, the complete films catalogue, and arbitrary SWAPI URLs for use in relation-resolution patterns (such as following a character’s homeworld URL).

Constructor / Dependencies

DependencyTokenRole
HttpClient@angular/common/httpIssues all HTTP GET requests
@Injectable({ providedIn: 'root' })
export class SwapiService {
  private baseUrl = 'https://swapi.py4e.com/api';
  constructor(private http: HttpClient) {}
}
HttpClientModule (or provideHttpClient() in standalone bootstrapping) must be registered in the application providers for SwapiService to function.

Methods

All methods return Promise<any> and resolve with the raw JSON body from the SWAPI response. They reject if the HTTP request fails.

getPeople()

Fetches a paginated list of Star Wars characters.
getPeople(page: number = 1): Promise<any>
// GET https://swapi.py4e.com/api/people/?page={page}
page
number
The page number to fetch. Defaults to 1. SWAPI returns up to 10 results per page.
The resolved value matches the SWAPI list envelope:
{
  "count": 82,
  "next": "https://swapi.py4e.com/api/people/?page=2",
  "previous": null,
  "results": [ { "name": "Luke Skywalker", "homeworld": "...", ... } ]
}

getPerson()

Fetches a single character by their SWAPI numeric ID.
getPerson(id: string): Promise<any>
// GET https://swapi.py4e.com/api/people/{id}/
id
string
required
The SWAPI person ID (e.g. "1" for Luke Skywalker).

getPlanets()

Fetches a paginated list of Star Wars planets.
getPlanets(page: number = 1): Promise<any>
// GET https://swapi.py4e.com/api/planets/?page={page}
page
number
The page number to fetch. Defaults to 1.
Each planet record includes name, climate, terrain, population, and a list of residents URLs.

getPlanet()

Fetches a single planet by its SWAPI numeric ID.
getPlanet(id: string): Promise<any>
// GET https://swapi.py4e.com/api/planets/{id}/
id
string
required
The SWAPI planet ID (e.g. "1" for Tatooine).

getFilms()

Fetches the complete Star Wars films catalogue. SWAPI returns all films in a single response (no pagination).
getFilms(): Promise<any>
// GET https://swapi.py4e.com/api/films/
Each film record includes title, director, producer, release_date, and episode_id.

getFilm()

Fetches a single film by its SWAPI numeric ID.
getFilm(id: string): Promise<any>
// GET https://swapi.py4e.com/api/films/{id}/
id
string
required
The SWAPI film ID (e.g. "1" for A New Hope).

getByUrl()

Resolves any fully-qualified SWAPI resource URL. This is the core of the homeworld-resolution pattern used by TriviaService — SWAPI embeds URLs (not IDs) in related-resource fields such as homeworld, residents, and films.
getByUrl(url: string): Promise<any>
// GET {url}
url
string
required
A full SWAPI resource URL, e.g. "https://swapi.py4e.com/api/planets/1/".
Only pass trusted SWAPI URLs to getByUrl(). The method issues an unconstrained GET to whatever string is provided; URLs sourced directly from SWAPI response bodies are safe, but do not forward user-supplied strings.

Usage Example

The following snippet mirrors how TriviaService uses SwapiService internally to build a “homeworld” question for a random character.
import { SwapiService } from './services/swapi.service';

@Injectable({ providedIn: 'root' })
export class ExampleService {
  constructor(private swapi: SwapiService) {}

  async buildHomeworldQuestion() {
    // 1. Fetch page 1 of characters
    const peopleResponse = await this.swapi.getPeople(1);
    const people = peopleResponse.results.filter((p: any) => p.homeworld);

    // 2. Pick a random character
    const person = people[Math.floor(Math.random() * people.length)];

    // 3. Resolve the homeworld URL to get the planet name
    const planet = await this.swapi.getByUrl(person.homeworld);

    console.log(`${person.name} is from ${planet.name}`);
    // → "Luke Skywalker is from Tatooine"
  }

  async getFilmDirectors() {
    const filmsResponse = await this.swapi.getFilms();
    return filmsResponse.results.map((f: any) => ({
      title: f.title,
      director: f.director,
    }));
  }
}

Build docs developers (and LLMs) love