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

socar-baseapi

v1.1.5

Published

实现基于 openApi 的接口请求服务

Downloads

2

Readme

说明

实现基于 openApi 的接口请求服务

调用方式

 // 直接解析
 baseApi.init(openapiDocument, config)
 // 生产实例
 baseApi.producing(openapiDocument, config)

二者区别在于, 直接解析后, 请求实例和schema会保留在 baseApi 中(重复调用 baseApi.init 会替换掉之前解析的结果), 直接调用baseApi的方法即可, 如:

/**
 * 获取api实列 如果传入取 api地址,则返回指定api否则返回 api实例
 * @param {string} path api地址
 * @returns  axios instance
 */
baseApi.getApi(path)
/**
 * 获取接口 Parameters 对象
 * @param {string} path 接口路径 例如:xx/xxx
 * @param {string} method 方法名称 默认:"get"
 * @returns {object} parameters
 */
baseApi.getParameters(path, method)
/**
 * 获取过滤模型
 * @param {string} path 接口路径 例如:xx/xxx
 * @returns {object} filter 过滤模型对象
 */
baseApi.filterSchema(path)
/**
 * 获取接口 描述 schema
 * @param {string} path 接口路径 例如:xx/xxx
 * @param {string} method 方法名称 默认:"get"
 * @returns {object} schema
 */
baseApi.getSchema(path, method)

而生产实例,不会保留在 baseApi 中,而是将解析结果 apiInstance , apiPathsMap 和上述例子中的 getApi , getParameters , filterSchema , getSchema , 方法拷贝一份给独立的实例返回给调用者

直接解析

  baseApi.init(openapiDocument, {
      //基础url
      baseUrl: baseUrl,
      //错误处理函数
      catchError(err) {},
      // axios 请求拦截器中的hook
      beforeRequest(config, instance) {
          // 如果此hook返回config,则axios请求时会使用该config
          // 不返回或返回任何为false的布尔值都将使用默认
          return config
      },
      // axios 响应拦截器中的hook
      beforeResponse(response, instance) {
          // 如果此hook返回response,则axios响应数据则会使用该response
          // 不返回或返回任何为false的布尔值都将使用默认
      }
  })

生产实例

baseApi
    .producing(openapiDocument, {
        // 请求基础路径
        baseUrl: baseUrl,
        // 请求前 hook
        beforeRequest(config, instance) {
            // 如果此hook返回config,则axios请求时会使用该config
            // 不返回或返回任何为false的布尔值都将使用默认
            return config
        },
        // axios 响应拦截器中的hook
        beforeResponse(response, instance) {
            // 如果此hook返回response,则axios响应数据则会使用该response
            // 不返回或返回任何为false的布尔值都将使用默认
        }
    })
    .then((instance) => {
        // 解析后实例
        console.log(instance)
        // 测试getApi
        instance
            .getApi(/xxx/xxx)
            .get()
            .then((res) => {
                console.log(res)
            })
        // 测试获取过滤模型
        console.log(instance.filterSchema(/xxx/xxx))
        // 测试获取 Schema
        console.log(instance.getSchema(/xxx/xxx))
    })

CHANGELOG

1.1.3

增加了 producing 方法用以生产解析实例

1.1.4

修改getHook 错误

1.1.4

增加 getSchema(path,method) 未传递 path 错误提示