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

ljc-axios

v1.1.1

Published

基于axios再封装一个大部分API可以通用的,快速二封方案

Downloads

3

Readme

ljc-axios

light-joint-case 轻量的联合的方案,缩写也是俺的名儿

一个可以方便使用的api封装方案,以后会将想要封装的东西放到这个 “ light-joint-case ” 系列里

Install

# npm 
npm i ljc-axios

# yarn
yarn add ljc-axios

Usage

In your api.js:

import { createAxios } from 'ljc-axios';

const server1 = createAxios({host:'https://my.host/**/data/'});
const server2 = createAxios({
  host:'https://media.host/**/media/',
  timeout: 30000,
  headers: {/*xxxxxxxx*/},
  filterGet: true, // v1.1.0 之后默认开启。去除GET请求的 undefined 和 空字符串
});

// 以下是旧版用法,不推荐
// import { createGroup } from 'ljc-axios';

// // or Proxy url
// const server1 = createGroup('https://my.host/**/data/');
// const server2 = createGroup('https://media.host/**/media/');

export default {
  login: server1.postRequest('user/login'),
  cdnImg: server2.getRequest('image/'),
  // ...
}

In your service scripts:

import apis from 'api.js'

apis.login({ account: 'xxx', password: 'xxx'}).then(() => {
  // ****
})

apis.cdnImg({}, { suffix: `${imgId}` }).then(() => {
  // ****
})

cancel

If you want to cancel a request, you need to watch out:

// work
const req = testApi({ id: 'xxx' }).then(res => {
  console.log(res);
}).catch(err => {
  console.log(err);
});
// but throw error here
setTimeout(req.cancel, 100);

The functions like createGroup('xxx').getRequest will return a Proxy. you can use req.then().catch()... directly, but after that, req will be a Promise without <Function> cancel;

// all work
const req = testApi({ id: 'xxx' });
req.then(res => {
  console.log(res);
}).catch(err => {
  console.log(err);
});
setTimeout(req.cancel, 100);

Details

createAxios

v1.1.0之后的写法,更贴近Axios @param CreateOption
@returns axios Instance with extends functions

CreateOption

| key | type | description | | --------- | ------- | ---------------------------------------------------- | | host | String | axios self | | timeout | Number | axios self | | headers | Object | axios self | | filterGet | Boolean | skip 'undefined' and '' in GET mothod, default: TRUE |

createGroup 不推荐 Not recommended

@param { String } host
@param { Number } timeout
@param { Object } config
@returns axios Instance with extends functions

getRequest(String:url [, Object: AskConfig])

@returns A proxy of a function called Asker ...more like : putRequestgetRequestpostRequestdeleteRequest

Asker(Object:params|data [, Object: AskConfig])

@returns A proxy of a Promise Send request when it apply

AskConfig

As arg in getRequest or Asker

| key | options | description | | --------- | :------ | :--------------------------------------------------------------------- | | suffix | String | requestUrl = url + suffix | | formData | Boolean | default: false; when it was true, parse data to FormData Object | | filterGet | Boolean | default: true; same as CreateOption, but specifically for this request | | ... | ... | any axios options |

For Proxy

Compatible with traditional browsers, you may need to configure something for proxy. 兼容传统浏览器您可能需要为 Proxy 配置一些东西。

like: <script src="https://unpkg.com/[email protected]/dist/es6-proxy-polyfill.js"></script>