项目作者: welovecoding

项目描述 :
A Swagger API client generator based on axios and written in TypeScript. 🌊
高级语言: TypeScript
项目地址: git://github.com/welovecoding/swaxios.git
创建时间: 2019-04-26T13:03:37Z
项目社区:https://github.com/welovecoding/swaxios

开源协议:

下载


Swaxios

Swaxios

A Swagger (OpenAPI v2) API client generator based on axios and written in TypeScript.

Motivation

The Swaxios project automates the creation of an API client for TypeScript applications, which can be used in web browsers and Node.js environments.

At the time of writing this tool (2019-04-26), the Swagger Codegen project only provided separate SDK generators for browsers and Node.js (typescript-fetch and typescript-node). Unfortunately, the typescript-fetch generator for browsers is not compatible with all Node.js applications since the fetch API became globally available only as an experimental feature in Node.js 18 (2022-04-19).

Swaxios offers an API generator that works in both environments (browsers & Node.js 16+) by utilizing the flexible axios request library. API calls are routed through axios and can be easily customized using request interceptors.

Limitations

This library can only work with a valid OpenAPI Version 2 specification, which must be in the form of a file or URL. It does not support OpenAPI Version 3 specifications.

If you need a generator for Open API v3 specs, you can test typescript-axios from Swagger Codegen (added on 2020-09-21).

  1. java -jar swagger-codegen-cli-3.0.24.jar generate -l typescript-axios -i ./swagger.json -o ./api-client

There is also an npm package (@openapitools/openapi-generator-cli"">@openapitools/openapi-generator-cli) which exposes this generator. You can use it the following way in your scripts:

  1. {
  2. "scripts": {
  3. "generate": "openapi-generator-cli generate -i ./swagger.json -g typescript-axios -p \"withoutRuntimeChecks=true,withInterfaces=true\" -o ./api-client"
  4. }
  5. }

Installation

You can install Swaxios globally (npm i -g swaxios) or add it to your devDependencies.

Usage

Display all CLI options:

  1. swaxios --help

If you pass an OpenAPI v2 specification (JSON or YAML) to Swaxios, then it generates a fully typed API client for you which uses axios under the hood:

  1. # Provide a Swagger input file (JSON or YAML)
  2. swaxios -i ./path/to/swagger.json -o ./path/to/output/directory
  3. swaxios -i ./path/to/swagger.yml -o ./path/to/output/directory
  4. # Alternative: Provide a URL to a Swagger endpoint
  5. swaxios -i http://127.0.0.1:3000/documentation-json -o ./path/to/output/directory

With the -f option, you can force Swaxios to overwrite existing files in the output path:

  1. swaxios -i ./path/to/swagger.json -o ./path/to/output/directory -f

Examples

You can find many examples of generated API client code in our snapshots section.

Here is a basic example:

ExchangeService.ts

  1. /* tslint:disable */
  2. /**
  3. * This file was automatically generated by "Swaxios".
  4. * It should not be modified by hand.
  5. */
  6. import {AxiosInstance, AxiosRequestConfig} from 'axios';
  7. export class ExchangeService {
  8. private readonly apiClient: AxiosInstance;
  9. constructor(apiClient: AxiosInstance) {
  10. this.apiClient = apiClient;
  11. }
  12. deleteExchange = async (id: number): Promise<void> => {
  13. const config: AxiosRequestConfig = {
  14. method: 'delete',
  15. url: `/api/v1/exchange/${id}`,
  16. };
  17. await this.apiClient.request(config);
  18. };
  19. }

It has been generated from the following path:

swagger.json

  1. {
  2. // ...
  3. "paths": {
  4. "/api/v1/exchange/{id}": {
  5. "delete": {
  6. "consumes": ["application/json"],
  7. "operationId": "deleteExchange",
  8. "parameters": [
  9. {
  10. "in": "path",
  11. "name": "id",
  12. "required": true,
  13. "type": "number"
  14. }
  15. ],
  16. "produces": ["application/json"],
  17. "responses": {
  18. "200": {
  19. "description": ""
  20. }
  21. }
  22. }
  23. }
  24. }
  25. // ...
  26. }

Alternatives