Skip to content

Commit

Permalink
merge: release v0.10.0 (#962)
Browse files Browse the repository at this point in the history
  • Loading branch information
attilaorosz authored Dec 9, 2022
2 parents f8e00f3 + fe049f7 commit dd93e36
Show file tree
Hide file tree
Showing 49 changed files with 13,389 additions and 11,249 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ extends:
- 'plugin:@typescript-eslint/recommended-requiring-type-checking'
- 'plugin:jest/recommended'
- 'prettier'
- 'prettier/@typescript-eslint'
rules:
'@typescript-eslint/explicit-member-accessibility': off
'@typescript-eslint/no-angle-bracket-type-assertion': off
Expand All @@ -32,3 +31,5 @@ rules:
'@typescript-eslint/no-unsafe-call': off
'@typescript-eslint/no-unsafe-member-access': off
'@typescript-eslint/explicit-module-boundary-types': off
'@typescript-eslint/no-unsafe-argument': off
'@typescript-eslint/no-var-requires': off
7 changes: 4 additions & 3 deletions .github/workflows/continuous-deployment-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ jobs:
name: Publish to NPM
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 'lts/*'
registry-url: https://registry.npmjs.org
- run: npm ci --ignore-scripts
- run: npm run prettier:check
Expand All @@ -23,4 +24,4 @@ jobs:
- run: jq 'del(.devDependencies) | del(.scripts)' package.json > build/package.json
- run: npm publish ./build
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
24 changes: 14 additions & 10 deletions .github/workflows/continuous-integration-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ jobs:
name: Linters
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 'lts/*'
- run: npm ci --ignore-scripts
- run: npm run prettier:check
- run: npm run lint:check
Expand All @@ -15,27 +17,29 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['10.x', '12.x', '14.x']
node-version: ['lts/*', 'current']
fail-fast: false
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3
- name: Setting up Node.js (v${{ matrix.node-version }}.x)
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm ci --ignore-scripts
- run: npm run test:ci
- run: npm install codecov -g
if: ${{ matrix.node-version == '14.x' }}
if: ${{ matrix.node-version == 'current' }}
- run: codecov -f ./coverage/clover.xml -t ${{ secrets.CODECOV_TOKEN }} --commit=$GITHUB_SHA --branch=${GITHUB_REF##*/}
if: ${{ matrix.node-version == '14.x' }}
if: ${{ matrix.node-version == 'current' }}
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 'lts/*'
- run: npm ci --ignore-scripts
- run: npm run build:es2015
- run: npm run build:cjs
- run: npm run build:types
- run: npm run build:types
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
# Changelog and release notes

## [0.10.0](https://github.com/typestack/routing-controllers/compare/v0.9.0...v0.10.0) (2022-12-9)

### Added

- `isArray` option for `@QueryParam`

### Changed

- `class-transformer` package updated to `0.5.1` from `0.3.1`
- `class-validator` package updated to `0.13.2` from `0.12.2`
- `cookie` package updated to `0.5.0` from `0.4.0`
- `glob` package updated to `8.0.3` from `7.1.4`
- `express` package updated to `4.18.2` from `4.17.1` and moved to `optionalDependencies`
- `express-session` package updated to `1.17.1` from `1.17.3` and moved to `optionalDependencies`
- `body-parser` package updated to `1.20.1` from `1.19.0`
- `multer` package updated to `1.4.4` from `1.4.2`
- `koa` package updated to `2.13.4` from `2.8.2`
- `koa-multer` package replaced with `@koa/multer`
- `koa-router` package replaced with `@koa/router`
- updated various dev dependencies

### Fixed

- Fixed parsing uuid as route parameter
- Fixed `target` property not set during controller inheritance
- Fixed `NaN` check for number route parameters
- Added missing null value handling in parameters
- Fixed middlewares not using the defined route prefix

### 0.9.0

#### Features
Expand Down
73 changes: 51 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
[![npm version](https://badge.fury.io/js/routing-controllers.svg)](https://badge.fury.io/js/routing-controllers)
[![Dependency Status](https://david-dm.org/typestack/routing-controllers.svg)](https://david-dm.org/typestack/routing-controllers)

<center>
<span>English</span> |
[中文](./docs/lang/chinese/README.md)
</center>
English | [中文](./docs/lang/chinese/README.md)

Allows to create controller classes with methods as actions that handle requests.
You can use routing-controllers with [express.js][1] or [koa.js][2].
Expand Down Expand Up @@ -87,8 +84,9 @@ You can use routing-controllers with [express.js][1] or [koa.js][2].

and make sure to import it before you use routing-controllers:

```typescript
```
```typescript
import 'reflect-metadata';
```

3. Install framework:

Expand All @@ -102,11 +100,11 @@ You can use routing-controllers with [express.js][1] or [koa.js][2].

**b. If you want to use routing-controllers with _koa 2_, then install it and all required dependencies:**

`npm install koa koa-router koa-bodyparser koa-multer`
`npm install koa @koa/router koa-bodyparser @koa/multer`

Optionally you can also install their typings:

`npm install -D @types/koa @types/koa-router @types/koa-bodyparser`
`npm install -D @types/koa @types/koa-bodyparser`

4. Install peer dependencies:

Expand Down Expand Up @@ -321,9 +319,10 @@ You can load all controllers from directories, by specifying array of directorie

```typescript
import { createExpressServer } from 'routing-controllers';
import path from 'path';

createExpressServer({
controllers: [__dirname + '/controllers/*.js'],
controllers: [path.join(__dirname + '/controllers/*.js')],
}).listen(3000); // register controllers routes in our express application
```

Expand Down Expand Up @@ -378,8 +377,30 @@ getUsers(@QueryParam("limit") limit: number) {
}
```

You can use `isArray` option to get a query param array. This will cast the query param :

```typescript
@Get("/users/by-multiple-ids")
getUsers(@QueryParam("ids", { isArray: true}) ids: string[]) {
}
```

`GET /users/by-multiple-ids?ids=a``ids = ['a']`
`GET /users/by-multiple-ids?ids=a&ids=b``ids = ['a', 'b']`

You can combine use `isArray` option with `type` option to get a query param array of one type. This will cast the query param :

```typescript
@Get("/users/by-multiple-ids")
getUsers(@QueryParam("ids", { isArray: true, type: Number}) ids: number[]) {
}
```

`GET /users/by-multiple-ids?ids=1``ids = [1]`
`GET /users/by-multiple-ids?ids=1&ids=3.5``ids = [1, 3.5]`

If you want to inject all query parameters use `@QueryParams()` decorator.
The bigest benefit of this approach is that you can perform validation of the params.
The biggest benefit of this approach is that you can perform validation of the params.

```typescript
enum Roles {
Expand All @@ -402,12 +423,17 @@ class GetUsersQuery {
@IsBoolean()
isActive: boolean;

@IsArray()
@IsNumber(undefined, { each: true })
@Type(() => Number)
ids: number[];
}

@Get("/users")
getUsers(@QueryParams() query: GetUsersQuery) {
// here you can access query.role, query.limit
// and others valid query parameters
// query.ids will be an array, of numbers, even with one element
}
```

Expand Down Expand Up @@ -510,7 +536,7 @@ You can also specify uploading options to multer this way:

```typescript
// to keep code clean better to extract this function into separate file
export const fileUploadOptions = () => {
export const fileUploadOptions = () => ({
storage: multer.diskStorage({
destination: (req: any, file: any, cb: any) => { ...
},
Expand All @@ -523,7 +549,7 @@ export const fileUploadOptions = () => {
fieldNameSize: 255,
fileSize: 1024 * 1024 * 2
}
};
});

// use options this way:
@Post("/files")
Expand Down Expand Up @@ -782,7 +808,7 @@ app.listen(3000);
```

To use cors you need to install its module.
For express its `npm i cors`, for koa its `npm i kcors`.
For express its `npm i cors`, for koa its `npm i @koa/cors`.
You can pass cors options as well:

```typescript
Expand Down Expand Up @@ -1089,10 +1115,12 @@ Also you can load middlewares from directories. Also you can use glob patterns:

```typescript
import { createExpressServer } from 'routing-controllers';
import path from 'path';
createExpressServer({
controllers: [__dirname + '/controllers/**/*.js'],
middlewares: [__dirname + '/middlewares/**/*.js'],
interceptors: [__dirname + '/interceptors/**/*.js'],
controllers: [path.join(__dirname, '/controllers/**/*.js')],
middlewares: [path.join(__dirname, '/middlewares/**/*.js')],
interceptors: [path.join(__dirname, '/interceptors/**/*.js')],
}).listen(3000);
```

Expand Down Expand Up @@ -1215,7 +1243,7 @@ If you want to disable it simply pass `classTransformer: false` to createExpress

## Controller Inheritance

Often your application may need to have an option to inherit controller from another to reuse code and void duplication.
Often your application may need to have an option to inherit controller from another to reuse code and avoid duplication.
A good example of the use is the CRUD operations which can be hidden inside `AbstractBaseController` with the possibility to add new and overload methods, the template method pattern.

```typescript
Expand All @@ -1227,7 +1255,7 @@ abstract class AbstractControllerTemplate {
@Post()
public create() {}

@Read()
@Get()
public read() {}

@Put()
Expand Down Expand Up @@ -1385,16 +1413,17 @@ Here is example how to integrate routing-controllers with [typedi](https://githu
```typescript
import { createExpressServer, useContainer } from 'routing-controllers';
import { Container } from 'typedi';
import path from 'path';

// its important to set container before any operation you do with routing-controllers,
// including importing controllers
useContainer(Container);

// create and run server
createExpressServer({
controllers: [__dirname + '/controllers/*.js'],
middlewares: [__dirname + '/middlewares/*.js'],
interceptors: [__dirname + '/interceptors/*.js'],
controllers: [path.join(__dirname, '/controllers/*.js')],
middlewares: [path.join(__dirname, '/middlewares/*.js')],
interceptors: [path.join(__dirname, '/interceptors/*.js')],
}).listen(3000);
```

Expand Down Expand Up @@ -1535,7 +1564,7 @@ export class QuestionController {

| Signature | Example | Description |
| ---------------------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| `@Authorized(roles?: string\|string[])` | `@Authorized("SUPER_ADMIN")` get() | Checks if user is authorized and has given roles on a given route. `authorizationChecker` should be defined in routing-controllers options. | |
| `@Authorized(roles?: string\|string[])` | `@Authorized("SUPER_ADMIN")` get() | Checks if user is authorized and has given roles on a given route. `authorizationChecker` should be defined in routing-controllers options. |
| `@CurrentUser(options?: { required?: boolean })` | get(@CurrentUser({ required: true }) user: User) | Injects currently authorized user. `currentUserChecker` should be defined in routing-controllers options. |
| `@Header(headerName: string, headerValue: string)` | `@Header("Cache-Control", "private")` get() | Allows to explicitly set any HTTP header returned in the response. |
| `@ContentType(contentType: string)` | `@ContentType("text/csv")` get() | Allows to explicitly set HTTP Content-Type returned in the response. |
Expand Down
36 changes: 19 additions & 17 deletions docs/lang/chinese/READEME.md → docs/lang/chinese/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
并确认在使用 routing-controllers 前引入

```typescript

```

3. 安装框架:
Expand All @@ -101,11 +102,11 @@

**b. 在 _koa 2_ 中使用 routing-controllers,需要安装以下依赖:**

`npm install koa koa-router koa-bodyparser koa-multer`
`npm install koa @koa/router koa-bodyparser @koa/multer`

可选装它们的类型声明:

`npm install -D @types/koa @types/koa-router @types/koa-bodyparser`
`npm install -D @types/koa @types/koa-bodyparser`

4. 可选依赖

Expand Down Expand Up @@ -944,23 +945,24 @@ app.listen(3000);

第二种,声明一个类:

```typescript
import { KoaMiddlewareInterface } from 'routing-controllers';
```typescript
import { KoaMiddlewareInterface } from "routing-controllers";
export class MyMiddleware implements KoaMiddlewareInterface { // 接口声明可选
use(context: any, next: (err?: any) => Promise<any>): Promise<any> {
console.log("do something before execution...");
return next().then(() => {
console.log("do something after execution");
}).catch(error => {
console.log("error handling is also here");
});
}
export class MyMiddleware implements KoaMiddlewareInterface {
// 接口声明可选
}
```
use(context: any, next: (err?: any) => Promise<any>): Promise<any> {
console.log('do something before execution...');
return next()
.then(() => {
console.log('do something after execution');
})
.catch(error => {
console.log('error handling is also here');
});
}
}
```

2. 应用:

Expand Down
14 changes: 8 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverageFrom: ['src/**/*.ts', '!src/**/index.ts', '!src/**/*.interface.ts'],
globals: {
'ts-jest': {
tsConfig: 'tsconfig.spec.json',
},
},
setupFilesAfterEnv: ["./jest.setup.js"]
globals: {},
setupFilesAfterEnv: ["./jest.setup.js"],
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{tsconfig: './tsconfig.spec.json'},
],
}
};
Loading

0 comments on commit dd93e36

Please sign in to comment.