This project demonstrates an approach to building OpenAPI specifications through separated definitions across the codebase. It provides a flexible and maintainable way to define API schemas, endpoints, and other OpenAPI components using TypeScript.
The OpenAPI DSL allows you to:
- Define your API structure in a modular way
- Generate OpenAPI schemas from TypeScript interfaces
- Separate concerns by defining different parts of your API in different files
- Easily maintain and update your API specification as your project evolves
The project uses several key components to build the OpenAPI specification:
baseOpenAPI.ts
: Initializes the OpenAPI builder with basic information.addSchema.ts
: Provides functions to add schemas and request bodies to the OpenAPI specification.- Individual definition files (e.g.,
addPet.ts
): Define specific parts of your API.
Here's an example of how to add a Pet schema and its corresponding request body:
import { addSchema, addRequestBody } from "./addSchema";
addSchema("Pet", "./types/Pet.ts");
addRequestBody("Pet", "./types/Pet.ts", "CreatePet");
This code:
- Adds the "Pet" schema to the OpenAPI specification, generated from the
Pet
interface in./types/Pet.ts
. - Adds a request body named "CreatePet" based on the same "Pet" schema.
- Modularity: Each part of your API can be defined in separate files, making it easier to manage large APIs.
- Type Safety: By using TypeScript interfaces, you ensure type consistency between your API definition and implementation.
- Maintainability: Changes to your API can be made in isolated files, reducing the risk of unintended side effects.
- Readability: The DSL approach makes it easy to understand the structure of your API at a glance.
- Clone this repository
- Install dependencies:
npm install
oryarn install
- Define your TypeScript interfaces in the
types
directory - Create definition files (like
addPet.ts
) to add schemas and endpoints - Run your generate npm script to generate the final OpenAPI specification