Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make 'Function' a generic type #203

Merged
merged 13 commits into from
May 2, 2018
8 changes: 4 additions & 4 deletions overlays/nodejs/lambda/createFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface Context {
/**
* Handler is the signature for a callback that be converted into an aws lambda entrypoint.
*/
export type Handler = (event: any, context: Context, callback: (error: any, result: any) => void) => any;
export type Handler<E,R> = (event: E, context: Context, callback: (error: any, result: R) => void) => any;

/**
* CallbackFunctionArgs specify the properties that can be passed in to configure the AWS Lambda
Expand All @@ -38,9 +38,9 @@ export type CallbackFunctionArgs = utils.Omit<lambda.FunctionArgs, "code" | "han
* The callback will be appropriately serialized into a form that the AWS Lambda infrastructure
* can call into.
*/
export function createFunction(
name: string, handler: Handler, args: CallbackFunctionArgs,
serialize?: (obj: any) => boolean, opts?: pulumi.ResourceOptions): lambda.Function {
export function createFunction<E,R>(
name: string, handler: Handler<E,R>, args: CallbackFunctionArgs,
serialize?: (obj: any) => boolean, opts?: pulumi.ResourceOptions): lambda.Function<E,R> {

if (!name) {
throw new Error("Missing required 'name'");
Expand Down
6 changes: 6 additions & 0 deletions overlays/nodejs/lambda/typedFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as func from "./function"

declare module "./function" {
export interface Function<E={}, R={}> {
}
}
28 changes: 3 additions & 25 deletions overlays/nodejs/serverless/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,8 @@
import * as crypto from "crypto";
import * as pulumi from "@pulumi/pulumi";
import { Role, RolePolicyAttachment } from "../iam";
import * as lambda from "../lambda";
import { ARN } from "../arn";

/**
* Context is the shape of the context object passed to a Function callback.
*/
export interface Context {
callbackWaitsForEmptyEventLoop: boolean;
readonly functionName: string;
readonly functionVersion: string;
readonly invokedFunctionArn: string;
readonly memoryLimitInMB: string;
readonly awsRequestId: string;
readonly logGroupName: string;
readonly logStreamName: string;
readonly identity: any;
readonly clientContext: any;
getRemainingTimeInMillis(): string;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not positive - but I think we may have dependencies on this type from @pulumi/cloud. We could choose to leave it here to avoid breaking clients (as an alias for the type in the other namespace), or could just assume that these changes you are making require a version bump to avoid getting automatically picked up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. tehse should go with a version bump.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

talking to matt about this.


/**
* Handler is the signature for a serverless function.
*/
export type Handler = (event: any, context: Context, callback: (error: any, result: any) => void) => any;
import * as lambda from "../lambda";

export interface FunctionOptions {
policies: ARN[];
Expand All @@ -44,15 +22,15 @@ export interface FunctionOptions {
* Function is a higher-level API for creating and managing AWS Lambda Function resources implemented
* by a Lumi lambda expression and with a set of attached policies.
*/
export class Function extends pulumi.ComponentResource {
export class Function<E, R> extends pulumi.ComponentResource {
public readonly options: FunctionOptions;
public readonly lambda: lambda.Function;
public readonly role: Role;
public readonly policies: RolePolicyAttachment[];

constructor(name: string,
options: FunctionOptions,
func: Handler,
func: lambda.Handler<E, R>,
opts?: pulumi.ResourceOptions,
serialize?: (obj: any) => boolean) {
if (!name) {
Expand Down
1 change: 1 addition & 0 deletions resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,7 @@ func Provider() tfbridge.ProviderInfo {
Files: []string{
"runtimes.ts", // a union type and constants for available Lambda runtimes.
"createFunction.ts", // helper method to simply create a lambda function from an arrow function
"typedFunction.ts", // makes the exported 'Function' type generic.
},
},
"serverless": {
Expand Down