📐 A TypeScript Framework on top of Express. It provide a lot of decorators and guideline to write your code.
Clone or download
Permalink
Failed to load latest commit information.
docs fix: Fix broken documentation Sep 26, 2018
packages feat: post api allows formData in body When api consumes application/… Oct 2, 2018
tasks fix: Fix broken documentation Sep 26, 2018
test feat: post api allows formData in body When api consumes application/… Oct 2, 2018
.gflowrc feat: Add container support Jun 1, 2018
.gitignore docs: Add 404 page customization example Sep 9, 2018
.lintstagedrc chore: Add Prettier code formatter May 10, 2018
.npmignore chore: Split ts-express-decorators into different packages Feb 18, 2018
.nycrc fix: fix Deployment script Aug 29, 2018
.prettierignore chore: Add Prettier code formatter May 10, 2018
.prettierrc chore: Add Prettier code formatter May 10, 2018
.travis.yml fix: Update semantic release job Aug 29, 2018
CNAME chore: add CNAME file Aug 8, 2018
CONTRIBUTING.md chore: Update website Mar 5, 2018
ISSUE_TEMPLATE.md feat(socket): Decorator Args support Model deserialization May 15, 2018
LICENSE Add license Dec 11, 2016
PULL_REQUEST_TEMPLATE.md feat(socket): Decorator Args support Model deserialization May 15, 2018
gulpfile.js fix: fix Deployment script Aug 29, 2018
package.json Travis CI build: 2250 v4.32.0 [ci skip] Oct 2, 2018
readme.md fix: Change url in package.json and documentation (tsed.io) Aug 9, 2018
release.config.js fix(doc): Update deployment task Sep 26, 2018
repo.config.js fix: Fix broken documentation Sep 26, 2018
tsconfig.compile.json fix: fix Deployment script Aug 29, 2018
tsconfig.json fix: Update architecture directory. Fix problem with npm package. Sep 8, 2018
tslint.json chore: Add Prettier code formatter May 10, 2018
yarn.lock Travis CI build: 2243 v4.31.12 [ci skip] Sep 26, 2018

readme.md

Ts.ED

Build Status Coverage Status npm npm version Dependencies img img Known Vulnerabilities semantic-release code style: prettier backers

A TypeScript Framework on top of Express !

What is it

Ts.ED is a framework on top of Express to write your application with TypeScript (or in ES6). It provides a lot of decorators to write your code.

Features

  • Define class as Controller,
  • Define class as Service (IoC),
  • Define class as Middleware and MiddlewareError,
  • Define class as Converter (POJ to Model and Model to POJ),
  • Define root path for an entire controller and versioning your Rest API,
  • Define as sub-route path for a method,
  • Define routes on GET, POST, PUT, DELETE and HEAD verbs,
  • Define middlewares on routes,
  • Define required parameters,
  • Inject data from query string, path parameters, entire body, cookies, session or header,
  • Inject Request, Response, Next object from Express request,
  • Template (View),
  • Swagger documentation and Swagger-ui,
  • Testing.

Documentation

Documentation is available on https://tsed.io

Examples

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

Installation

You can get the latest release using npm:

$ npm install --save @tsed/core @tsed/common express@4 @types/express

Important! TsExpressDecorators requires Node >= 6, Express >= 4, TypeScript >= 2.0 and the experimentalDecorators, emitDecoratorMetadata, types and lib compilation options in your tsconfig.json file.

{
  "compilerOptions": {
    "target": "es2015",
    "lib": ["es2015"],
    "types": ["reflect-metadata"],
    "module": "commonjs",
    "moduleResolution": "node",
    "experimentalDecorators":true,
    "emitDecoratorMetadata": true,
    "sourceMap": true,
    "declaration": false
  },
  "exclude": [
    "node_modules"
  ]
}

Quick start

Create your express server

TsExpressDecorators provide a ServerLoader class to configure your express quickly. Just create a server.ts in your root project, declare a new Server class that extends ServerLoader.

import * as Express from "express";
import {ServerLoader, ServerSettings} from "@tsed/common";
import Path = require("path");

@ServerSettings({
    rootDir: Path.resolve(__dirname),
    acceptMimes: ["application/json"]
})
export class Server extends ServerLoader {

    /**
     * This method let you configure the middleware required by your application to works.
     * @returns {Server}
     */
    public $onMountingMiddlewares(): void|Promise<any> {
    
        const cookieParser = require('cookie-parser'),
            bodyParser = require('body-parser'),
            compress = require('compression'),
            methodOverride = require('method-override');


        this
            .use(GlobalAcceptMimesMiddleware)
            .use(cookieParser())
            .use(compress({}))
            .use(methodOverride())
            .use(bodyParser.json())
            .use(bodyParser.urlencoded({
                extended: true
            }));

        return null;
    }

    public $onReady(){
        console.log('Server started...');
    }
   
    public $onServerInitError(err){
        console.error(err);
    }    
}

new Server().start();

By default ServerLoader load controllers in ${rootDir}/controllers and mount it to /rest endpoint.

To customize the server settings see Configure server with decorator

Create your first controller

Create a new calendarCtrl.ts in your controllers directory configured previously with ServerLoader.mount(). All controllers declared with @Controller decorators is considered as an Express router. An Express router require a path (here, the path is /calendars) to expose an url on your server. More precisely, it is a part of path, and entire exposed url depend on the Server configuration (see ServerLoader.setEndpoint()) and the controllers dependencies. In this case, we haven't a dependencies and the root endpoint is set to /rest. So the controller's url will be http://host/rest/calendars.

import {Controller, Get} from "@tsed/common";
import * as Express from "express";

export interface Calendar{
    id: string;
    name: string;
}

@Controller("/calendars")
export class CalendarCtrl {

    /**
     * Example of classic call. Use `@Get` for routing a request to your method.
     * In this case, this route "/calendars/:id" are mounted on the "rest/" path.
     *
     * By default, the response is sent with status 200 and is serialized in JSON.
     *
     * @param request
     * @param response
     * @returns {{id: any, name: string}}
     */
    @Get("/:id")
    async get(request: Express.Request, response: Express.Response): Promise<Calendar> {
        return {id: request.params.id, name: "test"};
    }

    @Get("/")
    @ResponseView("calendars/index") // Render "calendars/index" file using Express.Response.render internal
    async renderCalendars(request: Express.Request, response: Express.Response): Promise<Array<Calendar>> {

        return [{id: '1', name: "test"}];
    }
    
    @Post("/")
    @Authenticated()
    async post(
        @Required() @BodyParams("calendar") calendar: Calendar
    ): Promise<ICalendar> {
    
        return new Promise((resolve: Function, reject: Function) => {
        
            calendar.id = 1;
            
            resolve(calendar);
            
        });
    }
    
    @Delete("/")
    @Authenticated()
    async post(
        @BodyParams("calendar.id") @Required() id: string 
    ): Promise<ICalendar> {
    
        return new Promise((resolve: Function, reject: Function) => {
        
            calendar.id = id;
            
            resolve(calendar);
            
        });
    }
}

To test your method, just run your server.ts and send a http request on /rest/calendars/1.

Note : Decorators @Get support dynamic pathParams (see /:id) and RegExp like Express API.

Contributors

Please read contributing guidelines here.

Backers

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

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

License

The MIT License (MIT)

Copyright (c) 2016 - 2018 Romain Lenzotti

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.