easy-api-client
Version:
开源项目服务端api框架easy-spring-boot-api的typescript客户端api对接工具包
148 lines (121 loc) • 3.61 kB
Markdown
### easy-api-client 简介
> 此项目用于快速接入服务端 api 框架 easy-spring-boot-api,基于 axios,可用于 web 或者 node 环境。提供了 ApiClient 工具,自动对 api 加签、验签、加解密等等功能,在保障服务器 api 安全的前提下,能做到业务无感知,简化前后端开发人员的联调工作。
#### 安装依赖
```bash
npm install easy-api-client
# 或者
yarn add easy-api-client
```
#### 使用方式
**方式 1: Vite/Webpack/Rollup 等打包工具**
```typescript
import {ApiClient, newApiClientInstance, RequestType} from "easy-api-client";
let client: ApiClient = newApiClientInstance({
baseUrl: "http://localhost:8080/server/",
channelId: "web",
appId: "test-demo",
secret: "NKVNcuwwEF56c22A",
locale: "zh_CN",
apiSuffix: ".do"
});
client.request({
apiName: "/demo/pay",
type: RequestType.post,
needSignResult: true,
needSign: true
}, {
appId: client.options.appId,
orderId: "easy_api_client001",
description: "just a test!",
currency: "CNY",
totalAmount: 1,
originalAmount: 5,
userId: "U008958",
userNickname: "小明"
}).then((response: any) => {
console.log(response.data);
}, (reason: any) => {
console.log(reason);
});
```
**方式 2: 浏览器直接使用(UMD Bundle)**
```html
<html>
<head>
<title>Easy API Client Example</title>
</head>
<body>
<!-- 引入 UMD Bundle -->
<script src="./lib/index.umd.js"></script>
<script>
// 所有导出的 API 都在 window.EasyApiClient 对象下
const EasyApiClient = window.EasyApiClient;
// 创建客户端实例
const client = EasyApiClient.newApiClientInstance({
baseURL: "http://localhost:8080/server/",
channelId: "web",
appId: "test-demo",
secret: "NKVNcuwwEF56c22A",
locale: "zh_CN",
apiSuffix: ".do"
});
// 发起请求
client.request({
apiName: "/demo/pay",
type: EasyApiClient.RequestType.post,
needSignResult: true,
needSign: true
}, {
appId: client.options.appId,
orderId: "easy_api_client001",
description: "just a test!",
currency: "CNY",
totalAmount: 1,
originalAmount: 5,
userId: "U008958",
userNickname: "小明"
}).then((response) => {
console.log(response.data);
});
</script>
</body>
</html>
```
**方式 3: CDN 引入**
```html
<!-- unpkg -->
<script src="https://unpkg.com/easy-api-client/lib/index.umd.js"></script>
<!-- jsDelivr -->
<script src="https://cdn.jsdelivr.net/npm/easy-api-client/lib/index.umd.js"></script>
```
### npm 包的发布流程
1. 构建项目
```bash
npm run build
```
2. 登录账号
```bash
npm login --registry https://registry.npmjs.org/
```
3. 发布包
```bash
npm publish --registry https://registry.npmjs.org/
```
4. 切换回国内镜像
```bash
npm config set registry https://registry.npmmirror.com/
```
### 核心功能
- ✅ 自动签名/验签
- ✅ 请求加密/响应解密
- ✅ 会话管理
- ✅ 请求拦截器
- ✅ 响应拦截器
- ✅ 支持多种 HTTP 方法(GET/POST/PUT/DELETE 等)
- ✅ 浏览器和 Node.js 双端支持
### 更多文档
- [UMD Browser Bundle 使用指南](./BROWSER_BUNDLE_USAGE.md)
- [构建说明](./UMD_BUILD_README.md)
### License
MIT License