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

moyan-api

v1.0.45

Published

使用OpenApi 生成TypeScript的api调用skd

Downloads

21

Readme

moyan-api

描述

用于生成OAS3 的调用api

安装

npm i moyan-api -S 

pageage配置

#pageage.json
{
  "moyan-api":{
    "output":"src", // 生成的文件输出目录,默认值:src
    "apijson":"moyan.api.json" ,// 生成sdk 的openApi 3.0 数据 ,默认值:moyan.api.json
    "jsonurl":"远程获取数据的地址", 
    "apiprefix":"Api",
  }
}

使用

  • 调用命令前,必须保证你的项目根目录里面存在moyan.api.json(openApi 3.0格式的数据)或者在ageage.json中配置
{
   "moyan-api":{
       "jsonurl":"远程获取数据的地址"
   } 
}
cd 你的项目根目录
moyan-api 

调用命令后会在我们的src/api 生成index.ts、schemas.ts两个文件,其中index.ts中的class就是我们的api调用类了。

调用

  • 在我们开始调用api之前,我们需要对我们的 ApiCall 类做一些配置处理。
  • 我们可以在我们的src/lib/api下创建一个config.ts (具体文件名、路径可根据自己的喜好创建修改)。

import { ElMessage } from 'element-plus'
import { getConfig } from '../../config'
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'
import { ApiCall, ApiEntity } from 'moyan-api'
const AXIOS = Symbol('mo#Api#axios')

export class MoAxios {
  static [AXIOS]: AxiosInstance
  options: any = {}

  constructor(options: any = {}) {
    this.options = options
  }

  get $axios() {
    if (!MoAxios[AXIOS]) {
      MoAxios[AXIOS] = axios.create(this.config.axios)
      this.init()
    }
    return MoAxios[AXIOS]
  }

  get config() {
    return getConfig()
  }

  init() {
    this.$axios.interceptors.response.use(
      (res) => {
        if (typeof this.options.render === 'function') {
          return this.options.render(res)
        } else {
          return res
        }
      },
      async (error) => {
        // Do something with response error
        if (error && error.response) {
          switch (error.response.status) {
            case 502:
              error.message = '网关错误'
              break
            case 504:
              error.message = '网关超时'
              break
            case 505:
              error.message = '版本不受支持'
              break
            default:
              error.message = error.response.data.message
              break
          }
        }
        throw error
      }
    )
  }

  request(apiEntity: ApiEntity) {
    const url = `${apiEntity.path}`
    const requsetOption: AxiosRequestConfig = {
      responseType: 'json',
      baseURL: this.config.moApi.baseURL,
      url,
      method: apiEntity.method,
      headers: {}
    }

    if (apiEntity.method === 'GET') {
      requsetOption.params = { params: apiEntity.params }
    } else {
      requsetOption.data = apiEntity.params
    }
    return this.$axios(requsetOption)
  }
}

ApiCall.hintSuccessHandler = (apiCall) => {
  console.info('请求成功!!')
  ApiCall.hasPrompted = true
  ElMessage.success({
    message: apiCall.successMsg,
    onClose: () => {
      ApiCall.hasPrompted = false
    }
  })
}

ApiCall.hintFailHandler = (apiCall) => {
  console.info('请求错误!!')
  ApiCall.hasPrompted = true
  apiCall.failMsg &&
    ElMessage.error({
      message: apiCall.failMsg,
      onClose: () => {
        ApiCall.hasPrompted = false
      }
    })
}

ApiCall.use(new MoAxios())
  • 现在我们就可以在我们的项目中调用api了
new ApiGetUser({params:{id:1}}).then((res)=>{
    ...
})

Gitee

https://gitee.com/ymoo/moyan-api