From e1ca63c06a824e18fdd92f5c6c3efbf7f5f644d2 Mon Sep 17 00:00:00 2001 From: Raul Zaldana <35144857+zaldanaraul@users.noreply.github.com> Date: Fri, 18 Sep 2020 11:36:10 -0400 Subject: [PATCH] feat(Config Schema): Schema for AWS `websocket` event (#8218) --- lib/configSchema.js | 4 ++ .../compile/events/websockets/index.js | 32 ++++++++++++- .../compile/events/websockets/lib/validate.js | 14 ------ .../events/websockets/lib/validate.test.js | 45 ------------------- lib/plugins/aws/provider/awsProvider.js | 15 +++++++ 5 files changed, 49 insertions(+), 61 deletions(-) diff --git a/lib/configSchema.js b/lib/configSchema.js index fbf49a22dea..729890835cb 100644 --- a/lib/configSchema.js +++ b/lib/configSchema.js @@ -129,6 +129,10 @@ const schema = { type: 'string', pattern: '^[A-Z0-9_]+$', }, + functionName: { + type: 'string', + pattern: functionNamePattern, + }, }, }; diff --git a/lib/plugins/aws/package/compile/events/websockets/index.js b/lib/plugins/aws/package/compile/events/websockets/index.js index 3b51658db73..64bb2a0e37a 100644 --- a/lib/plugins/aws/package/compile/events/websockets/index.js +++ b/lib/plugins/aws/package/compile/events/websockets/index.js @@ -51,9 +51,37 @@ class AwsCompileWebsockets { }, }; - // TODO: Complete schema, see https://github.com/serverless/serverless/issues/8019 this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'websocket', { - anyOf: [{ type: 'string' }, { type: 'object' }], + anyOf: [ + { type: 'string' }, + { + type: 'object', + properties: { + route: { type: 'string' }, + routeResponseSelectionExpression: { + const: '$default', + }, + authorizer: { + anyOf: [ + { $ref: '#/definitions/awsArnString' }, + { $ref: '#/definitions/functionName' }, + { + type: 'object', + properties: { + name: { $ref: '#/definitions/functionName' }, + arn: { $ref: '#/definitions/awsArnString' }, + identitySource: { type: 'array', items: { type: 'string' } }, + }, + oneOf: [{ required: ['name'] }, { required: ['arn'] }], + additionalProperties: false, + }, + ], + }, + }, + required: ['route'], + additionalProperties: false, + }, + ], }); } } diff --git a/lib/plugins/aws/package/compile/events/websockets/lib/validate.js b/lib/plugins/aws/package/compile/events/websockets/lib/validate.js index cb63e65f388..f25e92bb4a7 100644 --- a/lib/plugins/aws/package/compile/events/websockets/lib/validate.js +++ b/lib/plugins/aws/package/compile/events/websockets/lib/validate.js @@ -13,19 +13,9 @@ module.exports = { _.forEach(this.serverless.service.functions, (functionObject, functionName) => { functionObject.events.forEach(event => { - // check if we have both, `http` and `websocket` events which is not supported - if (event.websocket && event.http) { - const errorMessage = 'The event type can either be "http" or "websocket" but not both.'; - throw new this.serverless.classes.Error(errorMessage); - } if (event.websocket) { // dealing with the extended object definition if (_.isObject(event.websocket)) { - if (!event.websocket.route) { - const errorMessage = 'You need to set the "route" when using the websocket event.'; - throw new this.serverless.classes.Error(errorMessage); - } - const websocketObj = { functionName, route: event.websocket.route, @@ -126,10 +116,6 @@ module.exports = { ], }; websocketObj.authorizer.permission = lambdaLogicalId; - } else { - const errorMessage = - 'You must specify name or arn properties when using a websocket authorizer'; - throw new this.serverless.classes.Error(errorMessage); } if (!event.websocket.authorizer.identitySource) { diff --git a/lib/plugins/aws/package/compile/events/websockets/lib/validate.test.js b/lib/plugins/aws/package/compile/events/websockets/lib/validate.test.js index 69f15259bfd..35c7d8a0de4 100644 --- a/lib/plugins/aws/package/compile/events/websockets/lib/validate.test.js +++ b/lib/plugins/aws/package/compile/events/websockets/lib/validate.test.js @@ -267,49 +267,4 @@ describe('#validate()', () => { .to.be.an('Array') .with.length(0); }); - - it('should reject a websocket event without a route', () => { - awsCompileWebsocketsEvents.serverless.service.functions = { - first: { - events: [ - { - websocket: {}, - }, - ], - }, - }; - expect(() => awsCompileWebsocketsEvents.validate()).to.throw(/set the "route"/); - }); - - it('should reject an authorizer definition without name nor arn', () => { - awsCompileWebsocketsEvents.serverless.service.functions = { - first: { - events: [ - { - websocket: { - route: '$connect', - authorizer: { - identitySource: ['route.request.header.Auth', 'route.request.querystring.Auth'], - }, - }, - }, - ], - }, - }; - expect(() => awsCompileWebsocketsEvents.validate()).to.throw(/You must specify name or arn/); - }); - - it('should reject a usage of both, http and websocket event types', () => { - awsCompileWebsocketsEvents.serverless.service.functions = { - first: { - events: [ - { - websocket: {}, - http: {}, - }, - ], - }, - }; - expect(() => awsCompileWebsocketsEvents.validate()).to.throw(/can either be/); - }); }); diff --git a/lib/plugins/aws/provider/awsProvider.js b/lib/plugins/aws/provider/awsProvider.js index bd90a405a6b..11f46d7a9c4 100644 --- a/lib/plugins/aws/provider/awsProvider.js +++ b/lib/plugins/aws/provider/awsProvider.js @@ -314,6 +314,18 @@ class AwsProvider { }, ], }, + websocket: { + oneOf: [ + { type: 'boolean' }, + { + type: 'object', + properties: { + level: { enum: ['INFO', 'ERROR'] }, + }, + additionalProperties: false, + }, + ], + }, }, }, resourcePolicy: { @@ -322,6 +334,9 @@ class AwsProvider { type: 'object', }, }, + websocketsApiName: { type: 'string' }, + websocketsApiRouteSelectionExpression: { type: 'string' }, + apiGateway: { type: 'object', properties: { websocketApiId: { type: 'string' } } }, }, }, function: {