Skip to content

Commit

Permalink
Dependencies update (#9)
Browse files Browse the repository at this point in the history
* Updated minor dependencies

* Updated dependencies
Added types in general

* Bumped version

* Fixed package vulnerabilities
  • Loading branch information
seidelmartin authored Mar 19, 2021
1 parent 210af85 commit 8eef527
Show file tree
Hide file tree
Showing 12 changed files with 2,203 additions and 2,017 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"extends": [
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
"prettier/@typescript-eslint"
"prettier"
],
"plugins": [
"@typescript-eslint/eslint-plugin",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ After installation is done you can set CORS same way as in normal NEST applicati
```typescript
const app = NestFactory.create<NestKoaApplication>(AppModule, new KoaAdapter());

app.enableCors();
app.enableCors({});

await app.init();
```
Expand Down
4,085 changes: 2,127 additions & 1,958 deletions package-lock.json

Large diffs are not rendered by default.

49 changes: 25 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nest-koa-adapter",
"version": "1.3.1",
"version": "1.4.0",
"description": "Koa HTTP adapter for Nest.js",
"main": "dist/index.js",
"files": [
Expand All @@ -26,9 +26,10 @@
"author": "Martin Seidel",
"license": "MIT",
"dependencies": {
"koa": "^2.11.0",
"@babel/helper-validator-identifier": "^7.12.11",
"koa": "^2.13.1",
"koa-bodyparser": "^4.3.0",
"koa-router": "^8.0.8"
"koa-router": "^10.0.0"
},
"peerDependencies": {
"@nestjs/common": ">=6.x <=7.x",
Expand All @@ -41,33 +42,33 @@
},
"devDependencies": {
"@koa/cors": "^3.0.0",
"@nestjs/common": "^7.0.9",
"@nestjs/core": "^7.0.9",
"@nestjs/testing": "^7.0.9",
"@types/koa": "^2.11.3",
"@nestjs/common": "^7.6.14",
"@nestjs/core": "^7.6.14",
"@nestjs/testing": "^7.6.14",
"@types/koa": "^2.13.1",
"@types/koa-bodyparser": "^4.3.0",
"@types/koa-router": "^7.4.0",
"@types/koa-router": "^7.4.1",
"@types/koa-static": "^4.0.1",
"@types/koa-views": "^2.0.4",
"@types/koa__cors": "^3.0.1",
"@types/mocha": "^7.0.2",
"@types/supertest": "^2.0.9",
"@typescript-eslint/eslint-plugin": "^2.31.0",
"@typescript-eslint/parser": "^2.31.0",
"eslint": "^7.0.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-mocha": "^6.3.0",
"eslint-plugin-prettier": "^3.1.3",
"@types/koa__cors": "^3.0.2",
"@types/mocha": "^8.2.1",
"@types/supertest": "^2.0.10",
"@typescript-eslint/eslint-plugin": "^4.18.0",
"@typescript-eslint/parser": "^4.18.0",
"eslint": "^7.22.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-mocha": "^8.1.0",
"eslint-plugin-prettier": "^3.3.1",
"koa-static": "^5.0.0",
"koa-views": "^6.2.1",
"lodash.template": "^4.5.0",
"mocha": "^7.1.2",
"np": "^6.2.3",
"prettier": "^2.0.5",
"mocha": "^8.3.2",
"np": "^7.4.0",
"prettier": "^2.2.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^6.5.5",
"supertest": "^4.0.2",
"ts-node": "^8.10.1",
"typescript": "^3.8.3"
"rxjs": "^6.6.6",
"supertest": "^6.1.3",
"ts-node": "^9.1.1",
"typescript": "^4.2.3"
}
}
44 changes: 28 additions & 16 deletions src/KoaAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import koaBodyBarser from 'koa-bodyparser';
import * as http from 'http';
import * as https from 'https';
import { RequestHandler } from '@nestjs/common/interfaces';
import { nestToKoaMiddleware } from './NestKoaMiddleware';
import {
NestKoaFunctionalMiddleware,
nestToKoaMiddleware,
} from './NestKoaMiddleware';
import { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface';
import { loadPackage } from '@nestjs/common/utils/load-package.util';
import { KoaCorsOptions } from './KoaCorsOptions';
import { Options as ServeStaticOptions } from 'koa-static';
import { KoaViewsOptions } from './KoaViews';
import { Stream } from 'stream';
import { koaReply } from './KoaReply';

type HttpMethods =
Expand Down Expand Up @@ -159,15 +161,17 @@ export class KoaAdapter extends AbstractHttpAdapter<
return [path as string, handler as KoaHandler];
}

public close() {
return new Promise((resolve) => this.httpServer.close(resolve));
public close(): Promise<void> {
return new Promise((resolve) =>
this.httpServer.close(() => resolve()),
);
}

public getType(): string {
return 'koa';
}

public initHttpServer(options: NestApplicationOptions) {
public initHttpServer(options: NestApplicationOptions): void {
if (options?.httpsOptions) {
this.httpServer = https.createServer(
options.httpsOptions,
Expand Down Expand Up @@ -200,37 +204,43 @@ export class KoaAdapter extends AbstractHttpAdapter<
);
}

public getRequestHostname(request: any) {
public getRequestHostname(request: Koa.Request): string {
return request.hostname;
}

public getRequestMethod(request: any): string {
public getRequestMethod(request: Koa.Request): string {
return request.method;
}

public getRequestUrl(request: any): string {
public getRequestUrl(request: Koa.Request): string {
return request.url;
}

public status(response: any, statusCode: number): any {
public status(response: Koa.Response, statusCode: number): any {
response.status = statusCode;
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
public reply(response: Koa.Response, body: any, statusCode?: number) {
return koaReply(response, body, statusCode);
}

public async render(
response: Koa.Response,
view: string,
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
options: any,
): Promise<void> {
const body = await response.ctx.render(view, options);

this.reply(response, body);
}

public redirect(response: any, statusCode: number, url: string): any {
public redirect(
response: Koa.Response,
statusCode: number,
url: string,
): any {
response.status = statusCode;
response.redirect(url);
response.res.end();
Expand All @@ -250,7 +260,7 @@ export class KoaAdapter extends AbstractHttpAdapter<
this.getInstance<Koa>().use(nestToKoaMiddleware(handler));
}

public setHeader(response: any, name: string, value: string): any {
public setHeader(response: Koa.Response, name: string, value: string): any {
response.set(name, value);
}

Expand All @@ -262,12 +272,12 @@ export class KoaAdapter extends AbstractHttpAdapter<
});
}

public enableCors(options?: CorsOptions): any {
public enableCors(options: CorsOptions): void {
const corsMiddleware = loadPackage('@koa/cors', 'KoaAdapter.enableCors()');

KoaCorsOptions.validateNestOptions(options);

const koaCorsOptions = options && new KoaCorsOptions(options);
const koaCorsOptions = new KoaCorsOptions(options);

if (koaCorsOptions) {
koaCorsOptions.handleOptionsSuccessStatus(
Expand All @@ -280,8 +290,10 @@ export class KoaAdapter extends AbstractHttpAdapter<

public createMiddlewareFactory(
requestMethod: RequestMethod,
): (path: string, callback: Function) => any {
return (path: string, callback: Function) => {
// eslint-disable-next-line @typescript-eslint/ban-types
): (path: string, middleware: Function) => any {
// eslint-disable-next-line @typescript-eslint/ban-types
return (path: string, middleware: Function) => {
const router = this.getRouter();

const routeMethodsMap: Record<RequestMethod, KoaRouteMethods> = {
Expand All @@ -302,7 +314,7 @@ export class KoaAdapter extends AbstractHttpAdapter<
return routeMethod(
path,
(ctx: Koa.ParameterizedContext, next: Koa.Next) =>
callback(ctx.request, ctx.response, next),
middleware(ctx.request, ctx.response, next),
);
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/KoaCorsOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class KoaCorsOptions implements Options {
readonly maxAge?: number | string;
readonly origin?: OriginResolver;

public static validateNestOptions(options: CorsOptions = {}) {
public static validateNestOptions(options: CorsOptions = {}): void {
if (typeof options.origin === 'function') {
throw new Error(
`KoaNestAdapter doesn't support CustomOrigin type as origin option.`,
Expand Down Expand Up @@ -71,7 +71,7 @@ export class KoaCorsOptions implements Options {
public handleOptionsSuccessStatus(
koa: Koa | KoaRouter,
nestOptions: CorsOptions = {},
) {
): void {
const { optionsSuccessStatus } = nestOptions;

if (!optionsSuccessStatus) {
Expand Down
3 changes: 2 additions & 1 deletion src/KoaReply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ function applyOverwrites(

export const koaReply = (
response: Koa.Response,
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
body: any,
statusCode?: number,
) => {
): void => {
const overwrites = getOverwrites(response, statusCode);

response.ctx.respond = false;
Expand Down
4 changes: 2 additions & 2 deletions test/KoaAdapterMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class TestModule {
.apply(ScopedMiddleware)
.forRoutes(HelloWorldController)
.apply(convertedMiddleware)
.forRoutes({ path: '/dummy/p*st', method: RequestMethod.POST })
.forRoutes({ path: '/dummy/p(.*)st', method: RequestMethod.POST })
.apply(globalMiddleware)
.forRoutes('*');
.forRoutes('(.*)');
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/KoaCorsOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe(KoaCorsOptions.name, () => {

describe('without options', () => {
beforeEach(async () => {
app.enableCors();
app.enableCors({});
await app.init();
});

Expand Down Expand Up @@ -103,7 +103,7 @@ describe(KoaCorsOptions.name, () => {
});

it('should return OPTIONS response with default 204 status', async () => {
app.enableCors();
app.enableCors({});
await app.init();

return supertest(app.getHttpServer())
Expand Down
6 changes: 3 additions & 3 deletions test/utils/DummyController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { Body, Controller, Get, Param, Post, Put, Query } from '@nestjs/common';
@Controller({ path: 'dummy' })
export class DummyController {
@Get('query')
getQuery(@Query() query: any) {
getQuery(@Query() query: Record<string, string>): Record<string, string> {
return query;
}

@Post('post')
getPost(@Body() body: any) {
getPost<T>(@Body() body: T): T {
return body;
}

@Put(':param')
getParam(@Param('param') param: string) {
getParam(@Param('param') param: string): string {
return param;
}
}
10 changes: 6 additions & 4 deletions test/utils/HelloWorldController.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { Controller, Get } from '@nestjs/common';
import { of } from 'rxjs';
import { Observable, of } from 'rxjs';

type HelloWorld = { hello: string };

@Controller('hello')
export class HelloWorldController {
@Get('world/sync')
helloSync() {
helloSync(): HelloWorld {
return { hello: 'world-sync' };
}

@Get('world/async')
async helloAsync() {
async helloAsync(): Promise<HelloWorld> {
return { hello: 'world-async' };
}

@Get('world/observable')
helloObservable() {
helloObservable(): Observable<HelloWorld> {
return of({ hello: 'world-observable' });
}
}
6 changes: 4 additions & 2 deletions test/utils/HostFilterController.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { Controller, Get, HostParam } from '@nestjs/common';

type Host = { for: string };

@Controller({
host: ':host.example.com',
})
export class HostFilterController {
@Get('host')
getHost() {
getHost(): Host {
return { for: 'host' };
}

@Get('host/param')
getHostParam(@HostParam('host') host: string) {
getHostParam(@HostParam('host') host: string): Host {
return { for: host };
}
}

0 comments on commit 8eef527

Please sign in to comment.