Skip to content

Commit 5c7b065

Browse files
authored
Merge pull request #23 from rogelio-o/develop
Develop
2 parents fde7f2a + 945148c commit 5c7b065

6 files changed

Lines changed: 36 additions & 7 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ lightweight. You can use the projects you need in your web application.
2020

2121
- [Core](https://github.com/rogelio-o/lambda-framework)
2222
- [AWS Lambda implementation](https://github.com/rogelio-o/lambda-framework-aws)
23-
- [DustJS template engine implementation for Lambda Framework](https://github.com/rogelio-o/lambda-framework-dustjs)
23+
- [Google Cloud Functions implementation](https://github.com/rogelio-o/lambda-framework-gcloud)
24+
- [DustJS template engine implementation](https://github.com/rogelio-o/lambda-framework-dustjs)
2425
- [Website](https://github.com/rogelio-o/lambda-framework-website)
2526
- [Website Resources](https://github.com/rogelio-o/lambda-framework-website-resources)
2627

@@ -60,7 +61,8 @@ import { AWSHandler } from "lambda-framework-aws";
6061

6162
const app: IApp = new App();
6263
...
63-
export.handler = AWSHandler(app);
64+
const handler: AWSHandler = new AWSHandler(app);
65+
export.handler = handler.handle;
6466
```
6567

6668
### Event handling

src/lib/App.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export default class App implements IApp {
6565
this._router.httpHandle(req, res, done);
6666
} else {
6767
const req: IEventRequest = new EventRequest(event);
68-
const done = eventFinalHandler(req, {
68+
const done = eventFinalHandler(req, callback, {
6969
env: this.get(configuration.ENVIRONMENT),
7070
onerror: this.logError.bind(this, req)
7171
});

src/lib/event/eventFinalHandler.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import IEventRequest from "./../types/event/IEventRequest";
22
import INext from "./../types/INext";
3+
import IRawCallback from "./../types/IRawCallback";
34

45
/**
56
* The final handler to be executed if no previous handler has stopped
@@ -9,7 +10,7 @@ import INext from "./../types/INext";
910
* @param {[name: string]: any} options the options of the final handler.
1011
* @return {[INext]}
1112
*/
12-
export default function eventFinalHandler(req: IEventRequest, options: {[name: string]: any}): INext {
13+
export default function eventFinalHandler(req: IEventRequest, callback: IRawCallback, options: {[name: string]: any}): INext {
1314
const opts = options || {};
1415

1516
// get error callback
@@ -20,5 +21,9 @@ export default function eventFinalHandler(req: IEventRequest, options: {[name: s
2021
if (onerror) {
2122
setImmediate(() => onerror(err, req));
2223
}
24+
25+
if (callback) {
26+
callback.finalize();
27+
}
2328
};
2429
}

src/lib/types/IRawCallback.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ export default interface IRawCallback {
77

88
send(statusCode: number, headers: {[name: string]: string|string[]}, body: object|Buffer): void;
99

10+
finalize(): void;
11+
1012
}

test/event/eventFinalHandler.spec.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* tslint:disable:no-unused-expression */
2+
import * as Chai from "chai";
23
import eventFinalHandler from "./../../src/lib/event/eventFinalHandler";
34
import EventRequest from "./../../src/lib/event/EventRequest";
5+
import DefaultCallback from "./../utils/DefaultCallback";
46
import otherEvent from "./../utils/otherEvent";
57

68
/**
@@ -19,7 +21,7 @@ describe("eventFinalHandler", () => {
1921
done();
2022
}
2123
};
22-
const handler = eventFinalHandler(req, options);
24+
const handler = eventFinalHandler(req, null, options);
2325
handler(new Error());
2426
});
2527

@@ -29,13 +31,21 @@ describe("eventFinalHandler", () => {
2931
done();
3032
}
3133
};
32-
const handler = eventFinalHandler(req, options);
34+
const handler = eventFinalHandler(req, null, options);
3335
handler();
3436
});
3537

3638
it("should do nothing without errors if no #onerror handler is given.", () => {
37-
const handler = eventFinalHandler(req, null);
39+
const handler = eventFinalHandler(req, null, null);
3840
handler();
3941
});
4042

43+
it("should calls the finalize method of raw callback if it exists.", () => {
44+
const rawCallback: DefaultCallback = new DefaultCallback();
45+
const handler = eventFinalHandler(req, rawCallback, null);
46+
handler();
47+
48+
Chai.expect(rawCallback.isFinalized).to.be.true;
49+
});
50+
4151
});

test/utils/DefaultCallback.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export default class DefaultCallback implements IRawCallback {
1111

1212
private _callback: () => void;
1313

14+
private _isFinalized: boolean;
15+
1416
get errorResult(): Error {
1517
return this._errorResult;
1618
}
@@ -19,6 +21,10 @@ export default class DefaultCallback implements IRawCallback {
1921
return this._successResult;
2022
}
2123

24+
get isFinalized(): boolean {
25+
return this._isFinalized;
26+
}
27+
2228
public sendError(error: Error): void {
2329
this._errorResult = error;
2430
}
@@ -34,4 +40,8 @@ export default class DefaultCallback implements IRawCallback {
3440
this._callback = callback;
3541
}
3642

43+
public finalize(): void {
44+
this._isFinalized = true;
45+
}
46+
3747
}

0 commit comments

Comments
 (0)