diff --git a/aws-lambda/aws-lambda-tests.ts b/aws-lambda/aws-lambda-tests.ts index 0d8d9bd47f..fb09beda8a 100644 --- a/aws-lambda/aws-lambda-tests.ts +++ b/aws-lambda/aws-lambda-tests.ts @@ -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) => {}; diff --git a/aws-lambda/index.d.ts b/aws-lambda/index.d.ts index 1aa41b9e62..e8b70db7bd 100644 --- a/aws-lambda/index.d.ts +++ b/aws-lambda/index.d.ts @@ -1,8 +1,44 @@ // Type definitions for AWS Lambda // Project: http://docs.aws.amazon.com/lambda -// Definitions by: James Darbyshire , Michael Skarum , Stef Heyenrath , Toby Hede +// Definitions by: James Darbyshire , Michael Skarum , Stef Heyenrath , Toby Hede , Rich 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;