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

@kscommon/easy-api

v1.0.6

Published

Wrapper for Axios that allows to easily requests

Downloads

1

Readme

easy-api

Wrapper for Axios that allows to easily requests

Installation

Before starting you need to install axios and qs, Because easy-api depends on axios and qs

CDN

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/qs.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/easy-api.umd.cjs"></script>

Npm

npm install axios
npm install qs
npm install easy-api

Usage

/* api/index.js */
import { defineConfig, createInstance, createApis } from 'easy-api';
// const { defineConfig, createInstance, createApis } = window.easyapi // usage by cdn

function streamType(config) {
    return ['blob', 'arraybuffer', 'stream'].includes(config.responseType);
}
/* your responseFormat function */
function responseFormat(response, format = false) {
    if (response && (response.status === 200 || response.status === 304)) {
        if (format) {
            return response.data
        } else {
            if (streamType(response.config)) { // stream type
                return response;
            }
            return response.data;
        }
    }
    // error status
    return response
}

defineConfig({
    timeout: 30000,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' },
    responseFormat, // responseFormat is used to transform the request's response 
    // ...  your other axios config
})

export const instance = createInstance();

// axios instance
instance.interceptors.request.use(config => {}, error => {})
instance.interceptors.response.use(response => {}, error => {})

export default createApis;
/* api/apis.js */
import createApis from 'api/index.js';
const options = { headers: { 'Content-Type': 'application/json' } };

const apis = createApis({
    getInfo: {
        url: '/api/get/info',
        method: 'get',
    },
    postList: null,
    save: {
        url: '/api/post/save',
        method: 'post',
        options: {
            headers: { 'Content-Type': 'application/json' },
            timeout: 8000 // 单独timeout; 未设置会采用默认配置的30000
        }
    },
    saveForm: {
        url: '/api/post/saveForm',
        method: 'post',
    },
    upload: {
        url: '/api/post/upload',
        method: 'upload',
    },
    other: {
        url: 'http://192.168.4.50:3000/api/post/xxx', // starts with http will miss proxy
        method: 'post',
    },
})

export default apis
import apis from 'api/apis.js';

const getInfo = () => {
    apis.getInfo({ name: 'zs' }).then((res) => {
        console.log(res);
    });
};
const postList = () => {
    // This is the request configuration, you can override the configuration in apis file
    const config = { url: '/api/post/list', method: 'post', options: { timeout: 3000, headers: { 'Content-Type': 'application/json' } } };

    apis.postList({ id: '9527' }, config).then((res) => {
        console.log(res);
    }).catch(err => {
        console.log(err);
    })
};

Cancel Request

const cancel = apis.getInfo.cancel; //this is cancel function
cancel && cancel(index); // The index is the order of requests to cancel the interface; if the index is not passed, it means to cancel all multiple calls of the apis.getInfo interface

Cancel All Apis Request

Object.keys(apis).forEach(key => {
    apis[key].cancel()
})

Demo

cd server
npm i
npm run start
cd demo
npm i
npm run dev