Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/chicunic/koa-ts-demo

Demo of Koa with TypeScript (tsoa & swagger UI)
https://github.com/chicunic/koa-ts-demo

koa swagger tsoa typescript

Last synced: 16 days ago
JSON representation

Demo of Koa with TypeScript (tsoa & swagger UI)

Awesome Lists containing this project

README

        

# koa-ts-demo

Demo of Koa with TypeScript (tsoa & swagger UI)

## Usage

Start with development mode:

```bash
pnpm run start:dev
```

Start with production mode:

```bash
pnpm run start:prod
```

Format code:

```bash
pnpm run format
```

Lint code:

```bash
pnpm run lint
```

Build code:

```bash
pnpm run build
```

## Router

How to add a new router (e.g. `/users`):

1. Create a new file `usersController.ts` in `src/users/` folder. (e.g.`src/users/UsersController.ts`)
2. Add a new router in `usersController.ts` file with `@Route('users')` decorator.
3. Add a new class `UsersController` in `usesrsController` folder extends `Controller` class from `tsoa` package.
4. Use `@Get()` decorator to add a new GET method or use `@Post()` decorator to add a new POST method, etc.

## Swagger

The swagger.json file is generated by `tsoa` package. The swagger UI is served by `koa2-swagger-ui` package.

How to generate swagger.json:

```bash
pnpm run build:swagger
```

This command will generate `swagger.json` file in `dist/static` folder and `routes.ts` file in `src/routes` folder. Please and these two files to `.gitignore` file and **DO NOT** commit them to git.

The development mode will generate `swagger.json` and `routes.ts` automatically so you don't need to run this command.

The `swagger.json` file is used by swagger UI to display the swagger UI page. The `routes.ts` file is used by `@koa/router` in Koa.

If you want to show the swagger UI page, please visit `http://localhost:8080/api/v1` in your browser.