grantholle / moviedb-promise

Interact with themoviedb.org's api with Node... now in TypeScript!
Other
220 stars 49 forks source link

appendToResponse with typescript question... #43

Closed chadacious closed 1 year ago

chadacious commented 3 years ago

I'm just getting into typescript, and attempting to implement the latest moviedb-promise in my project. Where I'm stuck is in how to specify the type when including appendToResponse in my movieInfo call.

I thought maybe I could use typescript intersection but it doesn't seem to work:

      const mainInfo = (await moviedb.movieInfo(
        {
          id: tmdbId,
          append_to_response:
            "credits,release_dates",
        },
        {
          timeout: LONG_AXIOS_TIMEOUT,
        }
      )) as MovieResponse & MovieReleaseDatesResponse & CreditsResponse;

So then I figured I could at least cast the mainInfo from above into what I wanted, but that isn't quite working either, I just get undefined: const releaseDatesUS = (mainInfo as MovieReleaseDatesResponse).results returns undefined.

How do I properly access the appendToResponse props that are not included in MovieResponse?

chadacious commented 3 years ago

So I can make it work using intersection with something like this:

const mainInfo = (await moviedb.movieInfo(
        {
          id: tmdbId,
          append_to_response:
            "alternative_titles,videos,credits,release_dates,images",
        },
        {
          timeout: LONG_AXIOS_TIMEOUT,
        }
      )) as MovieResponse & { release_dates: MovieReleaseDatesResponse };

But it seems a little complicated. Maybe that is how you intended it to be though.

grantholle commented 3 years ago

Yeah, props for a "beginner" figuring this out!

The catch with the append_to_response is that it's quite dynamic, although I think it is be possible to include the types... I've actually never personally used append_to_response. Once I see the response I'll have a better idea of how to accommodate it. I think it's possible and worth investigating.

chadacious commented 3 years ago

You know, the more I think about the solution using intersection on the specific props, the more I feel like that is actually a very reasonable way to handle appendToResponse. Perhaps it would just be helpful to include it in the documentation with a minimal example like:

await moviedb.movieInfo({ id: tmdbId, append_to_response: "release_dates" }))
  as MovieResponse & { release_dates: MovieReleaseDatesResponse };

There's my 2cents anyway.

shabith commented 3 years ago

Thanks for sharing this @chadacious I also think this should be in the documentation.

grantholle commented 3 years ago

I have added some documentation about this. Thanks again for finding a solution @chadacious