@deepkit/http

Deepkit HTTP is a high-performance HTTP router with built-in dependency injection, routing, and validation support.

Features

Runtime Types
Use just regular TypeScript for RPC functions definition.
Validation
Built-In validation for parameters using just TypeScript types.
Dependency Injection
Full fledged dependency injection for classes and even functions.
Event System
Powerful event system to hook into the HTTP request handling.
Middleware
Register global middleware for all routes or specific routes.
JSX Views
Use JSX to render views in a performant way.

Routes

Write HTTP routes with class methods or simple async functions.

import { App } from '@deepkit/app';
import { FrameworkModule } from '@deepkit/framework';
import { http } from '@deepkit/http';

class MyPage {
    @http.GET('/')
    helloWorld() {
        return "Hello World!";
    }
}

new App({
    controllers: [MyPage],
    imports: [new FrameworkModule]
}).run();
import { App } from '@deepkit/app';
import { FrameworkModule } from '@deepkit/framework';
import { HttpRouterRegistry } from '@deepkit/http';

const app = new App({
    imports: [new FrameworkModule]
});

const router = app.get(HttpRouterRegistry);

router.get('/', () => {
    return "Hello World!";
});

app.run();

Questions & Answers

1. How do I process HTTP requests using Deepkit?

To process HTTP requests using Deepkit, you can define routes either using the functional API or controller classes. In the functional API, you can use the HttpRouterRegistry to define routes and their corresponding handlers. Here's an example:

import { App } from '@deepkit/app';
import { FrameworkModule } from '@deepkit/framework';
import { HttpRouterRegistry, HttpBody } from '@deepkit/http';

const app = new App({
    imports: [new FrameworkModule()]
});

const router = app.get(HttpRouterRegistry);

router.get('/user/:id', (id: number, database: Database) => {
  // Do something with the id and database
  // Return the result
});

router.post('/user', (user: HttpBody<User>) => {
  // Do something with the user
  // Return the result
});

In the above example, we define two routes: a GET route /user/:id that takes an id parameter and a database dependency, and a POST route /user that takes a user object as the request body.

As an alternative to the functional API, you can also use controller classes to define routes. Here's an example:

import { http } from '@deepkit/http';

class UserController {
    constructor(private database: Database) {}

    @http.GET('/user/:id')
    getUser(id: number) {
        // Route logic
    }

    @http.POST('/user')
    createUser(user: HttpBody<User>) {
        // Route logic
    }
}

const app = new App({
    controllers: [UserController],
    imports: [new FrameworkModule()]
});

In both can be seen that you don't handle the HTTP request directly, but instead define a function or method that takes data out of the request automatically by declaring what you expect and need. Deepkit will automatically convert the parameters to the correct types and validate them based on the defined types. This way get API documentation for free either from Deepkit API Console or OpenAPI.

2. What are the advantages of using Deepkit over other HTTP libraries?

The advantages of using Deepkit over other HTTP libraries include its ability to automatically deserialize and validate input from HTTP requests, its built-in support for TypeScript and Dependency Injection, and its flexibility in defining routes via a functional API or controller classes.

3. Can Deepkit automatically deserialize and validate input from HTTP requests?

Yes, Deepkit can automatically deserialize and validate input from HTTP requests based on the defined types. This helps ensure that the input is correctly converted and validated before being processed.

import { MaxLength, MinLength } from '@deepkit/type';

interface User {
    username: string & MinLength<3> & MaxLength<20>;
    password: string & MinLength<4>;
}

router.post('/user/register', (user: HttpBody<User>) => {
    // Do something with the user
    // Return the result
    return true;
});
4. How can I define http routes using the functional API?

Routes can be defined using the functional API by getting an instance of the HttpRouterRegistry and using its methods to define routes. For example:

const app = new App({
    imports: [new FrameworkModule()]
});

const router = app.get(HttpRouterRegistry);

router.get('/user/:id', (id: number, database: Database) => {
    // Route logic
});

app.run();
5. Can I use controller classes to define http routes?

Yes, controller classes can be used to define http routes. The routes are defined as methods decorated with the appropriate HTTP verb decorators, such as @http.GET or @http.POST. For example:

import { http, HttpRouterRegistry } from '@deepkit/http';

class UserController {
    constructor(private database: Database) {}

    @http.GET('/user/:id')
    getUser(id: number) {
        // Route logic
    }
}

const app = new App({
    controllers: [UserController],
    imports: [new FrameworkModule()]
});

No answer for your question? Ask a question or see all questions.

See all questionsAsk a question

Examples

Made in Germany