Saturday, March 30, 2024

OpenAPI codegen tools

openapi-typescript - npm

openapi-typescript generates TypeScript types from static OpenAPI schemas quickly using only Node.js. It is fast, lightweight, (almost) dependency-free, and no Java/node-gyp/running OpenAPI servers necessary. (MIT license)

Tools for consuming OpenAPI schemas in TypeScript.

npm i -D openapi-typescript # supports Swagger/OpenAPI v3
# or
npm i -D openapi-typescript@5 # supports OpenAPI v2

npx openapi-typescript ./path/to/my/schema.yaml -o ./path/to/my/schema.d.ts 
# 🚀 ./path/to/my/schema.yaml -> ./path/to/my/schema.d.ts [7ms]

npx openapi-typescript https://myapi.dev/api/v1/openapi.yaml -o ./path/to/my/schema.d.ts 
# 🚀 https://myapi.dev/api/v1/openapi.yaml -> ./path/to/my/schema.d.ts [250ms]

it works, but the generated code is not quite "idiomatic" typescript.


alternatives

acacode/swagger-typescript-api: TypeScript API generator via Swagger scheme @GitHub

 npx swagger-typescript-api -p ./swagger.json -o ./src -n myApi.ts

swagger-api/swagger-codegen: swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition. @GitHub


Generating typescript API client from Swagger


OpenAPI-Specification/3.0.3.md at main · OAI/OpenAPI-Specification

About Swagger Specification | Documentation | Swagger

Swagger Editor


ferdikoomen/openapi-typescript-codegen: NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification


OpenAPITools/openapi-generator: OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)

example: Rustici Software SDK from Swagger codegen


viewing OpenApi in Swagger-UI

mapping local file swagger/index.yml to running docker container /tmp/index.yml

# unix bash
docker run -p 8000:8080 -e SWAGGER_JSON=/tmp/index.yml -v $(pwd)/swagger:/tmp --name=swagger-ui -d swaggerapi/swagger-ui
open http://localhost:8000

# windows powershell
docker run -p 8000:8080 -e SWAGGER_JSON=/tmp/index.yml -v ${pwd}/swagger:/tmp --name=swagger-ui -d swaggerapi/swagger-ui
start http://localhost:8000

# stop and remove
docker stop swagger-ui
docker rm swagger-ui

No comments: