feat(AWS HTTP API): Support shouldStartNameWithService option (#9758)

This commit is contained in:
Thiago Moraes 2021-08-18 17:36:00 -03:00 committed by GitHub
parent c9e851ea60
commit ef5a8faf13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 1 deletions

View File

@ -443,3 +443,13 @@ provider:
httpApi:
disableDefaultEndpoint: true
```
### Service Naming
You can use the `shouldStartNameWithService` option to change the naming scheme for HTTP API from the default `${stage}-${service}` to `${service}-${stage}`.
```yml
provider:
httpApi:
shouldStartNameWithService: true
```

View File

@ -652,7 +652,10 @@ module.exports = {
) {
return `${String(this.provider.serverless.service.provider.httpApi.name)}`;
}
return `${this.provider.getStage()}-${this.provider.serverless.service.service}`;
return _.get(this.provider.serverless.service.provider.httpApi, 'shouldStartNameWithService')
? `${this.provider.serverless.service.service}-${this.provider.getStage()}`
: `${this.provider.getStage()}-${this.provider.serverless.service.service}`;
},
getHttpApiLogicalId() {
return 'HttpApi';

View File

@ -912,6 +912,7 @@ class AwsProvider {
metrics: { type: 'boolean' },
useProviderTags: { const: true },
disableDefaultEndpoint: { type: 'boolean' },
shouldStartNameWithService: { const: true },
},
additionalProperties: false,
},

View File

@ -1025,4 +1025,28 @@ describe('#naming()', () => {
);
});
});
describe('#getHttpApiName()', () => {
it('should return the composition of service & stage name if custom name not provided and shouldStartNameWithService is true', () => {
serverless.service.service = 'myService';
serverless.service.provider.httpApi = { shouldStartNameWithService: true };
expect(sdk.naming.getHttpApiName()).to.equal(
`${serverless.service.service}-${sdk.naming.provider.getStage()}`
);
});
it('should return the composition of stage & service name if custom name not provided', () => {
serverless.service.service = 'myService';
expect(sdk.naming.getHttpApiName()).to.equal(
`${sdk.naming.provider.getStage()}-${serverless.service.service}`
);
});
it('should return the custom api name if provided', () => {
serverless.service.provider.httpApi = { name: 'app-dev-testApi' };
serverless.service.service = 'myService';
serverless.service.provider.stage = sdk.naming.provider.getStage();
expect(sdk.naming.getHttpApiName()).to.equal('app-dev-testApi');
});
});
});