Add tests, fix some missed type name normalizations.

This commit is contained in:
Simon Buchan
2018-02-08 12:39:00 +13:00
parent e43b209621
commit 50dafbd8e3
2 changed files with 95 additions and 20 deletions

View File

@@ -584,8 +584,72 @@ context.fail(str);
/* Handler */
let handler: AWSLambda.Handler = (event: any, context: AWSLambda.Context, cb: AWSLambda.Callback) => { };
// async methods return Promise, test assignability
let asyncHandler: AWSLambda.Handler = async (event: any, context: AWSLambda.Context, cb: AWSLambda.Callback) => { };
let inferredHandler: AWSLambda.S3Handler = (event, context, cb) => {
// $ExpectType S3Event
event;
str = event.Records[0].eventName;
// $ExpectType Context
context;
str = context.functionName;
// $ExpectType Callback<void>
cb;
cb();
cb(null);
cb(new Error());
// $ExpectError
cb(null, { });
};
// Test using default Callback type still works.
let defaultCallbackHandler: AWSLambda.APIGatewayProxyHandler = (event: AWSLambda.APIGatewayEvent, context: AWSLambda.Context, cb: AWSLambda.Callback) => { };
// Specific types
let s3Handler: AWSLambda.S3Handler = (event: AWSLambda.S3Event, context: AWSLambda.Context, cb: AWSLambda.Callback<void>) => {};
// Test old name
let s3CreateHandler: AWSLambda.S3Handler = (event: AWSLambda.S3CreateEvent, context: AWSLambda.Context, cb: AWSLambda.Callback<void>) => {};
s3Handler = s3CreateHandler;
let dynamoDBStreamHandler: AWSLambda.DynamoDBStreamHandler = (event: AWSLambda.DynamoDBStreamEvent, context: AWSLambda.Context, cb: AWSLambda.Callback<void>) => {};
let snsHandler: AWSLambda.SNSHandler = (event: AWSLambda.SNSEvent, context: AWSLambda.Context, cb: AWSLambda.Callback<void>) => {};
let cognitoUserPoolHandler: AWSLambda.CognitoUserPoolTriggerHandler = (event: AWSLambda.CognitoUserPoolEvent, context: AWSLambda.Context, cb: AWSLambda.Callback<void>) => {};
let cloudFormationCustomResourceHandler: AWSLambda.CloudFormationCustomResourceHandler =
(event: AWSLambda.CloudFormationCustomResourceEvent, context: AWSLambda.Context, cb: AWSLambda.Callback<void>) => {};
let cloudWatchLogsHandler: AWSLambda.CloudWatchLogsHandler = (event: AWSLambda.CloudWatchLogsEvent, context: AWSLambda.Context, cb: AWSLambda.Callback<void>) => {};
let scheduledHandler: AWSLambda.ScheduledHandler = (event: AWSLambda.ScheduledEvent, context: AWSLambda.Context, cb: AWSLambda.Callback<void>) => {};
let apiGtwProxyHandler: AWSLambda.APIGatewayProxyHandler = (event: AWSLambda.APIGatewayProxyEvent, context: AWSLambda.Context, cb: AWSLambda.APIGatewayProxyCallback) => { };
// Test old names
let proxyHandler: AWSLambda.ProxyHandler = (event: AWSLambda.APIGatewayEvent, context: AWSLambda.Context, cb: AWSLambda.ProxyCallback) => { };
let asyncProxyHandler: AWSLambda.ProxyHandler = async (event: AWSLambda.APIGatewayEvent, context: AWSLambda.Context, cb: AWSLambda.ProxyCallback) => { };
apiGtwProxyHandler = proxyHandler;
let cloudFrontRequestHandler: AWSLambda.CloudFrontRequestHandler = (event: AWSLambda.CloudFrontRequestEvent, context: AWSLambda.Context, cb: AWSLambda.CloudFrontRequestCallback) => { };
let cloudFrontResponseHandler: AWSLambda.CloudFrontResponseHandler = (event: AWSLambda.CloudFrontResponseEvent, context: AWSLambda.Context, cb: AWSLambda.CloudFrontResponseCallback) => { };
let customAuthorizerHandler: AWSLambda.CustomAuthorizerHandler = (event: AWSLambda.CustomAuthorizerEvent, context: AWSLambda.Context, cb: AWSLambda.CustomAuthorizerCallback) => { };
let asyncCustomAuthorizerHandler: AWSLambda.CustomAuthorizerHandler = async (event: AWSLambda.CustomAuthorizerEvent, context: AWSLambda.Context, cb: AWSLambda.CustomAuthorizerCallback) => { };
interface CustomEvent { eventString: string; eventBool: boolean; }
interface CustomResult { resultString: string; resultBool?: boolean; }
type CustomCallback = AWSLambda.Callback<CustomResult>;
let customHandler: AWSLambda.Handler<CustomEvent, CustomResult> = (event, context, cb) => {
// $ExpectType CustomEvent
event;
str = event.eventString;
bool = event.eventBool;
// $ExpectType Context
context;
// $ExpectType Callback<CustomResult>
cb;
cb(null, { resultString: str, resultBool: bool });
// $ExpectError
cb(null, { resultString: bool });
};

