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

@1szx1/vite-plugin-swagger2ts

v1.3.18

Published

forked from https://github.com/morelearn1990/vite-plugin-swagger2ts

Downloads

69

Readme

vite-plugin-swagger2ts

npm version monthly downloads types license

使用流程

  1. 安装
# npm i vite-plugin-swagger2ts -D
# or
# yarn add vite-plugin-swagger2ts -D
# or
pnpm add vite-plugin-swagger2ts -D
  1. 配置到 vite.config.ts
// vite.config.ts
import ViteSwagger2ts from "vite-plugin-swagger2ts";

export default {
    plugins: [
        ViteSwagger2ts({
            swaggerUrl: "url", // swagger-resources URL
            output: "pathToSave", // not require, default './src/swagger.ts'
            formatSchema: (schema) => {
                // not require, deal with schema. such as unwrapper common response
                if ("properties" in schema) {
                    const properties = schema["properties"];
                    if ("code" in properties && ("msg" in properties || "message" in properties) && "data" in properties) {
                        return properties["data"];
                    }
                }
                return schema;
            }
        })
    ]
};

插件流程

  1. 通过配置的 url 请求 ${baseUrl}/swagger-resources 地址,获取微服务的 resources 地址;
  2. 通过 resources 地址请求 swagger 具体文档;
  3. 将 swagger 接口生成具体的 typescript 类型:
interface SwaggerInterface {
    // SwaggerInterface start
    url: {
        get: {
            param: { path: { id: string }; query: { status: number }; body: { name: string } };
            reponse: { id: string; name: string; status: number };
        };
    };
}
  1. 附加 typescrip 转换模版内容:
import { PathsSwaggerInterface } from "./swagger.ts";

export type UrlKey = keyof PathsSwaggerInterface;
export type MethodKey<U extends UrlKey> = string & keyof PathsSwaggerInterface[U];

type SwaggerInterfaceSingle<U extends UrlKey, M extends MethodKey<U>> = PathsSwaggerInterface[U][M];
type SwaggerField<U extends UrlKey, M extends MethodKey<U>> = keyof SwaggerInterfaceSingle<U, M>;
type SwaggerFieldType<U extends UrlKey, M extends MethodKey<U>, F extends SwaggerField<U, M>> = SwaggerInterfaceSingle<U, M>[F];

export type Param<U extends UrlKey, M extends MethodKey<U>> = SwaggerFieldType<U, M, "param" & SwaggerField<U, M>>;
export type Response<U extends UrlKey, M extends MethodKey<U>> = SwaggerFieldType<U, M, "response" & SwaggerField<U, M>>;
  1. 将生成的文件保存到项目指定位置,项目通过以下代码进行使用类型提示;
import type { PathKey, MethodKey, Param, Respone } from "configfile";

export function customFetch<P extends PathKey, M extends MethodKey<P>>(path: P, method: M, params: Param<P, M>): Promise<Respone<P, M>> {
    // custome code
    return "" as any;
}
  1. 业务代码通过引用 customFetch 进行使用

License

MIT License © 2021 morelearn1990