0.4.1 • Published 7 months ago

ty-rest v0.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Typed REST

deno doc

Library inspired by tRPC for REST APIs.

Example

server.ts:

import {
  createEndpoint,
  createMethodFilter,
  createPathFilter,
} from "https://deno.land/x/t_rest/server/mod.ts";

const serveApi = createPathFilter({
  "hello": createMethodFilter({
    GET: createEndpoint(
      {
        query: {
          name: { type: "string" },
        },
      },
      async ({ query }) => {
        return {
          status: 200,
          body: {
            type: "text/plain",
            data: `Hello ${query.name}`,
          },
        };
      },
    ),
  }),
});

Deno.serve({ port: 8000 }, serveApi);

export type ApiHandler = typeof serveApi;

client.ts:

// @deno-types="https://deno.land/x/t_rest/client/mod.ts"
import { createFetcher } from "https://esb.deno.dev/https://deno.land/x/t_rest/client/mod.ts";
import { type ApiHandler } from "./server.ts";

const fetchApi = createFetcher<ApiHandler>({
  baseUrl: "http://localhost:8080/",
});

const response = await fetchApi("hello", "GET", {
  query: { name: "world" },
});

if (response.status !== 200) {
  throw new Error("Request failed");
}
console.log(response.body); // { type: "text/plain", data: "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)
0.4.1

7 months ago

0.4.0

7 months ago

0.3.2

7 months ago

0.3.1

7 months ago

0.3.0

7 months ago

0.2.1

7 months ago

0.2.0

7 months ago

0.1.5

7 months ago

0.1.4

7 months ago

0.1.3

7 months ago