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

@x-drive/fetch

v1.2.0

Published

NodeJs 服务端 Http 请求模块

Downloads

2

Readme

NodeJs 服务端 Http 请求模块

简易的支持自动域名替换、接口别名的请求模块

使用

  • @x-drive/fetch 加入到项目依赖中
  • 模块最基本的请求方法是 fetch, 由于是基础方法是以参数会比较复杂, 一般情况下建议直接使用 getpost 方法发起请求。
  • 模块默认使用 console 输出异常信息, 如系统本身有其他 logger , 可调用模块的 setLogger 方法设置模块的信息输出对象。
  • 如果希望使用别名、自动域名替换,请在 发起请求 前调用 addApiMap 方法提前设置好 api

方法

  • fetch 基础请求方法
    /**
    * 获取数据
    * @param type              请求类型
    * @param uri               请求地址
    * @param query             请求参数
    * @param data              发送数据
    * @param config            请求配置
    * @param needProcessConfig 是否需要处理配置
    */
    function fetch<T = unknown>(type: string, uri: string, query?: Record<string, any>, data?: any, config?: IFetchConfig, retry?: number, needProcessConfig?: boolean): Promise<T>;
  • get 发起一个 get 请求
    /**
    * 发起一个 get 请求
    * @param uri   请求地址
    * @param query 请求参数
    */
    function get<T = unknown>(uri: string, query?: Record<string, any>, config?: IFetchConfig): Promise<T>;
  • post 发起一个 post 请求
    /**
    * 发起一个 post 请求
    * @param uri   请求地址
    * @param query 请求参数
    * @param data  请求数据
    */
    function post<T>(uri: string, query?: any, data?: any, config?: IFetchConfig): Promise<T>;
  • setDefHeaders 设置默认 Headers
    /**设置默认 Headers */
    function setDefHeaders(headers: Record<string, any>): void;
  • setDefConfig 设置默认全局配置
    /**设置默认全局配置 */
    function setDefConfig(config: IFetchConfig): void;
  • setRandomStr 给指定对象增加一个随机字符串
    /**给指定对象增加一个随机字符串 */
    function setRandomStr(query: any): void;
  • addApiMap 添加并处理新的 api
    /**添加并处理新的 api */
    function addApiMap(apis: Record<string, string>, host?: Record<string, string>): void;
  • resolve 解析请求别名
    /**
    * 解析请求别名
    * @param uri 请求地址
    */
    function resolve(uri: string): string;
  • setLogger 设置模块 logger
    /**设置模块 logger */
    function setLogger(logger: any): void;

配置项

/**支持的返回数据类型 */
type IFetchType = "text" | "json" | "blob" | "buffer" | "arrayBuffer";

/**请求配置 */
interface IFetchConfig extends Partial<Omit<RequestInit, "body" | "method">> {
    /**数据返回类型 */
    type?: IFetchType;

    /**是否强制同源 */
    sameOrigin?: boolean;

    /**请求自动重试次数 */
    retry?: number;

    /**自动重试间隔时间 */
    retryDelay?: number;

    /**是否自动追加随机数retry,默认 false */
    random?: boolean;

    /**数据返回时的钩子 */
    onResponse?: (res?: Response) => Promise<unknown>;

    /**是否 stringify Post 请求数据,默认 true*/
    stringify?: boolean;

    /**
     * 是否不抛出异常, 默认 false
     * - 当有 retry 时会优先 retry, 都失败后才会抛异常
     * - 1.1.0 及以前版本不抛异常
     */
    silence?: boolean;
}