@larks/fetch v1.26.54-4
Table of Contents
- Installing
- Example
- Auto Download
- FormData Wrapper
- Global Events
- Private Events
- LarkFetch API
- LarkFetch Options
- Middleware
Installing
Using npm:
$ npm install @larks/fetchUsing bower:
$ bower install @larks/fetchUsing yarn:
$ yarn add @larks/fetchUsing pnpm:
$ pnpm add @larks/fetchExample
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
middlewareLark fetch middleware, used to intercept requests and responses.dispatcherUsed for global distribution and listening to Lark Events.requestSend a request to the server and receive a response from the server. When the method parameter is missing, it defaults toGET.createEventListenerCreate a private listener for the request and pass it to theeventsoption.formDataWhen 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" }downloadProvide a general method for browsers to download server response data without providing a file name, ensuring that the server addsAccess-Control-Expose-Headersto allow browsers to accessContent-Dispositionresponse headers.
import { download } from "@larks/fetch";
download({ body: new Blob() }, "filename");LarkFetch Options
baseURLThe basic path of all method request paths.timeoutCancel the current request when the request exceeds the time limit, Default to30000ms.paramsSerializerRequest path parameter serializer, default toURLSearchParams.bodySerializerUsed to replace the default serializer to serialize the response body.responseTypeProcess the data into the correct format based on the Content Type response header, defaulting toautoand automatically processed by LarkFetch.eventsReceive a set of request private events and automatically destroy the event after the request ends.queryRequest 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.downloadAfter settingLarkDownloadMiddleware, use this property to set the file name or turn off automatic download.filenameParserAfter settingLarkDownloadMiddleware, parse the downloaded file name.