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

js-xfetch

v1.3.1

Published

A Fetch request tool wrapper, convenient global management.

Downloads

787

Readme

js-xfetch

A Fetch request tool wrapper, convenient global management.

Fetch 被称为下一代 Ajax 技术,浏览器原生支持,采用 Promise 方式来处理数据。是一种简洁明了的 API,比 XMLHttpRequest 更加简单易用。

但是使用不太方便,本插件对 Fetch API 进行了了一次封装,解决了 Fetch API 原生的一些痛点,增加了一些常用功能并使用类初始化方便全局管理,具体功能如下。

  • 浏览器原生支持
  • 参数优化(参考 Axios)
  • 自动转换请求与返回数据
  • 增加请求/响应/错误拦截功能
  • 增加超时设置
  • 增加重试/重连设置
  • 主动取消请求、取消重复请求、添加请求白名单(abortController)
  • 请求头拦截处理(Headers)
  • 日志输出,请求完成时的回调(无论是否成功) Hooks。
  • 提供实例默认配置修改方法(timeout/baseUrl...)

Install

npm install js-xfetch -S

Import

// Es or Node
const {
  XFetch,
  XFetchContentType,
  XFetchHttpMethod,
  XFetchHeader,
  XFetchRequestConfig,
  XFetchOptions,
  CODE_MSG
} = require('js-xfetch');
import {
  XFetch,
  XFetchContentType,
  XFetchHttpMethod,
  XFetchHeader,
  XFetchRequestConfig,
  XFetchOptions,
  CODE_MSG
} from 'js-xfetch';
import XFetch from 'js-xfetch';

// Browser
<script src="xfetch.min.js"></script>
// const {
//   XFetch,
//   XFetchContentType,
//   XFetchHttpMethod,
//   XFetchHeader,
//   XFetchRequestConfig,
//   XFetchOptions,
//   CODE_MSG
// } = xfetch;

Use

$fetch = XFetch.create({
  baseUrl: '', // default: ''
  timeout: 5000, // timeout default: 30000
  cancelDuplicatedRequest: true, // Whether to cancel the duplicate request default: true
  // default: undefined
  retryConfig: {
    retry: 3, // count
    delay: 2000 // delay time
  },
  requestHandler: (config) => {
    console.log('requestHandler', config);
  },
  responseHandler: (response) => {
    console.log('responseHandler', response);
  },
  errorHandler: (error) => {
    if (XFetch.isCancel(error)) {
      return;
    }
    console.log('errorHandler', error);
  },
  setRequestHeaders: (config) => {
    console.log('setRequestHeaders', config);
    // return new Headers({});
    return config.headers;
  },
  requestFinally: () => {
    console.log('requestFinally Hooks'); // The callback when the request is completed, regardless of the result.
  }
});

$fetch
  .get('/test', { test: 1, v: 1 }) // params
  .then((data) => console.log(data))
  .catch((e) => console.log(e));

XFetch.post(
  '/test/post',
  { test: 'data' }, // data
  {
    // headers: Headers,
    // 'Content-Type': 'contentType',
    // autoContentType: Boolean,
    // qsStringifyOptions: { hasBrackets, hasIndex, arr2str, urlEncode }
  },
  true // WhiteList default: false
);

XFetch.request(
  XFetchHttpMethod.POST,
  '/test/post',
  {
    // params?: any;
    // data?: any;
    // headers?: XFetchHeader;
    // autoContentType?: boolean;
    // 'Content-Type'?: string;
    // qsStringifyOptions?: {
    //     arr2str?: boolean;
    //     hasIndex?: boolean;
    //     urlEncode?: boolean;
    //     hasBrackets?: boolean;
    // };
  },
  true // WhiteList default: false
);

XFetch.cancelRequest('reason');
XFetch.cancelWhiteListRequest('reason');
XFetch.qsStringify({ start: 0, count: 20, obj: { a: 1 }, arr: [1, 2, 3], str: '1' }, { hasIndex: true }); // 'start=0&count=20&obj[a]=1&arr[0]=1&arr[1]=2&arr[2]=3&str=1'
XFetch.qsParse('start=0&count=20&x=1&x=2&x=3'); // { start: '0', count: '20', x: [1, 2, 3], '/': 'start=0&count=20&x=1&x=2&x=3' };

API Docs

API Docs

Others