0.34.0 • Published 4 months ago

openapi-axios v0.34.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

openapi-axios

OpenAPI(2.0/3.0/3.1) Schema → Type-safe Axios

code-review dependency-review Codacy Badge Codacy Badge npm version

将 OpenAPI 规范声明文件转换为类型安全的基于 Axios 的函数。

  • 😆 同时支持 openAPI 2.0、3.0、3.1 规范
  • 😎 与最流行的 HTTP 客户端 axios 进行适配
  • 😉 类型安全的请求函数,轻松进行 tree shaking
  • 🤔 基于 zod 支持了接口出入参的校验(可选)
  • 😋 支持生成原 Schema 文件以及中间处理的 Schema 文件(可选)
  • 🤗 支持接口 Mock(可选)

安装

npm i -D openapi-axios@latest
npm i axios

使用

创建配置文件

npx openapi-axios init

将在项目根目录下创建配置文件 openapi.config.cjs:

const { defineConfig } = require('openapi-axios');

/**
 * openapi-axios config
 * @ref https://github.com/FrontEndDev-org/openapi-axios
 */
module.exports = defineConfig({
  documents: {
    petStore3: 'https://petstore3.swagger.io/api/v3/openapi.json'
  },
});

生成 OpenAPI 相关文件

# 根据配置文件生成typescript文件
npx openapi-axios

# 将会生成 src/apis/petStore3.ts 文件
# 将会生成 src/apis/petStore3.type.ts 文件
/**
 * @title Swagger Petstore - OpenAPI 3.0
 * @version 1.0.19
 * @contact <apiteam@swagger.io>
 * @description This is a sample Pet Store Server based on the OpenAPI 3.0 specification.  You can find out more about
 * Swagger at [http://swagger.io](http://swagger.io). In the third iteration of the pet store, we've switched to the design first approach!
 * You can now help us improve the API whether it's by making changes to the definition itself or to the code.
 * That way, with time, we can improve the API in general, and expose some of the new features in OAS3.
 *
 * Some useful links:
 * - [The Pet Store repository](https://github.com/swagger-api/swagger-petstore)
 * - [The source API definition for the Pet Store](https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml)
 */

import type * as Type from './pet-store.type.ts';
import axios from 'axios';

type AxiosRequestConfig = Parameters<typeof axios.request>[0];

/**
 * @description Add a new pet to the store
 * @summary Add a new pet to the store
 * @see pet Everything about your Pets {@link http://swagger.io Find out more}
 * @param data Create a new pet in the store
 * @param [config] request config
 * @returns Successful operation
 */
export async function addPet(data: Type.AddPetData, config?: AxiosRequestConfig): Promise<AxiosResponse<Type.AddPetResponse>> {
  return axios({
    method: 'POST',
    url: `/pet`,
    data,
    ...config
  });
}

// ... 省略 ...
/**
 * @title Swagger Petstore - OpenAPI 3.0
 * @version 1.0.19
 * @contact <apiteam@swagger.io>
 * @description This is a sample Pet Store Server based on the OpenAPI 3.0 specification.  You can find out more about
 * Swagger at [http://swagger.io](http://swagger.io). In the third iteration of the pet store, we've switched to the design first approach!
 * You can now help us improve the API whether it's by making changes to the definition itself or to the code.
 * That way, with time, we can improve the API in general, and expose some of the new features in OAS3.
 *
 * Some useful links:
 * - [The Pet Store repository](https://github.com/swagger-api/swagger-petstore)
 * - [The source API definition for the Pet Store](https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml)
 */

// ... 省略 ...

export interface Pet {
  /**
   * @format int64
   * @example 10
   */
  id?: number;
  /**
   * @example doggie
   */
  name: string;
  category?: Category;
  photoUrls: Array<string>;
  tags?: Array<Tag>;
  /**
   * @description pet status in the store
   */
  status?: ('available' | 'pending' | 'sold');
}

// ... 省略 ...

export type AddPetData = Pet;
export type AddPetResponse = Pet;

// ... 省略 ...

然后你可以直接导入一个函数来使用它:

import { addPet } from '@/apis/.petStore3';

// 类型安全
const { data: pet } = await addPet({
  name: 'MyCat',
});

// 类型安全
console.log(pet);
0.34.0

4 months ago

0.32.1

4 months ago

0.32.0

4 months ago

0.30.1

5 months ago

0.30.0

5 months ago

0.29.0

5 months ago

0.27.0

5 months ago

0.25.1

5 months ago

0.33.0

4 months ago

0.34.0-debug.5

4 months ago

0.31.0

5 months ago

0.34.0-debug.3

4 months ago

0.34.0-debug.4

4 months ago

0.34.0-debug.1

4 months ago

0.34.0-debug.2

4 months ago

0.28.1

5 months ago

0.28.0

5 months ago

0.26.1

5 months ago

0.26.0

5 months ago

0.20.0-debug.4

11 months ago

0.19.0-debug.2

11 months ago

0.19.0-debug.3

11 months ago

0.20.0-debug.2

11 months ago

0.20.0-debug.3

11 months ago

0.19.0-debug.1

11 months ago

0.21.0

11 months ago

0.20.0

11 months ago

0.20.0-debug.1

11 months ago

0.19.0

11 months ago

0.18.1

1 year ago

0.19.1

11 months ago

0.18.1-debug.2

12 months ago

0.18.1-debug.3

12 months ago

0.18.1-debug.1

12 months ago

0.18.0-debug.1

1 year ago

0.25.0

10 months ago

0.24.0

11 months ago

0.23.0

11 months ago

0.22.0

11 months ago

0.17.3-debug.5

1 year ago

0.17.3-debug.6

1 year ago

0.17.3-debug.13

1 year ago

0.17.3-debug.3

1 year ago

0.17.3-debug.12

1 year ago

0.17.3-debug.4

1 year ago

0.17.3-debug.1

1 year ago

0.17.3-debug.2

1 year ago

0.18.0

1 year ago

0.17.3-debug.11

1 year ago

0.17.3

1 year ago

0.17.1

1 year ago

0.16.0

2 years ago

0.15.2

2 years ago

0.15.3

2 years ago

0.13.0

2 years ago

0.14.0

2 years ago

0.15.0

2 years ago

0.15.1

2 years ago

0.12.0

2 years ago

0.11.0

2 years ago

0.10.0

2 years ago

0.9.0

2 years ago

0.8.0

2 years ago

0.7.0

2 years ago