Permalink
Browse files

Promisify lambdas

  • Loading branch information...
rricard committed Nov 29, 2015
1 parent bf62e38 commit 860fd190f9aea4ce091f264824e87bab98e35cc5
Showing with 74 additions and 1 deletion.
  1. +3 −1 lambdas/hello.js
  2. +26 −0 lib/lambda-promisifier.js
  3. +45 −0 lib/lambda-types.js
@@ -1,9 +1,11 @@
/* @flow */
import type {LambdaContext} from "../lib/lambda-types.js";
type HelloOptions = {
name: string
};
export function hello(options: HelloOptions, context: any): void {
export function hello(options: HelloOptions, context: LambdaContext): void {
context.succeed(`Hello ${options.name || "world"}!`);
}
@@ -0,0 +1,26 @@
/* @flow */
import type {LambdaContext} from "./lambda-types.js";
export function lambdaPromisifier(
lambda: (options: any, context: LambdaContext) => void
): (options: any) => Promise<any> {
return (options: any) =>
new Promise((resolve, reject) =>
lambda(options, {
succeed: resolve,
fail: reject,
done: (err, res) => err ? reject(err) : resolve(res),
getRemainingTimeInMillis: () => Infinity,
functionName: "fakeLambda",
functionVersion: "0",
invokedFunctionArn: "arn:aws:lambda:fake-region:fake-acc:function:fakeLambda",
memoryLimitInMB: Infinity,
awsRequestId: "fakeRequest",
logGroupName: "fakeGroup",
logStreamName: "fakeStream",
identity: null,
clientContext: null
})
);
};
@@ -0,0 +1,45 @@
/* @flow */
export type AmazonCognitoIdentity = {
cognito_identity_id: number | string,
cognito_identity_pool_id: number | string
};
export type AWSMobileSDKClient = {
installation_id: number | string,
app_title: string,
app_version_name: string,
app_version_code: number | string,
app_package_name: string
};
export type AWSMobileSDKClientEnv = {
platform_version: number | string,
platform: string,
make: any,
model: any,
locale: string
};
export type AWSMobileSDKClientContext = {
client: AWSMobileSDKClient,
Custom: any,
env: AWSMobileSDKClientEnv
};
export type LambdaContext = {
succeed: (result: any) => void,
fail: (error: Error) => void,
done: (error: Error, result: any) => void,
getRemainingTimeInMillis: () => number,
functionName: string,
functionVersion: number | string,
invokedFunctionArn: string,
memoryLimitInMB: number,
awsRequestId: number | string,
logGroupName: string,
logStreamName: string,
identity: ?AmazonCognitoIdentity,
clientContext: ?AWSMobileSDKClientContext
};

0 comments on commit 860fd19

Please sign in to comment.