diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b41ff40..bbad115b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [7.3.7] - 2023-03-06 ### Fixed -- Added filtering by stage for the `getBasePathMappings` method. +- Added filtering by stage for removing API mappings. That filter is skipped in case `allowPathMatching` is enabled. ## [7.3.6] - 2023-02-13 diff --git a/src/aws/api-gateway-v1-wrapper.ts b/src/aws/api-gateway-v1-wrapper.ts index 1e3e451f..5d1dd749 100644 --- a/src/aws/api-gateway-v1-wrapper.ts +++ b/src/aws/api-gateway-v1-wrapper.ts @@ -81,7 +81,12 @@ class APIGatewayV1Wrapper extends APIGatewayBase { } } - public async getCustomDomain (domain: DomainConfig): Promise { + /** + * Get Custom Domain Info + * @param domain: DomainConfig + * @param silent: To issue an error or not. Not by default. + */ + public async getCustomDomain (domain: DomainConfig, silent: boolean = true): Promise { // Make API call try { const domainInfo: GetDomainNameCommandOutput = await this.apiGateway.send( @@ -91,7 +96,7 @@ class APIGatewayV1Wrapper extends APIGatewayBase { ); return new DomainInfo(domainInfo); } catch (err) { - if (!err.$metadata || err.$metadata.httpStatusCode !== 404) { + if (!err.$metadata || err.$metadata.httpStatusCode !== 404 || !silent) { throw new Error( `V1 - Unable to fetch information about '${domain.givenDomainName}':\n${err.message}` ); @@ -122,8 +127,7 @@ class APIGatewayV1Wrapper extends APIGatewayBase { Logging.logInfo(`V1 - Created API mapping '${domain.basePath}' for '${domain.givenDomainName}'`); } catch (err) { throw new Error( - `V1 - Make sure the '${domain.givenDomainName}' exists. - Unable to create base path mapping for '${domain.givenDomainName}':\n${err.message}` + `V1 - Unable to create base path mapping for '${domain.givenDomainName}':\n${err.message}` ); } } @@ -139,9 +143,7 @@ class APIGatewayV1Wrapper extends APIGatewayBase { domainName: domain.givenDomainName }) ); - return items.filter((item) => { - return item.stage === domain.stage; - }).map((item) => { + return items.map((item) => { return new ApiGatewayMap(item.restApiId, item.basePath, item.stage, null); }); } catch (err) { diff --git a/src/aws/api-gateway-v2-wrapper.ts b/src/aws/api-gateway-v2-wrapper.ts index 12906264..d1b64805 100644 --- a/src/aws/api-gateway-v2-wrapper.ts +++ b/src/aws/api-gateway-v2-wrapper.ts @@ -83,8 +83,9 @@ class APIGatewayV2Wrapper extends APIGatewayBase { /** * Get Custom Domain Info * @param domain: DomainConfig + * @param silent: To issue an error or not. Not by default. */ - public async getCustomDomain (domain: DomainConfig): Promise { + public async getCustomDomain (domain: DomainConfig, silent: boolean = true): Promise { // Make API call try { const domainInfo: GetDomainNameCommandOutput = await this.apiGateway.send( @@ -94,7 +95,7 @@ class APIGatewayV2Wrapper extends APIGatewayBase { ); return new DomainInfo(domainInfo); } catch (err) { - if (!err.$metadata || err.$metadata.httpStatusCode !== 404) { + if (!err.$metadata || err.$metadata.httpStatusCode !== 404 || !silent) { throw new Error( `V2 - Unable to fetch information about '${domain.givenDomainName}':\n${err.message}` ); @@ -167,9 +168,7 @@ class APIGatewayV2Wrapper extends APIGatewayBase { DomainName: domain.givenDomainName }) ); - return items.filter((item) => { - return item.Stage === domain.stage; - }).map( + return items.map( (item) => new ApiGatewayMap(item.ApiId, item.ApiMappingKey, item.Stage, item.ApiMappingId) ); } catch (err) { diff --git a/src/index.ts b/src/index.ts index c156e350..258d9bf8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -353,7 +353,7 @@ class ServerlessCustomDomain { return mapping.apiId === domain.apiId; }); domain.apiMapping = filteredMappings ? filteredMappings[0] : null; - domain.domainInfo = await apiGateway.getCustomDomain(domain); + domain.domainInfo = await apiGateway.getCustomDomain(domain, false); if (!domain.apiMapping) { await apiGateway.createBasePathMapping(domain); @@ -385,7 +385,7 @@ class ServerlessCustomDomain { if (domain.allowPathMatching) { return mapping.basePath === domain.basePath; } - return mapping.apiId === domain.apiId; + return mapping.apiId === domain.apiId && mapping.stage === domain.stage; }); if (domain.preserveExternalPathMappings) { externalBasePathExists = mappings.length > filteredMappings.length; diff --git a/src/models/apigateway-base.ts b/src/models/apigateway-base.ts index a12089a9..9ae455c6 100644 --- a/src/models/apigateway-base.ts +++ b/src/models/apigateway-base.ts @@ -5,7 +5,7 @@ import DomainConfig = require("./domain-config"); abstract class APIGatewayBase { abstract createCustomDomain(domain: DomainConfig): Promise; - abstract getCustomDomain(domain: DomainConfig): Promise; + abstract getCustomDomain(domain: DomainConfig, silent?: boolean): Promise; abstract deleteCustomDomain(domain: DomainConfig): Promise; diff --git a/test/unit-tests/aws/api-gateway-v1-wrapper.test.ts b/test/unit-tests/aws/api-gateway-v1-wrapper.test.ts index 1d9b2f16..314b491e 100644 --- a/test/unit-tests/aws/api-gateway-v1-wrapper.test.ts +++ b/test/unit-tests/aws/api-gateway-v1-wrapper.test.ts @@ -339,13 +339,9 @@ describe("API Gateway V1 wrapper checks", () => { restApiId: "test_rest_api_id", basePath: "test", stage: "test" - }, { + },{ restApiId: "test_rest_api_id2", basePath: "test2", - stage: "test" - }, { - restApiId: "test_rest_api_id3", - basePath: "test3", stage: "dummy" }] }); @@ -359,7 +355,7 @@ describe("API Gateway V1 wrapper checks", () => { // should be filtered by stage const expectedResult = [ new ApiGatewayMap("test_rest_api_id", "test", "test", null), - new ApiGatewayMap("test_rest_api_id2", "test2", "test", null) + new ApiGatewayMap("test_rest_api_id2", "test2", "dummy", null) ]; expect(actualResult).to.eql(expectedResult); diff --git a/test/unit-tests/aws/api-gateway-v2-wrapper.test.ts b/test/unit-tests/aws/api-gateway-v2-wrapper.test.ts index ac2d7efc..527b18eb 100644 --- a/test/unit-tests/aws/api-gateway-v2-wrapper.test.ts +++ b/test/unit-tests/aws/api-gateway-v2-wrapper.test.ts @@ -372,16 +372,11 @@ describe("API Gateway V2 wrapper checks", () => { ApiMappingKey: "test", Stage: "test", ApiMappingId: "test_id" - }, { + },{ ApiId: "test_rest_api_id2", ApiMappingKey: "test2", - Stage: "test", - ApiMappingId: "test_id2" - }, { - ApiId: "test_rest_api_id3", - ApiMappingKey: "test3", Stage: "dummy", - ApiMappingId: "test_id3" + ApiMappingId: "test_id2" }] }); @@ -394,7 +389,7 @@ describe("API Gateway V2 wrapper checks", () => { // should be filtered by stage const expectedResult = [ new ApiGatewayMap("test_rest_api_id", "test", "test", "test_id"), - new ApiGatewayMap("test_rest_api_id2", "test2", "test", "test_id2") + new ApiGatewayMap("test_rest_api_id2", "test2", "dummy", "test_id2") ]; expect(actualResult).to.eql(expectedResult); diff --git a/test/unit-tests/index.test.ts b/test/unit-tests/index.test.ts index 01c4b0b8..43384ad3 100644 --- a/test/unit-tests/index.test.ts +++ b/test/unit-tests/index.test.ts @@ -490,6 +490,10 @@ describe("Custom Domain Plugin", () => { }); APIGatewayMock.on(GetBasePathMappingsCommand).resolves({ items: [{ + restApiId: "test_rest_api_id", + basePath: "test2", + stage: "dummy" + }, { restApiId: "test_rest_api_id", basePath: "test", stage: "test" @@ -518,8 +522,6 @@ describe("Custom Domain Plugin", () => { const deleteDomainSpy = chaiSpy.on(plugin, "deleteDomain"); await plugin.hooks["before:remove:remove"](); - const commandCalls = APIGatewayMock.commandCalls(DeleteBasePathMappingCommand); - expect(commandCalls.length).to.equal(1); expect(deleteDomainSpy).to.have.been.called(); });