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

@felibs/request

v0.1.4

Published

hooks for vue3.x, easy to use request or async function, inspired by umijs/hooks

Downloads

43

Readme

hooks for vue3.x, easy to use request or async function, inspired by umijs/hooks

对于 request 的设计

const result = request(api, option)

api:

  • '/v1/get/xxx', 内部使用axios, 默认get方法
  • object: { uri, method}
  • promise函数,可自定义
  • Array,数组,对array进行处理同步/异步处理

option:

  • manual: enum[true, false], default: false // 是否需要手动触发
  • onSuccess: Function, default: noop, // 成功回掉
  • onError: Function, default: noop, // 失败回掉
  • loading: enum[true, false], default: false // 默认loading的值
  • data: any, default: null // 默认的数据
  • params: object, default: {}, // 请求的参数
  • throttle: number, default: 0, // 节流时间(ms)
  • debounce: number, default: 0, // 防抖时间(ms)
  • format: Function, default: noop, //对结果进行转换
  • async: enum[true, false], default: true // 对于传入数组的API,是否异步执行,true:异步,false,同步,如果为false,则会把上一个函数的返回值传给下一个函数作为参数
  • cacheKey: string, // 对结果进行缓存, 全局, 预加载数据
  • key: key => key, // 可以实现多个loading
  • refreshDeps: array
  • delay: number // todo
  • cancelToken: // todo

result:

  • loading: enum[true, false] // 是否在请求中
  • data: any, // 请求的结果,
  • run: manual为true是手动触发的函数
  • error: Object,错误信息
  • cancel: Function, // todo

关于params 1、如果api为function, 则此function会接收多个参数,前部分为run的参数集合,最后一个为params 2、如果api为string, 则

demo1: 自动执行


import { useRequest } from '@felibs/request'

export default {
    setup() {
        const getUser = async () => {};
        const { data, loading, error } = request(getUser);
        
        if (error) {
            return <p>error</p>
        } else if (loading) {
            return <p>loading</p>
        } else {
            return <p>user:{data}</p>
        }
    },
}

demo2: 手动执行

import { useRequest } from "@felibs/request"

export default {
    setup() {
        const { loading, run, cancel, data } = useRequest('/v1/data/getButtonText', {
            manual: true,
        }, { config })
        return (
            <button onClick={run} loading={ loading }>{ data }</button>
            <button onClick={cancel} loading={ loading }>{ data }</button>
        )
    }
}

demo3: 防抖函数

import { useRequest } from '@felibs/request'

export default {
    setup() {
        const { loading, run, data } = useRequest('/v1/getUserByName', {
            manual: true,
            debounce: 500,
        })
        return <button onClick={run} loading={loading}> { data } </button>
    },
}

demo4: 节流函数

import { useRequest } from '@felibs/request'

export default {
    setup() {
        const { loading, run, data } = useRequest('/v1/getUserByName', {
            manual: true,
            throttle: 500,
        })
        return <button onClick={run} loading={loading}>{ data }</button>
    },
}

demo5: format对结果进行format

import { useRequest } from '@felibs/request'

export default {
    setup() {
        const { loading, run, data } = useRequest('/v1/getUserByName', {
            manual: true,
            format: (result) => {
                return result.map(item => item)
            }
        })
        return <button onClick={run} loading={loading}>{ data }</button>
    },
}

demo6: async: 对传入的接口进行串行或者并行 default: true

true:异步,false,同步,如果为false,则会把上一个函数的返回值传给下一个函数作为参数

import { useRequest } from '@felibs/request'

export default {
    setup() {
        const { loading, run, data } = request(['/v1/getUserByName', '/v1/getUserByName2'], {
            manual: true,
            async: false,
        })
        // data为数组,数组的顺序跟返回的顺序一致
        return <button onClick={run} loading={loading}>{ data }</button>
    },
}

demo7: cacheKey: 全局的预加载

import { useRequest } from '@felibs/request'

export default {
    setup() {
        const { loading, run, data } = useRequest('/v1/getUserByName2', {
            manual: true,
            cacheKey: 'globalUser',
        })
        // data为存储数据,等真正的接口返回时,它会动态改变
        return <button onClick={run} loading={loading}>{ data }</button>
    },
}

demo8: key: 可以多个动态key

import { useRequest } from '@felibs/request'

export default {
    setup() {
        const { loading, run, data } = useRequest('/v1/getUserByName2', {
            manual: true,
            cacheKey: 'globalUser',
        })
        // data为存储数据,等真正的接口返回时,它会动态改变
        return <button onClick={run} loading={loading}>{ data }</button>
    },
}