@abdallemo/routegen-client
The frontend part of my type-safe routing tool.
This is the NPM package I built to work alongside go-route-gen. While the Go tool finds the routes, this package is what I actually use in my frontend to make sure my API calls are safe.
Why I made this
I wanted a way to consume my Go APIs without having to manually type every endpoint string and every parameter. I wanted the TypeScript compiler to yell at me if I tried to hit a route that didn't exist or if I forgot a required ID.
How it works
It's basically a very thin wrapper around Axios that uses a lot of TypeScript generics. It takes the JSON file from the CLI tool and uses template literal types to enforce the METHOD /path format.
Type-Safe Requests
The best part is how it handles path parameters. If I have a route like GET /users/{id}, the package knows that I need to provide an id object.
// How I use it in my projects
import { GoApiClient } from '@abdallemo/routegen-client'
import { API_ROUTES } from './route' // Generated by my CLI tool
export const api = new GoApiClient<typeof API_ROUTES>({
baseURL: 'http://localhost:8080'
})
// I get autocomplete for the routes!
await api.expect<User>().request('GET /users/{id}', { id: 101 })
Things I learned
Building this taught me a lot about advanced TypeScript features like infer and recursive types. I had to learn how to parse strings at the type level, which was a huge challenge but really rewarding when it finally worked.
What's next
I'm looking into adding automatic retries and maybe some basic caching logic so I don't have to keep reimplementing those in every project.