# @aligov/util

> util

Latest version **1.3.7** (published 2023-04-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install @aligov/util
pnpm add @aligov/util
yarn add @aligov/util
bun add @aligov/util
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score; popular repo.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.3.7 |
| Published | 2023-04-10 |
| First published | 2019-08-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 524.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 18616 |
| Maintainers | shenyu.wsy, liunian, tao1991123, itrip, xiazhiqiang, mo.zhou, guoliang.hgl, daip, baizhao |
| Keywords | ice, react, component |

## Links

- npm: https://www.npmjs.com/package/@aligov/util
- Repository: https://github.com/alibaba/ice
- Homepage: https://unpkg.com/@aligov/util@1.3.7/build/index.html
- Issues: https://github.com/alibaba/ice/issues
- npm.io page: https://npm.io/package/@aligov/util

## Dependencies (7)

- [url](https://npm.io/package/url.md) ^0.11.0
- [moment](https://npm.io/package/moment.md) ^2.24.0
- [js-cookie](https://npm.io/package/js-cookie.md) ^2.2.1
- [prop-types](https://npm.io/package/prop-types.md) ^15.5.8
- [@alifd/next](https://npm.io/package/@alifd/next.md) 1.x
- [umi-request](https://npm.io/package/umi-request.md) 1.2.6
- [query-string](https://npm.io/package/query-string.md) ^6.13.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.3.7 (latest) — 2023-04-10
- 1.2.5-beta (beta) — 2020-05-13
- 1.3.6 — 2023-03-22
- 1.3.5 — 2023-03-22
- 1.3.4 — 2023-03-22
- 1.3.3 — 2022-06-24
- 1.3.2 — 2020-11-02
- 1.3.1 — 2020-10-28
- 1.3.0 — 2020-08-06
- 1.2.7 — 2020-07-07
- 1.2.6 — 2020-06-09
- 1.2.5 — 2020-05-13
- 1.2.4 — 2020-01-06
- 1.1.4 — 2020-01-06
- 1.2.3 — 2020-01-03
- … 19 more at https://npm.io/package/@aligov/util/versions

## README

# @aligov/util

数字政务中后台项目的一些工具包，包括 cookie、request 等。

## request

基于 [umi-request][] 封装的请求库，包含错误处理等功能。umi-request 底层使用了 [whatwg-fetch][] 提供的能力。

## 用法

```js
import { request } from '@aligov/util';

request('/api/list').then(res => { console.log(res); })
```

详细配置参数间 [umi-request][] 和 [whatwg-fetch][]，下面列出常见用法。

常见用法：

- `request.get(url, options)`，默认是 `get` 方法，所以直接用 `request` 即可
- `request.post(url, options)`
- `request.put(url, options)`
- `request.patch(url, options)`
- `request.delete(url, options)`

`options` 常用属性

- `params`，会作为查询参数附加到 url 上，如 `{ params: { status: 0 } }` 会变为 `status=0` 添加到 url 上。
- `data`，请求发送的 body，使用于 `post`，`put` 和 `patch` 方法
- `requestType`，data 数据的类型，可选值有 `json` 和 `form`，默认是 `json`
- `cache`，[fetch 请求的 cache 参数][Request.cache]，库默认设置为 `no-store`。`no-store` 和 `no-cache` 时，对于 GET 请求和 HEAD 请求，接口 url 添加了 `__noCache__=${timestamp}` 的参数。如果需要缓存，那么可以设置为 `default`。如果不需要缓存，但 `__noCache__` 和请求的参数冲突，那么可以先设为 `default`，再自行给 url 添加不冲突属性来防止缓存。

`options.extraOptions` umi-request 扩展属性

- `autoShowError`: `boolean`，如果设置了，那么会自动展示错误信息；设置为 `false` 则可不自动使用内置的方案来提示，而是自行处理，包括捕捉但不展示。默认为 `true`。
- `errorTitle`: `string`，如果设置了，那么作为错误提示的标题，默认为空
- `showErrorTitle`: `boolean`，如果没设置 `errorTitle`，并且选项为 `true`，那么根据请求状态来获取错误描述信息来作为错误提示标题，默认为 `false`

`window.__autoLogin__` === `true` 时会在要求登录时自动跳转到 `/login` 页面。

## csrf token

获取 csrf token，目前是从 cookie 中获取 `ctoken` 的值。

```js
import { getCToken } from '@aligov/util';

getCToken();
```

## url

```js
import { url } from '@aligov/util';

const urlObj = url.parse(location.search);

const link = url.format(urlObj);
```

基于 [url](https://www.npmjs.com/package/url)。

## cookie

```js
import { cookie } from '@aligov/util';

cookie.get('cookie-name');
```

基于 [js-cookie][]。

## 参考

- [umi-request][]
- [前后端接口规范][req-spec]

[whatwg-fetch]: https://github.com/github/fetch
[umi-request]: https://github.com/umijs/umi-request/blob/master/README_zh-CN.md
[req-spec]: https://yuque.antfin-inc.com/xgztel/fed/ue6sux "请求规范"
[js-cookie]: https://www.npmjs.com/package/js-cookie
[Request.cache]: https://developer.mozilla.org/zh-CN/docs/Web/API/Request/cache

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