Skip to content

Commit 8395465

Browse files
committed
Merge branch 'Elassyo-master' into next
2 parents 9a4befe + 311f2fb commit 8395465

File tree

7 files changed

+71
-37
lines changed

7 files changed

+71
-37
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ There are set of prepared errors you can use:
733733
* UnauthorizedError
734734

735735

736-
You can also create and use your own errors by extending `HttpError` class.
736+
You can also create and use your own errors by extending `HttpError` class.
737737
To define the data returned to the client, you could define a toJSON method in your error.
738738

739739
```typescript
@@ -755,7 +755,7 @@ class DbError extends HttpError {
755755
}
756756
}
757757
}
758-
```
758+
```
759759

760760
#### Enable CORS
761761

@@ -796,7 +796,7 @@ app.listen(3000);
796796

797797
#### Default settings
798798

799-
You can override default status code in routing-controllers options.
799+
You can override default status code in routing-controllers options.
800800

801801
```typescript
802802
import "reflect-metadata";
@@ -809,9 +809,9 @@ const app = createExpressServer({
809809
//with this option, null will return 404 by default
810810
nullResultCode: 404,
811811

812-
//with this option, void or Promise<void> will return 204 by default
812+
//with this option, void or Promise<void> will return 204 by default
813813
undefinedResultCode: 204,
814-
814+
815815
paramOptions: {
816816
//with this option, argument will be required by default
817817
required: true
@@ -1486,7 +1486,8 @@ export class QuestionController {
14861486
| `@Patch(route: string\|RegExp)` | `@Patch("/users/:id") patch()` | Methods marked with this decorator will register a request made with PATCH HTTP Method to a given route. In action options you can specify if action should response json or regular text response. | `app.patch("/users/:id", patch)` |
14871487
| `@Delete(route: string\|RegExp)` | `@Delete("/users/:id") delete()` | Methods marked with this decorator will register a request made with DELETE HTTP Method to a given route. In action options you can specify if action should response json or regular text response. | `app.delete("/users/:id", delete)` |
14881488
| `@Head(route: string\|RegExp)` | `@Head("/users/:id") head()` | Methods marked with this decorator will register a request made with HEAD HTTP Method to a given route. In action options you can specify if action should response json or regular text response. | `app.head("/users/:id", head)` |
1489-
| `@Method(methodName: string, route: string\|RegExp)` | `@Method("move", "/users/:id") move()` | Methods marked with this decorator will register a request made with given `methodName` HTTP Method to a given route. In action options you can specify if action should response json or regular text response. | `app.move("/users/:id", move)` |
1489+
| `@All(route: string\|RegExp)` | `@All("/users/me") rewrite()` | Methods marked with this decorator will register a request made with any HTTP Method to a given route. In action options you can specify if action should response json or regular text response. | `app.all("/users/me", rewrite)` |
1490+
| `@Method(methodName: string, route: string\|RegExp)` | `@Method("move", "/users/:id") move()` | Methods marked with this decorator will register a request made with given `methodName` HTTP Method to a given route. In action options you can specify if action should response json or regular text response. | `app.move("/users/:id", move)` |
14901491

14911492
#### Method Parameter Decorators
14921493

src/decorator/All.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {getMetadataArgsStorage} from "../index";
2+
3+
/**
4+
* Registers an action to be executed when a request comes on a given route.
5+
* Must be applied on a controller action.
6+
*/
7+
export function All(route?: RegExp): Function;
8+
9+
/**
10+
* Registers an action to be executed when a request comes on a given route.
11+
* Must be applied on a controller action.
12+
*/
13+
export function All(route?: string): Function;
14+
15+
/**
16+
* Registers an action to be executed when a request comes on a given route.
17+
* Must be applied on a controller action.
18+
*/
19+
export function All(route?: string|RegExp): Function {
20+
return function (object: Object, methodName: string) {
21+
getMetadataArgsStorage().actions.push({
22+
type: "all",
23+
target: object.constructor,
24+
method: methodName,
25+
route: route
26+
});
27+
};
28+
}

src/driver/express/ExpressDriver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export class ExpressDriver extends BaseDriver {
165165
// This causes a double action execution on our side, which results in an unhandled rejection,
166166
// saying: "Can't set headers after they are sent".
167167
// The following line skips action processing when the request method does not match the action method.
168-
if (request.method.toLowerCase() !== actionMetadata.type)
168+
if (actionMetadata.type !== "all" && request.method.toLowerCase() !== actionMetadata.type)
169169
return next();
170170

171171
return executeCallback({request, response, next});
@@ -207,7 +207,7 @@ export class ExpressDriver extends BaseDriver {
207207

208208
case "session-param":
209209
return request.session[param.name];
210-
210+
211211
case "session":
212212
return request.session;
213213

src/driver/koa/KoaDriver.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,7 @@ export class KoaDriver extends BaseDriver {
255255
options.response.body = Buffer.from(result as any);
256256
}
257257
else { // send regular result
258-
if (result instanceof Object) {
259-
options.response.body = result;
260-
} else {
261-
options.response.body = result;
262-
}
258+
options.response.body = result;
263259
}
264260

265261
// set http status code
@@ -329,24 +325,11 @@ export class KoaDriver extends BaseDriver {
329325
const middlewareFunctions: Function[] = [];
330326
uses.forEach(use => {
331327
if (use.middleware.prototype && use.middleware.prototype.use) { // if this is function instance of MiddlewareInterface
332-
middlewareFunctions.push((context: any, next: (err?: any) => Promise<any>) => {
328+
middlewareFunctions.push(async (context: any, next: (err?: any) => Promise<any>) => {
333329
try {
334-
const useResult = (getFromContainer(use.middleware, { context } as Action) as KoaMiddlewareInterface).use(context, next);
335-
if (isPromiseLike(useResult)) {
336-
useResult.catch((error: any) => {
337-
this.handleError(error, undefined, {
338-
request: context.req,
339-
response: context.res,
340-
context,
341-
next
342-
});
343-
return error;
344-
});
345-
}
346-
347-
return useResult;
330+
return await (getFromContainer(use.middleware) as KoaMiddlewareInterface).use(context, next);
348331
} catch (error) {
349-
this.handleError(error, undefined, {
332+
return await this.handleError(error, undefined, {
350333
request: context.request,
351334
response: context.response,
352335
context,
@@ -424,4 +407,4 @@ export class KoaDriver extends BaseDriver {
424407

425408
return await next();
426409
}
427-
}
410+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {importClassesFromDirectories} from "./util/importClassesFromDirectories"
1414

1515
export * from "./container";
1616

17+
export * from "./decorator/All";
1718
export * from "./decorator/Authorized";
1819
export * from "./decorator/Body";
1920
export * from "./decorator/BodyParam";

src/metadata/types/ActionType.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* Controller action type.
33
*/
4-
export type ActionType = "checkout"
4+
export type ActionType = "all"
5+
|"checkout"
56
|"connect"
67
|"copy"
78
|"delete"
@@ -26,4 +27,4 @@ export type ActionType = "checkout"
2627
|"subscribe"
2728
|"trace"
2829
|"unlock"
29-
|"unsubscribe";
30+
|"unsubscribe";

test/functional/koa-middlewares.spec.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,19 @@ describe("koa middlewares", () => {
6868

6969
class TestCustomMiddlewareWhichThrows implements KoaMiddlewareInterface {
7070

71-
use(request: any, response: any, next?: Function): any {
71+
use(context: any, next?: Function): any {
7272
throw new NotAcceptableError("TestCustomMiddlewareWhichThrows");
7373
}
7474

7575
}
7676

77+
class TestCustomAsyncMiddlewareWhichThrows implements KoaMiddlewareInterface {
78+
79+
async use(context: any, next?: Function): Promise<any> {
80+
throw new NotAcceptableError("TestCustomAsyncMiddlewareWhichThrows");
81+
}
82+
83+
}
7784
@Controller()
7885
class KoaMiddlewareController {
7986

@@ -127,9 +134,15 @@ describe("koa middlewares", () => {
127134
return "1234";
128135
}
129136

130-
@Get("/customMiddlewareWichThrows")
137+
@Get("/customMiddlewareWhichThrows")
131138
@UseBefore(TestCustomMiddlewareWhichThrows)
132-
customMiddlewareWichThrows() {
139+
customMiddlewareWhichThrows() {
140+
return "1234";
141+
}
142+
143+
@Get("/customAsyncMiddlewareWhichThrows")
144+
@UseBefore(TestCustomAsyncMiddlewareWhichThrows)
145+
TestCustomAsyncMiddlewareWhichThrows() {
133146
return "1234";
134147
}
135148

@@ -193,10 +206,17 @@ describe("koa middlewares", () => {
193206

194207
it("should handle errors in custom middlewares", () => {
195208
return chakram
196-
.get("http://127.0.0.1:3001/customMiddlewareWichThrows")
209+
.get("http://127.0.0.1:3001/customMiddlewareWhichThrows")
197210
.then((response: any) => {
198211
expect(response).to.have.status(406);
199212
});
200213
});
201214

202-
});
215+
it("should handle errors in custom async middlewares", () => {
216+
return chakram
217+
.get("http://127.0.0.1:3001/customAsyncMiddlewareWhichThrows")
218+
.then((response: any) => {
219+
expect(response).to.have.status(406);
220+
});
221+
});
222+
});

0 commit comments

Comments
 (0)