|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const BbPromise = require('bluebird'); |
| 4 | +const _ = require('lodash'); |
| 5 | + |
| 6 | +module.exports = { |
| 7 | + compileRequestValidators() { |
| 8 | + const apiGatewayConfig = this.serverless.service.provider.apiGateway || {}; |
| 9 | + |
| 10 | + this.pluginhttpValidated.events.forEach((event) => { |
| 11 | + const resourceName = this.getResourceName(event.http.path); |
| 12 | + const methodLogicalId = this.provider.naming |
| 13 | + .getMethodLogicalId(resourceName, event.http.method); |
| 14 | + const template = this.serverless.service.provider |
| 15 | + .compiledCloudFormationTemplate.Resources[methodLogicalId]; |
| 16 | + let validatorLogicalId; |
| 17 | + |
| 18 | + if (event.http.request && event.http.request.schemas) { |
| 19 | + for (const [contentType, schemaConfig] of Object.entries(event.http.request.schemas)) { |
| 20 | + let modelLogicalId; |
| 21 | + |
| 22 | + const referencedDefinitionFromProvider = !_.isObject(schemaConfig) && _.get(apiGatewayConfig, `request.schemas.${schemaConfig}`); |
| 23 | + |
| 24 | + if (referencedDefinitionFromProvider) { |
| 25 | + modelLogicalId = this.createProviderModel( |
| 26 | + schemaConfig, |
| 27 | + apiGatewayConfig.request.schemas[schemaConfig], |
| 28 | + ); |
| 29 | + } else { |
| 30 | + // In this situation, we have two options - schema is defined as |
| 31 | + // string that does not reference model from provider or as an object |
| 32 | + let modelName; |
| 33 | + let description; |
| 34 | + let definition; |
| 35 | + |
| 36 | + if (_.isObject(schemaConfig)) { |
| 37 | + if (schemaConfig.schema) { |
| 38 | + // In this case, schema is defined as an object with explicit properties |
| 39 | + modelName = schemaConfig.name; |
| 40 | + description = schemaConfig.description; |
| 41 | + definition = schemaConfig.schema; |
| 42 | + } else { |
| 43 | + // In this case, schema is defined as an implicit object that |
| 44 | + // stores whole schema definition |
| 45 | + definition = schemaConfig; |
| 46 | + } |
| 47 | + } else { |
| 48 | + // In this case, schema is defined as an implicit string |
| 49 | + definition = schemaConfig; |
| 50 | + } |
| 51 | + |
| 52 | + modelLogicalId = this.provider.naming.getEndpointModelLogicalId( |
| 53 | + resourceName, |
| 54 | + event.http.method, |
| 55 | + contentType, |
| 56 | + ); |
| 57 | + |
| 58 | + Object.assign( |
| 59 | + this.serverless.service.provider.compiledCloudFormationTemplate.Resources, |
| 60 | + { |
| 61 | + [modelLogicalId]: { |
| 62 | + Type: 'AWS::ApiGateway::Model', |
| 63 | + Properties: { |
| 64 | + RestApiId: this.provider.getApiGatewayRestApiId(), |
| 65 | + ContentType: contentType, |
| 66 | + Schema: definition, |
| 67 | + Name: modelName, |
| 68 | + Description: description, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + if (!validatorLogicalId) { |
| 76 | + const requestValidator = this.createRequestValidator(); |
| 77 | + validatorLogicalId = requestValidator.validatorLogicalId; |
| 78 | + } |
| 79 | + |
| 80 | + template.Properties.RequestValidatorId = { Ref: validatorLogicalId }; |
| 81 | + template.Properties.RequestModels = template.Properties.RequestModels || {}; |
| 82 | + template.Properties.RequestModels[contentType] = { Ref: modelLogicalId }; |
| 83 | + } |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + return BbPromise.resolve(); |
| 88 | + }, |
| 89 | + |
| 90 | + createProviderModel(schemaId, schemaConfig) { |
| 91 | + let modelName; |
| 92 | + let description; |
| 93 | + let definition; |
| 94 | + |
| 95 | + // If schema is not defined this will try to map resourceDefinition as the schema |
| 96 | + if (!schemaConfig.schema) { |
| 97 | + definition = schemaConfig; |
| 98 | + } else { |
| 99 | + definition = schemaConfig.schema; |
| 100 | + } |
| 101 | + |
| 102 | + const modelLogicalId = this.provider.naming.getModelLogicalId(schemaId); |
| 103 | + |
| 104 | + if (schemaConfig.name) { |
| 105 | + modelName = schemaConfig.name; |
| 106 | + } |
| 107 | + |
| 108 | + if (schemaConfig.description) { |
| 109 | + description = schemaConfig.description; |
| 110 | + } |
| 111 | + |
| 112 | + Object.assign(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, { |
| 113 | + [modelLogicalId]: { |
| 114 | + Type: 'AWS::ApiGateway::Model', |
| 115 | + Properties: { |
| 116 | + RestApiId: this.provider.getApiGatewayRestApiId(), |
| 117 | + Schema: definition, |
| 118 | + ContentType: 'application/json', |
| 119 | + }, |
| 120 | + }, |
| 121 | + }); |
| 122 | + |
| 123 | + if (modelName) { |
| 124 | + this.serverless.service.provider.compiledCloudFormationTemplate.Resources[ |
| 125 | + modelLogicalId |
| 126 | + ].Properties.Name = modelName; |
| 127 | + } |
| 128 | + |
| 129 | + if (description) { |
| 130 | + this.serverless.service.provider.compiledCloudFormationTemplate.Resources[ |
| 131 | + modelLogicalId |
| 132 | + ].Properties.Description = description; |
| 133 | + } |
| 134 | + |
| 135 | + return modelLogicalId; |
| 136 | + }, |
| 137 | + |
| 138 | + createRequestValidator() { |
| 139 | + const validatorLogicalId = this.provider.naming.getValidatorLogicalId(); |
| 140 | + const validatorName = `${ |
| 141 | + this.serverless.service.service |
| 142 | + }-${this.provider.getStage()} | Validate request body and querystring parameters`; |
| 143 | + |
| 144 | + this.serverless.service.provider.compiledCloudFormationTemplate |
| 145 | + .Resources[validatorLogicalId] = { |
| 146 | + Type: 'AWS::ApiGateway::RequestValidator', |
| 147 | + Properties: { |
| 148 | + RestApiId: this.provider.getApiGatewayRestApiId(), |
| 149 | + ValidateRequestBody: true, |
| 150 | + ValidateRequestParameters: true, |
| 151 | + Name: validatorName, |
| 152 | + }, |
| 153 | + }; |
| 154 | + |
| 155 | + return { |
| 156 | + validatorLogicalId, |
| 157 | + validatorName, |
| 158 | + }; |
| 159 | + }, |
| 160 | +}; |
0 commit comments