1.2.2 • Published 9 months ago

@andrewcaires/express v1.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

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 { Application, Argument, Body, Context, Controller, Ctx, Delete, Extends, Get, Middleware, Params, Post, Put } from ".";

@Controller("/test")
class TestController {

  public middleware(
    @Ctx() ctx: Context,
    @Argument() arg: string
  ): void {

    console.log("middleware", arg);

    ctx.next();
  }

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

    ctx.json(body);
  }

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

    ctx.json([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.json({ 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.json({ 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);

Links

License

1.2.2

9 months ago

1.2.1

9 months ago

1.2.0

9 months ago

1.1.11

10 months ago

1.1.9

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.10

1 year ago

1.1.4

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago