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

api-configer

v0.1.1

Published

api static conifg

Downloads

4

Readme

api-configer

接口配置化模块

    import resource ,{ axios } from 'api-configer'
    
    const api = resource({
            user:{
                get:'./user/info'
            },
            account:{
                login:{
                    post:'./account/login'
                }
            }
        })

    api.user().then()
    api.account.login().then()
    
    // 拦截器
    axios.interceptors.response
    axios.interceptors.request

差值参数

region_post:{
    post:'./region/update?id={id}',
}
配置中参数规则
{id} => 参数必填
{id:3} => 默认参数
{id:} => 默认为空,region_post({}) 映射的接口为 => ./region/update

payload 请求方式

  1. application/json
  2. application/x-www-form-urlencoded

payload:Boolean , default:true

region_post:{
    post:'./region/update?id={id}',
    payload:false
}

withCredentials 跨域携带凭证

withCredentials:Boolean , default:false

region_post:{
    post:'./region/update?id={id}',
    payload:false
}

abort 中断请求

当重复发送请求时,中断未响应的请求(但保留最后一个请求)

payload:Boolean , default:false

region_post:{
    post:'./region/update?id={id}',
    abort:true
}

compile

是否对url做编译

payload:Boolean , default:true

export default {
    user_list:{
        get:'./user/list?aaa={bbb}'
        compile:false
    }
}

生成 => Request URL:you root/user/list?aaa={bbb}

model 中 api 根路径配置 (ROOT)

axios全局配置在config mock中 axios.defaults.baseURL

有时期望特殊配置 如下

export default {
    ROOT:'http://www.baidu.com/console',
    user_list:{
        get:'./user/list'
    },
    user_list2:{
        get:'../user/list'
    }
}

user_list生成 => Request URL:http://www.baidu.com/console/user/list user_list2生成 => Request URL:http://www.baidu.com/user/list

api schema 配置

目的:为了解决服务端返回数据不可信问题, 业务场景如下

期望的字段类型是 plan object=>{}, 但是服务端返回null, 或其他基本类型,获取属性时出现 the_key is not defined 错误,导致页面 block;plan array => [], 场景和 object 一致

期望为Number类型 返回String 如:'33', '33' => 33

测试同学可以通过 warn log 定位到 bug 根源,不必通过前端中转

处理,根据以上经常出现的问题,做了如下两步主要处理

  1. 抛错:抛出 warn 警告
  1. 重写:根据定义的类型重写返回字段

响应结果如下:

avatar

类型配置:服务端返回的JSON对象数据类型

引用类型:Object=> {}、Array=> []

基本类型:String、Number、Boolean

规则:添加验证规则的字段开启验证,未添加的忽略验证

如下接口,在文档中有company字段,但验证规则中未添加company字段,则忽略company验证

export default {
    ...
    user_list:{
        get:'./user/info',
        // 这里既是schema配置
        schema:{
            data:{
                user:{
                    name:String
                },
                currentPermission:[Number],
                groupList:[{
                    companyId:Number,
                    companyName:String,
                    companyShortName:String
                    ...
                }]

            }
        }
    }
    ...
}