From 7f474481b60c545f3855efc7857474c4277413e0 Mon Sep 17 00:00:00 2001 From: Piotr Grzesik Date: Fri, 25 Sep 2020 16:38:32 +0200 Subject: [PATCH] feat(Config Schema): Schema for AWS `alexaSkill` event (#8290) --- docs/deprecations.md | 6 ++ .../compile/events/alexaSkill/index.js | 42 +++++------ .../compile/events/alexaSkill/index.test.js | 71 ------------------- 3 files changed, 23 insertions(+), 96 deletions(-) diff --git a/docs/deprecations.md b/docs/deprecations.md index d5d5b5102c1..2f7c4158472 100644 --- a/docs/deprecations.md +++ b/docs/deprecations.md @@ -6,6 +6,12 @@ layout: Doc # Serverless Framework Deprecations +
 
+ +## Support for `alexaSkill` event without `appId` is to be removed + +Starting with v3.0.0, support for `alexaSkill` event without `appId` provided will be removed. +
 
## Defining extensions to nonexistent resources in `resources.extensions` diff --git a/lib/plugins/aws/package/compile/events/alexaSkill/index.js b/lib/plugins/aws/package/compile/events/alexaSkill/index.js index 6f03c2c2704..63f387db59d 100644 --- a/lib/plugins/aws/package/compile/events/alexaSkill/index.js +++ b/lib/plugins/aws/package/compile/events/alexaSkill/index.js @@ -11,9 +11,19 @@ class AwsCompileAlexaSkillEvents { 'package:compileEvents': this.compileAlexaSkillEvents.bind(this), }; - // TODO: Complete schema, see https://github.com/serverless/serverless/issues/8023 this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'alexaSkill', { - anyOf: [{ type: 'string' }, { type: 'object' }], + anyOf: [ + { $ref: '#/definitions/awsAlexaEventToken' }, + { + type: 'object', + properties: { + appId: { $ref: '#/definitions/awsAlexaEventToken' }, + enabled: { type: 'boolean' }, + }, + required: ['appId'], + additionalProperties: false, + }, + ], }); } @@ -28,34 +38,16 @@ class AwsCompileAlexaSkillEvents { let enabled = true; let appId; if (event === 'alexaSkill') { - const warningMessage = [ - "Warning! You are using an old syntax for alexaSkill which doesn't", - ' restrict the invocation solely to your skill.', - ' Please refer to the documentation for additional information.', - ].join(''); - this.serverless.cli.log(warningMessage); + this.serverless._logDeprecation( + 'ALEXA_SKILL_EVENT_WITHOUT_APP_ID', + 'Starting with next major version, support for alexaSkill event without appId specified will be removed.' + ); } else if (typeof event.alexaSkill === 'string') { appId = event.alexaSkill; - } else if (_.isPlainObject(event.alexaSkill)) { - if (typeof event.alexaSkill.appId !== 'string') { - const errorMessage = [ - `Missing "appId" property for alexaSkill event in function ${functionName}`, - ' The correct syntax is: appId: amzn1.ask.skill.xx-xx-xx-xx-xx', - ' OR an object with "appId" property.', - ' Please check the docs for more info.', - ].join(''); - throw new this.serverless.classes.Error(errorMessage); - } + } else { appId = event.alexaSkill.appId; // Parameter `enabled` is optional, hence the explicit non-equal check for false. enabled = event.alexaSkill.enabled !== false; - } else { - const errorMessage = [ - `Alexa Skill event of function "${functionName}" is not an object or string.`, - ' The correct syntax is: alexaSkill.', - ' Please check the docs for more info.', - ].join(''); - throw new this.serverless.classes.Error(errorMessage); } alexaSkillNumberInFunction++; diff --git a/lib/plugins/aws/package/compile/events/alexaSkill/index.test.js b/lib/plugins/aws/package/compile/events/alexaSkill/index.test.js index 3c96f9128c1..49f78972be2 100644 --- a/lib/plugins/aws/package/compile/events/alexaSkill/index.test.js +++ b/lib/plugins/aws/package/compile/events/alexaSkill/index.test.js @@ -10,19 +10,11 @@ const Serverless = require('../../../../../../Serverless'); describe('AwsCompileAlexaSkillEvents', () => { let serverless; let awsCompileAlexaSkillEvents; - let consolePrinted; beforeEach(() => { serverless = new Serverless(); serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} }; serverless.setProvider('aws', new AwsProvider(serverless)); - consolePrinted = ''; - serverless.cli = { - // serverless.cli isn't available in tests, so we will mimic it. - log: txt => { - consolePrinted += `${txt}\r\n`; - }, - }; awsCompileAlexaSkillEvents = new AwsCompileAlexaSkillEvents(serverless); }); @@ -35,69 +27,6 @@ describe('AwsCompileAlexaSkillEvents', () => { }); describe('#compileAlexaSkillEvents()', () => { - it('should show a warning if alexaSkill appId is not specified', () => { - awsCompileAlexaSkillEvents.serverless.service.functions = { - first: { - events: ['alexaSkill'], - }, - }; - - awsCompileAlexaSkillEvents.compileAlexaSkillEvents(); - - expect(consolePrinted).to.contain.string('old syntax for alexaSkill'); - - expect( - awsCompileAlexaSkillEvents.serverless.service.provider.compiledCloudFormationTemplate - .Resources.FirstLambdaPermissionAlexaSkill1.Type - ).to.equal('AWS::Lambda::Permission'); - expect( - awsCompileAlexaSkillEvents.serverless.service.provider.compiledCloudFormationTemplate - .Resources.FirstLambdaPermissionAlexaSkill1.Properties.FunctionName - ).to.deep.equal({ 'Fn::GetAtt': ['FirstLambdaFunction', 'Arn'] }); - expect( - awsCompileAlexaSkillEvents.serverless.service.provider.compiledCloudFormationTemplate - .Resources.FirstLambdaPermissionAlexaSkill1.Properties.Action - ).to.equal('lambda:InvokeFunction'); - expect( - awsCompileAlexaSkillEvents.serverless.service.provider.compiledCloudFormationTemplate - .Resources.FirstLambdaPermissionAlexaSkill1.Properties.Principal - ).to.equal('alexa-appkit.amazon.com'); - expect( - awsCompileAlexaSkillEvents.serverless.service.provider.compiledCloudFormationTemplate - .Resources.FirstLambdaPermissionAlexaSkill1.Properties.EventSourceToken - ).to.be.undefined; - }); - - it('should throw an error if alexaSkill event is not a string or an object', () => { - awsCompileAlexaSkillEvents.serverless.service.functions = { - first: { - events: [ - { - alexaSkill: 42, - }, - ], - }, - }; - - expect(() => awsCompileAlexaSkillEvents.compileAlexaSkillEvents()).to.throw(Error); - }); - - it('should throw an error if alexaSkill event appId is not a string', () => { - awsCompileAlexaSkillEvents.serverless.service.functions = { - first: { - events: [ - { - alexaSkill: { - appId: 42, - }, - }, - ], - }, - }; - - expect(() => awsCompileAlexaSkillEvents.compileAlexaSkillEvents()).to.throw(Error); - }); - it('should create corresponding resources when multiple alexaSkill events are provided', () => { const skillId1 = 'amzn1.ask.skill.xx-xx-xx-xx'; const skillId2 = 'amzn1.ask.skill.yy-yy-yy-yy';