Skip to content

Commit 8aa9339

Browse files
committed
fix bug that handler return all result.
1 parent 402a9c6 commit 8aa9339

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

__tests__/mysql.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { middleware, MySQLPluginAux } from '../src';
2-
import { getLogger } from '../src/utils';
32

43
test('basic', async () => {
54
type Aux = MySQLPluginAux;
@@ -21,7 +20,7 @@ test('basic', async () => {
2120
}),
2221
]);
2322

24-
await handler(async ({ request, response, aux }) => {
23+
handler(async ({ request, response, aux }) => {
2524
const { db } = aux;
2625
expect(request).toBeDefined();
2726
expect(response).toBeDefined();

src/middleware/build.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class HandlerProxy<A extends HandlerAuxBase> {
5353
public call = async (
5454
middleware: HandlerMiddleware<A>,
5555
handler: Handler<A>,
56-
): Promise<any[]> => {
56+
) => {
5757
try {
5858
this.aux = await middleware.auxPromise;
5959
} catch (error) {
@@ -85,20 +85,23 @@ class HandlerProxy<A extends HandlerAuxBase> {
8585
handlers.map(each => this.safeCall(each, okResponsible, errorHandlers)),
8686
);
8787

88-
const result = [
88+
const results = [
8989
...(await iterate(beginHandlers)),
9090
...(await iterate(actualHandler, true)),
9191
...(await iterate(endHandlers)),
92-
];
92+
].filter(x => x);
9393
// In test phase, throws any exception if there was.
9494
if (process.env.NODE_ENV === 'test') {
95-
for (const each of result) {
95+
for (const each of results) {
9696
if (each instanceof Error) {
97+
logger.error(`Error occurred: ${stringifyError(each)}`);
9798
throw each;
9899
}
99100
}
100101
}
101-
return result;
102+
results.forEach(result =>
103+
logger.silly(`middleware result : ${JSON.stringify(result)}`),
104+
);
102105
};
103106

104107
private safeCall = async (
@@ -159,11 +162,12 @@ const build = <Aux extends HandlerAuxBase>(
159162
plugins: Array<HandlerPluginBase<any>>,
160163
) => {
161164
const middleware = new HandlerMiddleware<Aux>(plugins);
162-
return (handler: Handler<Aux>) => async (
165+
return (handler: Handler<Aux>) => (
163166
event: any,
164167
context: any,
165168
callback: any,
166-
) =>
169+
) => {
167170
new HandlerProxy<Aux>(event, context, callback).call(middleware, handler);
171+
};
168172
};
169173
export default build;

0 commit comments

Comments
 (0)