Skip to content

Commit

Permalink
fix(amplify): cannot map branch to domain root (#7621)
Browse files Browse the repository at this point in the history
Correctly handle the default for `prefix`. An empty string now maps to the domain
root. Also add a `mapRoot` method.

Closes #7590

BREAKING CHANGE: `mapSubDomain()` called with an empty string for `prefix` now
maps to the domain root.
  • Loading branch information
jogold committed May 3, 2020
1 parent 9df5486 commit da7c508
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-amplify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ amplifyApp.addCustomRule({
Add a domain and map sub domains to branches:
```ts
const domain = amplifyApp.addDomain('example.com');
domain.mapRoot(master); // map master branch to domain root
domain.mapSubDomain(master, 'www');
domain.mapSubDomain(dev); // sub domain prefix defaults to branch name
```
Expand Down
14 changes: 12 additions & 2 deletions packages/@aws-cdk/aws-amplify/lib/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,22 @@ export class Domain extends Resource {

/**
* Maps a branch to a sub domain
*
* @param branch The branch
* @param prefix The prefix. Use '' to map to the root of the domain. Defaults to branch name.
*/
public mapSubDomain(branch: IBranch, prefix?: string) {
this.subDomains.push({ branch, prefix });
return this;
}

/**
* Maps a branch to the domain root
*/
public mapRoot(branch: IBranch) {
return this.mapSubDomain(branch, '');
}

protected validate() {
if (this.subDomains.length === 0) {
return ['The domain doesn\'t contain any subdomains'];
Expand All @@ -112,7 +122,7 @@ export class Domain extends Resource {
private renderSubDomainSettings() {
return this.subDomains.map(s => ({
branchName: s.branch.branchName,
prefix: s.prefix || s.branch.branchName,
prefix: s.prefix === undefined ? s.branch.branchName : s.prefix,
}));
}
}
Expand All @@ -127,7 +137,7 @@ export interface SubDomain {
readonly branch: IBranch;

/**
* The prefix
* The prefix. Use '' to map to the root of the domain
*
* @default - the branch name
*/
Expand Down
39 changes: 39 additions & 0 deletions packages/@aws-cdk/aws-amplify/test/domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,45 @@ test('create a domain', () => {
});
});

test('map a branch to the domain root', () => {
// GIVEN
const stack = new Stack();
const app = new amplify.App(stack, 'App', {
sourceCodeProvider: new amplify.GitHubSourceCodeProvider({
owner: 'aws',
repository: 'aws-cdk',
oauthToken: SecretValue.plainText('secret'),
}),
});
const prodBranch = app.addBranch('master');

// WHEN
const domain = app.addDomain('amazon.com');
domain.mapRoot(prodBranch);

// THEN
expect(stack).toHaveResource('AWS::Amplify::Domain', {
AppId: {
'Fn::GetAtt': [
'AppF1B96344',
'AppId',
],
},
DomainName: 'amazon.com',
SubDomainSettings: [
{
BranchName: {
'Fn::GetAtt': [
'Appmaster71597E87',
'BranchName',
],
},
Prefix: '',
},
],
});
});

test('throws at synthesis without subdomains', () => {
// GIVEN
const app = new App();
Expand Down

0 comments on commit da7c508

Please sign in to comment.