Skip to content

Commit

Permalink
feat(cloudfront-origins): allow custom originPath for apigateway.Rest…
Browse files Browse the repository at this point in the history
…Api constructs
  • Loading branch information
oieduardorabelo committed Feb 10, 2023
1 parent 0570e01 commit 1b5eedc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { validateSecondsInRangeOrUndefined } from './private/utils';
/**
* Properties for an Origin for an API Gateway REST API.
*/
export interface RestApiOriginProps extends cloudfront.OriginOptions {
export interface RestApiOriginProps extends cloudfront.OriginProps {
/**
* Specifies how long, in seconds, CloudFront waits for a response from the origin, also known as the origin response timeout.
* The valid range is from 1 to 180 seconds, inclusive.
Expand Down Expand Up @@ -40,7 +40,7 @@ export class RestApiOrigin extends cloudfront.OriginBase {
// Splitting on '/' gives: ['https', '', '<rest-api-id>.execute-api.<region>.amazonaws.com', '<stage>']
// The element at index 2 is the domain name, the element at index 3 is the stage name
super(cdk.Fn.select(2, cdk.Fn.split('/', restApi.url)), {
originPath: `/${cdk.Fn.select(3, cdk.Fn.split('/', restApi.url))}`,
originPath: props.originPath ?? `/${cdk.Fn.select(3, cdk.Fn.split('/', restApi.url))}`,
...props,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,38 @@ test('Correctly renders the origin, with custom originId', () => {
},
});
});

test('Correctly renders the origin, with custom originPath', () => {
const api = new apigateway.RestApi(stack, 'RestApi');
api.root.addMethod('GET');

const origin = new RestApiOrigin(api, { originPath: '/my/custom/path' });
const originBindConfig = origin.bind(stack, { originId: 'StackOrigin029E19582' });

expect(stack.resolve(originBindConfig.originProperty)).toEqual({
id: 'StackOrigin029E19582',
domainName: {
'Fn::Select': [2, {
'Fn::Split': ['/', {
'Fn::Join': ['', [
'https://', { Ref: 'RestApi0C43BF4B' },
'.execute-api.',
{ Ref: 'AWS::Region' },
'.',
{ Ref: 'AWS::URLSuffix' },
'/',
{ Ref: 'RestApiDeploymentStageprod3855DE66' },
'/',
]],
}],
}],
},
originPath: '/my/custom/path',
customOriginConfig: {
originProtocolPolicy: 'https-only',
originSslProtocols: [
'TLSv1.2',
],
},
});
});

0 comments on commit 1b5eedc

Please sign in to comment.