Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getting an api id for different types of the API gateway #398

Merged
merged 4 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 9 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import APIGatewayWrapper = require("./aws/api-gateway-wrapper");
import CloudFormationWrapper = require("./aws/cloud-formation-wrapper");
import DomainConfig = require("./DomainConfig");
import DomainInfo = require("./DomainInfo");
import Globals from "./Globals";
import {CustomDomain, ServerlessInstance, ServerlessOptions} from "./types";
import {getAWSPagedResults, sleep, throttledCall} from "./utils";
Expand Down Expand Up @@ -524,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