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

electron-request

v1.8.3

Published

Zero-dependency, lightweight http request client for electron or nodejs

Downloads

162

Readme

English | 简体中文

electron-request

Zero-dependency, Lightweight HTTP request client for Electron or Node.js

Why electron-request ?

Electron-request uses its built-in net module in Electron environment and uses its built-in HTTP module in Node.js environment.

Net module is used in electron to better support proxy, authentication, traffic monitoring proxies and other features. Please refer to net for details.

Features

  • Zero-dependency, Lightweight
  • Quick start, similar window.fetch
  • No need to import other libraries, support file download progress and file verification
  • Support to run on Electron or Node.js, use Electron's net module first
  • Unified error handling

Install

npm install electron-request --save
# or
yarn add electron-request

Usage

import request from 'electron-request';

void (async () => {
  const url = 'https://github.com/';
  const defaultOptions = {
    method: 'GET',
    body: null,
    followRedirect: true,
    maxRedirectCount: 20,
    timeout: 0,
    size: 0,
  };
  const response = await request(url, defaultOptions);
  const text = await response.text();
})();

API

request(url[, options])

  • url: Request URL

  • options: Options

    interface Options {
      /**
       * Request method
       * @default 'GET'
       */
      method?: string;
      /**
       * Request body
       * @default null
       */
      body?: string | null | Buffer | Stream;
      /**
       * Request headers
       */
      headers?: Record<string, string | string[]>;
      /**
       * Request query
       */
      query?: Record<string, string>;
      /**
       * Allow redirect
       * @default true
       */
      followRedirect?: boolean;
      /**
       * Maximum redirect count. 0 to not follow redirect
       * @default 20
       */
      maxRedirectCount?: number;
      /**
       * Request/Response timeout in ms. 0 to disable
       * @default 0
       */
      timeout?: number;
      /**
       * Maximum response body size in bytes. 0 to disable
       * @default 0
       */
      size?: number;
      /**
       * Whether to use nodejs native request
       * @default false
       */
      useNative?: boolean;
    
      // Docs: https://www.electronjs.org/docs/api/client-request#new-clientrequestoptions
    
      /**
       * Only in Electron. When use authenticated HTTP proxy, username to use to authenticate
       */
      username?: string;
      /**
       * Only in Electron. When use authenticated HTTP proxy, password to use to authenticate
       */
      password?: string;
      /**
       * Only in Electron. Whether to send cookies with this request from the provided session
       * @default true
       */
      useSessionCookies?: boolean;
      /**
       * Only in Electron. The Session instance with which the request is associated
       * @default electron.session.defaultSession
       */
      session?: Session;
    }

Response

interface Response {
  /** Whether the response was successful (status in the range 200-299) */
  ok: boolean;
  /** Response status code */
  statusCode: number;
  /** Response headers */
  headers: Record<string, string | string[]>;
  /** Return origin stream */
  stream: Stream;
  /** Decode response as ArrayBuffer */
  arrayBuffer(): Promise<ArrayBuffer>;
  /** Decode response as Blob */
  blob(): Promise<Blob>;
  /** Decode response as text */
  text(): Promise<string>;
  /** Decode response as json */
  json<T>(): Promise<T>;
  /** Decode response as buffer */
  buffer(): Promise<Buffer>;
  /**
   * Download file to destination
   * @param {Writable} destination Writable destination stream
   * @param {ProgressCallback=} onProgress Download progress callback
   * @param {ValidateOptions=} validateOptions Validate options
   */
  download: (
    destination: Writable,
    onProgress?: ProgressCallback,
    validateOptions?: ValidateOptions,
  ) => Promise<void>;
}

/** Download progress information */
interface ProgressInfo {
  /** Total file bytes */
  total: number;
  /** Delta file bytes */
  delta: number;
  /** Transferred file bytes */
  transferred: number;
  /** Transferred percentage */
  percent: number;
  /** Bytes transferred per second */
  bytesPerSecond: number;
}

License

MIT License

electron-request vs. the Competition

| Package | Size | | --- | --- | | request | request package size | | axios | axios package size | | node-fetch | node-fetch package size | | request-pure | request-pure package size | | electron-request | electron-request package size |