View File

@@ -46,7 +46,7 @@ export interface APIGatewayEventRequestContext {
}
// API Gateway "event"
export interface APIGatewayEvent {
export interface APIGatewayProxyEvent {
body: string | null;
headers: { [name: string]: string };
httpMethod: string;
@@ -58,6 +58,7 @@ export interface APIGatewayEvent {
requestContext: APIGatewayEventRequestContext;
resource: string;
}
export type APIGatewayEvent = APIGatewayProxyEvent; // Old name
// API Gateway CustomAuthorizer "event"
export interface CustomAuthorizerEvent {
@@ -201,7 +202,7 @@ export type S3CreateEvent = S3Event; // old name
* Cognito User Pool event
* http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html
*/
export interface CognitoUserPoolEvent {
export interface CognitoUserPoolTriggerEvent {
version: number;
triggerSource:
| "PreSignUp_SignUp"
@@ -261,6 +262,7 @@ export interface CognitoUserPoolEvent {
answerCorrect?: boolean;
};
}
export type CognitoUserPoolEvent = CognitoUserPoolTriggerEvent;
/**
* CloudFormation Custom Resource event and response
@@ -496,6 +498,15 @@ export interface CloudFrontEvent {
};
}
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-generating-http-responses.html#lambda-generating-http-responses-object
export interface CloudFrontResultResponse {
status: string;
statusDescription?: string;
headers?: CloudFrontHeaders;
bodyEncoding?: 'text' | 'base64';
body?: string;
}
export interface CloudFrontResponseEvent {
Records: Array<{
cf: CloudFrontEvent & {
@@ -505,6 +516,8 @@ export interface CloudFrontResponseEvent {
}>;
}
export type CloudFrontRequestResult = undefined | null | CloudFrontResultResponse;
export interface CloudFrontRequestEvent {
Records: Array<{
cf: CloudFrontEvent & {
@@ -513,14 +526,7 @@ export interface CloudFrontRequestEvent {
}>;
}
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-generating-http-responses.html#lambda-generating-http-responses-object
export interface CloudFrontResult {
status: string;
statusDescription?: string;
headers?: CloudFrontHeaders;
bodyEncoding?: 'text' | 'base64';
body?: string;
}
export type CloudFrontResponseResult = undefined | null | CloudFrontResultResponse;
/**
* AWS Lambda handler function.
@@ -547,15 +553,17 @@ export type Callback<TResult = any> = (error?: Error | null, result?: TResult) =
export type S3Handler = Handler<S3Event, void>;
export type DynamoDBHandler = Handler<DynamoDBStreamEvent, void>;
export type DynamoDBStreamHandler = Handler<DynamoDBStreamEvent, void>;
export type SNSHandler = Handler<SNSEvent, void>;
// No SESHandler: SES event source is delivered as SNS notifications
// https://docs.aws.amazon.com/lambda/latest/dg/invoking-lambda-function.html#supported-event-source-ses
export type CognitoUserPoolTriggerHandler = Handler<CognitoUserPoolEvent>;
// TODO: Different event triggers/response pairs?
// Result type is weird: docs and samples say to return the mutated event, but it only requires an object
// with a "response" field, the type of which is specific to the event.triggerType. Leave as any for now.
export type CognitoUserPoolTriggerHandler = Handler<CognitoUserPoolTriggerEvent>;
// TODO: Different event/handler types for each event trigger so we can type the result?
// TODO: CognitoSync
@@ -567,21 +575,24 @@ export type CloudWatchLogsHandler = Handler<CloudWatchLogsEvent, void>;
// TODO: CodeCommit
export type ScheduledEventHandler = Handler<ScheduledEvent, void>;
export type ScheduledHandler = Handler<ScheduledEvent, void>;
// TODO: AWS Config
// TODO: Alexa
export type APIGatewayProxyHandler = Handler<APIGatewayEvent, APIGatewayProxyResult>;
export type ProxyHandler = APIGatewayProxyHandler; // Old name
export type APIGatewayProxyHandler = Handler<APIGatewayProxyEvent, APIGatewayProxyResult>;
export type APIGatewayProxyCallback = Callback<APIGatewayProxyResult>;
export type ProxyHandler = APIGatewayProxyHandler; // Old name
export type ProxyCallback = APIGatewayProxyCallback; // Old name
// TODO: IoT
export type CloudFrontRequestHandler = Handler<CloudFrontRequestEvent, undefined | null | CloudFrontResult>;
export type CloudFrontResponseHandler = Handler<CloudFrontResponseEvent, undefined | null | CloudFrontResult>;
export type CloudFrontRequestHandler = Handler<CloudFrontRequestEvent, CloudFrontRequestResult>;
export type CloudFrontRequestCallback = Callback<CloudFrontRequestResult>;
export type CloudFrontResponseHandler = Handler<CloudFrontResponseEvent, CloudFrontResponseResult>;
export type CloudFrontResponseCallback = Callback<CloudFrontResponseResult>;
// TODO: Kinesis (should be very close to DynamoDB stream?)