#making API routes typesafe

9 messages · Page 1 of 1 (latest)

icy jungle
#

I'm trying to make an api wrapper for tmdb (https://developer.themoviedb.org)
and I like the way octokit for github does it... is there a way i can implement this with type safety?

await octokit.request("POST /repos/{owner}/{repo}/issues", {
  owner: "octocat",
  repo: "hello-world",
  title: "Hello, world!",
  body: "I created this issue using Octokit!",
});

i think this would be pretty easy

await tmdb.get["/title/{query}"]({
  query: 'South Park', 
  sort: 'ascending',
});

In that example if i understand correctly i would just make tmdb.get a proxy object that returns a function that makes a request to that path and replaces the templates with the values. and then i would manually use tyescript to declare tmdb.get as an object where each property is a function that takes in the parameters and returns the data. it looks kinda janky with the square brackets so i was thinking if there was some kind of solution to make the path the first function parameter? is it possible?

The Movie Database (TMDB)

Get started with the basics of the TMDB API.

waxen stag
icy jungle
waxen stag
#

yeah

#

it can look like anything because the function doesn't actually use the type

#

could be [Params, ReturnValue] or { params: Params, return: ReturnValue } instead

icy jungle
#

I will wrap it with Promise<> though

icy jungle
#

!solved

#

!resolved