2020-02-14 17:18:39 +00:00
|
|
|
// Declare a bunch of basic types up front. Since this file has no imports
|
|
|
|
|
// or exports, typescript treats these as global declarations that can be
|
|
|
|
|
// used in tests/* files.
|
|
|
|
|
// Don't add declarations for service-specific types here; i.e. not from
|
|
|
|
|
// common/ or trigger/, but handler.d.ts is fine. Those can be in the
|
|
|
|
|
// service-specific tests in tests/{service}-tests.ts
|
2018-02-07 22:59:17 +00:00
|
|
|
declare let str: string;
|
|
|
|
|
declare let strOrNull: string | null;
|
|
|
|
|
declare let strOrUndefined: string | undefined;
|
2019-02-07 09:52:54 +00:00
|
|
|
declare let strOrUndefinedOrNull: string | undefined | null;
|
2018-02-07 22:59:17 +00:00
|
|
|
declare let date: Date;
|
2020-08-12 03:15:37 +00:00
|
|
|
declare let obj: {};
|
|
|
|
|
declare let array: any[];
|
2018-02-07 22:59:17 +00:00
|
|
|
declare let anyObj: any;
|
|
|
|
|
declare let num: number;
|
|
|
|
|
declare let error: Error;
|
|
|
|
|
declare let bool: boolean;
|
|
|
|
|
declare let boolOrUndefined: boolean | undefined;
|
2019-03-12 17:44:04 +00:00
|
|
|
declare let boolOrNumOrStr: boolean | number | string;
|
2019-02-07 09:52:54 +00:00
|
|
|
declare let numOrUndefined: number | undefined;
|
2020-06-24 11:31:44 +00:00
|
|
|
declare let strArrayOrUndefined: string[] | undefined;
|
2020-08-12 03:15:37 +00:00
|
|
|
declare let nullOrUndefined: null | undefined;
|
|
|
|
|
declare let objectOrUndefined: {} | undefined;
|
2020-02-14 17:18:39 +00:00
|
|
|
|
|
|
|
|
// handler.d.ts types
|
|
|
|
|
declare let context: AWSLambda.Context;
|
2018-02-07 22:59:17 +00:00
|
|
|
declare let clientCtx: AWSLambda.ClientContext;
|
|
|
|
|
declare let clientCtxOrUndefined: AWSLambda.ClientContext | undefined;
|
|
|
|
|
declare let clientContextEnv: AWSLambda.ClientContextEnv;
|
|
|
|
|
declare let clientContextClient: AWSLambda.ClientContextClient;
|
|
|
|
|
declare let identity: AWSLambda.CognitoIdentity;
|
|
|
|
|
declare let identityOrUndefined: AWSLambda.CognitoIdentity | undefined;
|
2018-01-23 19:06:53 +00:00
|
|
|
|
2016-08-08 13:18:34 +00:00
|
|
|
/* Context */
|
2018-02-07 22:59:17 +00:00
|
|
|
bool = context.callbackWaitsForEmptyEventLoop;
|
2016-09-06 14:11:58 +00:00
|
|
|
str = context.functionName;
|
|
|
|
|
str = context.functionVersion;
|
|
|
|
|
str = context.invokedFunctionArn;
|
2019-12-13 10:51:08 +00:00
|
|
|
str = context.memoryLimitInMB;
|
2016-08-08 13:18:34 +00:00
|
|
|
str = context.awsRequestId;
|
2016-09-06 14:11:58 +00:00
|
|
|
str = context.logGroupName;
|
|
|
|
|
str = context.logStreamName;
|
2018-02-07 22:59:17 +00:00
|
|
|
identityOrUndefined = context.identity;
|
|
|
|
|
clientCtxOrUndefined = context.clientContext;
|
2016-08-19 09:21:44 +00:00
|
|
|
|
|
|
|
|
str = identity.cognitoIdentityId;
|
|
|
|
|
str = identity.cognitoIdentityPoolId;
|
|
|
|
|
|
2016-10-23 18:09:46 +00:00
|
|
|
/* ClientContext */
|
|
|
|
|
clientContextClient = clientCtx.client;
|
2019-05-20 17:11:09 +00:00
|
|
|
anyObj = clientCtx.Custom;
|
2016-10-23 18:09:46 +00:00
|
|
|
clientContextEnv = clientCtx.env;
|
|
|
|
|
|
|
|
|
|
/* ClientContextEnv */
|
|
|
|
|
str = clientContextEnv.locale;
|
|
|
|
|
str = clientContextEnv.make;
|
|
|
|
|
str = clientContextEnv.model;
|
|
|
|
|
str = clientContextEnv.platform;
|
|
|
|
|
str = clientContextEnv.platformVersion;
|
|
|
|
|
|
|
|
|
|
/* ClientContextClient */
|
|
|
|
|
str = clientContextClient.appPackageName;
|
|
|
|
|
str = clientContextClient.appTitle;
|
|
|
|
|
str = clientContextClient.appVersionCode;
|
|
|
|
|
str = clientContextClient.appVersionName;
|
|
|
|
|
str = clientContextClient.installationId;
|
|
|
|
|
|
2020-02-14 17:18:39 +00:00
|
|
|
declare const untypedCallback: AWSLambda.Callback;
|
|
|
|
|
untypedCallback();
|
|
|
|
|
untypedCallback(undefined);
|
|
|
|
|
untypedCallback(null);
|
|
|
|
|
untypedCallback(error);
|
|
|
|
|
untypedCallback(str); // https://docs.aws.amazon.com/apigateway/latest/developerguide/handle-errors-in-lambda-integration.html
|
|
|
|
|
untypedCallback(null, anyObj);
|
|
|
|
|
untypedCallback(undefined, bool);
|
|
|
|
|
untypedCallback(null, bool);
|
|
|
|
|
untypedCallback(null, str);
|
|
|
|
|
untypedCallback(null, num);
|
|
|
|
|
untypedCallback(null, { foo: 123 });
|
|
|
|
|
untypedCallback(null, { bar: 123 });
|
|
|
|
|
// $ExpectError
|
|
|
|
|
untypedCallback(null, anyObj, anyObj);
|
|
|
|
|
|
|
|
|
|
interface TestResult { foo: number; }
|
|
|
|
|
declare const typedCallback: AWSLambda.Callback<TestResult>;
|
|
|
|
|
typedCallback();
|
|
|
|
|
typedCallback(undefined);
|
|
|
|
|
typedCallback(null);
|
|
|
|
|
typedCallback(error);
|
|
|
|
|
typedCallback(str); // https://docs.aws.amazon.com/apigateway/latest/developerguide/handle-errors-in-lambda-integration.html
|
|
|
|
|
typedCallback(null, anyObj);
|
|
|
|
|
typedCallback(null, { foo: 123 });
|
|
|
|
|
// $ExpectError
|
|
|
|
|
typedCallback(null, str);
|
|
|
|
|
// $ExpectError
|
|
|
|
|
typedCallback(null, { bar: 123 });
|
2018-04-05 22:02:13 +00:00
|
|
|
|
2016-10-23 18:09:46 +00:00
|
|
|
/* Compatibility functions */
|
|
|
|
|
context.done();
|
|
|
|
|
context.done(error);
|
|
|
|
|
context.done(error, anyObj);
|
|
|
|
|
context.succeed(str);
|
|
|
|
|
context.succeed(anyObj);
|
|
|
|
|
context.succeed(str, anyObj);
|
|
|
|
|
context.fail(error);
|
2017-01-18 06:31:00 +00:00
|
|
|
context.fail(str);
|
|
|
|
|
|
2019-10-25 20:16:03 +00:00
|
|
|
interface CustomEvent {
|
|
|
|
|
eventString: string;
|
|
|
|
|
eventBool: boolean;
|
|
|
|
|
}
|
|
|
|
|
interface CustomResult {
|
|
|
|
|
resultString: string;
|
|
|
|
|
resultBool?: boolean;
|
|
|
|
|
}
|
2020-02-14 17:18:39 +00:00
|
|
|
type CustomHandler = AWSLambda.Handler<CustomEvent, CustomResult>;
|
2018-02-07 23:39:00 +00:00
|
|
|
type CustomCallback = AWSLambda.Callback<CustomResult>;
|
2020-02-14 17:18:39 +00:00
|
|
|
|
|
|
|
|
// Untyped handlers should work
|
|
|
|
|
const untypedAsyncHandler: AWSLambda.Handler = async (event, context, cb) => {
|
|
|
|
|
// $ExpectType any
|
2018-02-07 23:39:00 +00:00
|
|
|
event;
|
|
|
|
|
// $ExpectType Context
|
|
|
|
|
context;
|
2020-02-14 17:18:39 +00:00
|
|
|
// $ExpectType Callback<any>
|
2018-02-07 23:39:00 +00:00
|
|
|
cb;
|
2020-02-14 17:18:39 +00:00
|
|
|
// Can still use callback
|
|
|
|
|
cb(null, { resultString: str });
|
|
|
|
|
if (bool) {
|
|
|
|
|
// Uncaught error
|
|
|
|
|
return { resultString: bool };
|
|
|
|
|
}
|
|
|
|
|
return { resultString: str };
|
2018-07-21 00:31:00 +00:00
|
|
|
};
|
|
|
|
|
|
2020-02-14 17:18:39 +00:00
|
|
|
const untypedCallbackHandler: AWSLambda.Handler = (event, context, cb) => {
|
|
|
|
|
// $ExpectType any
|
2018-07-21 00:31:00 +00:00
|
|
|
event;
|
|
|
|
|
// $ExpectType Context
|
|
|
|
|
context;
|
2020-02-14 17:18:39 +00:00
|
|
|
// $ExpectType Callback<any>
|
2018-07-21 00:31:00 +00:00
|
|
|
cb;
|
2020-02-14 17:18:39 +00:00
|
|
|
cb(null, { resultString: str });
|
|
|
|
|
// Uncaught error
|
|
|
|
|
cb(null, { resultString: bool });
|
2018-07-21 00:31:00 +00:00
|
|
|
};
|
|
|
|
|
|
2020-02-14 17:18:39 +00:00
|
|
|
/* In node8.10 runtime, handlers may return a promise for the result value, so existing async
|
|
|
|
|
* handlers that return Promise<void> before calling the callback will now have a `null` result.
|
|
|
|
|
* Be safe and make that badly typed with a major verson bump to 8.10 so users expect the breaking change,
|
|
|
|
|
* since the upgrade effort should be pretty low in most cases, and it points them at a nicer solution.
|
|
|
|
|
*/
|
2019-03-06 14:31:31 +00:00
|
|
|
|
2020-02-14 17:18:39 +00:00
|
|
|
// Test we get error for unsafe old style
|
|
|
|
|
// $ExpectError
|
|
|
|
|
const unsafeAsyncHandler: CustomHandler = async (event, context, cb) => {
|
|
|
|
|
cb(null, { resultString: 'No longer valid' });
|
2018-07-21 00:31:00 +00:00
|
|
|
};
|
2018-08-01 18:39:21 +00:00
|
|
|
|
2020-02-14 17:18:39 +00:00
|
|
|
// Test safe old style still works
|
|
|
|
|
const typedCallbackHandler: CustomHandler = (event, context, cb) => {
|
|
|
|
|
// $ExpectType CustomEvent
|
2018-08-01 18:39:21 +00:00
|
|
|
event;
|
2020-02-14 17:18:39 +00:00
|
|
|
str = event.eventString;
|
2018-08-01 18:39:21 +00:00
|
|
|
// $ExpectType Context
|
|
|
|
|
context;
|
|
|
|
|
str = context.functionName;
|
2020-02-14 17:18:39 +00:00
|
|
|
// $ExpectType Callback<CustomResult>
|
|
|
|
|
cb;
|
|
|
|
|
cb();
|
|
|
|
|
cb(null);
|
|
|
|
|
cb(new Error());
|
|
|
|
|
// $ExpectError
|
|
|
|
|
cb(null, {});
|
|
|
|
|
cb(null, { resultString: str });
|
|
|
|
|
// $ExpectError
|
|
|
|
|
cb(null, { resultString: bool });
|
2019-01-28 20:48:38 +00:00
|
|
|
};
|
|
|
|
|
|
2020-02-14 17:18:39 +00:00
|
|
|
// Test preferred new type
|
|
|
|
|
const typedAsyncHandler: CustomHandler = async (event, context, cb) => {
|
|
|
|
|
// $ExpectType CustomEvent
|
2019-01-28 20:48:38 +00:00
|
|
|
event;
|
|
|
|
|
// $ExpectType Context
|
|
|
|
|
context;
|
2020-02-14 17:18:39 +00:00
|
|
|
// $ExpectType Callback<CustomResult>
|
|
|
|
|
cb;
|
|
|
|
|
// Can still use callback
|
|
|
|
|
cb(null, { resultString: str });
|
|
|
|
|
return { resultString: 'Is now valid!' };
|
2019-09-24 21:23:29 +00:00
|
|
|
};
|
|
|
|
|
|
2020-02-14 17:18:39 +00:00
|
|
|
// $ExpectError
|
|
|
|
|
const badTypedAsyncHandler: CustomHandler = async (event, context, cb) => ({ resultString: bool });
|
2019-09-24 21:23:29 +00:00
|
|
|
|
2020-02-14 17:18:39 +00:00
|
|
|
// Test using untyped Callback type still works.
|
|
|
|
|
const mixedUntypedCallbackTypedHandler: CustomHandler = (
|
|
|
|
|
event: CustomEvent,
|
|
|
|
|
context: AWSLambda.Context,
|
|
|
|
|
cb: AWSLambda.Callback,
|
|
|
|
|
) => {};
|