@snack-kit/lib 0.3.0 → 0.5.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.
Files changed (38) hide show
  1. package/README.md +261 -17
  2. package/dist/cjs/chunk-IMHC6CTK.cjs +13 -0
  3. package/dist/{es/chunk-BEL6AFK4.js.map → cjs/chunk-IMHC6CTK.cjs.map} +1 -1
  4. package/dist/cjs/{chunk-N7BJS6LI.cjs → chunk-XEQEQWDB.cjs} +48 -14
  5. package/dist/cjs/chunk-XEQEQWDB.cjs.map +1 -0
  6. package/dist/cjs/{chunk-YOWLTZM5.cjs → chunk-ZJMTV2GJ.cjs} +7 -6
  7. package/dist/cjs/chunk-ZJMTV2GJ.cjs.map +1 -0
  8. package/dist/cjs/debugger.cjs +7 -3
  9. package/dist/cjs/http.cjs +22 -14
  10. package/dist/cjs/index.cjs +29 -17
  11. package/dist/cjs/index.cjs.map +1 -1
  12. package/dist/es/{chunk-4DLFIN3C.js → chunk-4SGFAIRT.js} +4 -3
  13. package/dist/es/chunk-4SGFAIRT.js.map +1 -0
  14. package/dist/es/chunk-MHCY2PIP.js +3 -0
  15. package/dist/{cjs/chunk-YY5WQN7B.cjs.map → es/chunk-MHCY2PIP.js.map} +1 -1
  16. package/dist/es/{chunk-JQYH5FWE.js → chunk-YV6SGXUJ.js} +48 -15
  17. package/dist/es/chunk-YV6SGXUJ.js.map +1 -0
  18. package/dist/es/debugger.js +2 -2
  19. package/dist/es/http.js +2 -2
  20. package/dist/es/index.js +4 -4
  21. package/dist/es/index.js.map +1 -1
  22. package/dist/types/context-C4dFUDbw.d.ts +237 -0
  23. package/dist/types/debugger.d.ts +3 -0
  24. package/dist/types/http.d.ts +61 -236
  25. package/dist/types/index.d.ts +4 -3
  26. package/dist/umd/debugger.global.js +28 -5
  27. package/dist/umd/debugger.global.js.map +1 -1
  28. package/dist/umd/http.global.js +46 -12
  29. package/dist/umd/http.global.js.map +1 -1
  30. package/dist/umd/index.global.js +49 -13
  31. package/dist/umd/index.global.js.map +1 -1
  32. package/package.json +1 -1
  33. package/dist/cjs/chunk-N7BJS6LI.cjs.map +0 -1
  34. package/dist/cjs/chunk-YOWLTZM5.cjs.map +0 -1
  35. package/dist/cjs/chunk-YY5WQN7B.cjs +0 -4
  36. package/dist/es/chunk-4DLFIN3C.js.map +0 -1
  37. package/dist/es/chunk-BEL6AFK4.js +0 -3
  38. package/dist/es/chunk-JQYH5FWE.js.map +0 -1
@@ -1,200 +1,6 @@
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
- * 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
+ export { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, default as axios } from '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
- * @param url 请求地址
268
- * @param config 请求配置
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 使用 ctx 自动路由
82
+ * @example 配置对象
279
83
  * ```ts
280
- * const { data } = await Get('/profile', { ctx: 'user-svc' })
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?: HttpRequestConfig): Promise<HttpResult<T>>;
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
- * @param url 请求地址
295
- * @param data 请求体数据
296
- * @param config 请求配置
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
- * if (!error) console.log(data?.token)
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?: HttpRequestConfig): Promise<HttpResult<T>>;
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
- * @param url 请求地址
314
- * @param data 请求体数据
315
- * @param config 请求配置
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?: HttpRequestConfig): Promise<HttpResult<T>>;
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
- * @param url 请求地址
329
- * @param data 请求体数据
330
- * @param config 请求配置
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?: HttpRequestConfig): Promise<HttpResult<T>>;
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
- * @param url 请求地址
344
- * @param config 请求配置
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?: HttpRequestConfig): Promise<HttpResult<T>>;
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, Context, type ContextInfo, Ctx, Del, Get, HttpContext, type HttpError, type HttpRequestConfig, type HttpResult, Origin, Patch, Post, Put, Request, type ServerItem };
180
+ export { Cancel, CancelAll, Del, Get, HttpMethodConfig, HttpRequestConfig, HttpResult, Patch, Post, Put, Request };
@@ -1,8 +1,9 @@
1
- export { Cancel, CancelAll, Context, ContextInfo, Ctx, Del, Get, HttpContext, HttpError, HttpRequestConfig, HttpResult, Origin, Patch, Post, Put, Request, ServerItem } from './http.js';
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';
3
+ export { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, default as axios } from 'axios';
2
4
  export { Debugger, DebuggerOptions } from './debugger.js';
3
5
  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
- import 'axios';
5
6
 
6
- var version = "0.3.0";
7
+ var version = "0.5.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 远程加载传入网关 URL;本地注入传入键值对象
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(url2, config) {
15541
- return Request({ ...config, method: "GET", url: url2 });
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;