TypeScript REST API Library

3.0.4 · active · verified Sun Apr 19

typescript-rest is a lightweight, annotation-based library designed to simplify the creation of RESTful APIs with TypeScript and Express.js. It leverages decorators to define API endpoints, parameters, and HTTP methods, allowing developers to structure their API services cleanly within TypeScript classes. The library is currently stable at version 3.0.4, with recent minor updates indicating active maintenance. Key differentiators include its tight integration with Express, built-in support for dependency injection via IoC containers like `typescript-ioc` (with a dedicated factory module), and its focus on a declarative API definition style using standard TypeScript decorators. This approach aims to reduce boilerplate and improve type safety for Node.js backend development, providing a structured way to build scalable backend services.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates defining a simple REST endpoint using decorators (`@Path`, `@GET`, `@PathParam`) and integrating it with an Express application.

import * as express from "express";
import { Server, Path, GET, PathParam } from "typescript-rest";

// Ensure you have 'npm install reflect-metadata' and import it once globally if using IoC/complex types with decorators:
// import 'reflect-metadata'; 

@Path("/hello")
class HelloService {
  @Path(":name")
  @GET
  sayHello( @PathParam('name') name: string ): string {
    return "Hello " + name;
  }
}

let app: express.Application = express();

// Build and register all decorated services with the Express app
Server.buildServices(app);

app.listen(3000, function() {
  console.log('typescript-rest server listening on port 3000!');
  console.log('Try: GET http://localhost:3000/hello/john_doe');
});

view raw JSON →