XUtils

Ts.ED

Intituive TypeScript framework for building server-side apps on top of Express.js or Koa.js.


Ts.ED logo

Build & Release Coverage Status npm version Known Vulnerabilities semantic-release code style: prettier backers

A Node.js and TypeScript Framework on top of Express. It provides a lot of decorators and guidelines to write your code.

What it is

Ts.ED is a Node.js and TypeScript Framework on top of Express/Koa.js. Ts.ED is a framework on top of Express/Koa to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.

Documentation

Documentation is available on https://tsed.io

Getting started

See our getting started here to create new Ts.ED project or use our CLI

Examples

Examples are available on https://tsed.io/tutorials/

Overview

Server example

Here an example to create a Server with Ts.ED:

import {Configuration, Inject} from "@tsed/di";
import {PlatformApplication} from "@tsed/common";
import "@tsed/platform-express";
import * as Path from "path";                              

export const rootDir = Path.resolve(__dirname);

@Configuration({
  rootDir,
  port: 3000
})
export class Server {
  @Inject()
  app: PlatformApplication;

  public $beforeRoutesInit() {
    const cookieParser = require('cookie-parser'),
      bodyParser = require('body-parser'),
      compress = require('compression'),
      methodOverride = require('method-override');
 
    this.app
      .use(cookieParser())
      .use(compress({}))
      .use(methodOverride())
      .use(bodyParser.json())
      .use(bodyParser.urlencoded({
        extended: true
      }));
  }   
}

To run your server, you have to use Platform API to bootstrap your application with the expected platform like Express.

import {$log} from "@tsed/common";
import {PlatformExpress} from "@tsed/platform-express";
import {Server} from "./Server";

async function bootstrap() {
  try {
    $log.debug("Start server...");
    const platform = await PlatformExpress.bootstrap(Server);

    await platform.listen();
    $log.debug("Server initialized");
  } catch (er) {
    $log.error(er);
  }
}

bootstrap();

To customize the server settings see Configure server with decorator

Controller example

This is a simple controller to expose user resource. It use decorators to build the endpoints:

import {Inject} from "@tsed/di";
import {Summary} from "@tsed/swagger";
import {Controller, Get, QueryParams, PathParams, Delete, Post, Required, BodyParams, Status, Put, Returns, ReturnsArray} from "@tsed/common";
import {BadRequest} from "@tsed/exceptions";
import {UsersService} from "../services/UsersService";
import {User} from "../models/User"; 

@Controller("/users")
export class UsersCtrl {
  @Inject()
  usersService: UsersService;

  @Get("/:id")
  @Summary("Get a user from his Id")
  @Returns(User)
  async getUser(@PathParams("id") id: string): Promise<User> {
     return this.usersService.findById(id);
  }

  @Post("/")
  @Status(201)
  @Summary("Create a new user")
  @Returns(User)
  async postUser(@Required() @BodyParams() user: User): Promise<User> {
    return this.usersService.save(user);
  }

  @Put("/:id")
  @Status(201)
  @Summary("Update the given user")
  @Returns(User)
  async putUser(@PathParams("id") id: string, @Required() @BodyParams() user: User): Promise<User> {
    if (user.id !== id) {
      throw new BadRequest("ID mismatch with the given payload")
    }

    return this.usersService.save(user);
  }
  
  @Delete("/:id")
  @Summary("Remove a user")
  @Status(204)
  async deleteUser(@PathParams("id") @Required() id: string ): Promise<User> {
     await this.usersService.delete(user);
  }
  
  @Get("/")
  @Summary("Get all users")
  @ReturnsArray(User)
  async findUser(@QueryParams("name") name: string){
    return this.usersService.find({name});
  }
}

Backers

Thank you to all our backers! 🙏 [Become a backer]


Articles

  • coming soon...