Repository: amplify-education/serverless-domain-manager (ts)

Issue:
Version 6.4.2 causes error setting base path mappings for API Gateway V1
<!--- Please keep this note for the community --->

### Community Note

* Please vote on this issue by adding a 👍 [reaction](https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) to the original issue to help the community and maintainers prioritize this request
* Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
* If you are interested in working on this issue or have submitted a pull request, please leave a comment

<!--- Thank you for keeping this note for the community --->

## Bug Report

**Error Description**

Since our pipeline picked up and used version 6.4.2 it has been failing with an error saying that API Gateway V1 doesn't support the slash character (/) in base path mappings. However this is not the case it is simply that V2 of the API needs to be used for this to work, even for V1.

Please ref to this discussion around something similar using Terraform that mentions the CLI of the same for AWS functionality used here: https://github.com/hashicorp/terraform-provider-aws/issues/21986#issuecomment-996231026

Our build works fine if we set the version of this plugin to 6.4.1

**Command Run**
`sls deploy`.

**Console Output**
```sh
Error:
--
234 | Error: V1 - Unable to update base path mapping for '###.#####.####.com':
235 | API Gateway V1 doesn't support the slash character (/) in base path mappings. To create a multi-level base path mapping, use API Gateway V2.
236 | at APIGatewayV1Wrapper.<anonymous> (/codebuild/output/src/#######/node_modules/serverless-domain-manager/dist/src/aws/api-gateway-v1-wrapper.js:144:23)
237 | at Generator.throw (<anonymous>)
238 | at rejected (/codebuild/output/src/#######/node_modules/serverless-domain-manager/dist/src/aws/api-gateway-v1-wrapper.js:6:65)
239 | at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
240 | ERROR [deploy_serverless]: sls deploy failed
```

**Domain Manager Configuration**
```yaml
custom:
  customDomain:
    domainName: api.blah.blah.com
    basePath: this/is/my/basepath
    stage: dev
    createRoute53Record: false
    endpointType: 'regional'
```

**Versions**
- Domain Manager version(s): 6.4.2
- Node/npm version: 18.14.0 / 8
- Serverless Version: 3.72.0
- Lambda Code: JavaScript


Pull request description:
Check base path and not stage for '/' in rest API creation
<!--- Information about referencing Github Issues: https://help.github.com/articles/basic-writing-and-formatting-syntax/#referencing-issues-and-pull-requests --->
Fixes #558 

**Description of Issue Fixed**
Hi there, I've encountered the same issue that was described in #558 , however the update to 6.4.3 doesn't seem to have fixed this. I think the check for a '/' in the baseStage parameter should instead be checking the 'basePath' parameter, when determining the API wrapper version to use.

**Changes proposed in this pull request**:

* Check basePath parameter for '/'
* Test case for the above

<!--- Please remember to allow edits from maintainers: https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/allowing-changes-to-a-pull-request-branch-created-from-a-fork --->


Patch:
diff --git a/src/index.ts b/src/index.ts
index 3b20148..034173d 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -193,7 +193,7 @@ class ServerlessCustomDomain {
         // multi-level base path mapping is supported by Gateway V2
         // https://github.com/amplify-education/serverless-domain-manager/issues/558
         // https://aws.amazon.com/blogs/compute/using-multiple-segments-in-amazon-api-gateway-base-path-mapping/
-        if (domain.baseStage.includes("/")) {
+        if (domain.basePath.includes("/")) {
             return this.apiGatewayV2Wrapper;
         }
 


Test patch:
diff --git a/test/unit-tests/index.test.ts b/test/unit-tests/index.test.ts
index 7cafae9..ee33ebf 100644
--- a/test/unit-tests/index.test.ts
+++ b/test/unit-tests/index.test.ts
@@ -324,6 +324,36 @@ describe("Custom Domain Plugin", () => {
             });
         });
 
+        it("Creates basepath mapping for regional tls 1.2 REST api with '/' in base path", async () => {
+            AWS.mock("ApiGatewayV2", "createApiMapping", (params, callback) => {
+                callback(null, params);
+            });
+            const plugin = constructPlugin({
+                apiType: "rest",
+                basePath: "test/basepath",
+                domainName: "test_domain",
+                endpointType: "REGIONAL",
+            });
+            plugin.initializeVariables();
+            plugin.initAWSResources();
+
+            const dc: DomainConfig = new DomainConfig(plugin.serverless.service.custom.customDomain);
+            dc.apiId = "test_rest_api_id";
+
+            const apiGateway = plugin.getApiGateway(dc);
+            const spy = chai.spy.on(apiGateway.apiGateway, "createApiMapping");
+
+            await apiGateway.createBasePathMapping(dc);
+
+            expect(spy).to.have.been.called.exactly(1)
+            expect(spy).to.have.been.called.with({
+                ApiId: "test_rest_api_id",
+                ApiMappingKey: "test/basepath",
+                DomainName: "test_domain",
+                Stage: "test",
+            });
+        });
+
         it("Creates basepath mapping for regional HTTP/Websocket api", async () => {
             AWS.mock("ApiGatewayV2", "createApiMapping", (params, callback) => {
                 callback(null, params);