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

xhr-fetch-lib

v1.3.0

Published

简单好用的xhr请求库

Downloads

26

Readme

简单好用的 xhr 请求库,包含 get,post,put,delete, upload 文件上传方法

1.安装 npm / yarn 安装

npm install xhr-fetch-lib
yarn add xhr-fetch-lib

2.ts类型定义

  • 默认导出的fetch
declare const fetch: ({ method, url, data, headers, withCredentials, responseParser, xhrSetting, }: Options) => Promise<Record<string, unknown> | string>;

export declare type Options = {
    /** 请求方法 */
    method?: 'get' | 'post' | 'put' | 'delete' | 'head';
    /** 请求url */
    url: string;
    /** 请求数据,对于get请求,data用object默认会转为 key=value&key1=value1的格式 */
    data?: Record<string, unknown> | string;
    /** 请求头 */
    headers?: Record<string, string>;
    /** withCredentials设置,默认true */
    withCredentials?: boolean;
    /** 处理xhr响应 */
    responseParser?: (xhr: XMLHttpRequest) => Record<string, unknown> | string;
    /** xhr设置,e.g. responseType,timeout等设置  */
    xhrSetting?: XHRSetting;
};

export default fetch;
  • get/post/put/delete 方法
declare type simpleRequest = (
/** 请求url */
url: string,
/** 请求数据, 对于get请求,data用object默认会转为 key=value&key1=value1的格式 */
data: Record<string, unknown> | string,
/** 请求头 */
headers: Record<string, string>) => Promise<Record<string, unknown> | string>;

export declare const get: simpleRequest;
export declare const post: simpleRequest;
export declare const put: simpleRequest;
export declare const del: simpleRequest;
  • upload文件上传
/**
 * 上传文件
 *
 * @export
 * @param {string} url api地址
 * @param {(Record<string, unknown> | null)} data 数据
 * @param {(File | Blob)} file 待上传文件
 * @param {(Record<string, string> | null)} [headers] 自定义请求头部
 * @param {((e: ProgressEvent & { percent: number }) => void)} [onProgress] 上传进度回调
 * @return {*}  {Promise<XMLHttpRequest>}
 */
export default function upload(url: string, data: Record<string, unknown> | null, file: File | Blob, headers?: Record<string, string> | null, onProgress?: (e: ProgressEvent & {
    percent: number;
}) => void): Promise<XMLHttpRequest>;
  1. 使用示例
import fetch, { get, post, put, del, upload } from 'xhr-fetch-lib';

// 直接调用get post put del
get(apiUrl).then((res) => {
  responseHandler(res);
});

// 用fetch设置返回blob实现下载
fetch({
  url,
  method,
  data,
  xhrSetting: { responseType: 'blob' },
  responseParser: (xhr) => xhr.response,
}).then((blob) => {
  const url = window.URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = fileName;
  a.click();
});

//文件上传
const file = e.target.files[0];
upload(api, { key: 'value' }, file, null, (e) => console.log(e.percent));
  1. 返回值

默认是 json-bigint parse 的 object 对象 , 同 JSON.parse 返回值

import JSONbig from 'json-bigint';

const JSONBig = JSONbig({ storeAsString: true });

function parseResponse(xhr: XMLHttpRequest): unknown {
  let result;

  try {
    result = JSONBig.parse(xhr.responseText);
  } catch (e) {
    result = xhr.responseText;
  }
  return result;
}
  1. 对于 get 请求,data 用 object 默认会转为 key=value&key1=value1 的格式

  2. 默认导出的 fetch 函数,可以自定义 responseParser,默认是JSONBig.parse(xhr.responseText)

  3. fetch 可以自定义 XMLHttpRequest 属性, 例如 responseType,timeout 等