@longzai-intelligence-transport/http 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +147 -0
- package/dist/chunk-CenpyGzJ.js +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1 -0
- package/package.json +109 -0
package/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# @longzai-intelligence-transport/http
|
|
2
|
+
|
|
3
|
+
HTTP 工具统一入口,整合核心模块和各框架适配器,提供类型安全的 HTTP 客户端解决方案。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @longzai-intelligence-transport/http
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 功能特性
|
|
12
|
+
|
|
13
|
+
- **类型安全的 API 定义** - 基于 Zod Schema 的请求/响应类型推断
|
|
14
|
+
- **多框架适配器** - 支持 React(SWR)和 NestJS(Swagger)
|
|
15
|
+
- **统一的 HTTP 客户端** - 基于 Axios 的 REST 客户端
|
|
16
|
+
- **API 函数工厂** - 自动生成类型安全的 API 调用函数
|
|
17
|
+
|
|
18
|
+
## 子模块
|
|
19
|
+
|
|
20
|
+
### 核心模块
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { createHttpClient, createApiFunctions } from '@longzai-intelligence-transport/http';
|
|
24
|
+
// 或
|
|
25
|
+
import { createHttpClient, createApiFunctions } from '@longzai-intelligence-transport/http/core';
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### React 适配器
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import {
|
|
32
|
+
createQueryHook,
|
|
33
|
+
createMutationHook,
|
|
34
|
+
} from '@longzai-intelligence-transport/http/adapters/react';
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### NestJS 适配器
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import {
|
|
41
|
+
ApiRoute,
|
|
42
|
+
RequireAuth,
|
|
43
|
+
zodToOpenApi,
|
|
44
|
+
} from '@longzai-intelligence-transport/http/adapters/nestjs';
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## 依赖
|
|
48
|
+
|
|
49
|
+
### 必需依赖
|
|
50
|
+
|
|
51
|
+
- `zod` - Schema 验证(peer dependency)
|
|
52
|
+
|
|
53
|
+
### 可选依赖
|
|
54
|
+
|
|
55
|
+
- `react` - React 框架(使用 React 适配器时需要)
|
|
56
|
+
- `swr` - SWR 数据请求库(使用 React 适配器时需要)
|
|
57
|
+
- `@nestjs/common` - NestJS 核心模块(使用 NestJS 适配器时需要)
|
|
58
|
+
- `@nestjs/swagger` - Swagger 集成模块(使用 NestJS 适配器时需要)
|
|
59
|
+
|
|
60
|
+
## 使用示例
|
|
61
|
+
|
|
62
|
+
### 定义 API 路由
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { defineRoute } from '@longzai-intelligence-transport/http';
|
|
66
|
+
import { z } from 'zod';
|
|
67
|
+
|
|
68
|
+
const UserSchema = z.object({
|
|
69
|
+
id: z.string(),
|
|
70
|
+
name: z.string(),
|
|
71
|
+
email: z.string().email(),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const routes = {
|
|
75
|
+
getUser: defineRoute({
|
|
76
|
+
method: 'GET',
|
|
77
|
+
path: '/users/:id',
|
|
78
|
+
response: UserSchema,
|
|
79
|
+
}),
|
|
80
|
+
createUser: defineRoute({
|
|
81
|
+
method: 'POST',
|
|
82
|
+
path: '/users',
|
|
83
|
+
body: UserSchema.omit({ id: true }),
|
|
84
|
+
response: UserSchema,
|
|
85
|
+
}),
|
|
86
|
+
};
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### 创建客户端和 API 函数
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { createHttpClient, createApiFunctions } from '@longzai-intelligence-transport/http';
|
|
93
|
+
|
|
94
|
+
const client = createHttpClient({
|
|
95
|
+
baseURL: 'https://api.example.com',
|
|
96
|
+
headers: {
|
|
97
|
+
Authorization: 'Bearer token',
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const api = createApiFunctions(client, routes);
|
|
102
|
+
|
|
103
|
+
// 类型安全的 API 调用
|
|
104
|
+
const user = await api.getUser({ id: '123' });
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### React Hook 使用
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import { createQueryHook, createMutationHook } from '@longzai-intelligence-transport/http/adapters/react';
|
|
111
|
+
|
|
112
|
+
const useGetUser = createQueryHook(api.getUser, 'user');
|
|
113
|
+
const useCreateUser = createMutationHook(api.createUser);
|
|
114
|
+
|
|
115
|
+
function UserComponent() {
|
|
116
|
+
const { data, isLoading } = useGetUser({ id: '123' });
|
|
117
|
+
const { trigger } = useCreateUser();
|
|
118
|
+
|
|
119
|
+
return (
|
|
120
|
+
// ...
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### NestJS 控制器使用
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
import { ApiRoute, RequireAuth } from '@longzai-intelligence-transport/http/adapters/nestjs';
|
|
129
|
+
import { Controller, Get, Post, Body } from '@nestjs/common';
|
|
130
|
+
|
|
131
|
+
@Controller('users')
|
|
132
|
+
@RequireAuth()
|
|
133
|
+
export class UserController {
|
|
134
|
+
@Get(':id')
|
|
135
|
+
@ApiRoute({
|
|
136
|
+
summary: '获取用户信息',
|
|
137
|
+
response: UserSchema,
|
|
138
|
+
})
|
|
139
|
+
async getUser() {
|
|
140
|
+
// ...
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## 许可证
|
|
146
|
+
|
|
147
|
+
UNLICENSED
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e=Object.defineProperty,t=Object.getOwnPropertyDescriptor,n=Object.getOwnPropertyNames,r=Object.prototype.hasOwnProperty,i=(t,n)=>{let r={};for(var i in t)e(r,i,{get:t[i],enumerable:!0});return n||e(r,Symbol.toStringTag,{value:`Module`}),r},a=(i,a,o,s)=>{if(a&&typeof a==`object`||typeof a==`function`)for(var c=n(a),l=0,u=c.length,d;l<u;l++)d=c[l],!r.call(i,d)&&d!==o&&e(i,d,{get:(e=>a[e]).bind(null,d),enumerable:!(s=t(a,d))||s.enumerable});return i},o=(e,t,n)=>(a(e,t,`default`),n&&a(n,t,`default`));export{o as n,i as t};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import"./chunk-CenpyGzJ.js";export*from"@longzai-intelligence-transport/http-core";export{};
|
package/package.json
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@longzai-intelligence-transport/http",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"api",
|
|
6
|
+
"facade",
|
|
7
|
+
"http",
|
|
8
|
+
"typescript",
|
|
9
|
+
"utils"
|
|
10
|
+
],
|
|
11
|
+
"license": "UNLICENSED",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./core": {
|
|
24
|
+
"types": "./dist/core.module.d.ts",
|
|
25
|
+
"import": "./dist/core.module.js"
|
|
26
|
+
},
|
|
27
|
+
"./adapters/react": {
|
|
28
|
+
"types": "./dist/react-adapter.d.ts",
|
|
29
|
+
"import": "./dist/react-adapter.js"
|
|
30
|
+
},
|
|
31
|
+
"./adapters/nestjs": {
|
|
32
|
+
"types": "./dist/nestjs-adapter.d.ts",
|
|
33
|
+
"import": "./dist/nestjs-adapter.js"
|
|
34
|
+
},
|
|
35
|
+
"./adapters/taro": {
|
|
36
|
+
"types": "./dist/taro-adapter.d.ts",
|
|
37
|
+
"import": "./dist/taro-adapter.js"
|
|
38
|
+
},
|
|
39
|
+
"./adapters/bun": {
|
|
40
|
+
"types": "./dist/bun-adapter.d.ts",
|
|
41
|
+
"import": "./dist/bun-adapter.js"
|
|
42
|
+
},
|
|
43
|
+
"./presets/browser": {
|
|
44
|
+
"types": "./dist/browser-preset.d.ts",
|
|
45
|
+
"import": "./dist/browser-preset.js"
|
|
46
|
+
},
|
|
47
|
+
"./presets/node": {
|
|
48
|
+
"types": "./dist/node-preset.d.ts",
|
|
49
|
+
"import": "./dist/node-preset.js"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "tsgo --build tsconfig/build.json && resolve-aliases -p tsconfig/build.json",
|
|
54
|
+
"build:prod": "NODE_ENV=production tsdown",
|
|
55
|
+
"prepublishOnly": "bun run build:prod",
|
|
56
|
+
"typecheck": "bun run typecheck:app && bun run typecheck:node && bun run typecheck:test",
|
|
57
|
+
"typecheck:app": "tsgo --noEmit -p tsconfig/app.json",
|
|
58
|
+
"typecheck:node": "tsgo --noEmit -p tsconfig/node.json",
|
|
59
|
+
"typecheck:test": "tsgo --noEmit -p tsconfig/test.json",
|
|
60
|
+
"lint": "oxlint && oxfmt --check",
|
|
61
|
+
"lint:fix": "oxlint --fix && oxfmt",
|
|
62
|
+
"test": "bun test",
|
|
63
|
+
"test:watch": "bun test --watch",
|
|
64
|
+
"test:coverage": "bun test --coverage",
|
|
65
|
+
"clean": "rimraf dist out .cache"
|
|
66
|
+
},
|
|
67
|
+
"dependencies": {
|
|
68
|
+
"@longzai-intelligence-transport/http-adapter-bun": "0.1.0",
|
|
69
|
+
"@longzai-intelligence-transport/http-adapter-nestjs": "0.1.0",
|
|
70
|
+
"@longzai-intelligence-transport/http-adapter-react": "0.1.0",
|
|
71
|
+
"@longzai-intelligence-transport/http-adapter-taro": "0.1.0",
|
|
72
|
+
"@longzai-intelligence-transport/http-core": "0.1.0",
|
|
73
|
+
"@longzai-intelligence-transport/http-preset-browser": "0.1.0",
|
|
74
|
+
"@longzai-intelligence-transport/http-preset-node": "0.1.0"
|
|
75
|
+
},
|
|
76
|
+
"devDependencies": {
|
|
77
|
+
"@nestjs/common": "^11.1.26",
|
|
78
|
+
"@nestjs/swagger": "^11.4.4",
|
|
79
|
+
"@types/react": "^19.2.17",
|
|
80
|
+
"react": "^19.2.7",
|
|
81
|
+
"swr": "^2.4.1",
|
|
82
|
+
"zod": "^4.4.3"
|
|
83
|
+
},
|
|
84
|
+
"peerDependencies": {
|
|
85
|
+
"@nestjs/common": "^11.0.0",
|
|
86
|
+
"@nestjs/swagger": "^11.0.0",
|
|
87
|
+
"@tarojs/taro": ">=3.6.0",
|
|
88
|
+
"react": ">=18.0.0",
|
|
89
|
+
"swr": ">=2.0.0",
|
|
90
|
+
"zod": "^4.4.3"
|
|
91
|
+
},
|
|
92
|
+
"peerDependenciesMeta": {
|
|
93
|
+
"react": {
|
|
94
|
+
"optional": true
|
|
95
|
+
},
|
|
96
|
+
"swr": {
|
|
97
|
+
"optional": true
|
|
98
|
+
},
|
|
99
|
+
"@nestjs/common": {
|
|
100
|
+
"optional": true
|
|
101
|
+
},
|
|
102
|
+
"@nestjs/swagger": {
|
|
103
|
+
"optional": true
|
|
104
|
+
},
|
|
105
|
+
"@tarojs/taro": {
|
|
106
|
+
"optional": true
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|