├── README.md ├── index.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # flow-aws-lambda 2 | 3 | Flow type definitions for AWS Lambda and API Gateway. 4 | 5 | Since lambda function interface itself is not a npm library you cannot use Flow npm type definitions (https://github.com/flowtype/flow-typed) 6 | 7 | But it's still very usefull and convinient to have types for objects which lambda operates on. 8 | 9 | Taken and adopted from TypeScript https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/aws-lambda 10 | 11 | # Usage 12 | 13 | ``` 14 | import type {APIGatewayEvent, Context, ProxyCallback} from 'flow-aws-lambda' 15 | ``` 16 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // @flow 2 | // Type definitions for AWS Lambda 3 | // Project: http://docs.aws.amazon.com/lambda 4 | // Definitions by: James Darbyshire , Michael Skarum , Stef Heyenrath , Toby Hede , Rich Buggy , Yoriki Yamaguchi , wwwy3y3 5 | // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped 6 | 7 | export type CloudFrontRequestEventRecordRequestBody = {| 8 | action: 'read-only' | 'replace'; 9 | data: string; 10 | encoding: 'base64' | 'text'; 11 | +inputTruncated: boolean; 12 | |} 13 | 14 | export type CloudFrontHeader = {| 15 | key?: string; 16 | value: string; 17 | |} 18 | 19 | export type CloudFrontHeaders = {| 20 | [name: string]: CloudFrontHeader[]; 21 | |} 22 | 23 | export type CloudFrontCustomOrigin = {| 24 | customHeaders: CloudFrontHeaders; 25 | domainName: string; 26 | keepaliveTimeout: number; 27 | path: string; 28 | port: number; 29 | protocol: 'http' | 'https'; 30 | readTimeout: number; 31 | sslProtocols: string[]; 32 | |} 33 | 34 | export type CloudFrontS3Origin = {| 35 | authMethod: 'origin-access-identity' | 'none'; 36 | customHeaders: CloudFrontHeaders; 37 | domainName: string; 38 | path: string; 39 | region: string; 40 | |} 41 | 42 | export type CloudFrontOrigin = 43 | | {| s3: CloudFrontS3Origin |} 44 | | {| custom: CloudFrontCustomOrigin |}; 45 | 46 | export type CloudFrontRequest = {| 47 | body?: CloudFrontRequestEventRecordRequestBody; 48 | +clientIp: string; 49 | +method: string; 50 | uri: string; 51 | querystring: string; 52 | headers: CloudFrontHeaders; 53 | origin?: CloudFrontOrigin; 54 | |} 55 | 56 | export type CloudFrontRequestEventRecordConfig = {| 57 | +distributionDomainName: string; 58 | +distributionId: string; 59 | +eventType: 'origin-request' | 'origin-response' | 'viewer-request' | 'viewer-response'; 60 | +requestId: string; 61 | |} 62 | 63 | export type CloudFrontEvent = { 64 | config: CloudFrontRequestEventRecordConfig; 65 | } 66 | 67 | export type CloudFrontRequestEventRecordDetail = CloudFrontEvent & { request: CloudFrontRequest }; 68 | 69 | export type CloudFrontResponse = {| 70 | status: string; 71 | statusDescription: string; 72 | headers: CloudFrontHeaders; 73 | |} 74 | 75 | export type CloudFrontResponseEventRecordDetail = CloudFrontEvent & { 76 | +request: $Diff; 77 | response: CloudFrontResponse; 78 | }; 79 | 80 | export type CloudFrontRequestEventRecord = {| 81 | cf: CloudFrontRequestEventRecordDetail; 82 | |} 83 | 84 | export type CloudFrontRequestEvent = {| 85 | Records: Array; 86 | |} 87 | 88 | export type CloudFrontResponseEventRecord = {| 89 | cf: CloudFrontResponseEventRecordDetail; 90 | |} 91 | 92 | /** 93 | * CloudFront viewer response or origin response event 94 | * 95 | * https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html#lambda-event-structure-response 96 | */ 97 | export type CloudFrontResponseEvent = {| 98 | Records: CloudFrontResponseEventRecord[]; 99 | |} 100 | 101 | /** 102 | * Generated HTTP response in viewer request event or origin request event 103 | * 104 | * https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-generating-http-responses-in-requests.html#lambda-generating-http-responses-object 105 | */ 106 | export type CloudFrontResultResponse = {| 107 | status: string; 108 | statusDescription?: string; 109 | headers?: CloudFrontHeaders; 110 | bodyEncoding?: 'text' | 'base64'; 111 | body?: string; 112 | |} 113 | 114 | export type CloudFrontResponseResult = void | null | CloudFrontResultResponse; 115 | 116 | // API Gateway "event" 117 | export type APIGatewayEvent = { 118 | body: T | null; 119 | headers: { [name: string]: string }; 120 | multiValueHeaders: { [name: string]: string[] }; 121 | httpMethod: string; 122 | isBase64Encoded: boolean; 123 | path: string; 124 | pathParameters: { [name: string]: string } | null; 125 | queryStringParameters: { [name: string]: string } | null; 126 | multiValueQueryStringParameters: { [name: string]: string[] } | null; 127 | stageVariables: { [name: string]: string } | null; 128 | requestContext: { 129 | accountId: string; 130 | apiId: string; 131 | authorizer?: AuthResponseContext | any; 132 | httpMethod: string; 133 | identity: { 134 | accessKey: string | null; 135 | accountId: string | null; 136 | apiKey: string | null; 137 | caller: string | null; 138 | cognitoAuthenticationProvider: string | null; 139 | cognitoAuthenticationType: string | null; 140 | cognitoIdentityId: string | null; 141 | cognitoIdentityPoolId: string | null; 142 | sourceIp: string; 143 | user: string | null; 144 | userAgent: string | null; 145 | userArn: string | null; 146 | }, 147 | stage: string; 148 | requestId: string; 149 | resourceId: string; 150 | resourcePath: string; 151 | }; 152 | resource: string; 153 | } 154 | 155 | // API Gateway CustomAuthorizer "event" 156 | export interface CustomAuthorizerEvent { 157 | type: string; 158 | authorizationToken: string; 159 | methodArn: string; 160 | } 161 | 162 | // SNS "event" 163 | export interface SNSMessageAttribute { 164 | Type: string; 165 | Value: string; 166 | } 167 | 168 | export interface SNSMessageAttributes { 169 | [name: string]: SNSMessageAttribute; 170 | } 171 | 172 | export interface SNSMessage { 173 | SignatureVersion: string; 174 | Timestamp: string; 175 | Signature: string; 176 | SigningCertUrl: string; 177 | MessageId: string; 178 | Message: string; 179 | MessageAttributes: SNSMessageAttributes; 180 | Type: string; 181 | UnsubscribeUrl: string; 182 | TopicArn: string; 183 | Subject: string; 184 | } 185 | 186 | export interface SNSEventRecord { 187 | EventVersion: string; 188 | EventSubscriptionArn: string; 189 | EventSource: string; 190 | Sns: SNSMessage; 191 | } 192 | 193 | export interface SNSEvent { 194 | Records: Array; 195 | } 196 | 197 | /** 198 | * S3Create event 199 | * https://docs.aws.amazon.com/AmazonS3/latest/dev/notification-content-structure.html 200 | */ 201 | export interface S3EventRecord { 202 | eventVersion: string; 203 | eventSource: string; 204 | awsRegion: string; 205 | eventTime: string; 206 | eventName: string; 207 | userIdentity: { 208 | principalId: string; 209 | }, 210 | requestParameters: { 211 | sourceIPAddress: string; 212 | }, 213 | responseElements: { 214 | 'x-amz-request-id': string; 215 | 'x-amz-id-2': string; 216 | }, 217 | s3: { 218 | s3SchemaVersion: string; 219 | configurationId: string; 220 | bucket: { 221 | name: string; 222 | ownerIdentity: { 223 | principalId: string; 224 | }, 225 | arn: string; 226 | }, 227 | object: { 228 | key: string; 229 | size: number; 230 | eTag: string; 231 | versionId: string; 232 | sequencer: string; 233 | } 234 | } 235 | } 236 | 237 | export interface S3CreateEvent { 238 | Records: Array; 239 | } 240 | 241 | /** 242 | * Cognito User Pool event 243 | * http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html 244 | */ 245 | export interface CognitoUserPoolEvent { 246 | version: number; 247 | triggerSource: "PreSignUp_SignUp" | "PostConfirmation_ConfirmSignUp" | "PreAuthentication_Authentication" | "PostAuthentication_Authentication" | "CustomMessage_SignUp" | "CustomMessage_AdminCreateUser" | "CustomMessage_ResendCode" | "CustomMessage_ForgotPassword" | "CustomMessage_UpdateUserAttribute" | "CustomMessage_VerifyUserAttribute" | "CustomMessage_Authentication" | "DefineAuthChallenge_Authentication" | "CreateAuthChallenge_Authentication" | "VerifyAuthChallengeResponse_Authentication"; 248 | region: string; 249 | userPoolId: string; 250 | userName?: string; 251 | callerContext: { 252 | awsSdkVersion: string; 253 | clientId: string; 254 | }; 255 | request: { 256 | userAttributes: {[key: string]: string}; 257 | validationData?: {[key: string]: string}; 258 | codeParameter?: string; 259 | usernameParameter?: string; 260 | newDeviceUsed?: boolean; 261 | session?: { 262 | challengeName: "CUSTOM_CHALLENGE" | "PASSWORD_VERIFIER" | "SMS_MFA" | "DEVICE_SRP_AUTH" | "DEVICE_PASSWORD_VERIFIER" | "ADMIN_NO_SRP_AUTH"; 263 | challengeResult: boolean; 264 | challengeMetaData?: string; 265 | }[]; 266 | challengeName?: string; 267 | privateChallengeParameters?: {[key: string]: string}; 268 | challengeAnswer?: {[key: string]: string}; 269 | }; 270 | response: { 271 | autoConfirmUser?: boolean; 272 | smsMessage?: string; 273 | emailMessage?: string; 274 | emailSubject?: string; 275 | challengeName?: string; 276 | issueTokens?: boolean; 277 | failAuthentication?: boolean; 278 | publicChallengeParameters?: {[key: string]: string}; 279 | privateChallengeParameters?: {[key: string]: string}; 280 | challengeMetaData?: string; 281 | answerCorrect?: boolean; 282 | }; 283 | } 284 | 285 | /** 286 | * CloudFormation Custom Resource event and response 287 | * http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/crpg-ref.html 288 | */ 289 | type CloudFormationCustomResourceEventCommon = { 290 | ServiceToken: string; 291 | ResponseURL: string; 292 | StackId: string; 293 | RequestId: string; 294 | LogicalResourceId: string; 295 | ResourceType: string; 296 | ResourceProperties: { 297 | ServiceToken: string; 298 | [Key: string]: any; 299 | } 300 | } 301 | 302 | export type CloudFormationCustomResourceCreateEvent = CloudFormationCustomResourceEventCommon & { 303 | RequestType: "Create"; 304 | } 305 | 306 | export type CloudFormationCustomResourceUpdateEvent = CloudFormationCustomResourceEventCommon & { 307 | RequestType: "Update"; 308 | PhysicalResourceId: string; 309 | OldResourceProperties: { 310 | [Key: string]: any; 311 | }; 312 | } 313 | 314 | export type CloudFormationCustomResourceDeleteEvent = CloudFormationCustomResourceEventCommon & { 315 | RequestType: "Delete"; 316 | PhysicalResourceId: string; 317 | } 318 | 319 | export type CloudFormationCustomResourceEvent = CloudFormationCustomResourceCreateEvent | CloudFormationCustomResourceUpdateEvent | CloudFormationCustomResourceDeleteEvent; 320 | 321 | type CloudFormationCustomResourceResponseCommon = { 322 | PhysicalResourceId: string; 323 | StackId: string; 324 | RequestId: string; 325 | LogicalResourceId: string; 326 | Data?: { 327 | [Key: string]: any; 328 | } 329 | } 330 | 331 | export type CloudFormationCustomResourceSuccessResponse = CloudFormationCustomResourceResponseCommon & { 332 | Status: "SUCCESS"; 333 | Reason?: string; 334 | } 335 | 336 | export type CloudFormationCustomResourceFailedResponse = CloudFormationCustomResourceResponseCommon & { 337 | Status: "FAILED"; 338 | Reason: string; 339 | } 340 | 341 | export type CloudFormationCustomResourceResponse = CloudFormationCustomResourceSuccessResponse | CloudFormationCustomResourceFailedResponse; 342 | 343 | /** 344 | * See http://docs.aws.amazon.com/lambda/latest/dg/eventsources.html#eventsources-cloudwatch-logs 345 | */ 346 | export type CloudWatchLogsEvent = { 347 | awslogs: CloudWatchLogsEventData; 348 | } 349 | 350 | export type CloudWatchLogsEventData = { 351 | data: string; 352 | } 353 | 354 | export type CloudWatchLogsDecodedData = { 355 | owner: string; 356 | logGroup: string; 357 | logStream: string; 358 | subscriptionFilters: Array; 359 | messageType: string; 360 | logEvents: Array; 361 | } 362 | 363 | /** 364 | * See http://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/SubscriptionFilters.html#LambdaFunctionExample 365 | */ 366 | export type CloudWatchLogsLogEvent = { 367 | id: string; 368 | timestamp: number; 369 | message: string; 370 | extractedFields?: {[key: string]: string}; 371 | } 372 | 373 | // Context 374 | // http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html 375 | export type Context = { 376 | // Properties 377 | callbackWaitsForEmptyEventLoop: boolean; 378 | functionName: string; 379 | functionVersion: string; 380 | invokedFunctionArn: string; 381 | memoryLimitInMB: number; 382 | awsRequestId: string; 383 | logGroupName: string; 384 | logStreamName: string; 385 | identity?: CognitoIdentity; 386 | clientContext?: ClientContext; 387 | 388 | // Functions 389 | getRemainingTimeInMillis(): number; 390 | 391 | // Functions for compatibility with earlier Node.js Runtime v0.10.42 392 | // For more details see http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-using-old-runtime.html#nodejs-prog-model-oldruntime-context-methods 393 | done(error: ?Error, result?: any): void; 394 | fail(error: Error): void; 395 | fail(message: string): void; 396 | succeed(message: string): void; 397 | succeed(object: any): void; 398 | succeed(message: string, object: any): void; 399 | } 400 | 401 | export interface CognitoIdentity { 402 | cognitoIdentityId: string; 403 | cognitoIdentityPoolId: string; 404 | } 405 | 406 | export interface ClientContext { 407 | client: ClientContextClient; 408 | Custom?: any; 409 | env: ClientContextEnv; 410 | } 411 | 412 | export interface ClientContextClient { 413 | installationId: string; 414 | appTitle: string; 415 | appVersionName: string; 416 | appVersionCode: string; 417 | appPackageName: string; 418 | } 419 | 420 | export interface ClientContextEnv { 421 | platformVersion: string; 422 | platform: string; 423 | make: string; 424 | model: string; 425 | locale: string; 426 | } 427 | 428 | export type ProxyResult = { 429 | statusCode: number; 430 | headers?: { 431 | [header: string]: boolean | number | string; 432 | }, 433 | body: string; 434 | isBase64Encoded?: boolean; 435 | } 436 | 437 | // Kinesis Streams 438 | // https://docs.aws.amazon.com/lambda/latest/dg/eventsources.html#eventsources-kinesis-streams 439 | export type KinesisStreamRecordPayload = { 440 | approximateArrivalTimestamp: number; 441 | data: string; 442 | kinesisSchemaVersion: string; 443 | partitionKey: string; 444 | sequenceNumber: string; 445 | } 446 | 447 | export type KinesisStreamRecord = { 448 | awsRegion: string; 449 | eventID: string; 450 | eventName: string; 451 | eventSource: string; 452 | eventSourceARN: string; 453 | eventVersion: string; 454 | invokeIdentityArn: string; 455 | kinesis: KinesisStreamRecordPayload; 456 | } 457 | 458 | export type KinesisStreamEvent = { 459 | Records: Array; 460 | } 461 | 462 | /** 463 | * API Gateway CustomAuthorizer AuthResponse. 464 | * http://docs.aws.amazon.com/apigateway/latest/developerguide/use-custom-authorizer.html#api-gateway-custom-authorizer-output 465 | */ 466 | export interface AuthResponse { 467 | principalId: string; 468 | policyDocument: PolicyDocument; 469 | context?: AuthResponseContext; 470 | } 471 | 472 | /** 473 | * API Gateway CustomAuthorizer AuthResponse.PolicyDocument. 474 | * http://docs.aws.amazon.com/apigateway/latest/developerguide/use-custom-authorizer.html#api-gateway-custom-authorizer-output 475 | */ 476 | export interface PolicyDocument { 477 | Version: string; 478 | Statement: Array; 479 | } 480 | 481 | /** 482 | * API Gateway CustomAuthorizer AuthResponse.PolicyDocument.Statement. 483 | * http://docs.aws.amazon.com/apigateway/latest/developerguide/use-custom-authorizer.html#api-gateway-custom-authorizer-output 484 | */ 485 | export interface Statement { 486 | Action: string | [string]; 487 | Effect: string; 488 | Resource: string | [string]; 489 | } 490 | 491 | /** 492 | * API Gateway CustomAuthorizer AuthResponse.PolicyDocument.Statement. 493 | * http://docs.aws.amazon.com/apigateway/latest/developerguide/use-custom-authorizer.html#api-gateway-custom-authorizer-output 494 | */ 495 | export interface AuthResponseContext { 496 | [name: string]: string | number | boolean; 497 | } 498 | 499 | /** 500 | * AWS Lambda handler function. 501 | * http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html 502 | * 503 | * @param event – event data. 504 | * @param context – runtime information of the Lambda function that is executing. 505 | * @param callback – optional callback to return information to the caller, otherwise return value is null. 506 | */ 507 | export type Handler = (event: any, context: Context, callback?: Callback) => Promise | void; 508 | export type ProxyHandler = (event: APIGatewayEvent, context: Context, callback?: ProxyCallback) => Promise | void; 509 | export type CustomAuthorizerHandler = (event: CustomAuthorizerEvent, context: Context, callback?: CustomAuthorizerCallback) => Promise | void; 510 | 511 | /** 512 | * Optional callback parameter. 513 | * http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html 514 | * 515 | * @param error – an optional parameter that you can use to provide results of the failed Lambda function execution. 516 | * @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. 517 | */ 518 | export type Callback = (error: ?Error, result?: any) => void; 519 | export type ProxyCallback = (error: ?Error, result?: ProxyResult) => void; 520 | export type CustomAuthorizerCallback = (error: ?Error, result?: AuthResponse) => void; 521 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "flow-aws-lambda", 3 | "version": "1.0.6", 4 | "description": "Flow types for AWS Lambda functions", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/yarax/flow-aws-lambda.git" 12 | }, 13 | "author": "Roman Krivtsov", 14 | "license": "ISC", 15 | "bugs": { 16 | "url": "https://github.com/yarax/flow-aws-lambda/issues" 17 | }, 18 | "homepage": "https://github.com/yarax/flow-aws-lambda#readme" 19 | } 20 | --------------------------------------------------------------------------------