https://github.com/thoroc/aws-construct-openapi
AWS CDK construct for OpenApi generation
https://github.com/thoroc/aws-construct-openapi
Last synced: 3 months ago
JSON representation
AWS CDK construct for OpenApi generation
- Host: GitHub
- URL: https://github.com/thoroc/aws-construct-openapi
- Owner: thoroc
- License: apache-2.0
- Created: 2024-04-11T20:37:04.000Z (about 1 year ago)
- Default Branch: martzcodes/blog-cdk-openapi
- Last Pushed: 2024-08-12T14:56:59.000Z (10 months ago)
- Last Synced: 2025-01-13T01:08:32.092Z (5 months ago)
- Language: TypeScript
- Size: 583 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# aws-construct-openapi
From the [blog post from matt martz](https://matt.martz.codes/openapi-specs-from-cdk-stack-without-deploying-first)
## Usage
Declare a new instance of the `OpenApiConstruct`:
```typescript
const api = new OpenApiConstruct(this, 'OpenApi', {
tsconfigPath: `${join(__dirname, '..', 'tsconfig.json')}`,
apiProps: {
defaultMethodOptions: {
authorizer: auth,
},
},
models: `${__dirname}/interfaces`,
});
```Declare the `methodResponses` and the `lambda` function
```typescript
const methodResponses = [
{
// Successful response from the integration
statusCode: '200',
// Define what parameters are allowed or not
responseParameters: {
'method.response.header.Content-Type': true,
'method.response.header.Access-Control-Allow-Origin': true,
'method.response.header.Access-Control-Allow-Credentials': true,
},
// Validate the schema on the response
responseModels: {
'application/json': 'Response',
},
},
];
```Add the endpoints as needed:
```typescript
api.addEndpoint('/example/{hello}/basic', 'POST', {
lambda: basicLambda,
requiredParameters: ['hello'],
requestModels: {
'application/json': 'Basic',
},
methodResponses,
});
```Generate the schema:
```typescript
api.generateOpenApiSpec(join(`${__dirname}`, '..', 'openapigenerated.json'));
```