Skip to main content
The Deno 2 Release Candidate is here
Learn more

Typed REST

deno doc

Library inspired by tRPC for REST APIs.

Example

server.ts:

import { Api, Endpoint } from "https://deno.land/x/t_rest/server/mod.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:

// @deno-types="https://deno.land/x/t_rest/client/mod.ts"
import { Client } from "https://esb.deno.dev/https://deno.land/x/t_rest/client/mod.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)