Skip to content

Commit

Permalink
Enhance readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Clebiez committed Nov 22, 2024
1 parent 3dccbe8 commit afa939a
Showing 1 changed file with 59 additions and 3 deletions.
62 changes: 59 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ ApiGateway OpenAPI is a construct for generating OpenAPI documentation.
- Install dependency `npm install apigateway-openapi`
- Instanciate the construct:
```JavaScript
import { ApigatewayOpenapiConstructv} from 'apigateway-openapi'
...

this.restApi = new apigateway.RestApi(this, "api", {
defaultCorsPreflightOptions: {
Expand All @@ -17,14 +19,68 @@ this.restApi = new apigateway.RestApi(this, "api", {
new ApiGatewayOpenApiConstruct(this, 'id', api, schemas)
```

`schemas` is a key value object where key is the schema's name and value is the jsonschema.
`api` is your `RestApi` instance.

## Inject schemas
`schemas` is a key value object where key is the schema's name and value is the valid jsonschema.

```JSON
{
"User": {...jsonschema},
"Car": {...jsonschema}
}
```

- run a CDK diff `cdk diff`
- a file named `openapi.json` will be generated at your project root.

The construct will auto generated routes and guess path params based on the format, however it cannot create request body and response schema for you and link it to API routes.

## Generate your schemas.

We canno't automatically discover your schemas, and we canno't generate it for you.
If you use a validator you can use a jsonschema corresponding package which will infer schema into jsonschema.

- [Class-validator-jsonschema](https://www.npmjs.com/package/class-validator-jsonschema)
- [zod-jsonschema](https://www.npmjs.com/package/zod-jsonschema)

If you generate all schemas
For example, with `class-validator` you can create your annotated class then import your Input/Output validator classes in one file. Finally call `validationMetadatasToSchemas`
```JavaScript
import { validationMetadatasToSchemas } from 'class-validator-jsonschema'

import * from 'UserDto'
import * from 'CarDto'

const schemas = validationMetadatasToSchemas()
```

Schemas will be directly as the required format.

At this moment, schemas will not be linked to your api root.

## Link schemas to routes

For each route, you will map input and output schemas by adding metadatas to the ressource. The construct will parse them after.

- `requestBodySchema`: Schema used in input body
- `responseSchema`: The API will respond that schema
- `responseTypeIsArray`: The response will be an array of `responseSchema`

```JavaScript
import { addMetadatas } from 'apigateway-openapi/dist/utils/open-api'

...
// Create api ressource
const resource = currentRoute.addMethod("GET" | "PUT | "POST""DELETE", lambdaIntegration)
// Map your current ressource with schemas
addMetadatas(resource.node, {
requestBodySchema: "UpdateUser"; // Input schema
responseTypeIsArray: true; // Tell this is a listing routes
responseSchema: "User"; // Output schema
})
```
## TODO
- Handle queryStringParameters where schemas can be a little bit tricky because no schema generator works for them. We should provide another way to handle it easily.
- Publish it on specified S3 bucket

0 comments on commit afa939a

Please sign in to comment.