aws / aws/aws-cdk

(apigateway): Imported RestAPIs do not have access to its methods due to casting

Open
#26,247 0 comments 1 reaction 0 assignees View on GitHub
@aws-cdk/aws-apigateway bug effort/small p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

Imported RestAPIs are returned by CDK as `IRestAPI`. However in the source code, the imports extend `RestAPIBase` which features additional helpful methods, such as the `metric` methods. The `IRestAPI` interface does not come with methods such as the `metric` methods, which prevents methods from being called without casting the imported RestAPI as a `RestAPIBase`.

### Expected Behavior

I expect to be able to use methods that are okay to use on an imported RestAPI.

### Current Behavior

I cannot use some methods that I should be able to

### Reproduction Steps

You can successfully call the method using the following code. Note that removing the typecasting causes this to fail

```ts
export class TestingStack extends cdk.Stack {
public restApi: apigw.RestApiBase;
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

this.restApi = apigw.RestApi.fromRestApiId(this,"MyGatewayApi","restApiId") as apigw.RestApiBase;

const api = new cw.Alarm(this, `api-error-alarm`, {
alarmName: 'api-error-alarm',
threshold: 5,
evaluationPeriods: 1,
metric: this.restApi.metricServerError()
});
}
}
```

### Possible Solution

Probably put these methods on `IRestAPI` interface, but I didn't dive too deep into this so that could have other side effects.

### Additional Information/Context

_No response_

### CDK CLI Version

latest

### Framework Version

_No response_

### Node.js Version

16

### OS

mac

### Language

Typescript

### Language Version

_No response_

### Other information

Sourced from [this discussion](https://github.com/aws/aws-cdk/discussions/26234)

Contributor guide

Open the contributing guide

Research direction

Start with the IRestAPI and RestAPIBase definitions and the RestApi.fromRestApiId entry point. Check how imported RestAPIs are typed, then run the provided reproduction to verify that metricServerError() and other supported methods work without a cast; done means the TypeScript example compiles using the imported API directly.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.