Needlessly Provoking Marsupials

    koatty
    TypeScript icon, indicating that this package has built-in type declarations

    3.6.8 • Public • Published

    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: any;
      beforeAll(async () => {
        jest.useFakeTimers();
        const appInstance = await ExecBootStrap()(App);
        server = await appInstance.listen();
      });
    
      afterAll(done => {
        server.close();
        done();
      });
    
      it('request', async () => {
        const rsp = await request(server).get('/');
        expect(rsp.status).toBe(200);
      });
    });

    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.

    Dependents (1)

    Install

    npm i koatty

    DownloadsWeekly Downloads

    51

    Version

    3.6.8

    License

    BSD-3-Clause

    Unpacked Size

    115 kB

    Total Files

    13

    Last publish

    Collaborators

    • richenlin