blakejoy / tmdb-ts

Typescript client wrapper for TheMovieDB (TMDB) v3
MIT License
37 stars 12 forks source link

[feat] append_to_result #21

Closed bobbymannino closed 1 year ago

bobbymannino commented 1 year ago

Having append_to_result would be useful, took it upon my self to figure it out and came up with this. its type safe so it would work with this. would be uesful to include to save sending multiple requests.

type FindArgs<T extends ('credits' | 'videos')[]> = {
    id: number;
    include?: T;
};

tmdb.movies.find = async <T extends ("credits" | "videos")[]>(args: FindArgs<T>) => {
    type Credits = "credits" extends T[number] ? CreditResponse : undefined; // CreditResponse is the type that '[api]/movie/[movieId]/credits' would return (same as below)
    type Videos = "videos" extends T[number] ? VideoResponse : undefined;

    type Response = Movie & {
      credits: Credits;
      videos: Videos;
    };

    return await get<Response>(`movie/${args.id}?append_to_response=${args.include}`);
  },

You could use this with more then just videos and credits but its more of a proof of concept.

blakejoy commented 1 year ago

Can you append multiple args to the response?

bobbymannino commented 1 year ago

https://developers.themoviedb.org/3/getting-started/append-to-response

Yes, saves sending multiple requests. its a nice feature and its quicker also

blakejoy commented 1 year ago

Ahh yeah that would be nice.