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

@xlou/xfetch

v1.0.3

Published

Use fetch/xhr more conveniently and quickly.

Downloads

5

Readme

Language

Introduction

  • Simplify and expedite your use of fetch.
  • Don't worry if your browser environment lacks fetch support; if you are using an older browser, xfetch will emulate fetch using XMLHttpRequest.
  • Respects the native implementation and prioritizes performance.

Usage

Using Script Tags for Import

<script src="https://unpkg.com/@xlou/[email protected]/dist/umd/xfetch.min.js"></script>
<!-- It is recommended to download and use the script locally -->
<script>
  /* After including this script, the xfetch object is assigned to the window */
  xfetch("https://xxx.com", {
    method: "post",
    query: {
      id: 1
    },
    data: {
      name: "Tom",
      age: 18
    }
  })
  .then((res) => { })
  .catch((err) => { })
</script>

Using in Node.js Projects

Installation

npm i @xlou/xfetch -S

Usage

import xfetch from '@xlou/xfetch'

xfetch("https://xxx.com", {
  method: "post",
  query: {
    id: 1
  },
  data: {
    name: "Tom",
    age: 18
  }
})
.then((res) => { })
.catch((err) => { })

API

xfetch

Retrieval

Script Tag: window.xfetch

Module: import xfetch from "@xlou/xfetch"

Usage

xfetch("https://xxx.com", {
  method: "post",
  query: { // Converted to URL parameters
    id: 1
  },
  data: { // Converted to body parameters; if body is specified simultaneously, this parameter will be ignored
    name: "Tom",
    age: 18
  },
  contentType: "json", // Specifies the Content-Type in the request headers
  responseType: "json" // Enables preprocessing of response data and specifies the response data type
})

Types

function xfetch(input: RequestInfo | URL, init?: XfetchInit | undefined): Promise<XfetchResponse>

type RequestInfo = Request | string

interface XfetchInit extends RequestInit {
  query?: { [prop: string]: any }
  data?: BodyInit | null | { [prop: string]: any }
  contentType?: "json" | "urlencoded" | "formData" | "text" | "xml" | "stream"
  responseType?: "json" | "text" | "blob" | "arrayBuffer" | "formData"
}

type BodyInit = ReadableStream | XMLHttpRequestBodyInit

const ContentType: { [prop: string]: string } = {
  json: "application/json;charset=UTF-8",
  urlencoded: "application/x-www-form-urlencoded;charset=UTF-8",
  formData: "multipart/form-data",
  text: "text/plain;charset=UTF-8",
  xml: "application/xml;charset=UTF-8",
  stream: "application/octet-stream"
}

Parameter Description

query

  • This parameter supports JavaScript objects and will be converted into a URL parameter string appended to the request URL.
  • It will not interfere with any existing parameters in the request URL.

data

  • When sending the request, it will be converted into body parameters based on the Content-Type value in the headers.
  • If the body parameter is specified simultaneously, this parameter will be ignored.
  • This parameter supports general JavaScript objects.
    • When Content-Type is "application/json," data will be converted to a JSON string.
    • When Content-Type is "application/x-www-form-urlencoded," data will be converted to a URL-encoded string.
    • When Content-Type is "multipart/form-data," data will be converted to FormData.
    • If data can be converted to JSON and Content-Type is not specified, the Content-Type will be set to "application/json."

contentType

  • Specifies the Content-Type in the request headers.
  • If Content-Type is already specified in the headers, this parameter will be ignored.
  • contentType values correspond to Content-Type in the headers:
    • json: "application/json;charset=UTF-8"
    • urlencoded: "application/x-www-form-urlencoded;charset=UTF-8"
    • formData: "multipart/form-data"
    • text: "text/plain;charset=UTF-8"
    • xml: "application/xml;charset=UTF-8"
    • stream: "application/octet-stream"

responseType

  • If this parameter is not specified, the response data obtained will be the same as with fetch.

  • When this parameter is specified, it means that response data preprocessing is enabled, and a preprocessed synchronous data will be returned.

    /* Normal data retrieval */
    xfetch("https://xxx.com")
    .then(async res => {
      let json = await res.json()
    })
    
    /* Specifying responseType as json */
    xfetch("https://xxx.com", {
      responseType: "json"
    })
    .then(res => {
      let json = res.jsonSync
      /* If responseType is set to blob, use blobSync, and so on */
    })

Other parameters are the same as fetch.

xhrFetch

Retrieval

Script Tag: xfetch.xhrFetch

Module: import { xhrFetch } from "@xlou/xfetch"

Usage

Usage is the same as fetch, but it is implemented using XMLHttpRequest. It is recommended to use xfetch instead of this.

Request parameters like cache, mode, signal, keepalive, redirect, integrity, referrer, and referrerPolicy are not effective.

The body in the response parameters is of Blob type, not ReadableStream.

Types

function xhrFetch(input: RequestInfo | URL, init?: RequestInit | undefined): Promise<XHRFetchResponse>;

interface XHRFetchResponse {
  readonly body: Blob | null
  readonly bodyUsed: boolean
  readonly headers: any
  readonly ok: boolean
  readonly redirected: boolean
  readonly status: number
  readonly statusText: string
  readonly type: string
  readonly url: string
  clone(): XHRFetchResponse
  arrayBuffer(): Promise<ArrayBuffer>
  blob(): Promise<Blob>
  formData(): Promise<FormData>
  json(): Promise<any>
  text(): Promise<string>
}