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

@huolala-tech/request

v1.1.4

Published

This is just a request library to support browsers and MiniProgram platforms.

Downloads

88

Readme

request · LICENSE codecov

This is just a request library to support browsers and MiniProgram platforms.

Include

yarn add @huolala-tech/request

or

npm install @huolala-tech/request --save

Params

| name | type | description | | ----------------- | ------------------------------------------------- | ------------------------------------------------------ | | method (required) | string | Request method | | url (required) | string | Request URL | | data | any | Request payload (or query string for GET/HEAD methods) | | timeout | number | Request timeout in milliseconds | | headers | Record<string, string> | Request header | | files | Record<string, Blob | File | string> | Payload files | | responseType | text | json | arraybuffer | blob | document | Response type | | withCredentials | boolean | The withCredentials flag for XHR object | | signal | AbortSignal | An abort signal like fetch | | onUploadProgress | (info: { total: number, loaded: number }) => void | The uploading progress event |

NOTE 1: The method field

  1. Some MiniProgram platforms can only support "GET" or "POST" methods, so using a RESTful API is not the best solution for MiniPrograms.

NOTE 2: The files field

  1. In browsers, the file is represented as a Blob or File object, whereas in other MiniProgram platforms, the file is represented as a string filePath.
  2. MiniProgram platforms doese not suport multiple files uploading in once.

NOTE 3: The responseType field

  1. The values of blob and document can only be used on browser.
  2. The responseType can not be used with files on MiniProgram platforms.

NOTE 4: The withCredentials field

  1. This can only be used on browser.

Return Promise<InvokeResult>

The InvokeResult is

| name | type | description | | ---------- | ---------------------- | -------------------- | | statusCode | number | Response status code | | data | any | Response body | | headers | Record<string, string> | Response headers |

Demo

import { request } from '@huolala-tech/request';

async function main() {
  const res = await request({
    method: 'POST',
    url: 'http://localhost/api',
    data: {
      xxx: 'xxx',
    },
  });

  if (res.statusCode === 200) {
    console.log(res.data);
  }
}

Advanced Features

1. Interceptors

Taking into account the learning cost, our interceptors API design is modeled after the Axios.

import { request, interceptors } from '@huolala-tech/request';

// Add Authorization: xxx header for all requests.
interceptors.request.use((req) => {
  req.headers = { ...Object(req.headers), Authorization: 'xxx' };
});

// If any request responds with a 401 code, go to login.
interceptors.response.use((res) => {
  if (res.statusCode === 401) {
    location.href = 'http://blahblahblah/';
  }
});

request({ method: 'POST', url: 'http://localhost/api/user' });

2. New Instance

You can use create method to create a pairs request and intercepters and set a common request parameters.

import { create } from '@huolala-tech/request';
const { request, interceptors } = create({
  header: {
    'x-request-with': '@huolala-tech/request'
  }
});

// TODO

NOTE: The created pairs is an isolated instance, global intercepters will not act on the created request.