Last active
March 29, 2024 04:24
-
-
Save SivaramPg/d8d20e957105659655a65a2ebe0d69e2 to your computer and use it in GitHub Desktop.
Using Star Wars APIs (Swapi.info)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // TO Explore all the available APIs on SWAPI.INFO, please visit the explorer | |
| // https://swapi.info | |
| const SWAPI_BASE_URL = 'https://swapi.info/api' | |
| async function fetchSwapiData(url) { | |
| try { | |
| const response = await fetch(url) | |
| const data = await response.json() | |
| return data | |
| } catch (error) { | |
| console.log(error) | |
| process.exit(1) | |
| } | |
| } | |
| async function fetchRootUrls() { | |
| const data = await fetchSwapiData(SWAPI_BASE_URL) | |
| console.log(data) | |
| // Response: | |
| // { | |
| // films: 'https://swapi.info/api/films', | |
| // people: 'https://swapi.info/api/people', | |
| // planets: 'https://swapi.info/api/planets', | |
| // species: 'https://swapi.info/api/species', | |
| // vehicles: 'https://swapi.info/api/vehicles', | |
| // starships: 'https://swapi.info/api/starships', | |
| // }; | |
| } | |
| async function fetchSwapiFilms() { | |
| const data = await fetchSwapiData(SWAPI_BASE_URL + '/films') | |
| console.log(data) | |
| } | |
| async function fetchSwapiPeople() { | |
| const data = await fetchSwapiData(SWAPI_BASE_URL + '/people') | |
| console.log(data) | |
| } | |
| async function fetchSwapiPlanets() { | |
| const data = await fetchSwapiData(SWAPI_BASE_URL + '/planets') | |
| console.log(data) | |
| } | |
| async function fetchSwapiSpecies() { | |
| const data = await fetchSwapiData(SWAPI_BASE_URL + '/species') | |
| console.log(data) | |
| } | |
| async function fetchSwapiVehicles() { | |
| const data = await fetchSwapiData(SWAPI_BASE_URL + '/vehicles') | |
| console.log(data) | |
| } | |
| async function fetchSwapiStarships() { | |
| const data = await fetchSwapiData(SWAPI_BASE_URL + '/starships') | |
| console.log(data) | |
| } | |
| async function fetchIndividualSwapiFilm(id = 1) { | |
| const data = await fetchSwapiData(SWAPI_BASE_URL + '/films/' + id) | |
| console.log(data) | |
| } | |
| async function fetchIndividualSwapiPerson(id = 1) { | |
| const data = await fetchSwapiData(SWAPI_BASE_URL + '/people/' + id) | |
| console.log(data) | |
| } | |
| async function fetchIndividualSwapiPlanet(id = 1) { | |
| const data = await fetchSwapiData(SWAPI_BASE_URL + '/planets/' + id) | |
| console.log(data) | |
| } | |
| async function fetchIndividualSwapiSpecie(id = 1) { | |
| const data = await fetchSwapiData(SWAPI_BASE_URL + '/species/' + id) | |
| console.log(data) | |
| } | |
| async function fetchIndividualSwapiVehicle(id = 1) { | |
| const data = await fetchSwapiData(SWAPI_BASE_URL + '/vehicles/' + id) | |
| console.log(data) | |
| } | |
| async function fetchIndividualSwapiStarship(id = 1) { | |
| const data = await fetchSwapiData(SWAPI_BASE_URL + '/starships/' + id) | |
| console.log(data) | |
| } | |
| fetchRootUrls() | |
| fetchSwapiFilms() | |
| fetchSwapiPeople() | |
| fetchSwapiPlanets() | |
| fetchSwapiSpecies() | |
| fetchSwapiVehicles() | |
| fetchSwapiStarships() | |
| fetchIndividualSwapiFilm(2) | |
| fetchIndividualSwapiPerson(3) | |
| fetchIndividualSwapiPlanet(4) | |
| fetchIndividualSwapiSpecie(5) | |
| fetchIndividualSwapiVehicle(6) | |
| fetchIndividualSwapiStarship(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment