# Link

Link 是一个用于处理 AJAX 的 Node.js 和 浏览器端的工具，经过 httpbin 和 Vitest 浏览器端、Node 端测试。

## 使用说明

```js
import { Link } from "@forsee/link";
const link = new Link({
    url: "",
    method: "post",
    params: {
        // 这个是写在 URL 后面的参数
    },
    body: {
        type: "json",
        // json form urlencoded 为对象格式

        // 其余类型直接使用那个对象就可以了
        // 比如 Buffer 就直接填入 Buffer 对象就可以上传了
        data: {
            // 这里写 Body 里面的信息
        },
    },
});

link.send().then((res) => {
    return res.json();
});
```

## 插件

### 内置 Cookie

浏览器由于被限制了，所以对 Cookie 操作有点局限。在 Nodejs 中 Cookie 会根据 Jar 中持续保存的 Cookie 进行携带或者是写入。

```js
import { Link, globalCookie } from "@forsee/link";
const link = new Link({
    url: "",
    cookie: {
        tag: "default", // cookieJar 的一个标志
        read: true, // 是否发送 cookie
        write: false, // 是否写入 cookie
    },
});
link.send().then((res) => {
    return res.json();
});
```

### Auth 操作

```js
import { Link, authPlugin } from "@forsee/link";
const link = new Link({
    url: "",
});
link.use(
    authPlugin({
        type: "basic", // 使用不同的 auth 模式
        username,
        password: passwd,
    })
)
    .send()
    .then((res) => {
        return res.json();
    });
```

### 随机 UA

浏览器无效，Node 端可以使用。

```js
import { Link, randomUA } from "@forsee/link";
const link = new Link({
    url: root + "/user-agent",
});
await link.use(randomUA()).send();
```
