ts-route-kit
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

ts-route-kit

ts-route-kit is a lightweight framework based on http package for creating HTTP routes using decorators. It simplifies the definition of routes with TypeScript.

Installation

npm install ts-route-kit

Features

  • Simplifies HTTP route creation using decorators.
  • Fully written in TypeScript for robust types and modern features.

Configuration

Add a tsconfig.json file to your project for TypeScript compilation:

{
  "compilerOptions": {
    "experimentalDecorators": true,                   /* Enable experimental support for legacy experimental decorators. */
    "emitDecoratorMetadata": true,                    /* Emit design-type metadata for decorated declarations in source files. */
    "target": "ES2020",
    "module": "CommonJS",
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true
  }
}

Usage

Create a Controller

import { AbstractController, Get, Server, HttpRequest, HttpResponse, HttpResponse, HttpMessage } from "ts-route-kit";

class HelloController extends AbstractController {
    @Get('/hello')
    helloHandler(req: HttpRequest): HttpResponse {
        return {
            status: 200,
            headers: {},
            body: req.method
        }
    }

    @Get('/hello/:name')
    helloHandlerWithParams(req: HttpRequest): HttpResponse {
      const { name } = req.params
      return {
            status: 200,
            headers: {},
            body: { message: `Hello ${name}` }
        }
    }

    @Post('/hello')
    helloPostHandler(req: HttpRequest): HttpResponse {
      const { name } = req.body
      if(!name) {
          return HttpMessage.BAD_REQUEST;
      }
      return {
          status: 200,
          headers: {},
          body: { message: `Hello ${name}` }
      }
    }
}

Set Up the Server

import { HelloController, Server } from "./HelloController";

const server = Server(8080);

// Instantiate your controller
new HelloController(server);


// Start the server
server.start()

API

Decorators

  • @Get(path: string) : Defines an HTTP GET route.
  • @Post(path: string) : Defines an HTTP POST route.
  • @Put(path: string) : Defines an HTTP PUT route.
  • @Delete(path: string): Defines an HTTP DELETE route.
  • @Patch(path: string) : Defines an HTTP PATCH route.

Contributions

Contributions are welcome! Open an issue or a pull request on GitHub.

License

MIT

Package Sidebar

Install

npm i ts-route-kit

Weekly Downloads

67

Version

2.0.0

License

MIT

Unpacked Size

32.2 kB

Total Files

38

Last publish

Collaborators

  • bastien2203