diff --git a/packages/@aws-cdk/aws-apigateway/lib/authorizer.ts b/packages/@aws-cdk/aws-apigateway/lib/authorizer.ts index 3a2f3698a554d..a51f30e14514c 100644 --- a/packages/@aws-cdk/aws-apigateway/lib/authorizer.ts +++ b/packages/@aws-cdk/aws-apigateway/lib/authorizer.ts @@ -1,14 +1,29 @@ -import { Resource } from '@aws-cdk/core'; +import { Construct, Resource, ResourceProps } from '@aws-cdk/core'; import { AuthorizationType } from './method'; import { RestApi } from './restapi'; +const AUTHORIZER_SYMBOL = Symbol.for('@aws-cdk/aws-apigateway.Authorizer'); + /** * Base class for all custom authorizers */ export abstract class Authorizer extends Resource implements IAuthorizer { + /** + * Return whether the given object is an Authorizer. + */ + public static isAuthorizer(x: any): x is Authorizer { + return x !== null && typeof(x) === 'object' && AUTHORIZER_SYMBOL in x; + } + public readonly abstract authorizerId: string; public readonly authorizationType?: AuthorizationType = AuthorizationType.CUSTOM; + public constructor(scope: Construct, id: string, props?: ResourceProps) { + super(scope, id, props); + + Object.defineProperty(this, AUTHORIZER_SYMBOL, { value: true }); + } + /** * Called when the authorizer is used from a specific REST API. * @internal diff --git a/packages/@aws-cdk/aws-apigateway/lib/method.ts b/packages/@aws-cdk/aws-apigateway/lib/method.ts index a459c4265a8d4..baafd1be53242 100644 --- a/packages/@aws-cdk/aws-apigateway/lib/method.ts +++ b/packages/@aws-cdk/aws-apigateway/lib/method.ts @@ -185,7 +185,7 @@ export class Method extends Resource { `which is different from what is required by the authorizer [${authorizer.authorizationType}]`); } - if (authorizer instanceof Authorizer) { + if (Authorizer.isAuthorizer(authorizer)) { authorizer._attachToApi(this.restApi); } diff --git a/packages/@aws-cdk/aws-apigateway/test/test.authorizer.ts b/packages/@aws-cdk/aws-apigateway/test/test.authorizer.ts new file mode 100644 index 0000000000000..ce5a5279228e3 --- /dev/null +++ b/packages/@aws-cdk/aws-apigateway/test/test.authorizer.ts @@ -0,0 +1,21 @@ +import { Stack } from '@aws-cdk/core'; +import { Test } from 'nodeunit'; +import { Authorizer, RestApi } from '../lib'; + +export = { + 'isAuthorizer correctly detects an instance of type Authorizer'(test: Test) { + class MyAuthorizer extends Authorizer { + public readonly authorizerId = 'test-authorizer-id'; + public _attachToApi(_: RestApi): void { + // do nothing + } + } + const stack = new Stack(); + const authorizer = new MyAuthorizer(stack, 'authorizer'); + + test.ok(Authorizer.isAuthorizer(authorizer), 'type Authorizer expected but is not'); + test.ok(!Authorizer.isAuthorizer(stack), 'type Authorizer found, when not expected'); + + test.done(); + }, +}; \ No newline at end of file