GithubHelp home page GithubHelp logo

shprota / ts-express-decorators Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tsedio/tsed

0.0 1.0 0.0 16.57 MB

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

Home Page: http://tsed.io

License: MIT License

TypeScript 96.75% HTML 1.10% JavaScript 2.16%

ts-express-decorators's Introduction

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 it is

Ts.ED is a framework on top of Express that helps you write your application in TypeScript (or in ES6). It provides a lot of decorators to make your code more readable and less error-prone.

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 provides 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 {ServerLoader, ServerSettings} from "@tsed/common";
import * as Path from "path";                              

const rootDir = Path.resolve(__dirname);

@ServerSettings({
    rootDir,
    acceptMimes: ["application/json"]
})
export class Server extends ServerLoader {
  /**
   * This method lets you configure the middleware required for your application to work.
   * @returns {Server}
   */
  public $beforeRoutesInit(): 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;
  }   
}

By default ServerLoader loads controllers in ${rootDir}/controllers and mounts them on the /rest endpoint.

And finally:

import {$log, ServerLoader} from "@tsed/common";
import {Server} from "./Server";

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

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

bootstrap();

To customize the server settings see Configure server with decorator

Create your first controller

Create a new calendarCtrl.ts in your controllers directory, as previously configured with ServerLoader.mount(). All controllers declared with @Controller decorators are considered Express routers. An Express router requires a path (here, the path is /calendars) to expose an url on your server. More precisely, it is part of a path, and the entire exposed url depends on the Server configuration (see ServerLoader.setEndpoint()) and the controller's dependencies. In this case, we don't have 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" is 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.

ts-express-decorators's People

Contributors

alex-dev avatar alexproca avatar amorgulis avatar chr33s avatar denisprsa93 avatar dependabot[bot] avatar emmanuelgautier avatar eugenepisotsky avatar iiegor avatar ionaru avatar jestersimpps avatar jindev avatar kennanseno avatar kpapadatos avatar lwallent avatar milewski avatar mitevs avatar nicojs avatar nitzanhardonvonage avatar oskarlebuda avatar romakita avatar sampaioletti avatar scopsy avatar semantic-release-bot avatar szauka avatar tomer-amir-vonage avatar troyanskiy avatar vincent178 avatar vologab avatar xinpascal avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.