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.
npm install ts-route-kit
- Simplifies HTTP route creation using decorators.
- Fully written in TypeScript for robust types and modern features.
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
}
}
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}` }
}
}
}
import { HelloController, Server } from "./HelloController";
const server = Server(8080);
// Instantiate your controller
new HelloController(server);
// Start the server
server.start()
-
@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 are welcome! Open an issue or a pull request on GitHub.
MIT