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

tiny-axios-wrap

v0.1.6

Published

This is an axios wrap

Downloads

8

Readme

npm install --save tiny-axios-wrap

Use

// import tiny-axios
import ajax from 'tiny-axios-wrap'

ajax(config, options)
.then(res => {
  // 若 code == 0, 为有效返回,则 res 返回的都是有效数据
  // res => {
  //  config: {},  请求的配置
  //  data: { code: 0, data: '' },  返回的数据
  //  ...
  // }
})
.catch(err => {
  // 请求错误、code != 0,都在这里返回
})

/**-------------config--------------------------**/
config = {
  method: 'post',
  url: '',
  data: {}
}
config = {
  method: 'get',
  url: '',
  params: {}
}

// 设置超时,10秒
config.timeout = 10000;

// 设置 json 格式请求
config = {
  headers: {
    'Content-Type': 'application/json'
  },
  transformRequest: [function (data) {
    return JSON.stringify(data);
  }]
}

/**------------options---------------------------**/
options = {
  // loading组件,加载状态,不需要自动关闭
  loading: {
    open() {},
    close() {}
  },
  // toast组件,错误弹框,应能自动关闭
  toast: {
    open() {},
    success() {},
    error() {},
    warn() {}
  },
  // 允许正常输出的,非成功返回码, number
  outputCodes: [],
  // 根据返回码拦截处理
  resCodeIntercept: [
    { code: '', handler() {} }
  ],
  utils: {
    auth: {
    	// 特征码字段
    	code: 'code',
    	// 成功代号
    	num: 0,
    	// 失败提示字段
    	message: 'error',
      defaultReqErrorBefore: 'rq',
      defaultResErrorBefore: 'rs',
      defaultReqErrorCode: '000',
      defaultResErrorCode: '000',
    },
    alert: {
       /* timeout 连接超时*/
    	timeoutError: '连接超时,请重试',
      // 没有数据返回
      notFoundData: '没有数据',
    	otherError: '网络错误'
    }
  }
}

// 简单的自定义封装
import axios from 'tiny-axios-wrap'
import Loading from 'tiny-loading'
import Tost from 'tiny-tost'
let ajax = (config) => {
  let options = {
    loading: Loading,
    toast: Tost,
    resCodeIntercept: {
      code: 202,
      handler() {
        console.log(202)
      }
    },
    utils: {
      auth: {
      	code: 'code',
      	num: 0,
      	message: 'msg'
      }
    }
  }
  return new Promise((resolve, reject) => {
    axios(config, options)
    .then(res => {
      res.data.code === 0 && resolve(res.data)
    })
    .catch(err => reject(err))
  })
}
ajax.prototype.then = res => {}
ajax.prototype.catch = err => {}

export default ajax

// 使用
ajax({
  method: 'post',
  url: 'http://xxxxxx',
  data: {}
})
.then(res => {
  console.warn(res);
})
.catch(err => {
  console.error(err);
})