Simplifying Schemas in Coda Pack

:wave: Hey there!

Is there a way to simplify the schemas that help map data from an API’s response to make them more easily accessible within a Coda formula?

Heres a GIF to help explain my issue

Screen Cast 2022-01-13 at 10.18.31 AM

Im returning data from Strava and they have some pretty nested objects. Im specifically after a photo URL but its nested pretty deep and this (shown in the GIF) is the only way I know how to get it as of right now

1 Like

When building a Pack you are free to design a schema that is quite different from the shape the API, and usually that’s a good idea. APIs are often built for computers, while schemas should be built for the people that build docs.

In a case like this, you could have a top-level schema field called photo and then populate it with that deeply nested URL. As an example in the Dungeons and Dragons sample I do something similar:

return {
  // Start with all of the properties in the API response.
  ...spell,
  description: spell.desc?.join("\n"),
  higher_level: spell.higher_level?.join("\n"),
  damage_type: spell.damage?.damage_type?.name,
}
2 Likes

Thank you Eric! I don’t have time to look in the next couple days, but Im going to circle back around to this for sure

1 Like

Hey @Eric_Koleda ,

Coming back to this now. Im looking at your dungeon and dragons pack and have learned a lot! I see you using the function in question later on as well:

for (let response of responses) {
    spells.push(formatSpell(response.body));
  }
  return spells;
}

I don’t fully understand the function itself though as it looks like there is code omitted

Im a little thrown off by the whole "// Start with all the properties in the API response" piece of your function? I know thats a comment, but my understanding would have been that I already defined all the properties of an API response in the defined schema using coda.makeObjectSchema prior.

Can you clarify what is going on or what should be in the omitted code section?

There shouldn’t be any code omitted from that sample, but let me know if you find something missing.

So the basic idea in that block of code, and in the formatSpell() function, is that we are going to take the object returned the API, and use it to create a new object that matches the schema. You could do it very manually like this:

return {
  name: spell.name,
  duration: spell.duration,
  level: spell.level,
  // ...
};

But in all the cases where the API field name and the schema properties match exactly, it’s a lot of busy work. However there is a shortcut in JavaScript that allows you to dump all the contents of one object into another, and that’s the three dots technique you see in the original code:

return {
  ...spell,
  // ...
}

The ...spell says to take all of the fields in the spell object and dump them into the new object. Then the sample goes on to do more customized mapping, where the data returned from the API needs to be changed to fit the schema.

Let me know if that helps clarify things!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.