JSPM

  • Created
  • Published
  • Downloads 406
  • Score
    100M100P100Q74866F
  • License MIT

Decorators for express

Package Exports

  • @andrewcaires/express
  • @andrewcaires/express/package.json

Readme

npm downloads size license

express

Decorators for express

Installation

The module is now available on npm! npm i @andrewcaires/express

Example usage

// index.ts

import { TypeCallbackFunction } from "@andrewcaires/utils.js";

import { Application, Argument, Body, Context, Controller, Ctx, Delete, Extends, Get, Middleware, Next, Params, Post, Put } from ".";

@Controller("/test")
class TestController {

  public middleware(
    @Argument() arg: string,
    @Next() next: TypeCallbackFunction
  ): void {

    console.log("middleware", arg);

    next();
  }

  @Post()
  @Middleware("middleware", "add")
  public add(
    @Ctx() ctx: Context,
    @Body() body: any
  ): void {

    ctx.data(body);
  }

  @Get()
  @Middleware("middleware", "all")
  public all(
    @Ctx() ctx: Context
  ): void {

    ctx.list([1, 2, 3, 4, 5, 6, 7, 8, 9]);
  }

  @Delete("/:id")
  @Middleware("middleware", "del")
  public del(
    @Ctx() ctx: Context,
    @Params("id") id: string
  ): void {

    ctx.success(id);
  }

  @Get("/:id")
  @Middleware("middleware", "get")
  public get(
    @Ctx() ctx: Context,
    @Params("id") id: string
  ): void {

    ctx.data({ id, name: "TestController" });
  }

  @Put("/:id")
  @Middleware("middleware", "set")
  public set(
    @Ctx() ctx: Context,
    @Params("id") id: string
  ): void {

    ctx.success(id);
  }
}

@Controller("/test2")
@Extends(TestController)
class Test2Controller extends TestController {

  @Get("/:id")
  @Middleware("middleware", "get")
  public get(
    @Ctx() ctx: Context,
    @Params("id") id: string
  ): void {

    ctx.data({ id, name: "Test2Controller" });
  }
}

const main = async () => {

  const app = new Application([

    new TestController,
    new Test2Controller,

  ], {

    path: "/api",
    port: 8081,

  });

  await app.listen();
};

main().catch(console.log);

License