@snack-kit/lib 0.3.0 → 0.4.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 +248 -17
- package/dist/cjs/{chunk-N7BJS6LI.cjs → chunk-XEQEQWDB.cjs} +48 -14
- package/dist/cjs/chunk-XEQEQWDB.cjs.map +1 -0
- package/dist/cjs/{chunk-YOWLTZM5.cjs → chunk-ZJMTV2GJ.cjs} +7 -6
- package/dist/cjs/chunk-ZJMTV2GJ.cjs.map +1 -0
- package/dist/cjs/debugger.cjs +7 -3
- package/dist/cjs/http.cjs +13 -13
- package/dist/cjs/index.cjs +20 -16
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/es/{chunk-4DLFIN3C.js → chunk-4SGFAIRT.js} +4 -3
- package/dist/es/chunk-4SGFAIRT.js.map +1 -0
- package/dist/es/{chunk-JQYH5FWE.js → chunk-YV6SGXUJ.js} +48 -15
- package/dist/es/chunk-YV6SGXUJ.js.map +1 -0
- package/dist/es/debugger.js +2 -2
- package/dist/es/http.js +1 -1
- package/dist/es/index.js +3 -3
- package/dist/es/index.js.map +1 -1
- package/dist/types/context-C4dFUDbw.d.ts +237 -0
- package/dist/types/debugger.d.ts +3 -0
- package/dist/types/http.d.ts +61 -236
- package/dist/types/index.d.ts +3 -2
- package/dist/umd/debugger.global.js +28 -5
- package/dist/umd/debugger.global.js.map +1 -1
- package/dist/umd/http.global.js +45 -12
- package/dist/umd/http.global.js.map +1 -1
- package/dist/umd/index.global.js +48 -13
- package/dist/umd/index.global.js.map +1 -1
- package/package.json +1 -1
- package/dist/cjs/chunk-N7BJS6LI.cjs.map +0 -1
- package/dist/cjs/chunk-YOWLTZM5.cjs.map +0 -1
- package/dist/es/chunk-4DLFIN3C.js.map +0 -1
- package/dist/es/chunk-JQYH5FWE.js.map +0 -1
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* HTTP 请求配置,扩展自 AxiosRequestConfig
|
|
5
|
+
*/
|
|
6
|
+
interface HttpRequestConfig<D = unknown> extends AxiosRequestConfig<D> {
|
|
7
|
+
/** Context key,自动从 HttpContext 获取 baseURL */
|
|
8
|
+
ctx?: string;
|
|
9
|
+
/** 自定义请求 ID,用于主动取消 */
|
|
10
|
+
cancelId?: string;
|
|
11
|
+
/** 回调接收生成的 cancelId */
|
|
12
|
+
onCancelId?: (id: string) => void;
|
|
13
|
+
/**
|
|
14
|
+
* 是否添加时间戳防缓存,默认 false(添加防缓存时间戳)
|
|
15
|
+
* 设为 true 时不添加时间戳
|
|
16
|
+
*/
|
|
17
|
+
cache?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 快捷请求方法(Get / Post 等)的 config 入参类型。
|
|
21
|
+
*
|
|
22
|
+
* 与 `HttpRequestConfig` 相同,但不含 `method`(由快捷方法预置)。
|
|
23
|
+
* 适用于以纯配置对象方式调用时,避免重复指定已知的 method。
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const config: HttpMethodConfig = { url: '/api/user', ctx: 'user-svc' }
|
|
28
|
+
* const { data } = await Get(config)
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
type HttpMethodConfig<D = unknown> = Omit<HttpRequestConfig<D>, 'method'>;
|
|
32
|
+
/**
|
|
33
|
+
* HTTP 请求结果,扩展自 AxiosResponse
|
|
34
|
+
*/
|
|
35
|
+
interface HttpResult<T = unknown> extends Partial<AxiosResponse<T>> {
|
|
36
|
+
/** 有 error 则请求失败,无 error 则成功 */
|
|
37
|
+
error?: HttpError;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* HTTP 错误信息
|
|
41
|
+
*/
|
|
42
|
+
interface HttpError {
|
|
43
|
+
/** HTTP 状态码 */
|
|
44
|
+
status?: number;
|
|
45
|
+
/** 错误描述 */
|
|
46
|
+
message: string;
|
|
47
|
+
/** 服务端返回的原始错误体 */
|
|
48
|
+
data?: unknown;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* 网关 `/ngw/context` 接口返回的元数据(`$info` 字段)
|
|
52
|
+
*/
|
|
53
|
+
interface ContextInfo {
|
|
54
|
+
/** 网关版本号,例如 `"4.6.2-r10"` */
|
|
55
|
+
version: string;
|
|
56
|
+
/** 网关域名,例如 `"http://172.16.32.155:20000"` */
|
|
57
|
+
domain: string;
|
|
58
|
+
/** 客户端标识 */
|
|
59
|
+
client: string;
|
|
60
|
+
/** 当前语言 */
|
|
61
|
+
lang: string;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* 网关 `/web-debug/host/list` 接口返回的服务列表项
|
|
65
|
+
*/
|
|
66
|
+
interface ServerItem {
|
|
67
|
+
/** 服务唯一标识 */
|
|
68
|
+
key: string;
|
|
69
|
+
/** 服务名称 */
|
|
70
|
+
name: string;
|
|
71
|
+
/** 服务类型分组,可为空字符串 */
|
|
72
|
+
type: string;
|
|
73
|
+
/** 服务地址,用于 `context.load()` 切换路由映射 */
|
|
74
|
+
origin: string;
|
|
75
|
+
/** 代理配置(由网关管理,前端只读) */
|
|
76
|
+
proxyInfo: Record<string, unknown>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* globalThis 上的标志位 key,由 `Debugger.init()` 写入。
|
|
81
|
+
* `Context.load()` 无参数调用时,若检测到此标志则跳过自动加载,
|
|
82
|
+
* 由 Debugger 负责在用户选择服务后调用 `Context.load(origin)`。
|
|
83
|
+
*/
|
|
84
|
+
declare const DEBUGGER_ACTIVE_KEY = "__snackkit_debugger__active";
|
|
85
|
+
/**
|
|
86
|
+
* 管理服务地址映射的上下文单例
|
|
87
|
+
*
|
|
88
|
+
* 支持三种加载方式:
|
|
89
|
+
* - **不传参数**:生产环境自动以当前页面 Origin 为网关,开发环境由 `Debugger` 接管
|
|
90
|
+
* - **远程加载**:传入网关 URL,自动请求 `GET /ngw/context` 获取映射表
|
|
91
|
+
* - **本地注入**:直接传入键值对象,不发起网络请求
|
|
92
|
+
*
|
|
93
|
+
* @example 生产环境不传参(推荐)
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { Context } from '@snack-kit/lib/http'
|
|
96
|
+
*
|
|
97
|
+
* await Context.load()
|
|
98
|
+
* Context.get('ngw') // 'https://app.example.com/ngw'
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* @example 远程加载(指定网关)
|
|
102
|
+
* ```ts
|
|
103
|
+
* await Context.load('http://172.16.32.155:20000')
|
|
104
|
+
* Context.get('ngw') // 'http://172.16.32.155:20000/ngw'
|
|
105
|
+
* Context.info // { version: '4.6.2-r10', domain: '...', ... }
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* @example 本地注入
|
|
109
|
+
* ```ts
|
|
110
|
+
* await Context.load({
|
|
111
|
+
* 'user-svc': 'http://user.api.com',
|
|
112
|
+
* 'order-svc': 'http://order.api.com',
|
|
113
|
+
* })
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
declare class HttpContext {
|
|
117
|
+
private _store;
|
|
118
|
+
private _loaded;
|
|
119
|
+
private _info;
|
|
120
|
+
/**
|
|
121
|
+
* 加载服务地址映射
|
|
122
|
+
*
|
|
123
|
+
* 支持三种调用方式:
|
|
124
|
+
* - **传入字符串**:作为网关地址,自动请求 `GET /ngw/context` 获取映射表
|
|
125
|
+
* - **传入对象**:直接注入键值映射,不发起网络请求
|
|
126
|
+
* - **不传参数**:
|
|
127
|
+
* - 生产环境(`NODE_ENV === 'production'`):以当前页面 `Origin` 为网关地址自动加载
|
|
128
|
+
* - 开发环境:立即返回,不做任何操作(应由 `Debugger.init()` 接管加载流程)
|
|
129
|
+
*
|
|
130
|
+
* 远程加载时,响应中的 `$info` 元数据字段会被单独存储,不计入路由映射表。
|
|
131
|
+
*
|
|
132
|
+
* @param source 网关 URL / 键值对象 / 省略
|
|
133
|
+
* @param timeout 远程加载超时(ms),默认 5000
|
|
134
|
+
*
|
|
135
|
+
* @example 不传参——生产环境自动使用当前页面 Origin
|
|
136
|
+
* ```ts
|
|
137
|
+
* await Context.load()
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @example 传入网关地址
|
|
141
|
+
* ```ts
|
|
142
|
+
* await Context.load('http://172.16.32.155:20000', 3000)
|
|
143
|
+
* ```
|
|
144
|
+
*
|
|
145
|
+
* @example 本地注入
|
|
146
|
+
* ```ts
|
|
147
|
+
* await Context.load({ 'user-svc': 'http://user.api.com' })
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
load(source?: string | Record<string, string>, timeout?: number): Promise<void>;
|
|
151
|
+
/**
|
|
152
|
+
* 获取指定 ctx key 对应的服务地址
|
|
153
|
+
* @param key ctx key,例如 `'ngw'`、`'osc'`
|
|
154
|
+
* @returns 服务地址,未找到时返回空字符串
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```ts
|
|
158
|
+
* await Context.load('http://172.16.32.155:20000')
|
|
159
|
+
* Context.get('osc') // 'http://172.16.32.155:20000/osc'
|
|
160
|
+
* Context.get('none') // ''
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
get(key: string): string;
|
|
164
|
+
/**
|
|
165
|
+
* 网关元数据(仅远程加载后可用)
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```ts
|
|
169
|
+
* await Context.load('http://172.16.32.155:20000')
|
|
170
|
+
* Context.info?.version // '4.6.2-r10'
|
|
171
|
+
* Context.info?.domain // 'http://172.16.32.155:20000'
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
get info(): ContextInfo | undefined;
|
|
175
|
+
/**
|
|
176
|
+
* Context 是否已加载
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* Context.loaded // false
|
|
181
|
+
* await Context.load({ 'user-svc': 'http://user.api.com' })
|
|
182
|
+
* Context.loaded // true
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
get loaded(): boolean;
|
|
186
|
+
/**
|
|
187
|
+
* 清空所有映射并重置加载状态
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* Context.clear()
|
|
192
|
+
* Context.loaded // false
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
clear(): void;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* HttpContext 全局单例
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```ts
|
|
202
|
+
* import { Context } from '@snack-kit/lib/http'
|
|
203
|
+
*
|
|
204
|
+
* await Context.load('http://172.16.32.155:20000')
|
|
205
|
+
* Context.get('osc') // 'http://172.16.32.155:20000/osc'
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
declare const Context: HttpContext;
|
|
209
|
+
/**
|
|
210
|
+
* 当前页面的 origin(`protocol + host`)
|
|
211
|
+
*
|
|
212
|
+
* 在非浏览器环境(如 SSR/Node)返回空字符串。
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```ts
|
|
216
|
+
* import { Origin } from '@snack-kit/lib/http'
|
|
217
|
+
* // 浏览器中:'https://app.example.com'
|
|
218
|
+
* // Node 环境:''
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
declare const Origin: string;
|
|
222
|
+
/**
|
|
223
|
+
* 当前页面路径的首段,可直接用作 `ctx` 参数
|
|
224
|
+
*
|
|
225
|
+
* 例如 `/osc/employee` → `'osc'`,在非浏览器环境返回空字符串。
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* ```ts
|
|
229
|
+
* import { Ctx, Get } from '@snack-kit/lib/http'
|
|
230
|
+
*
|
|
231
|
+
* // 当前路径为 /osc/... 时,Ctx === 'osc'
|
|
232
|
+
* const result = await Get('/employee', { ctx: Ctx })
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
declare const Ctx: string;
|
|
236
|
+
|
|
237
|
+
export { Context as C, DEBUGGER_ACTIVE_KEY as D, HttpContext as H, Origin as O, type ServerItem as S, type ContextInfo as a, Ctx as b, type HttpError as c, type HttpMethodConfig as d, type HttpRequestConfig as e, type HttpResult as f };
|
package/dist/types/debugger.d.ts
CHANGED
package/dist/types/http.d.ts
CHANGED
|
@@ -1,200 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* HTTP 请求配置,扩展自 AxiosRequestConfig
|
|
5
|
-
*/
|
|
6
|
-
interface HttpRequestConfig<D = unknown> extends AxiosRequestConfig<D> {
|
|
7
|
-
/** Context key,自动从 HttpContext 获取 baseURL */
|
|
8
|
-
ctx?: string;
|
|
9
|
-
/** 自定义请求 ID,用于主动取消 */
|
|
10
|
-
cancelId?: string;
|
|
11
|
-
/** 回调接收生成的 cancelId */
|
|
12
|
-
onCancelId?: (id: string) => void;
|
|
13
|
-
/**
|
|
14
|
-
* 是否添加时间戳防缓存,默认 false(添加防缓存时间戳)
|
|
15
|
-
* 设为 true 时不添加时间戳
|
|
16
|
-
*/
|
|
17
|
-
cache?: boolean;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* HTTP 请求结果,扩展自 AxiosResponse
|
|
21
|
-
*/
|
|
22
|
-
interface HttpResult<T = unknown> extends Partial<AxiosResponse<T>> {
|
|
23
|
-
/** 有 error 则请求失败,无 error 则成功 */
|
|
24
|
-
error?: HttpError;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* HTTP 错误信息
|
|
28
|
-
*/
|
|
29
|
-
interface HttpError {
|
|
30
|
-
/** HTTP 状态码 */
|
|
31
|
-
status?: number;
|
|
32
|
-
/** 错误描述 */
|
|
33
|
-
message: string;
|
|
34
|
-
/** 服务端返回的原始错误体 */
|
|
35
|
-
data?: unknown;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* 网关 `/ngw/context` 接口返回的元数据(`$info` 字段)
|
|
39
|
-
*/
|
|
40
|
-
interface ContextInfo {
|
|
41
|
-
/** 网关版本号,例如 `"4.6.2-r10"` */
|
|
42
|
-
version: string;
|
|
43
|
-
/** 网关域名,例如 `"http://172.16.32.155:20000"` */
|
|
44
|
-
domain: string;
|
|
45
|
-
/** 客户端标识 */
|
|
46
|
-
client: string;
|
|
47
|
-
/** 当前语言 */
|
|
48
|
-
lang: string;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* 网关 `/web-debug/host/list` 接口返回的服务列表项
|
|
52
|
-
*/
|
|
53
|
-
interface ServerItem {
|
|
54
|
-
/** 服务唯一标识 */
|
|
55
|
-
key: string;
|
|
56
|
-
/** 服务名称 */
|
|
57
|
-
name: string;
|
|
58
|
-
/** 服务类型分组,可为空字符串 */
|
|
59
|
-
type: string;
|
|
60
|
-
/** 服务地址,用于 `context.load()` 切换路由映射 */
|
|
61
|
-
origin: string;
|
|
62
|
-
/** 代理配置(由网关管理,前端只读) */
|
|
63
|
-
proxyInfo: Record<string, unknown>;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* 管理服务地址映射的上下文单例
|
|
68
|
-
*
|
|
69
|
-
* 支持两种加载方式:
|
|
70
|
-
* - **远程加载**:传入网关 URL,自动请求 `GET /ngw/context` 获取映射表
|
|
71
|
-
* - **本地注入**:直接传入键值对象
|
|
72
|
-
*
|
|
73
|
-
* @example 远程加载
|
|
74
|
-
* ```ts
|
|
75
|
-
* import { Context } from '@snack-kit/lib/http'
|
|
76
|
-
*
|
|
77
|
-
* await Context.load('http://172.16.32.155:20000')
|
|
78
|
-
* Context.get('ngw') // 'http://172.16.32.155:20000/ngw'
|
|
79
|
-
* Context.info // { version: '4.6.2-r10', domain: '...', ... }
|
|
80
|
-
* ```
|
|
81
|
-
*
|
|
82
|
-
* @example 本地注入
|
|
83
|
-
* ```ts
|
|
84
|
-
* await Context.load({
|
|
85
|
-
* 'user-svc': 'http://user.api.com',
|
|
86
|
-
* 'order-svc': 'http://order.api.com',
|
|
87
|
-
* })
|
|
88
|
-
* ```
|
|
89
|
-
*/
|
|
90
|
-
declare class HttpContext {
|
|
91
|
-
private _store;
|
|
92
|
-
private _loaded;
|
|
93
|
-
private _info;
|
|
94
|
-
/**
|
|
95
|
-
* 加载服务地址映射
|
|
96
|
-
*
|
|
97
|
-
* 远程加载时,响应中的 `$info` 元数据字段会被单独存储,不计入路由映射表。
|
|
98
|
-
*
|
|
99
|
-
* @param source 远程加载传入网关 URL;本地注入传入键值对象
|
|
100
|
-
* @param timeout 远程加载超时(ms),默认 5000
|
|
101
|
-
*
|
|
102
|
-
* @example 远程加载
|
|
103
|
-
* ```ts
|
|
104
|
-
* await Context.load('http://172.16.32.155:20000', 3000)
|
|
105
|
-
* ```
|
|
106
|
-
*
|
|
107
|
-
* @example 本地注入
|
|
108
|
-
* ```ts
|
|
109
|
-
* await Context.load({ 'user-svc': 'http://user.api.com' })
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
|
-
load(source: string | Record<string, string>, timeout?: number): Promise<void>;
|
|
113
|
-
/**
|
|
114
|
-
* 获取指定 ctx key 对应的服务地址
|
|
115
|
-
* @param key ctx key,例如 `'ngw'`、`'osc'`
|
|
116
|
-
* @returns 服务地址,未找到时返回空字符串
|
|
117
|
-
*
|
|
118
|
-
* @example
|
|
119
|
-
* ```ts
|
|
120
|
-
* await Context.load('http://172.16.32.155:20000')
|
|
121
|
-
* Context.get('osc') // 'http://172.16.32.155:20000/osc'
|
|
122
|
-
* Context.get('none') // ''
|
|
123
|
-
* ```
|
|
124
|
-
*/
|
|
125
|
-
get(key: string): string;
|
|
126
|
-
/**
|
|
127
|
-
* 网关元数据(仅远程加载后可用)
|
|
128
|
-
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```ts
|
|
131
|
-
* await Context.load('http://172.16.32.155:20000')
|
|
132
|
-
* Context.info?.version // '4.6.2-r10'
|
|
133
|
-
* Context.info?.domain // 'http://172.16.32.155:20000'
|
|
134
|
-
* ```
|
|
135
|
-
*/
|
|
136
|
-
get info(): ContextInfo | undefined;
|
|
137
|
-
/**
|
|
138
|
-
* Context 是否已加载
|
|
139
|
-
*
|
|
140
|
-
* @example
|
|
141
|
-
* ```ts
|
|
142
|
-
* Context.loaded // false
|
|
143
|
-
* await Context.load({ 'user-svc': 'http://user.api.com' })
|
|
144
|
-
* Context.loaded // true
|
|
145
|
-
* ```
|
|
146
|
-
*/
|
|
147
|
-
get loaded(): boolean;
|
|
148
|
-
/**
|
|
149
|
-
* 清空所有映射并重置加载状态
|
|
150
|
-
*
|
|
151
|
-
* @example
|
|
152
|
-
* ```ts
|
|
153
|
-
* Context.clear()
|
|
154
|
-
* Context.loaded // false
|
|
155
|
-
* ```
|
|
156
|
-
*/
|
|
157
|
-
clear(): void;
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* HttpContext 全局单例
|
|
161
|
-
*
|
|
162
|
-
* @example
|
|
163
|
-
* ```ts
|
|
164
|
-
* import { Context } from '@snack-kit/lib/http'
|
|
165
|
-
*
|
|
166
|
-
* await Context.load('http://172.16.32.155:20000')
|
|
167
|
-
* Context.get('osc') // 'http://172.16.32.155:20000/osc'
|
|
168
|
-
* ```
|
|
169
|
-
*/
|
|
170
|
-
declare const Context: HttpContext;
|
|
171
|
-
/**
|
|
172
|
-
* 当前页面的 origin(`protocol + host`)
|
|
173
|
-
*
|
|
174
|
-
* 在非浏览器环境(如 SSR/Node)返回空字符串。
|
|
175
|
-
*
|
|
176
|
-
* @example
|
|
177
|
-
* ```ts
|
|
178
|
-
* import { Origin } from '@snack-kit/lib/http'
|
|
179
|
-
* // 浏览器中:'https://app.example.com'
|
|
180
|
-
* // Node 环境:''
|
|
181
|
-
* ```
|
|
182
|
-
*/
|
|
183
|
-
declare const Origin: string;
|
|
184
|
-
/**
|
|
185
|
-
* 当前页面路径的首段,可直接用作 `ctx` 参数
|
|
186
|
-
*
|
|
187
|
-
* 例如 `/osc/employee` → `'osc'`,在非浏览器环境返回空字符串。
|
|
188
|
-
*
|
|
189
|
-
* @example
|
|
190
|
-
* ```ts
|
|
191
|
-
* import { Ctx, Get } from '@snack-kit/lib/http'
|
|
192
|
-
*
|
|
193
|
-
* // 当前路径为 /osc/... 时,Ctx === 'osc'
|
|
194
|
-
* const result = await Get('/employee', { ctx: Ctx })
|
|
195
|
-
* ```
|
|
196
|
-
*/
|
|
197
|
-
declare const Ctx: string;
|
|
1
|
+
import { d as HttpMethodConfig, f as HttpResult, e as HttpRequestConfig } from './context-C4dFUDbw.js';
|
|
2
|
+
export { C as Context, a as ContextInfo, b as Ctx, H as HttpContext, c as HttpError, O as Origin, S as ServerItem } from './context-C4dFUDbw.js';
|
|
3
|
+
import 'axios';
|
|
198
4
|
|
|
199
5
|
/**
|
|
200
6
|
* 取消指定 id 的请求并从注册表移除
|
|
@@ -264,92 +70,111 @@ declare function Request<T>(config: HttpRequestConfig): Promise<HttpResult<T>>;
|
|
|
264
70
|
/**
|
|
265
71
|
* 发起 GET 请求
|
|
266
72
|
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
73
|
+
* 支持两种调用方式:
|
|
74
|
+
* - `Get(url, config?)` — 位置参数形式
|
|
75
|
+
* - `Get(config)` — 纯配置对象形式(`method` 已预置,无需传入)
|
|
269
76
|
*
|
|
270
|
-
* @example
|
|
77
|
+
* @example 位置参数
|
|
271
78
|
* ```ts
|
|
272
|
-
* import { Get } from '@snack-kit/lib/http'
|
|
273
|
-
*
|
|
274
79
|
* const { data, error } = await Get<{ name: string }>('/api/user/1')
|
|
275
|
-
* if (!error) console.log(data?.name)
|
|
276
80
|
* ```
|
|
277
81
|
*
|
|
278
|
-
* @example
|
|
82
|
+
* @example 配置对象
|
|
279
83
|
* ```ts
|
|
280
|
-
* const { data } = await Get('/
|
|
84
|
+
* const { data } = await Get({ url: '/api/user/1', ctx: 'user-svc' })
|
|
281
85
|
* ```
|
|
282
86
|
*
|
|
283
87
|
* @example 获取 cancelId 以便主动取消
|
|
284
88
|
* ```ts
|
|
285
89
|
* let cancelId = ''
|
|
286
90
|
* Get('/api/list', { onCancelId: (id) => { cancelId = id } })
|
|
287
|
-
* Cancel(cancelId)
|
|
91
|
+
* Cancel(cancelId)
|
|
288
92
|
* ```
|
|
289
93
|
*/
|
|
290
|
-
declare function Get<T>(url: string, config?:
|
|
94
|
+
declare function Get<T>(url: string, config?: HttpMethodConfig): Promise<HttpResult<T>>;
|
|
95
|
+
declare function Get<T>(config: HttpMethodConfig): Promise<HttpResult<T>>;
|
|
291
96
|
/**
|
|
292
97
|
* 发起 POST 请求
|
|
293
98
|
*
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
99
|
+
* 支持两种调用方式:
|
|
100
|
+
* - `Post(url, data?, config?)` — 位置参数形式
|
|
101
|
+
* - `Post(config)` — 纯配置对象形式(`data` 通过 `config.data` 传入,`method` 已预置)
|
|
297
102
|
*
|
|
298
|
-
* @example
|
|
103
|
+
* @example 位置参数
|
|
299
104
|
* ```ts
|
|
300
|
-
* import { Post } from '@snack-kit/lib/http'
|
|
301
|
-
*
|
|
302
105
|
* const { data, error } = await Post<{ token: string }>('/api/login', {
|
|
303
106
|
* username: 'admin',
|
|
304
107
|
* password: '123456',
|
|
305
108
|
* })
|
|
306
|
-
*
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @example 配置对象
|
|
112
|
+
* ```ts
|
|
113
|
+
* const { data } = await Post({
|
|
114
|
+
* url: '/api/login',
|
|
115
|
+
* data: { username: 'admin', password: '123456' },
|
|
116
|
+
* ctx: 'user-svc',
|
|
117
|
+
* })
|
|
307
118
|
* ```
|
|
308
119
|
*/
|
|
309
|
-
declare function Post<T>(url: string, data?: unknown, config?:
|
|
120
|
+
declare function Post<T>(url: string, data?: unknown, config?: HttpMethodConfig): Promise<HttpResult<T>>;
|
|
121
|
+
declare function Post<T>(config: HttpMethodConfig): Promise<HttpResult<T>>;
|
|
310
122
|
/**
|
|
311
123
|
* 发起 PUT 请求
|
|
312
124
|
*
|
|
313
|
-
*
|
|
314
|
-
*
|
|
315
|
-
*
|
|
125
|
+
* 支持两种调用方式:
|
|
126
|
+
* - `Put(url, data?, config?)` — 位置参数形式
|
|
127
|
+
* - `Put(config)` — 纯配置对象形式(`data` 通过 `config.data` 传入,`method` 已预置)
|
|
316
128
|
*
|
|
317
|
-
* @example
|
|
129
|
+
* @example 位置参数
|
|
318
130
|
* ```ts
|
|
319
|
-
* import { Put } from '@snack-kit/lib/http'
|
|
320
|
-
*
|
|
321
131
|
* await Put('/api/user/1', { name: 'Alice' })
|
|
322
132
|
* ```
|
|
133
|
+
*
|
|
134
|
+
* @example 配置对象
|
|
135
|
+
* ```ts
|
|
136
|
+
* await Put({ url: '/api/user/1', data: { name: 'Alice' }, ctx: 'user-svc' })
|
|
137
|
+
* ```
|
|
323
138
|
*/
|
|
324
|
-
declare function Put<T>(url: string, data?: unknown, config?:
|
|
139
|
+
declare function Put<T>(url: string, data?: unknown, config?: HttpMethodConfig): Promise<HttpResult<T>>;
|
|
140
|
+
declare function Put<T>(config: HttpMethodConfig): Promise<HttpResult<T>>;
|
|
325
141
|
/**
|
|
326
142
|
* 发起 PATCH 请求
|
|
327
143
|
*
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
*
|
|
144
|
+
* 支持两种调用方式:
|
|
145
|
+
* - `Patch(url, data?, config?)` — 位置参数形式
|
|
146
|
+
* - `Patch(config)` — 纯配置对象形式(`data` 通过 `config.data` 传入,`method` 已预置)
|
|
331
147
|
*
|
|
332
|
-
* @example
|
|
148
|
+
* @example 位置参数
|
|
333
149
|
* ```ts
|
|
334
|
-
* import { Patch } from '@snack-kit/lib/http'
|
|
335
|
-
*
|
|
336
150
|
* await Patch('/api/user/1', { avatar: 'https://...' })
|
|
337
151
|
* ```
|
|
152
|
+
*
|
|
153
|
+
* @example 配置对象
|
|
154
|
+
* ```ts
|
|
155
|
+
* await Patch({ url: '/api/user/1', data: { avatar: 'https://...' }, ctx: 'user-svc' })
|
|
156
|
+
* ```
|
|
338
157
|
*/
|
|
339
|
-
declare function Patch<T>(url: string, data?: unknown, config?:
|
|
158
|
+
declare function Patch<T>(url: string, data?: unknown, config?: HttpMethodConfig): Promise<HttpResult<T>>;
|
|
159
|
+
declare function Patch<T>(config: HttpMethodConfig): Promise<HttpResult<T>>;
|
|
340
160
|
/**
|
|
341
161
|
* 发起 DELETE 请求
|
|
342
162
|
*
|
|
343
|
-
*
|
|
344
|
-
*
|
|
163
|
+
* 支持两种调用方式:
|
|
164
|
+
* - `Del(url, config?)` — 位置参数形式
|
|
165
|
+
* - `Del(config)` — 纯配置对象形式(`method` 已预置)
|
|
345
166
|
*
|
|
346
|
-
* @example
|
|
167
|
+
* @example 位置参数
|
|
347
168
|
* ```ts
|
|
348
|
-
* import { Del } from '@snack-kit/lib/http'
|
|
349
|
-
*
|
|
350
169
|
* await Del('/api/user/1')
|
|
351
170
|
* ```
|
|
171
|
+
*
|
|
172
|
+
* @example 配置对象
|
|
173
|
+
* ```ts
|
|
174
|
+
* await Del({ url: '/api/user/1', ctx: 'user-svc' })
|
|
175
|
+
* ```
|
|
352
176
|
*/
|
|
353
|
-
declare function Del<T>(url: string, config?:
|
|
177
|
+
declare function Del<T>(url: string, config?: HttpMethodConfig): Promise<HttpResult<T>>;
|
|
178
|
+
declare function Del<T>(config: HttpMethodConfig): Promise<HttpResult<T>>;
|
|
354
179
|
|
|
355
|
-
export { Cancel, CancelAll,
|
|
180
|
+
export { Cancel, CancelAll, Del, Get, HttpMethodConfig, HttpRequestConfig, HttpResult, Patch, Post, Put, Request };
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { C as Context, a as ContextInfo, b as Ctx, D as DEBUGGER_ACTIVE_KEY, H as HttpContext, c as HttpError, d as HttpMethodConfig, e as HttpRequestConfig, f as HttpResult, O as Origin, S as ServerItem } from './context-C4dFUDbw.js';
|
|
2
|
+
export { Cancel, CancelAll, Del, Get, Patch, Post, Put, Request } from './http.js';
|
|
2
3
|
export { Debugger, DebuggerOptions } from './debugger.js';
|
|
3
4
|
export { CleanObject, Debounce, DeepClone, Delay, FormatDate, GetDateOffset, GetDayRange, GetURLParam, GetURLParams, IsArray, IsBoolean, IsEmail, IsEqual, IsFunction, IsInteger, IsIpv4, IsNull, IsNumber, IsObject, IsPhone, IsPositiveInteger, IsRealNumber, IsString, IsUrl, Minus, ObjectToQuery, Omit, Pick, QueryToObject, REGEX, Throttle, UUID, Unique, UniqueByKey } from './utils.js';
|
|
4
5
|
import 'axios';
|
|
5
6
|
|
|
6
|
-
var version = "0.
|
|
7
|
+
var version = "0.4.0";
|
|
7
8
|
|
|
8
9
|
export { version as VERSION };
|
|
@@ -15373,6 +15373,7 @@ var SnackKit = (function (exports, crypto2, url, http, https, http2, util2, zlib
|
|
|
15373
15373
|
} = axios_default;
|
|
15374
15374
|
|
|
15375
15375
|
// src/http/context.ts
|
|
15376
|
+
var DEBUGGER_ACTIVE_KEY = "__snackkit_debugger__active";
|
|
15376
15377
|
var HttpContext = class {
|
|
15377
15378
|
constructor() {
|
|
15378
15379
|
this._store = /* @__PURE__ */ new Map();
|
|
@@ -15381,12 +15382,24 @@ var SnackKit = (function (exports, crypto2, url, http, https, http2, util2, zlib
|
|
|
15381
15382
|
/**
|
|
15382
15383
|
* 加载服务地址映射
|
|
15383
15384
|
*
|
|
15385
|
+
* 支持三种调用方式:
|
|
15386
|
+
* - **传入字符串**:作为网关地址,自动请求 `GET /ngw/context` 获取映射表
|
|
15387
|
+
* - **传入对象**:直接注入键值映射,不发起网络请求
|
|
15388
|
+
* - **不传参数**:
|
|
15389
|
+
* - 生产环境(`NODE_ENV === 'production'`):以当前页面 `Origin` 为网关地址自动加载
|
|
15390
|
+
* - 开发环境:立即返回,不做任何操作(应由 `Debugger.init()` 接管加载流程)
|
|
15391
|
+
*
|
|
15384
15392
|
* 远程加载时,响应中的 `$info` 元数据字段会被单独存储,不计入路由映射表。
|
|
15385
15393
|
*
|
|
15386
|
-
* @param source
|
|
15394
|
+
* @param source 网关 URL / 键值对象 / 省略
|
|
15387
15395
|
* @param timeout 远程加载超时(ms),默认 5000
|
|
15388
15396
|
*
|
|
15389
|
-
* @example
|
|
15397
|
+
* @example 不传参——生产环境自动使用当前页面 Origin
|
|
15398
|
+
* ```ts
|
|
15399
|
+
* await Context.load()
|
|
15400
|
+
* ```
|
|
15401
|
+
*
|
|
15402
|
+
* @example 传入网关地址
|
|
15390
15403
|
* ```ts
|
|
15391
15404
|
* await Context.load('http://172.16.32.155:20000', 3000)
|
|
15392
15405
|
* ```
|
|
@@ -15397,6 +15410,11 @@ var SnackKit = (function (exports, crypto2, url, http, https, http2, util2, zlib
|
|
|
15397
15410
|
* ```
|
|
15398
15411
|
*/
|
|
15399
15412
|
async load(source, timeout = 5e3) {
|
|
15413
|
+
if (source === void 0) {
|
|
15414
|
+
if (globalThis[DEBUGGER_ACTIVE_KEY]) return;
|
|
15415
|
+
if (!Origin) return;
|
|
15416
|
+
source = Origin;
|
|
15417
|
+
}
|
|
15400
15418
|
if (typeof source === "string") {
|
|
15401
15419
|
const url2 = `${source}/ngw/context`;
|
|
15402
15420
|
const res = await axios_default.get(url2, { timeout });
|
|
@@ -15478,7 +15496,7 @@ var SnackKit = (function (exports, crypto2, url, http, https, http2, util2, zlib
|
|
|
15478
15496
|
var _instance = new HttpContext();
|
|
15479
15497
|
var Context = _instance;
|
|
15480
15498
|
var _loc = typeof globalThis !== "undefined" ? globalThis["location"] : void 0;
|
|
15481
|
-
_loc?.protocol && _loc?.host ? `${_loc.protocol}//${_loc.host}` : "";
|
|
15499
|
+
var Origin = _loc?.protocol && _loc?.host ? `${_loc.protocol}//${_loc.host}` : "";
|
|
15482
15500
|
_loc?.pathname ? _loc.pathname.split("/")[1] ?? "" : "";
|
|
15483
15501
|
|
|
15484
15502
|
// src/http/cancel.ts
|
|
@@ -15537,8 +15555,11 @@ var SnackKit = (function (exports, crypto2, url, http, https, http2, util2, zlib
|
|
|
15537
15555
|
Unregister(cancelId);
|
|
15538
15556
|
}
|
|
15539
15557
|
}
|
|
15540
|
-
function Get(
|
|
15541
|
-
|
|
15558
|
+
function Get(urlOrConfig, config) {
|
|
15559
|
+
if (typeof urlOrConfig === "string") {
|
|
15560
|
+
return Request({ ...config, method: "GET", url: urlOrConfig });
|
|
15561
|
+
}
|
|
15562
|
+
return Request({ ...urlOrConfig, method: "GET" });
|
|
15542
15563
|
}
|
|
15543
15564
|
|
|
15544
15565
|
// src/debugger/debugger.ts
|
|
@@ -16111,6 +16132,7 @@ var SnackKit = (function (exports, crypto2, url, http, https, http2, util2, zlib
|
|
|
16111
16132
|
* ```
|
|
16112
16133
|
*/
|
|
16113
16134
|
static async init(options) {
|
|
16135
|
+
globalThis[DEBUGGER_ACTIVE_KEY] = true;
|
|
16114
16136
|
const instance = new _Debugger({ timeout: 1e4, ...options });
|
|
16115
16137
|
instance.injectStyle();
|
|
16116
16138
|
instance.buildDOM();
|
|
@@ -16420,6 +16442,7 @@ var SnackKit = (function (exports, crypto2, url, http, https, http2, util2, zlib
|
|
|
16420
16442
|
*)
|
|
16421
16443
|
*/
|
|
16422
16444
|
|
|
16445
|
+
exports.DEBUGGER_ACTIVE_KEY = DEBUGGER_ACTIVE_KEY;
|
|
16423
16446
|
exports.Debugger = Debugger;
|
|
16424
16447
|
|
|
16425
16448
|
return exports;
|