Skip to content

Commit 012eeed

Browse files
jogoldmergify[bot]
authored andcommitted
feat(apigateway): add convenience url property at resource level (#4686)
* feat(apigateway): add convenience url property at resource level This allows to reference the URL of a resource without having to do `api.urlForPath(resource.path)` or `resource.restApi.urlForPath(resource.path)`. * use getter
1 parent 561bb73 commit 012eeed

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

packages/@aws-cdk/aws-apigateway/lib/resource.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,10 @@ export abstract class ResourceBase extends ResourceConstruct implements IResourc
352352

353353
return resource.resourceForPath(parts.join('/'));
354354
}
355+
356+
public get url(): string {
357+
return this.restApi.urlForPath(this.path);
358+
}
355359
}
356360

357361
export class Resource extends ResourceBase {

packages/@aws-cdk/aws-apigateway/test/test.resource.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,51 @@ export = {
178178
test.done();
179179
},
180180

181+
'url for a resource'(test: Test) {
182+
// GIVEN
183+
const stack = new Stack();
184+
const api = new apigw.RestApi(stack, 'api');
185+
186+
// WHEN
187+
const aResource = api.root.addResource('a');
188+
const cResource = aResource.addResource('b').addResource('c');
189+
190+
// THEN
191+
test.deepEqual(stack.resolve(aResource.url), {
192+
'Fn::Join': [
193+
'',
194+
[
195+
'https://',
196+
{ Ref: 'apiC8550315' },
197+
'.execute-api.',
198+
{ Ref: 'AWS::Region' },
199+
'.',
200+
{ Ref: 'AWS::URLSuffix' },
201+
'/',
202+
{ Ref: 'apiDeploymentStageprod896C8101' },
203+
'/a'
204+
]
205+
]
206+
});
207+
test.deepEqual(stack.resolve(cResource.url), {
208+
'Fn::Join': [
209+
'',
210+
[
211+
'https://',
212+
{ Ref: 'apiC8550315' },
213+
'.execute-api.',
214+
{ Ref: 'AWS::Region' },
215+
'.',
216+
{ Ref: 'AWS::URLSuffix' },
217+
'/',
218+
{ Ref: 'apiDeploymentStageprod896C8101' },
219+
'/a/b/c'
220+
]
221+
]
222+
});
223+
test.done();
224+
},
225+
181226
'getResource': {
182227

183228
'root resource': {

0 commit comments

Comments
 (0)