Added Lambda Proxy and API Gateway Event definitions

This commit is contained in:
Rich Buggy
2017-01-29 12:49:30 +11:00
parent 14db63a765
commit 6ab0599be2
2 changed files with 94 additions and 1 deletions

View File

@@ -6,11 +6,48 @@ var anyObj: any = { abc: 123 };
var num: number = 5;
var error: Error = new Error();
var b: boolean = true;
var apiGwEvt: AWSLambda.APIGatewayEvent;
var clientCtx: AWSLambda.ClientContext;
var clientContextEnv: AWSLambda.ClientContextEnv;
var clientContextClient: AWSLambda.ClientContextClient;
var context: AWSLambda.Context;
var identity: AWSLambda.CognitoIdentity;
var proxyResult: AWSLambda.ProxyResult;
/* API Gateway Event */
str = apiGwEvt.body;
str = apiGwEvt.headers["example"];
str = apiGwEvt.httpMethod;
b = apiGwEvt.isBase64Encoded;
str = apiGwEvt.path;
str = apiGwEvt.pathParameters["example"];
str = apiGwEvt.queryStringParameters["example"];
str = apiGwEvt.stageVariables["example"];
str = apiGwEvt.requestContext.accountId;
str = apiGwEvt.requestContext.apiId;
str = apiGwEvt.requestContext.httpMethod;
str = apiGwEvt.requestContext.identity.accessKey;
str = apiGwEvt.requestContext.identity.accountId;
str = apiGwEvt.requestContext.identity.apiKey;
str = apiGwEvt.requestContext.identity.caller;
str = apiGwEvt.requestContext.identity.cognitoAuthenticationProvider;
str = apiGwEvt.requestContext.identity.cognitoAuthenticationType;
str = apiGwEvt.requestContext.identity.cognitoIdentityId;
str = apiGwEvt.requestContext.identity.cognitoIdentityPoolId;
str = apiGwEvt.requestContext.identity.sourceIp;
str = apiGwEvt.requestContext.identity.user;
str = apiGwEvt.requestContext.identity.userAgent;
str = apiGwEvt.requestContext.identity.userArn;
str = apiGwEvt.requestContext.stage;
str = apiGwEvt.requestContext.requestId;
str = apiGwEvt.requestContext.resourceId;
str = apiGwEvt.requestContext.resourcePath;
str = apiGwEvt.resource;
/* Lambda Proxy Result */
num = proxyResult.statusCode;
str = proxyResult.headers["example"];
str = proxyResult.body
/* Context */
b = context.callbackWaitsForEmptyEventLoop;
@@ -54,6 +91,15 @@ function callback(cb: AWSLambda.Callback) {
cb(error);
cb(null, anyObj);
}
/* Proxy Callback */
function proxyCallback(cb: AWSLambda.ProxyCallback) {
cb();
cb(null);
cb(error);
cb(null, proxyResult);
}
/* Compatibility functions */
context.done();
context.done(error);
@@ -66,3 +112,4 @@ context.fail(str);
/* Handler */
let handler: AWSLambda.Handler = (event: any, context: AWSLambda.Context, cb: AWSLambda.Callback) => {};
let proxyHandler: AWSLambda.ProxyHandler = (event: AWSLambda.APIGatewayEvent, context: AWSLambda.Context, cb: AWSLambda.ProxyCallback) => {};

48
aws-lambda/index.d.ts vendored
View File

@@ -1,8 +1,44 @@
// Type definitions for AWS Lambda
// Project: http://docs.aws.amazon.com/lambda
// Definitions by: James Darbyshire <https://github.com/darbio/aws-lambda-typescript>, Michael Skarum <https://github.com/skarum>, Stef Heyenrath <https://github.com/StefH/DefinitelyTyped>, Toby Hede <https://github.com/tobyhede>
// Definitions by: James Darbyshire <https://github.com/darbio/aws-lambda-typescript>, Michael Skarum <https://github.com/skarum>, Stef Heyenrath <https://github.com/StefH/DefinitelyTyped>, Toby Hede <https://github.com/tobyhede>, Rich Buggy <https://github.com/buggy>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// API Gateway "event"
interface APIGatewayEvent {
body: string | null;
headers: { [name: string]: string };
httpMethod: string;
isBase64Encoded: boolean;
path: string;
pathParameters: { [name: string]: string } | null;
queryStringParameters: { [name: string]: string } | null;
stageVariables: { [name: string]: string } | null;
requestContext: {
accountId: string;
apiId: string;
httpMethod: string;
identity: {
accessKey: string | null;
accountId: string | null;
apiKey: string | null;
caller: string | null;
cognitoAuthenticationProvider: string | null;
cognitoAuthenticationType: string | null;
cognitoIdentityId: string | null;
cognitoIdentityPoolId: string | null;
sourceIp: string;
user: string | null;
userAgent: string | null;
userArn: string | null;
},
stage: string;
requestId: string;
resourceId: string;
resourcePath: string;
};
resource: string;
}
// Context
// http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html
interface Context {
@@ -58,6 +94,14 @@ interface ClientContextEnv {
locale: string;
}
interface ProxyResult {
statusCode: number;
headers?: {
[header: string]: string;
},
body: string;
}
/**
* AWS Lambda handler function.
* http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html
@@ -67,6 +111,7 @@ interface ClientContextEnv {
* @param callback optional callback to return information to the caller, otherwise return value is null.
*/
export type Handler = (event: any, context: Context, callback?: Callback) => void;
export type ProxyHandler = (event: APIGatewayEvent, context: Context, callback?: ProxyCallback) => void;
/**
* Optional callback parameter.
@@ -76,5 +121,6 @@ export type Handler = (event: any, context: Context, callback?: Callback) => vo
* @param result an optional parameter that you can use to provide the result of a successful function execution. The result provided must be JSON.stringify compatible.
*/
export type Callback = (error?: Error, result?: any) => void;
export type ProxyCallback = (error?: Error, result?: ProxyResult) => void;
export as namespace AWSLambda;