diff --git a/README.md b/README.md index fda6d1f..bd922e4 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,8 @@ lightweight. You can use the projects you need in your web application. - [Core](https://github.com/rogelio-o/lambda-framework) - [AWS Lambda implementation](https://github.com/rogelio-o/lambda-framework-aws) -- [DustJS template engine implementation for Lambda Framework](https://github.com/rogelio-o/lambda-framework-dustjs) +- [Google Cloud Functions implementation](https://github.com/rogelio-o/lambda-framework-gcloud) +- [DustJS template engine implementation](https://github.com/rogelio-o/lambda-framework-dustjs) - [Website](https://github.com/rogelio-o/lambda-framework-website) - [Website Resources](https://github.com/rogelio-o/lambda-framework-website-resources) @@ -60,7 +61,8 @@ import { AWSHandler } from "lambda-framework-aws"; const app: IApp = new App(); ... -export.handler = AWSHandler(app); +const handler: AWSHandler = new AWSHandler(app); +export.handler = handler.handle; ``` ### Event handling diff --git a/src/lib/App.ts b/src/lib/App.ts index f865b81..5dccc99 100644 --- a/src/lib/App.ts +++ b/src/lib/App.ts @@ -65,7 +65,7 @@ export default class App implements IApp { this._router.httpHandle(req, res, done); } else { const req: IEventRequest = new EventRequest(event); - const done = eventFinalHandler(req, { + const done = eventFinalHandler(req, callback, { env: this.get(configuration.ENVIRONMENT), onerror: this.logError.bind(this, req) }); diff --git a/src/lib/event/eventFinalHandler.ts b/src/lib/event/eventFinalHandler.ts index e3bdf77..0bcbd88 100644 --- a/src/lib/event/eventFinalHandler.ts +++ b/src/lib/event/eventFinalHandler.ts @@ -1,5 +1,6 @@ import IEventRequest from "./../types/event/IEventRequest"; import INext from "./../types/INext"; +import IRawCallback from "./../types/IRawCallback"; /** * The final handler to be executed if no previous handler has stopped @@ -9,7 +10,7 @@ import INext from "./../types/INext"; * @param {[name: string]: any} options the options of the final handler. * @return {[INext]} */ -export default function eventFinalHandler(req: IEventRequest, options: {[name: string]: any}): INext { +export default function eventFinalHandler(req: IEventRequest, callback: IRawCallback, options: {[name: string]: any}): INext { const opts = options || {}; // get error callback @@ -20,5 +21,9 @@ export default function eventFinalHandler(req: IEventRequest, options: {[name: s if (onerror) { setImmediate(() => onerror(err, req)); } + + if (callback) { + callback.finalize(); + } }; } diff --git a/src/lib/types/IRawCallback.ts b/src/lib/types/IRawCallback.ts index 81abc06..739e81b 100644 --- a/src/lib/types/IRawCallback.ts +++ b/src/lib/types/IRawCallback.ts @@ -7,4 +7,6 @@ export default interface IRawCallback { send(statusCode: number, headers: {[name: string]: string|string[]}, body: object|Buffer): void; + finalize(): void; + } diff --git a/test/event/eventFinalHandler.spec.ts b/test/event/eventFinalHandler.spec.ts index 093999d..800a4e8 100644 --- a/test/event/eventFinalHandler.spec.ts +++ b/test/event/eventFinalHandler.spec.ts @@ -1,6 +1,8 @@ /* tslint:disable:no-unused-expression */ +import * as Chai from "chai"; import eventFinalHandler from "./../../src/lib/event/eventFinalHandler"; import EventRequest from "./../../src/lib/event/EventRequest"; +import DefaultCallback from "./../utils/DefaultCallback"; import otherEvent from "./../utils/otherEvent"; /** @@ -19,7 +21,7 @@ describe("eventFinalHandler", () => { done(); } }; - const handler = eventFinalHandler(req, options); + const handler = eventFinalHandler(req, null, options); handler(new Error()); }); @@ -29,13 +31,21 @@ describe("eventFinalHandler", () => { done(); } }; - const handler = eventFinalHandler(req, options); + const handler = eventFinalHandler(req, null, options); handler(); }); it("should do nothing without errors if no #onerror handler is given.", () => { - const handler = eventFinalHandler(req, null); + const handler = eventFinalHandler(req, null, null); handler(); }); + it("should calls the finalize method of raw callback if it exists.", () => { + const rawCallback: DefaultCallback = new DefaultCallback(); + const handler = eventFinalHandler(req, rawCallback, null); + handler(); + + Chai.expect(rawCallback.isFinalized).to.be.true; + }); + }); diff --git a/test/utils/DefaultCallback.ts b/test/utils/DefaultCallback.ts index 668b3f9..54d1342 100644 --- a/test/utils/DefaultCallback.ts +++ b/test/utils/DefaultCallback.ts @@ -11,6 +11,8 @@ export default class DefaultCallback implements IRawCallback { private _callback: () => void; + private _isFinalized: boolean; + get errorResult(): Error { return this._errorResult; } @@ -19,6 +21,10 @@ export default class DefaultCallback implements IRawCallback { return this._successResult; } + get isFinalized(): boolean { + return this._isFinalized; + } + public sendError(error: Error): void { this._errorResult = error; } @@ -34,4 +40,8 @@ export default class DefaultCallback implements IRawCallback { this._callback = callback; } + public finalize(): void { + this._isFinalized = true; + } + }