swagger-extract-dto
The problem
Most complex frontend applications have a complex API connection. The API structure is not so simple in a typical case, so it would be great to have some formal description.
Fortunately, in many cases we have Swagger/OpenAPI description which is generated by some tools on the backend side. However, this Swagger/OpenAPI is just a JSON file which is not very usable without additional efforts.
Solution
This library helps to work on the frontend with Swagger/OpenAPI API description in next way
- it takes the Swagger/OpenAPI JSON as input
- it produces Typescript defintion for each API method
- it produces ENDPOINT string/function - depends on parameters existance
- it produces types definitions for each DTO which is used in the API
- it produces enums definitions
- it produces response DTO definitions
- it produces body DTO definitions
- it produces dereferenced JSON schema for each endpoint; it can be used for API response validation
Example
> npm i swagger-extract-dto
> npx swagger-extract-dto -j -f ./src/sample/petstore.json -o ./tmp/petstore
> tree ./tmp/petstore
./tmp/petstore
├── ApiResponse.ts
├── Category.ts
├── Order.ts
├── Pet.ts
├── Tag.ts
├── User.ts
├── info.json
├── petFindByStatus_get.ts
├── petFindByStatus_get_response.schema.json
├── petFindByTags_get.ts
├── petFindByTags_get_response.schema.json
├── petPetIdUploadImage_post.ts
├── petPetIdUploadImage_post_response.schema.json
├── petPetId_delete.ts
├── petPetId_get.ts
├── petPetId_get_response.schema.json
├── petPetId_post.ts
├── pet_post.ts
├── pet_put.ts
├── storeInventory_get.ts
├── storeInventory_get_response.schema.json
├── storeOrderOrderId_delete.ts
├── storeOrderOrderId_get.ts
├── storeOrderOrderId_get_response.schema.json
├── storeOrder_post.ts
├── storeOrder_post_response.schema.json
├── userCreateWithArray_post.ts
├── userCreateWithList_post.ts
├── userLogin_get.ts
├── userLogin_get_response.schema.json
├── userLogout_get.ts
├── userUsername_delete.ts
├── userUsername_get.ts
├── userUsername_get_response.schema.json
├── userUsername_put.ts
└── user_post.ts
0 directories, 36 files
Options
> npx ./dist/swagger-extract-dto.js
Usage: swagger-extract-dto.js [options]
Options:
--version Show version number [boolean]
-f, --file use a Swagger API file [required]
-o, --out output folder [required]
-j, --json generate response JSON schema [boolean]
-h, --help Show help [boolean]
Examples:
swagger-extract-dto.js -f foo.json -o process foo.json and place
./api definitions to ./api folder
copyright 2021
Missing required arguments: f, o