npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

openapi-axios

v0.25.0

Published

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

Downloads

163

Readme

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 规范
  • 😉 生成的每个 API 都是一个函数,用于在构建时轻松进行 tree shaking
  • 😎 与最流行的 HTTP 客户端 axios 进行适配
  • 🤗 轻松与本地请求客户端集成,例如在本地项目中创建的 Axios 实例(通常我们在本地都是需要自定义一些拦截器什么的)
  • 😁 易于使用,易于学习,类型安全

安装

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({
    modules: {
        'petStore3': 'https://petstore31.swagger.io/api/v31/openapi.json'
    },
});

生成 API 文件

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

# 将会生成 src/apis/petStore3.ts 文件
/**
 * @title Swagger Petstore - OpenAPI 3.1
 * @version 1.0.6
 * @contact <[email protected]>
 * @description This is a sample Pet Store Server based on the OpenAPI 3.1 specification.
You can find out more about
Swagger at [http://swagger.io](http://swagger.io).
 * @summary Pet Store 3.1
 * @see {@link http://swagger.io Find out more about Swagger}
 */

import axios from "axios";
import type {AxiosRequestConfig, AxiosPromise} from "axios";

// ... 省略 ...

/**
 * @description Pet
 */
export type Pet = {
    /**
     * @format int64
     * @example 10
     */
    id?: number;
    /**
     * @description Pet Category
     */
    category?: Category;
    /**
     * @example doggie
     */
    name: string;
    photoUrls: string;
    tags?: Tag;
    /**
     * @description pet status in the store
     */
    status?: 'available' | 'pending' | 'sold';
    /**
     * @format int32
     * @example 7
     */
    availableInstances?: number;
    petDetailsId?: unknown;
    petDetails?: unknown;
};

// ... 省略 ...

/**
 * @description Update an existing pet by Id
 * @summary Update an existing pet
 * @see pet Everything about your Pets {@link http://swagger.io Find out more}
 * @param data Pet object that needs to be updated in the store
 * @param [config] request config
 * @returns Successful operation
 */
export async function updatePet(data: Pet, config?: AxiosRequestConfig): AxiosPromise<Pet> {
    return axios({
        method: 'put',
        url: `/pet`,
        data: data,
        ...config,
    });
}

// ... 省略 ...

然后你可以直接导入一个函数并使用它。 调用接口就像调用本地函数一样简单。

import { updatePet } from '@/apis/petStore3';

// 类型安全
const { data: pet } = await updatePet({
  name: 'MyCat',
  photoUrls: ['photo1', 'photo2']
});

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