Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/lib/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
Expand Down
7 changes: 6 additions & 1 deletion src/lib/event/eventFinalHandler.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -20,5 +21,9 @@ export default function eventFinalHandler(req: IEventRequest, options: {[name: s
if (onerror) {
setImmediate(() => onerror(err, req));
}

if (callback) {
callback.finalize();
}
};
}
2 changes: 2 additions & 0 deletions src/lib/types/IRawCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ export default interface IRawCallback {

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

finalize(): void;

}
16 changes: 13 additions & 3 deletions test/event/eventFinalHandler.spec.ts
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand All @@ -19,7 +21,7 @@ describe("eventFinalHandler", () => {
done();
}
};
const handler = eventFinalHandler(req, options);
const handler = eventFinalHandler(req, null, options);
handler(new Error());
});

Expand All @@ -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;
});

});
10 changes: 10 additions & 0 deletions test/utils/DefaultCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export default class DefaultCallback implements IRawCallback {

private _callback: () => void;

private _isFinalized: boolean;

get errorResult(): Error {
return this._errorResult;
}
Expand All @@ -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;
}
Expand All @@ -34,4 +40,8 @@ export default class DefaultCallback implements IRawCallback {
this._callback = callback;
}

public finalize(): void {
this._isFinalized = true;
}

}