Skip to content

Commit

Permalink
Merge pull request #398 from amplify-education/bug/366-get-api-id-for…
Browse files Browse the repository at this point in the history
…-rest-type

Fix getting an api id for different types of the API gateway
  • Loading branch information
rddimon authored Nov 4, 2020
2 parents 7443bbe + 14eb68b commit c5b5beb
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
1 change: 0 additions & 1 deletion .sonarcloud.properties

This file was deleted.

5 changes: 5 additions & 0 deletions src/Globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export default class Globals {
websocket: "WEBSOCKET",
};

public static gatewayAPIIdKeys = {
[Globals.apiTypes.rest]: "restApiId",
[Globals.apiTypes.websocket]: "websocketApiId",
};

public static tlsVersions = {
tls_1_0: "TLS_1_0",
tls_1_2: "TLS_1_2",
Expand Down
17 changes: 9 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,17 +523,18 @@ class ServerlessCustomDomain {
* Gets rest API id from existing config or CloudFormation stack
*/
public async getApiId(domain: DomainConfig): Promise<string> {
const apiGateway = this.serverless.service.provider.apiGateway;
if (apiGateway && apiGateway.restApiId) {
const restApiId = apiGateway.restApiId;
const apiGateway = this.serverless.service.provider.apiGateway || {};
const apiIdKey = Globals.gatewayAPIIdKeys[domain.apiType];
const apiId = apiGateway[apiIdKey];
if (apiId) {
// if string value exists return the value
if (typeof restApiId === "string") {
Globals.logInfo(`Mapping custom domain to existing API ${restApiId}.`);
return restApiId;
if (typeof apiId === "string") {
Globals.logInfo(`Mapping custom domain to existing API ${apiId}.`);
return apiId;
}
// in case object and Fn::ImportValue try to get restApiId from the CloudFormation exports
if (typeof restApiId === "object" && restApiId["Fn::ImportValue"]) {
const importName = restApiId["Fn::ImportValue"];
if (typeof apiId === "object" && apiId["Fn::ImportValue"]) {
const importName = apiId["Fn::ImportValue"];
let importValues;
try {
importValues = await this.cloudFormationWrapper.getImportValues([importName]);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface ServerlessInstance { // tslint:disable-line
},
apiGateway: {
restApiId: string,
websocketApiId: string,
},
}
custom: {
Expand Down
1 change: 1 addition & 0 deletions test/unit-tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const constructPlugin = (customDomainOptions, multiple: boolean = false) => {
provider: {
apiGateway: {
restApiId: null,
websocketApiId: null,
},
compiledCloudFormationTemplate: {
Outputs: null,
Expand Down

0 comments on commit c5b5beb

Please sign in to comment.