Quick Start

TEAKIT.org aims to provide an all-in-one toolkit for building HTTP services using TypeScript,Here are some common examples.

Prerequisites

  • Operating System: Windows、Linux、macOS
  • Runtime:it is recommended that you use LTS Releases, 1.x or newer

Hello World

A simple hello world

import { Application } from "@teakit/core";
new Application().get("/", () => "Hello World").listen(3000);

Custom HTTP Method

Define route using custom HTTP methods/verbs

import { Application } from "@teakit/core";
new Application()
.get("/hi", () => "Hi")
.post("/hi", () => "From Post")
.put("/hi", () => "From Put")
.route("M-SEARCH", "/hi", () => "Custom Method")
.listen(3000);

Path Parameter

Using dynamic path parameter

import { Application } from "@teakit/core";
new Application()
.get("/id/:id", ({ params: { id } }) => id)
.get("/rest/*", () => "Rest")
.listen(3000);

Return JSON

Convert JSON to response automatically

import { Application } from "@teakit/core";
new Application()
.get("/json", () => {
return {
hello: "Application",
};
})
.listen(3000);

Return a file

A file can be return in as formdata response

The response must 1-level deep object

import { Application } from "@teakit/core";
new Application()
.get("/json", () => {
return {
hello: "Application",
image: Bun.file("public/cat.jpg"),
};
})
.listen(3000);

Header and status

Set a custom header and a status code

import { Application } from "@teakit/core";
new Application()
.get("/", ({ set, error }) => {
set.headers["x-powered-by"] = "Application";
return error(418, "I'm teapod");
})
.listen(3000);

Group

Define a prefix once for sub routes

import { Application } from "@teakit/core";
new Application()
.get("/", () => "Hi")
.group("/auth", (app) => {
return app
.get("/", () => "Hi")
.post("/sign-in", ({ body }) => body)
.put("/sign-up", ({ body }) => body);
})
.listen(3000);

Schema

Enforce a data type of a route

import { Application, t } from "@teakit/core";
new Application()
.post("/mirror", ({ body: { username } }) => username, {
body: t.Object({
username: t.String(),
password: t.String(),
}),
})
.listen(3000);

Lifecycle Hook

Intercept an Application event in order

import { Application, t } from "@teakit/core";
new Application()
.onRequest(() => {
console.log("On request");
})
.on("beforeHandle", () => {
console.log("Before handle");
})
.post("/mirror", ({ body }) => body, {
body: t.Object({
username: t.String(),
password: t.String(),
}),
afterHandle: () => {
console.log("After handle");
},
})
.listen(3000);

Guard

Enforce a data type of sub routes

// @errors: 2345
import { Application, t } from "@teakit/core";
new Application()
.guard(
{
response: t.String(),
},
(app) =>
app
.get("/", () => "Hi")
// Invalid: will throws error, and TypeScript will report error
.get("/invalid", () => 1)
)
.listen(3000);

Customize context

Add custom variable to route context

import { Application } from "@teakit/core";
new Application()
.state("version", 1)
.decorate("getDate", () => Date.now())
.get("/version", ({ getDate, store: { version } }) => `${version} ${getDate()}`)
.listen(3000);

Redirect

Redirect a response

import { Application } from "@teakit/core";
new Application()
.get("/", () => "hi")
.get("/redirect", ({ redirect }) => {
return redirect("/");
})
.listen(3000);

Plugin

Create a separate instance

import { Application } from "@teakit/core";
const plugin = new Application().state("plugin-version", 1).get("/hi", () => "hi");
new Application()
.use(plugin)
.get("/version", ({ store }) => store["plugin-version"])
.listen(3000);

Web Socket

Create a realtime connection using Web Socket

import { Application } from "@teakit/core";
new Application()
.ws("/ping", {
message(ws, message) {
ws.send("hello " + message);
},
})
.listen(3000);

OpenAPI documentation

Create a interactive documentation using Scalar (or optionally Swagger)

import { Application } from "@teakit/core";
import { pluginDocs } from "@teakit/docs";
const app = new Application().use(pluginDocs()).listen(3000);
console.log(`View documentation at "${app.server!.url}swagger" in your browser`);

Unit Test

Write a unit test of your Application app

// test/index.test.ts
import { describe, expect, it } from "bun:test";
import { Application } from "@teakit/core";
describe("Application", () => {
it("return a response", async () => {
const app = new Application().get("/", () => "hi");
const response = await app.handle(new Request("http://localhost/")).then((res) => res.text());
expect(response).toBe("hi");
});
});

Custom body parser

Create a custom logic for parsing body

import { Application } from "@teakit/core";
new Application().onParse(({ request, contentType }) => {
if (contentType === "application/custom-type") return request.text();
});

GraphQL

Create a custom GraphQL server using GraphQL Yoga or Apollo

import { Application } from "@teakit/core";
import { yoga } from "@teakit/graphql-yoga";
const app = new Application()
.use(
yoga({
typeDefs: /* GraphQL */ ` type Query { hi: String } `,
resolvers: {
Query: {
hi: () => "Hello from Application",
},
},
})
)
.listen(3000);
NextRoute