Skip to content

fix: koa deps #963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ import 'reflect-metadata';

**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 @@ -808,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
4 changes: 2 additions & 2 deletions docs/lang/chinese/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,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
4 changes: 2 additions & 2 deletions lang/chinese/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,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
2 changes: 1 addition & 1 deletion src/RoutingControllersOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CurrentUserChecker } from './CurrentUserChecker';
export interface RoutingControllersOptions {
/**
* Indicates if cors are enabled.
* This requires installation of additional module (cors for express and kcors for koa).
* This requires installation of additional module (cors for express and @koa/cors for koa).
*/
cors?: boolean | Object;

Expand Down
2 changes: 1 addition & 1 deletion src/driver/BaseDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export abstract class BaseDriver {

/**
* Indicates if cors are enabled.
* This requires installation of additional module (cors for express and kcors for koa).
* This requires installation of additional module (cors for express and @koa/cors for koa).
*/
cors?: boolean | Object;

Expand Down
37 changes: 8 additions & 29 deletions src/driver/koa/KoaDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class KoaDriver extends BaseDriver {
this.koa.use(bodyParser());
if (this.cors) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const cors = require('kcors');
const cors = require('@koa/cors');
if (this.cors === true) {
this.koa.use(cors());
} else {
Expand Down Expand Up @@ -121,9 +121,6 @@ export class KoaDriver extends BaseDriver {
.forEach(param => {
defaultMiddlewares.push(multer(param.extraOptions).array(param.name));
});

// eslint-disable-next-line @typescript-eslint/unbound-method
defaultMiddlewares.push(this.fixMulterRequestAssignment);
}

// user used middlewares
Expand Down Expand Up @@ -353,7 +350,7 @@ export class KoaDriver extends BaseDriver {
}

/**
* Dynamically loads koa and required koa-router module.
* Dynamically loads koa module.
*/
protected loadKoa() {
if (require) {
Expand All @@ -370,16 +367,16 @@ export class KoaDriver extends BaseDriver {
}

/**
* Dynamically loads koa-router module.
* Dynamically loads @koa/router module.
*/
private loadRouter() {
if (require) {
if (!this.router) {
try {
this.router = new (require('koa-router'))();
this.router = new (require('@koa/router'))();
} catch (e) {
throw new Error(
'koa-router package was not found installed. Try to install it: npm install koa-router@next --save'
'@koa/router package was not found installed. Try to install it: npm install @koa/router --save'
);
}
}
Expand All @@ -389,31 +386,13 @@ export class KoaDriver extends BaseDriver {
}

/**
* Dynamically loads koa-multer module.
* Dynamically loads @koa/multer module.
*/
private loadMulter() {
try {
return require('koa-multer');
return require('@koa/multer');
} catch (e) {
throw new Error('koa-multer package was not found installed. Try to install it: npm install koa-multer --save');
}
}

/**
* This middleware fixes a bug on koa-multer implementation.
*
* This bug should be fixed by koa-multer PR #15: https://github.com/koa-modules/multer/pull/15
*/
private async fixMulterRequestAssignment(ctx: any, next: Function) {
if ('request' in ctx) {
if (ctx.req.body) ctx.request.body = ctx.req.body;
if (ctx.req.file) ctx.request.file = ctx.req.file;
if (ctx.req.files) {
ctx.request.files = ctx.req.files;
ctx.files = ctx.req.files;
}
throw new Error('@koa/multer package was not found installed. Try to install it: npm install @koa/multer --save');
}

return await next();
}
}