Eden Treaty is a object-like representation of an Teasim server, providing an end-to-end type safety, and a significantly improved developer experience.
With Eden, you can make requests to an Teasim server, with full type-safety, without the need of code generation.
To use Eden Treaty, first export your existing Teasim server type:
// server.tsimport{ Teasim, t }from"teasim";const app =newTeasim().get("/",()=>"Hi Teasim").get("/id/:id",({ params:{ id }})=> id).post("/mirror",({ body })=> body,{ body: t.Object({ id: t.Number(), name: t.String(),}),}).listen(8080);exporttypeApp=typeof app;
Then import the server type, and consume the Teasim API on client:
const{ data: nendoroid, error }= app.mirror.post({ id:1895, name:"Skadi",});if(error){switch(error.status){case400:case401:// narrow down to type 'error' described in the serverwarnUser(error.value);break;default:// typed as unknownreportError(error.value);break;}throw error;}
You can use schema to enforce type-safety on WebSockets, just like a normal route.
Eden.subscribe returns EdenWebSocket which extends the WebSocket class with type-safety. The syntax is mostly identical if you're familiar with the WebSocket API.
If you need more control over EdenWebSocket, you can access EdenWebSocket.raw to access the native WebSocket API instead.