Skip to content

Commit 5d42784

Browse files
committed
rewrite sddNestedResource method to be more readable
1 parent a62435c commit 5d42784

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

packages/rest-api-construct/src/construct.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,16 @@ export class RestApiConstruct extends Construct {
6868
root: apiGateway.IResource,
6969
path: string,
7070
): apiGateway.IResource {
71-
return path.split('/').reduce((resource, part) => {
72-
return resource.getResource(part) ?? resource.addResource(part);
73-
}, root);
71+
// Split the path into parts (e.g. "posts/comments" → ["posts", "comments"])
72+
const parts = path.split('/');
73+
74+
// Traverse the path, adding any missing nested resources along the way
75+
let current = root;
76+
for (const part of parts) {
77+
const existing = current.getResource(part);
78+
current = existing ?? current.addResource(part);
79+
}
80+
81+
return current;
7482
}
7583
}

0 commit comments

Comments
 (0)