Introduction to OpenAPI Typescript Codegen
OpenAPI Typescript Codegen is a dynamic tool designed to bridge the gap between OpenAPI specifications and Typescript clients. It is crafted for developers who appreciate the robustness of OpenAPI but want to incorporate it into their modern JavaScript environments without delving into Java-based solutions.
Why Use OpenAPI Typescript Codegen?
OpenAPI Typescript Codegen stands out for several compelling reasons:
- Frontend-Friendly: Developers involved in frontend development love OpenAPI. This tool facilitates the generation of Typescript clients that seamlessly integrate into their workflow without needing to incorporate Java-based solutions into their build processes.
- Lightweight and Flexible: Its quick and lightweight nature ensures minimal overhead. Furthermore, it is framework-agnostic, making it adaptable to various technology stacks.
- Versatile Client Generation: It supports the creation of HTTP clients using Fetch, Node-Fetch, Axios, Angular, and XHR. This versatility ensures that developers can choose the client that best suits their project requirements.
- Comprehensive OpenAPI Support: The tool is compatible with OpenAPI specification versions 2.0 and 3.0, supporting both JSON and YAML inputs.
- Convenient CLI Tools: Leveraging CLI, Node.js, and NPX, it allows developers to generate types effortlessly.
- Cancelation Support: The library includes support for the cancelable promise pattern, enabling the aborting of requests when necessary.
- External References Handling: It can handle external references using the json-schema-ref-parser, streamlining complex schema management.
Installation Guide
To install OpenAPI Typescript Codegen, execute the following command in your project's root directory:
npm install openapi-typescript-codegen --save-dev
How to Use
Once installed, the tool can be invoked through the command line. Here's how it can be utilized:
$ openapi --help
Usage: openapi [options]
Options:
-V, --version output the version number
-i, --input <value> OpenAPI specification, can be a path, url or string content (required)
-o, --output <value> Output directory (required)
-c, --client <value> HTTP client to generate [fetch, xhr, node, axios, angular] (default: "fetch")
--name <value> Custom client class name
--useOptions Use options instead of arguments
--useUnionTypes Use union types instead of enums
--exportCore <value> Write core files to disk (default: true)
--exportServices <value> Write services to disk (default: true)
--exportModels <value> Write models to disk (default: true)
--exportSchemas <value> Write schemas to disk (default: false)
--indent <value> Indentation options [4, 2, tab] (default: "4")
--postfixServices Service name postfix (default: "Service")
--postfixModels Model name postfix
--request <value> Path to custom request file
-h, --help display help for command
Examples
$ openapi --input ./spec.json --output ./generated
$ openapi --input ./spec.json --output ./generated --client xhr
Documentation and Support
For developers seeking in-depth understanding, comprehensive documentation is available in the OpenAPI Typescript Codegen Wiki. This resource serves as a treasure trove of information for users of all levels.
A Message from the Creator
The creator of OpenAPI Typescript Codegen has announced an exciting opportunity for the community: the project is transitioning to a new home at @hey-api/openapi-ts
. This initiative aims to ensure that the project receives the maintenance and attention it deserves. Users are encouraged to migrate their projects and continue benefiting from the community-driven advancements at @hey-api/openapi-ts.
Supporting the Project
If you or your company benefit from this tool, consider supporting its ongoing development and enhancements. By sponsoring the project, you provide invaluable support to ensure its continual growth. Sponsorship details are available here.
In Conclusion
OpenAPI Typescript Codegen remains a cornerstone for developers seeking to harness the power of OpenAPI in TypeScript environments. With its powerful features and adaptable nature, it continues to empower developers, enabling them to create robust and maintainable TypeScript clients efficiently.