Typed REST
Library inspired by tRPC for REST APIs.
Example
server.ts
:
import { Api } from "https://deno.land/x/t_rest/server/Api.ts";
import { Endpoint } from "https://deno.land/x/t_rest/server/Endpoint.ts";
const myApi = new Api({
"hello": {
GET: new Endpoint(
{
query: { name: { type: "string" } },
body: null,
},
async ({ query }) => {
return {
status: 200,
type: "text/plain",
body: `Hello ${query.name}`,
};
},
),
},
});
Deno.serve({ port: 8000 }, myApi.serve);
export type MyApi = typeof myApi;
client.ts
:
import { Client } from "https://deno.land/x/t_rest/client/Client.ts";
import { type MyApi } from "./server.ts";
const client = new Client<MyApi>("http://localhost:8080");
const response = await client.fetch("hello", "GET", {
query: { name: "world" },
});
if (response.status !== 200) {
throw new Error("Request failed");
}
console.log(response.body); // Hello world
See more examples in tests.
Features / TODO
- Query params
- JSON body (
application/json
) - File uploads (
multipart/form-data
) - Path segment params
- Custom headers
- Subscriptions (Server Sent Events / WebSockets)