koatty

Koa2 + Typescript = koatty. Use Typescript's decorator implement auto injection.

Usage no npm install needed!

<script type="module">
  import koatty from 'https://cdn.skypack.dev/koatty';
</script>

README

koatty

Koa2 + Typescript + IOC = koatty.

Use Typescript's decorator implement IOC and AOP.

Version npmnpm Downloads

New features

  • HTTP、HTTPS、HTTP2、gRPC、WebSocket server.
  • Support loading environment configuration, parsing command line parameters (process. argv) and environment variables (process.env)
  • @ExceptionHandler() Register global exception handling

Documentation

koatty_doc_CN (In progress💪)

Installation

npm i -g koatty_cli

Quick Start

1.Create Project

koatty new projectName

cd ./projectName

yarn install

npm start

2.Create a Controller

koatty controller test

3.Create a Service

koatty service test

4.Create a DTOClass (Optional)

koatty dto test

5.Define TestController

import { Controller, BaseController, Autowired, GetMapping, RequestBody, PathVariable,
 PostMapping, RequestMapping, RequestMethod, Valid } from "koatty";
import { TestDTO } from "../model/dto/TestDTO";
import { TestService } from "../service/TestService";
import { App } from "../App";

@Controller()
export class IndexController extends BaseController {
    app: App;

    @Autowired()
    private testService: TestService;

    init() {
        this.cache = {};
    }

    @RequestMapping("/:name", RequestMethod.ALL)
    async default(@PathVariable("name") @Valid("IsNotEmpty") name: string) {
        try {
            const info = await this.testService.sayHello(name);
            return this.ok("success", info);
        } catch (err: Error) {
            return this.fail(err.message));
        }
    }

    @PostMapping("/test")
    @Validated() //need DTOClass
    test(@RequestParam() params: TestDTO) {
        return this.ok("test", params);
    }
}

How to do Unit Testing

now only support jest UT framework

import request from 'supertest';
import { ExecBootStrap } from 'koatty';

import { App } from '../src/App';

describe('UT example', () => {

  let server;
  beforeAll(async () => {
    const appInstance = await ExecBootStrap()(App);
    server = appInstance.callback();
  });

  it('request', async (done) => {
    const rsp = await request(server).get('/path/to/server');
    expect(rsp.status).toBe(200);
    done();
  });
});

How to debug

if you use vscode , edit the .vscode/launch.json , like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "TS Program",
            "args": [
                "${workspaceRoot}/src/App.ts" 
            ],
            "runtimeArgs": [
                "--nolazy",
                "-r",
                "ts-node/register"
            ],
            "sourceMaps": true,
            "cwd": "${workspaceRoot}",
            "protocol": "inspector",
            "internalConsoleOptions": "neverOpen"
        }
    ]
}

Select TS Program to debug run. Try to call http://localhost:3000/ .

Example

Check out the quick start example.