npm package: @larks/fetch
Description: Request library based on fetch method suitable for modern browser feature construction.
Author: Little Randy
Latest version: 1.26.54-4

## Table of Contents

- [Installing](#installing)
- [Example](#example)
- [Auto Download](#auto-download)
- [FormData Wrapper](#formdata-wrapper)
- [Global Events](#global-events)
- [Private Events]()
- [LarkFetch API](#larkfetch-api)
- [LarkFetch Options](#larkfetch-options)
- [Middleware](#middleware)

## Installing

Using npm:

```bash
$ npm install @larks/fetch
```

Using bower:

```bash
$ bower install @larks/fetch
```

Using yarn:

```bash
$ yarn add @larks/fetch
```

Using pnpm:

```bash
$ pnpm add @larks/fetch
```

## Example

Once the package is installed, you can import the library using `import`, Create a global instance in your project.

```js
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`.

```js
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.

```js
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.

```js
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.

```js
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.

```js
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.

```js
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.

```js
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.

```js
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.