Rob's garden / notes / note

Note

I got a bit sidetracked this afternoon, I wondered how hard it would be to generate a typed API client based on my schema structure. Turns out it is possible!

First you define your route using a defineRoute primative with a secret _Response structure export

When generated it creates a fetch-based ApiClient for that module, even converting URLPattern parameters to method params (not shown) like so:

Which uses a generated namespace type which is created using the secret _Response export. It nicely compacts the union type too

Then you get a simple to import and use TypeScript client.

I don't think I'll be using it in production any time soon, but it was a fun exploration

I originally tried to do it purely on TypeScript types but my god the API to parse and navigate them is horrendous

TypeScript code with a route called "meRoute" and a structure called "_Response" exported

TypeScript code with a generated method _auth that performs the route

a TypeScript namespace "auth" with generated types MeRequest and MeResposne from the previous structure

A JavaScript script that imports the generated client, instantiates it, calls the "me" endpoint and shows the strongly typed response