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

stc-setclass

v1.0.4

Published

A tool for converting OpenApi/Swagger/Apifox into code.

Downloads

361

Readme

STC

安装

将Swagger 2/3(OpenApi)的接口生成为typescript的接口文件

  • 更改自@loogwoo/stc项目,将原版生成的interface类型文件修改为class类型,使用时直接new实体,将不需要再复制表单.

npm安装:

npm i stc-setclass

⚠️安装后需要在根目录下的package.json中的scripts添加如下配置:

  "api": "stc --url=你的openApi地址 --outDir=导出目录 --client=axios --lang=ts"

例如:

{
  "scripts": {
    "api": "stc --url=http://127.0.0.1:4523/export/openapi/2?version=3.1 --outDir=out/api --client=axios --lang=ts"
  }
}

安装并配置后运行:

npm run api

以下为原版文档:

Features

  • 🐹 Support for Swagger 2/3(OpenApi) and Apifox.
  • 💡 Support plug-in development.
  • 🐣 Built-in transformation languages:
    • TypeScript, almost equivalent to handwriting. Depends on axios, wx.request, fetch.

    xhr/ajax、ofetch planned

    • JavaScript, from TypeScript to it.
    • Dart, dependency on dio.
    • 🚧 Swift ...

Quick start

Download executable files

download by system:

  • stc: Intel-based Mac
  • stc-m: M-series Mac
  • stc-linux:Linux
  • stc-win.exe: Windows

NPM

1.Install the @loogwoo/stc npm package.

pnpm add @loongwoo/stc -D

2.Open the project's package.json file and add the following command to scripts:

{
  "scripts": {
    "api": "stc --url=http://127.0.0.1:4523/export/openapi/2?version=3.1"
  }
}

Use

⚠️ Note: deno will not parse the ~ character as the user's home directory.

stc --url=https://petstore3.swagger.io/api/v3/openapi.json --outDir=out

终端输出信息

输出文件

Existing Project

Assume a project directory is:

.
├── src
│   └── apis # Copy the shared directory here.
│       └── shared
│       └── xxx.ts # Other files.

Axios

  1. Find the directory of outDir, copy the entire shared directory to the directory of the axios module you encapsulated.

  2. Open the shared > axios > index.ts file, copy the request method, and add it to the axios module you encapsulated. If it is not encapsulated, copy the index.ts file as a new file to avoid the problem of modification being overwritten.

  3. Taking Vue as an example, add the following code to the main.ts file:

import { createApiClient } from './apis/shared/fetchRuntime';

createApiClient({
  baseURL: 'https://api.xxx.com'
  // onError(msg) {
  //   // 处理错误信息
  // }
})

Wechat

  1. Find the directory of outDir, copy the entire directory of shared to the directory of the wechat module you encapsulated.

  2. Open the shared > wechat > ​​index.ts file, copy the request method, and add it to the wx.request code file you encapsulated. If it is not encapsulated, copy the index.ts file as a new file to avoid the problem of modification being overwritten.

  3. Add the following code to the app.ts file:

import { createApiClient } from './apis/shared/fetchRuntime';
// import Notify from './miniprogram_npm/@vant/weapp/notify/notify';

App<IAppOption>({
  onLaunch() {
    createApiClient({
      baseURL: 'https://api.xxx.com,
      onError(msg) {
        // Notify({ type: 'danger', message: msg, selector: '#v-notify'})
      }
    })
  }
});

Options

| Option | Alias | Type | Default | Description | | ----------- | ----- | -------- | --------- | ------------------------------------------------------------------------------------------------------------ | | url | | string | | Swagger/OpenApi/Apifox document address, or local path. | | outDir | o | string | ./stc_out | Output Directory. | | client | | string | axios | http request client. When lang is ts/js, the possible values ​​are: axios, wechat, fetch. | | lang | l | string | ts | Language, used for output file suffix. | | tag | | number | | Specify the tag from the interface url. By default, the first tag is read for the file name. | | filter | f | string[] | | Filter interfaces. Interfaces that meet the filter conditions will be generated. Example: --filter "/pet/*", generate an interface for /pet, and support multiple --filter | | conjunction | c | string | By | The method's connector, the default value is By. | | version | v | boolean | | Output version information. | | help | h | boolean | | Output help information. |

Plug-in development

For convenience, STC can not only develop plugins in Deno, but also provides @loongwoo/stc npm library, which can develop plugins in Node environment.

examples

Deno

⚠️ Prepare the Deno environment.

Create a myPlugin.ts file:

// 引用模块
// import { start } from 'https://deno.land/x/[email protected]/mod.ts'
import { start } from 'jsr:@loongwoo/stc@^2.4.0'

// Defining plugins
const myPlugin: IPlugin = {
  name: 'stc:MyPlugin',
  lang: 'ts',
  setup(options) {
    console.log(options)
  },
  onTransform(def, action) {
    // definition
    const defContent: string = parserDefinition(
      def
    )
    // action
    const actionContent: Map<string, string> = parserAction(
      action
    )

    return {
      definition: {
        filename: '_types.ts',
        content: defContent,
      },
      action: actionContent // Here actionContent is of type Map<string, string>, key is the file name, value is the converted code.
    }
  },
  onEnd() {
    console.log('end')
  }
}

// 使用插件
start({
  // ...其他配置
  plugins: [myPlugin]
})

Node

  1. Create a myPlugin.ts file.

  2. Add the @loongwoo/stc reference and use the start method:

import { start } from '@loongwoo/stc'
  1. Implement the code that converts definition and action into the target language in the plugin's onTransform hook function.
export const myPlugin: IPlugin = {
  name: 'stc:MyPlugin',
  lang: 'ts',
  setup(options) {
    console.log(options)
  },
  onTransform(def, action) {
    // definition
    const defContent: string = parserDefinition(
      def
    )
    // action
    const actionContent: Map<string, string> = parserAction(
      action
    )

    return {
      definition: defContent,
      action: actionContent
    }
  },
  onEnd() {
    console.log('end')
  }
}

4.In the start method, add plugins:

start({
  // ...other options
  plugins: [myPlugin]
})