1.26.54-4 • Published 6 months ago

@larks/fetch v1.26.54-4

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

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.
1.26.54-3

6 months ago

1.26.54-4

6 months ago

1.26.54-1

6 months ago

1.26.54-2

6 months ago

1.26.54

11 months ago

1.26.53

12 months ago

1.26.52

12 months ago

1.26.51

12 months ago

1.26.50

12 months ago

1.26.49

12 months ago