# js-request-lib

> 基于JS实现的前后端（browser and node）共用请求模块

Latest version **1.0.10** (published 2024-04-15) · ISC license · 0 weekly downloads

## Install

```sh
npm install js-request-lib
pnpm add js-request-lib
yarn add js-request-lib
bun add js-request-lib
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.10 |
| Published | 2024-04-15 |
| First published | 2024-01-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 81.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | diehunter |
| Keywords | JSRequest, request, js-request, axios, fetch, ajax, js-request-lib |

## Links

- npm: https://www.npmjs.com/package/js-request-lib
- Repository: https://gitee.com/DieHunter/js-request-lib
- npm.io page: https://npm.io/package/js-request-lib

## Dependencies (2)

- [utils-lib-js](https://npm.io/package/utils-lib-js.md) 2.0.21
- [abort-controller](https://npm.io/package/abort-controller.md) ^3.0.0

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.0.10 (latest) — 2024-04-15
- 1.0.9 — 2024-04-15
- 1.0.7 — 2024-01-11
- 1.0.6 — 2024-01-11
- 1.0.5 — 2024-01-11
- 1.0.4 — 2024-01-11
- 1.0.2 — 2024-01-11

## README

# js-request

#### 介绍

基于 JS 实现的前后端（browser and node）共用请求模块
代码冗余，故从https://gitee.com/DieHunter/utils-lib-js中分离

#### 博客介绍

https://hunter1024.blog.csdn.net/article/details/126719561

#### 使用说明

1.  使用 pnpm i utils-lib-js 安装工具包依赖（二选一）
2.  使用 pnpm i js-request-lib 安装请求依赖（二选一）
3.  使用 pnpm debug 进行源码调试
4.  在代码中引入 js-request-lib 模块, 创建一个请求实例

```javascript
import { Request } from "../dist/esm/index.js";
const request = new Request("https://www.xxx.com");
```

5.  使用 GET 方法发起一个简单的请求：

```javascript
request
  .GET("/users", { page: 1, limit: 10 })
  .then((response) => {
    console.log("GET 请求成功", response);
  })
  .catch((error) => {
    console.error("GET 请求失败", error);
  });
```

6.  发起 POST 请求

```javascript
const requestBody = { username: "hunter", password: "secret" };

request
  .POST("/login", null, requestBody)
  .then((response) => {
    console.log("POST 请求成功", response);
  })
  .catch((error) => {
    console.error("POST 请求失败", error);
  });
```

7.  除了 GET 和 POST，该工具类还支持其他请求方法，例如 PUT、DELETE、OPTIONS、HEAD 和 PATCH。使用方式相似，只需调用相应的方法：

```javascript
// 发起 PUT 请求
request
  .PUT("/users/1", null, { name: "hunter" })
  .then((response) => {
    console.log("PUT 请求成功", response);
  })
  .catch((error) => {
    console.error("PUT 请求失败", error);
  });
```

8.  拦截器，通过拦截器，你可以在请求中添加额外的头部信息、处理响应数据

```javascript
// 添加请求拦截器
request.use("request", (config) => {
  // 在请求发送前执行逻辑
  console.log("请求拦截器 - 请求发送前", config);
  return config;
});

// 添加响应拦截器
request.use("response", (response) => {
  // 在响应处理后执行逻辑
  console.log("响应拦截器 - 响应处理后", response);
  return response;
});
// 添加错误拦截器
request.use("error", (err) => {
  // 在出错时执行逻辑
  console.log("出错 - 处理后", err);
  return err;
});
```

9.  在创建请求实例时设置其他配置，例如设置超时时间、自定义请求头等：

```javascript
const customRequest = new Request("https://api.example.com", {
  timeout: 5000, // 设置超时时间为 5 秒
  headers: {
    Authorization: "Bearer YOUR_ACCESS_TOKEN", // 设置自定义请求头
    "Content-Type": "application/json",
  },
});

customRequest
  .GET("/data")
  .then((response) => {
    console.log("请求成功", response);
  })
  .catch((error) => {
    console.error("请求失败", error);
  });
```

#### 参与贡献

1.  Fork 本仓库
2.  Star 本仓库
3.  提出建议
4.  新建 Pull Request

---
_Source: https://npm.io/package/js-request-lib · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
