1.0.5 • Published 4 years ago

http-decorators v1.0.5

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

http-decorators

This project is a typescript library. It will provide some common HTTP method decorators, such as @Control(), @Get(), @post(), @Delete(), @Put(). For details, please refer to the declaration file. We can use these decorators to combine routes as gracefully as a nestjs.

Install

npm i http-decorators

Usage

import { Control, HttpDecrator, Get, Put } from 'http-decorators';

@Control('foo')
class Foo {
  private name: string;

  @Get('name')
  getName() {
    return this.name;
  }

  @Put('name')
  setName(_name: string) {
    this.name = _name;
  }
}

const httpDecrator = new HttpDecrator(Foo, { prefix: '/api' });

const routerArr = httpDecrator.getRouters();

console.log(routerArr);

image-20200122171744559

Use with Egg

$ mkdir showcase && cd showcase
$ npm init egg --type=ts
$ npm i
// home.ts
import { Control, Get } from 'http-decorators'
import { Controller } from 'egg';

@Control('home')
export default class HomeController extends Controller {
  @Get()
  public async index() {
    const { ctx } = this;
    ctx.body = await ctx.service.test.sayHi('egg');
  }

  @Get('http-decorator')
  public async httpDecorator() {
    const { ctx } = this;
    ctx.body = await ctx.service.test.sayHi('http-decorator');
  }
}
// router.ts
import { Application } from 'egg';
import { HttpDecrator } from 'http-decorators'
import { readdirSync } from 'fs';
import * as path from 'path';

export default (app: Application) => {
  const { controller, router } = app;
  const controlDir = path.resolve(__dirname, './controller');
  const fileNameArr = readdirSync(controlDir);

  for (const controlFileName of fileNameArr) {
    const controlFilePath = path.join(controlDir, controlFileName);
    const target = require(controlFilePath).default;
    const {name: nameNoFromat} = path.parse(controlFilePath);
    const httpDecIns = new HttpDecrator(target, { prefix: '/api' });
    const routerArr = httpDecIns.getRouters();

    console.log(Date.now(), routerArr);

    for (const it of routerArr) {
      router[it.method](it.path, controller[nameNoFromat][it.member]);
    }
  }
};
npm run dev

image-20200122171456787

Contributing

Feel free to dive in! Open an issue or submit PRs.

License

MIT © Richard McRichface

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago