Juriy / swapi

SWAPI - Star Wars API service
https://swapi.dev
BSD 3-Clause "New" or "Revised" License
283 stars 91 forks source link

Id missing on the resources #28

Closed Gio85 closed 3 years ago

Gio85 commented 3 years ago

Hi @Juriy, I just wanted to check if there is any way to get the id from each resource. Otherwise will be quite hard try to get a single resource. Thanks

hector4213 commented 3 years ago

Hey, good question! First of all, I'm here to see if this was an issue for anyone else. I encountered this problem myself. But if you take a look at the data, the "id" is provided in the URL property of the character response. So I made a helper function to extract the number from the URL property with regex. it was a problem for me because React cries if no keys are provided and I wanted to do the same thing, fetch resources by a unique identifier. Hope it helps!

  const getIDFromUrl = (charURL) => {
  const regex = /\/people\/(\d+)\//;
  const test = charURL.match(regex);
  return test[1];
};
Juriy commented 3 years ago

Thanks for your feedback! Right now I'm working over a set of API improvements and having IDs is one of the open points. I wouldn't want to change an existing API shape (even in backwards compatible way) since there are plenty resources and tutorials that rely on it. I'd rater introduce a completely new API version that would incorporate those changes.