-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
2,137 additions
and
236 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); | ||
|
||
}); |
Oops, something went wrong.