What is swagger-typescript-api?
The swagger-typescript-api npm package is a tool that generates TypeScript API client code from Swagger/OpenAPI definitions. It helps developers to create strongly-typed API clients, reducing the risk of runtime errors and improving the development experience.
What are swagger-typescript-api's main functionalities?
Generate TypeScript API Client
This feature allows you to generate a TypeScript API client from a Swagger/OpenAPI definition. The `generateApi` function takes an object with configuration options such as the name of the output file, the URL of the Swagger definition, and the output directory.
const { generateApi } = require('swagger-typescript-api');
generateApi({
name: 'MyApi.ts',
url: 'http://api.example.com/swagger.json',
output: './src/api',
});
Custom Templates
This feature allows you to use custom templates for generating the TypeScript API client. By specifying the `templates` option, you can provide a path to your custom templates, giving you full control over the generated code's structure and style.
const { generateApi } = require('swagger-typescript-api');
generateApi({
name: 'MyApi.ts',
url: 'http://api.example.com/swagger.json',
output: './src/api',
templates: './my-custom-templates',
});
Generate API Client from Local File
This feature allows you to generate a TypeScript API client from a local Swagger/OpenAPI definition file. The `input` option specifies the path to the local Swagger file.
const { generateApi } = require('swagger-typescript-api');
generateApi({
name: 'MyApi.ts',
input: './swagger.json',
output: './src/api',
});
Other packages similar to swagger-typescript-api
swagger-codegen
The swagger-codegen package is a tool that generates client libraries, server stubs, and API documentation from Swagger/OpenAPI definitions. It supports multiple languages and frameworks. While it is similar to swagger-typescript-api, it is more mature and widely used but may not be as focused on TypeScript-specific features.
typescript-fetch
The typescript-fetch package is a TypeScript client generator for OpenAPI/Swagger definitions that uses the Fetch API. It is similar to swagger-typescript-api in that it generates TypeScript clients, but it specifically targets the Fetch API for making HTTP requests.
Swagger TypeScript API
- Support for OpenAPI 3.0, 2.0, JSON and YAML
- Generate the API Client for Fetch or Axios from an OpenAPI Specification
Any questions you can ask here
Examples
All examples you can find here
Usage
You can use this package in two ways:
CLI
npx swagger-typescript-api generate --path ./swagger.json
Or install locally in your project:
npm install --save-dev swagger-typescript-api
npx swagger-typescript-api generate --path ./swagger.json
Library
npm install --save-dev swagger-typescript-api
import * as path from "node:path";
import * as process from "node:process";
import { generateApi } from "swagger-typescript-api";
await generateApi({ input: path.resolve(process.cwd(), "./swagger.json") });
For more detailed configuration options, please consult the documentation.
Mass media
License
Licensed under the MIT License.