Skip to content

Commit

Permalink
Merge pull request #222 from getlift/fix-extension-array
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericbarthelet committed Jun 6, 2022
2 parents 94387e6 + 45af38c commit 9a5bc53
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
1 change: 0 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"request": "launch",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"cwd": "${workspaceFolder}",
"runtimeExecutable": "yarn",
"args": ["jest", "--runInBand", "--watchAll=false"]
Expand Down
25 changes: 23 additions & 2 deletions src/constructs/abstracts/AwsConstruct.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Construct as CdkConstruct } from "constructs";
import type { AwsProvider } from "@lift/providers";
import type { ConstructInterface } from "@lift/constructs";
import { get, isEmpty, isObject } from "lodash";
import { get, isArray, isEmpty, isObject } from "lodash";
import { paths } from "traverse";
import type { CfnResource } from "aws-cdk-lib";
import ServerlessError from "../../utils/error";
Expand All @@ -22,12 +22,33 @@ export abstract class AwsConstruct extends CdkConstruct implements ConstructInte
);
}
if (isObject(extensionObject)) {
const accumulatedPathsPointingToArray: string[] = [];
paths(extensionObject)
.filter((path) => !isEmpty(path))
.map((path) => {
return path.join(".");
})
.filter((path) => !isObject(get(extensionObject, path)))
.filter((path) => {
if (
accumulatedPathsPointingToArray.some((previouslySelectedPath) =>
path.startsWith(previouslySelectedPath)
)
) {
return false;
}

const pointedValue: unknown = get(extensionObject, path);
const isPathPointingToArray = isArray(pointedValue);
if (isPathPointingToArray) {
accumulatedPathsPointingToArray.push(path);

return true;
}

const isPathPointingToLeaf = !isObject(pointedValue);

return isPathPointingToLeaf;
})
.map((path) => {
availableExtensions[extensionKey].addOverride(path, get(extensionObject, path));
});
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/storage/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,19 @@ constructs:
bucket:
Properties:
ObjectLockEnabled: True
extendedBucketWithArray:
type: storage
extensions:
bucket:
Properties:
CorsConfiguration:
CorsRules:
- AllowedOrigins:
- "*"
AllowedHeaders:
- "*"
AllowedMethods:
- GET
- HEAD
- PUT
- POST
14 changes: 14 additions & 0 deletions test/unit/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,18 @@ describe("storage", () => {
ObjectLockEnabled: true,
});
});

it("allows overriding bucket properties with array", () => {
expect(cfTemplate.Resources[computeLogicalId("extendedBucketWithArray", "Bucket")].Properties).toMatchObject({
CorsConfiguration: {
CorsRules: [
{
AllowedOrigins: ["*"],
AllowedHeaders: ["*"],
AllowedMethods: ["GET", "HEAD", "PUT", "POST"],
},
],
},
});
});
});

0 comments on commit 9a5bc53

Please sign in to comment.