moyan-api
TypeScript icon, indicating that this package has built-in type declarations

1.0.45 • Public • Published

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

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.450latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.450
1.0.440
1.0.430
1.0.420
1.0.410
1.0.400
1.0.390
1.0.380
1.0.370
1.0.350
1.0.330
1.0.320
1.0.310
1.0.300
1.0.290
1.0.280
1.0.270
1.0.260
1.0.250
1.0.230
1.0.220
1.0.210
1.0.200
1.0.190
1.0.180
1.0.170
1.0.160
1.0.150
1.0.140
1.0.130
1.0.120
1.0.110
1.0.100
1.0.90
1.0.80
1.0.70
1.0.60
1.0.50
1.0.40
1.0.30
1.0.20
1.0.10

Package Sidebar

Install

npm i moyan-api

Weekly Downloads

0

Version

1.0.45

License

ISC

Unpacked Size

133 kB

Total Files

44

Last publish

Collaborators

  • moyan