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

request-decorate

v1.1.7

Published

**目前内部使用,尚在完善中,请勿下载**

Downloads

1

Readme

目前内部使用,尚在完善中,请勿下载

进行如下配置后,基本等同 axios-expand 模块

import RequestDecorate, { plugins } from "request-decorate";
import axios from "axios";

// 使用 axios 处理请求
RequestDecorate.defaults.request = axios;
// 注册内置插件
RequestDecorate.plugins(plugins);

const RD = new RequestDecorate({
    apis: {
        login: {
            url: "/api/login",
            method: "POST"
        },
    }
});

RD.request({
    api: "login",
    requestType: "form",
    data: {
        username: "admin",
        password: "123456"
    }
});

可以的对axios $.ajax wx.request 等任意环境的请求方法进行装饰 使用配制式请求 统一请求参数格式 统一响应处理 统一错误拦截处理

import RequestDecorate from "request-decorate";
import axios from "request-decorate";
import $ from "juqery";

// 所有实例的默认参数
// apis - 接口配置列表 可以在实例参数中配置
RequestDecorate.defaults.apis = {
    userLogin: {
        url: "/api/login",
        method: "POST",
    },
};


// -- 不同的请求方式适配发送 JSON 数据 --

// 适配 axios
const RDAxios = new RequestDecorate({
    // apis, // 实例接口配置列表
    // 适配方法 必须返回 promise
    request(options) {
        return axios(options);
    },
});

// 适配 jQuery
const RDJquery = new RequestDecorate({
    request(options) {
        // 返回 promise
        return new Promise((resolve, reject) => {
            options.success = resolve;
            options.error = reject;
            $.ajax(options);
        });
    },
    // $.ajax JSON数据相关设置
    contentType: "application/json;charset=utf-8", // 任意参数都会合并到 options 中
    processData: false,
    // options 传入 request 方法前的处理
    before: [
        // $.ajax 用 type 设置请求方法,在此转换
        function (options) {
            options.type = options.method;
        },
        // 将 data 转换为 JSON 字符串
        function (options) {
            options.data = JSON.stringify(options.data);
        },
    ],
});

// 适配 fetch
const RDFetch = new RequestDecorate({
    request(options) {
        return fetch(options.url, options);
    },
    // 设置 plugin 可以同时注入 before middle after
    plugins: [
        {
            // fetch data 转换为 body
            before(options) {
                if (options.data) {
                    options.headers = {
                        "Content-Type": "application/json;charset=utf-8",
                    };
                    options.body = JSON.stringify(options.data);
                }
            },
            // fetch 404 等状态不会触发 catch。手动抛出异常
            middle(promise) {
                return promise.then((response) => {
                    if (response.status > 300) {
                        throw response;
                    }
                });
            },
        },
    ],
});

// -- 调用请求。通过上边的适配,可以使用统一格式 --

RDAxios.request({
    api: "userLogin", // apis 中对应的键名
    data: { username: "admin", password: "123456" },
});
// 参数简写方式 同上
RDJquery.request("userLogin", { username: "admin", password: "123456" });
RDFetch.request("userLogin", { username: "admin", password: "123456" });