Skip to content

Commit

Permalink
Finalize and added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
rogelio-o committed Dec 23, 2017
1 parent 7bd9560 commit a00d215
Show file tree
Hide file tree
Showing 13 changed files with 2,137 additions and 236 deletions.
1,868 changes: 1,734 additions & 134 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lambda-framework-aws",
"version": "1.0.0",
"version": "1.0.6",
"description": "AWS Lambda implementation of Lambda Framework",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand All @@ -15,7 +15,7 @@
"test": "nyc --reporter=html --reporter=text mocha --recursive dist/test/",
"posttest": "npm run lint && nyc check-coverage --statements 70 --branches 70 --functions 70 --lines 70",
"compile": "tsc",
"lint": "tslint --config tslint.json --type-check --project tsconfig.json src/{,**/,**/**/,**/**/**/}*.ts",
"lint": "tslint --config tslint.json --project tsconfig.json",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"prepublish": "npm-auto-version"
},
Expand All @@ -31,17 +31,25 @@
"@types/chai": "^4.0.4",
"@types/mocha": "^2.2.43",
"@types/node": "^8.0.28",
"@types/sinon": "^2.3.7",
"chai": "^4.1.2",
"coveralls": "^3.0.0",
"mocha": "^3.5.3",
"mock-fs": "^4.4.2",
"nyc": "^11.3.0",
"sinon": "^4.0.2",
"tslint": "^5.7.0",
"tslint-microsoft-contrib": "^5.0.1",
"typescript": "^2.5.2"
},
"dependencies": {
"@types/aws-lambda": "0.0.21",
"aws-sdk": "^2.161.0",
"lambda-framework": "^1.0.18"
"lambda-framework": "^1.0.19"
},
"nyc": {
"exclude": [
"**/*.spec.js"
]
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
*/

export { default } from "./lib/AWSHandler";
export { default as AWSTransformer } from "./lib/AWSTransformer";
export { default as S3TemplateLoader } from "./lib/S3TemplateLoader";
29 changes: 20 additions & 9 deletions src/lib/AWSHandler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { Callback, Context } from "aws-lambda";
import { IApp, IRawCallback, IRawEvent } from "lambda-framework";
import AWSTransformer from "./AWSTranformer";
import AWSTransformer from "./AWSTransformer";

const AWSHandler = (app: IApp) => {
return (event: any, context: Context, callback: Callback): void => {
rawEvent: IRawEvent = AWSTransformer.rawEvent(event);
rawCallback: IRawCallback = AWSTransformer.rawCallback(callback);
/**
* The class that implements the AWS handler.
*/
export default class AWSHandler {

app.handle(rawEvent, rawCallback);
};
}
private _app: IApp;
private _transformer: AWSTransformer;

constructor(app: IApp) {
this._app = app;
this._transformer = new AWSTransformer();
}

public handle(event: any, context: Context, callback: Callback): void {
const rawEvent: IRawEvent = this._transformer.transformRawEvent(event);
const rawCallback: IRawCallback = this._transformer.transformRawCallback(callback);

export default AWSHandler;
this._app.handle(rawEvent, rawCallback);
}

}
33 changes: 0 additions & 33 deletions src/lib/AWSTranformer.ts

This file was deleted.

87 changes: 87 additions & 0 deletions src/lib/AWSTransformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { APIGatewayEvent, Callback } from "aws-lambda";
import { IRawCallback, IRawEvent, RawEvent } from "lambda-framework";
import AWSRawCallback from "./AWSRawCallback";

/**
* The class that transform the AWS event and callback to LF raws.
*/
export default class AWSTransformer {

public transformRawEvent(event: any): IRawEvent {
const result: IRawEvent = new RawEvent();
result.type = this.getEventType(event);
result.original = event;
result.isHttp = result.type === "APIGatewayEvent";

if (result.isHttp) {
const httpEvent: APIGatewayEvent = event as APIGatewayEvent;
result.body = httpEvent.body;
result.headers = httpEvent.headers;
result.queryParams = httpEvent.queryStringParameters;
result.stageVariables = httpEvent.stageVariables;
result.ip = httpEvent.requestContext ? httpEvent.requestContext.identity.sourceIp.replace("\:d+$", "") : null;
result.path = httpEvent.path;
result.httpMethod = httpEvent.httpMethod;
}

return result;
}

public transformRawCallback(callback: Callback): IRawCallback {
if (callback === undefined) {
return null;
} else {
return new AWSRawCallback(callback);
}
}

private getEventType(obj: any): string {
if ("httpMethod" in obj) {
return "APIGatewayEvent";
} else if ("authorizationToken" in obj) {
return "CustomAuthorizerEvent";
} else if ("Records" in obj) {
const obj2 = obj.Records[0] || {};
if ("Sns" in obj2) {
return "SNSEvent";
} else if ("s3" in obj2) {
return "S3CreateEvent";
} else if ("kinesis" in obj2) {
return "KinesisEvent";
} else if ("dynamodb" in obj2) {
return "DynamoDbEvent";
} else {
return null;
}
} else if ("triggerSource" in obj) {
return "CognitoUserPoolEvent";
} else if ("RequestType" in obj) {
const obj2 = obj.RequestType;
switch (obj2) {
case "Create":
return "CloudFormationCustomResourceCreateEvent";
case "Update":
return "CloudFormationCustomResourceUpdateEvent";
case "Delete":
return "CloudFormationCustomResourceDeleteEvent";
default:
return null;
}
} else if ("Status" in obj) {
const obj2 = obj.Status;
switch (obj2) {
case "SUCCESS":
return "CloudFormationCustomResourceSuccessResponse";
case "FAILED":
return "CloudFormationCustomResourceFailedResponse";
default:
return null;
}
} else if ("time" in obj) {
return "ScheduledEvent";
} else {
return null;
}
}

}
48 changes: 0 additions & 48 deletions src/lib/utils/utils.ts

This file was deleted.

36 changes: 36 additions & 0 deletions test/AWSHandler.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* tslint:disable:no-unused-expression */
import { APIGatewayEvent, Callback, Context } from "aws-lambda";
import * as Chai from "chai";
import lambdaFramework, { IApp, RawEvent } from "lambda-framework";
import { SinonStub, stub } from "sinon";
import AWSHandler from "./../src/lib/AWSHandler";
import AWSRawCallback from "./../src/lib/AWSRawCallback";
import aPIGatewayEvent from "./utils/aPIGatewayEvent";

/**
* Test for AWSHandler.
*/
describe("AWSHandler", () => {
const event: APIGatewayEvent = aPIGatewayEvent;
const context: Context = null;
const callback: Callback = null;
const app: IApp = new lambdaFramework();
const appHandle: SinonStub = stub(app, "handle");
const handler: AWSHandler = new AWSHandler(app);

afterEach(() => {
appHandle.reset();
});

describe("#handle", () => {

it("calls the app `handle` method with a raw event and a raw callback.", () => {
handler.handle(event, context, callback);

Chai.expect(appHandle.args[0][0]).instanceOf(RawEvent);
Chai.expect(appHandle.args[0][1]).instanceOf(AWSRawCallback);
});

});

});
44 changes: 44 additions & 0 deletions test/AWSRawCallback.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* tslint:disable:no-unused-expression */
import { Callback } from "aws-lambda";
import * as Chai from "chai";
import { SinonSpy, spy } from "sinon";
import AWSRawCallback from "./../src/lib/AWSRawCallback";

/**
* Test for AWSRawCallback.
*/
describe("AWSRawCallback", () => {
const callback: SinonSpy = spy();
const rawCallback: AWSRawCallback = new AWSRawCallback(callback as Callback);

afterEach(() => {
callback.reset();
});

describe("#sendError", () => {

it("calls the original `callback` function with the error as first argument and null as second argument.", () => {
const error: Error = new Error("Produced error.");

rawCallback.sendError(error);

Chai.expect(callback.args[0][0]).to.be.equal(error);
Chai.expect(callback.args[0][1]).to.be.null;
});

});

describe("#send", () => {

it("calls the original `callback` function with the null as first argument and {statusCode, headers, body} object as second argument.", () => {
const resultObject = {statusCode: 200, headers: {header1: "value 1"}, body: new Buffer("body")};

rawCallback.send(resultObject.statusCode, resultObject.headers, resultObject.body);

Chai.expect(callback.args[0][0]).to.be.null;
Chai.expect(callback.args[0][1]).to.be.deep.equal(resultObject);
});

});

});
Loading

0 comments on commit a00d215

Please sign in to comment.