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

@larks/fetch

v1.26.54-4

Published

Request library based on fetch method suitable for modern browser feature construction.

Downloads

6

Readme

Table of Contents

Installing

Using npm:

$ npm install @larks/fetch

Using bower:

$ bower install @larks/fetch

Using yarn:

$ yarn add @larks/fetch

Using pnpm:

$ pnpm add @larks/fetch

Example

Once the package is installed, you can import the library using import, Create a global instance in your project.

import { LarkFetch } from "@larks/fetch";

const larkFetch = new LarkFetch();

larkFetch
  .get("/product?id=1")
  .then(function ({ data }) {
    console.log(data);
  })
  .catch(function (error) {
    console.log(error);
  });

larkFetch.get("/product", { id: 1 });

larkFetch.post("/insert", { name: "sakamoto" });

Auto Download

If the server responds with a data type of data stream, Lark Fetch will automatically download the data, provided that you have set LarkDownloadMiddleware.

import { LarkDownloadMiddleware } from "@larks/fetch";

const larkFetch = new LarkFetch();
larkFetch.middleware.set(LarkDownloadMiddleware);

You can specify the file name through the 'download' option, or Lark can automatically recognize the response header to obtain the file name.

import { LarkFetch } from "@larks/fetch";

const larkFetch = new LarkFetch();
larkFetch.get("/download", "", { download: "filename" }); // Turn off automatic download when the download parameter is false.

FormData Wrapper

The files present in the submitted object will be automatically wrapped by Lark Fetch.

import { LarkFetch } from "@larks/fetch";

const larkFetch = new LarkFetch();
larkFetch.post("/upload", { file: new File([], "filename") });

Global Events

Use the dispatcher attribute to globally listen for Lark Events events.

import { LarkFetch } from "@larks/fetch";

const larkFetch = new LarkFetch();
larkFetch.dispatcher.on("timeout", function (options) {
  console.log(options);
});

Private Events

Events can be private to a single request, and after the request is completed, the event automatically cancels listening.

import { larkFetch } from "@larks/fetch";

const larkFetch = new LarkFetch();
const timeoutListener = larkFetch.createEventListener("timeout", (options) => {
  console.log(options);
});
larkFetch.get("/list", null, { events: [timeoutListener] });

Middleware

Global interception of requests and responses through lark fetch middleware.

import { LarkFetch } from "@larks/fetch";

const larkFetch = new LarkFetch();
const middleware = function (next) {
  return function (options) {
    options.headers.append("Authorization", "token");
    return next(options)
      .then(function (response) {
        return response;
      })
      .catch(function () {});
  };
};
larkFetch.middleware.set(middleware);

LarkFetch API

  • middleware Lark fetch middleware, used to intercept requests and responses.
  • dispatcher Used for global distribution and listening to Lark Events.
  • request Send a request to the server and receive a response from the server. When the method parameter is missing, it defaults to GET.
  • createEventListener Create a private listener for the request and pass it to the events option.
  • formData When the value of FormData is of object type, serialize the object as a specific key value pair.
import { formData } from "@larks/fetch";

formData({ file: new File(["1"], "file"), products: ["apple", "banana"] }); // FormData: { file: File, products[0]: "apple", products[1]: "banana" }
  • download Provide a general method for browsers to download server response data without providing a file name, ensuring that the server adds Access-Control-Expose-Headers to allow browsers to access Content-Disposition response headers.
import { download } from "@larks/fetch";

download({ body: new Blob() }, "filename");

LarkFetch Options

  • baseURL The basic path of all method request paths.
  • timeout Cancel the current request when the request exceeds the time limit, Default to 30000 ms.
  • paramsSerializer Request path parameter serializer, default to URLSearchParams.
  • bodySerializer Used to replace the default serializer to serialize the response body.
  • responseType Process the data into the correct format based on the Content Type response header, defaulting to auto and automatically processed by LarkFetch.
  • events Receive a set of request private events and automatically destroy the event after the request ends.
  • query Request path query parameters, separated by commas when the value of the object is an array, a custom query parameter serializer can be provided to change this behavior.
  • download After setting LarkDownloadMiddleware, use this property to set the file name or turn off automatic download.
  • filenameParser After setting LarkDownloadMiddleware, parse the downloaded file name.