@h-ai/kit 0.1.0-alpha.67 → 0.1.0-alpha.69
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.en.md +67 -13
- package/README.ja.md +67 -13
- package/README.md +67 -13
- package/dist/adapter/index.d.ts +17 -2
- package/dist/adapter/index.js.map +1 -1
- package/dist/browser.d.ts +12 -6
- package/dist/browser.js +6 -7
- package/dist/browser.js.map +1 -1
- package/dist/{chunk-DVW7CQ3H.js → chunk-2PWGJCWX.js} +3 -3
- package/dist/chunk-2PWGJCWX.js.map +1 -0
- package/dist/chunk-OVRZIDCD.js +175 -0
- package/dist/chunk-OVRZIDCD.js.map +1 -0
- package/dist/{chunk-VOTDAVCB.js → chunk-QNLZLTVC.js} +29 -42
- package/dist/chunk-QNLZLTVC.js.map +1 -0
- package/dist/{chunk-MLSP67JF.js → chunk-UTCS6FR7.js} +67 -110
- package/dist/chunk-UTCS6FR7.js.map +1 -0
- package/dist/{chunk-NVYIUBU7.js → chunk-XC7I2L3G.js} +2 -2
- package/dist/chunk-XC7I2L3G.js.map +1 -0
- package/dist/{chunk-L7BTMAKX.js → chunk-Y2YUA3VR.js} +5 -6
- package/dist/chunk-Y2YUA3VR.js.map +1 -0
- package/dist/client/index.d.ts +47 -1
- package/dist/client/index.js +3 -3
- package/dist/client/index.js.map +1 -1
- package/dist/index.d.ts +251 -347
- package/dist/index.js +347 -171
- package/dist/index.js.map +1 -1
- package/dist/kit-a2a-types-DwcmE668.d.ts +136 -0
- package/dist/{index-B7s-0mZl.d.ts → kit-client-auth-B5y1g5_A.d.ts} +228 -102
- package/dist/kit-crypto-types-Cwj9bprV.d.ts +196 -0
- package/dist/{kit-i18n-D9Qqk-1k.d.ts → kit-i18n-DEzmzE5T.d.ts} +142 -29
- package/dist/{kit-types-ByWdbCeO.d.ts → kit-types-DQYqP1Cn.d.ts} +242 -162
- package/dist/modules/a2a/index.d.ts +13 -111
- package/dist/modules/a2a/index.js +2 -2
- package/dist/modules/crypto/index.d.ts +28 -195
- package/dist/modules/crypto/index.js +3 -3
- package/dist/modules/crypto/index.js.map +1 -1
- package/package.json +16 -11
- package/dist/chunk-DVW7CQ3H.js.map +0 -1
- package/dist/chunk-L7BTMAKX.js.map +0 -1
- package/dist/chunk-MLSP67JF.js.map +0 -1
- package/dist/chunk-NVYIUBU7.js.map +0 -1
- package/dist/chunk-QHYNL24G.js +0 -117
- package/dist/chunk-QHYNL24G.js.map +0 -1
- package/dist/chunk-VOTDAVCB.js.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,22 @@
|
|
|
1
|
-
import { c as createKitClient, a as createKitAuthActions, i as installBrowserTransport, b as installBrowserTransportFetch,
|
|
2
|
-
export { C as ClientTransportConfig, K as KitClient,
|
|
3
|
-
import { d as defineCrud, s as setAllModulesLocale } from './kit-i18n-
|
|
4
|
-
export { C as CrudApiDef, a as CrudFieldDef, b as CrudFieldOption, c as CrudFieldOptions, e as CrudFieldType, f as CrudFieldValidation, g as CrudFilterParams, h as CrudOperations, i as CrudPaginatedResult, j as CrudPaginationParams, k as CrudResourceDef, l as CrudTableColumn, m as CrudText, r as resolveOptions, n as resolveText } from './kit-i18n-
|
|
5
|
-
import { S as SessionData,
|
|
6
|
-
export { A as ApiResponse, a as AuthOperations, b as AuthOperationsProvider, F as FormError, c as FormValidationResult, G as GuardConfig, d as GuardResult, H as HaiRequestEvent, e as HandleA2AConfig, f as HandleA2AOperations, g as HandleAuthConfig, i as HookCryptoConfig,
|
|
7
|
-
import * as _sveltejs_kit from '@sveltejs/kit';
|
|
8
|
-
import { RequestEvent, Cookies, Handle, RequestHandler } from '@sveltejs/kit';
|
|
1
|
+
import { c as createKitClient, a as createKitAuthActions, i as installBrowserTransport, b as installBrowserTransportFetch, o as login, p as loginWithOtp, q as loginWithLdap, t as loginWithApiKey, u as registerAndLogin, v as logout, s as setBrowserToken, d as clearBrowserToken } from './kit-client-auth-B5y1g5_A.js';
|
|
2
|
+
export { C as ClientTransportConfig, K as KitClient, e as KitClientConfig, f as KitConfig, g as KitConfigInput, h as KitConfigSchema, j as KitServerConfig, k as KitServerConfigInput, l as KitTransportConfig, m as KitTransportConfigInput, r as resolveKitConfig, n as resolveKitTransportConfig } from './kit-client-auth-B5y1g5_A.js';
|
|
3
|
+
import { d as defineCrud, s as setAllModulesLocale } from './kit-i18n-DEzmzE5T.js';
|
|
4
|
+
export { C as CrudApiDef, a as CrudFieldDef, b as CrudFieldOption, c as CrudFieldOptions, e as CrudFieldType, f as CrudFieldValidation, g as CrudFilterParams, h as CrudOperations, i as CrudPaginatedResult, j as CrudPaginationParams, k as CrudResourceDef, l as CrudTableColumn, m as CrudText, r as resolveOptions, n as resolveText } from './kit-i18n-DEzmzE5T.js';
|
|
5
|
+
import { S as SessionData, l as SessionLike, h as HandleConfig, r as rateLimitMiddleware, m as createCacheRateLimitStore, n as createMemoryRateLimitStore } from './kit-types-DQYqP1Cn.js';
|
|
6
|
+
export { A as ApiResponse, a as AuthOperations, b as AuthOperationsProvider, C as CorsConfig, F as FormError, c as FormValidationResult, G as GuardConfig, d as GuardResult, H as HaiRequestEvent, e as HandleA2AConfig, f as HandleA2AOperations, g as HandleAuthConfig, i as HookCryptoConfig, o as MemoryRateLimitStore, M as Middleware, j as MiddlewareContext, p as RateLimitCache, R as RateLimitConfig, q as RateLimitEntry, s as RateLimitStore, k as RouteGuard, T as TransportEncryptionOptions } from './kit-types-DQYqP1Cn.js';
|
|
9
7
|
import { CryptoFunctions } from '@h-ai/crypto';
|
|
10
8
|
export { EncryptedPayload, TransportEncryptionManager, TransportKeyPair } from '@h-ai/crypto';
|
|
9
|
+
import { FeedbackFunctions, FeedbackContext } from '@h-ai/feedback';
|
|
10
|
+
import * as _sveltejs_kit from '@sveltejs/kit';
|
|
11
|
+
import { Handle, RequestEvent, RequestHandler } from '@sveltejs/kit';
|
|
12
|
+
import * as _h_ai_core from '@h-ai/core';
|
|
13
|
+
import { HaiError } from '@h-ai/core';
|
|
11
14
|
import * as zod_v4_core from 'zod/v4/core';
|
|
12
15
|
import * as zod from 'zod';
|
|
13
16
|
import { z } from 'zod';
|
|
14
17
|
import { IamFunctions } from '@h-ai/iam';
|
|
15
|
-
|
|
16
|
-
export {
|
|
17
|
-
export { CryptoCsrfConfig, CryptoServiceLike, EncryptedCookieConfig, TransportEncryptionConfig, WebhookVerifyConfig, createCsrfManager, createEncryptedCookie, signRequest, transportEncryptionMiddleware, verifyWebhookSignature } from './modules/crypto/index.js';
|
|
18
|
+
export { A as A2AApiKeyAuthConfig, K as KitA2AHandlerConfig, R as ResolvedA2AConfig } from './kit-a2a-types-DwcmE668.js';
|
|
19
|
+
export { C as CryptoCsrfConfig, a as CryptoServiceLike, E as EncryptedCookieConfig, T as TransportEncryptionConfig, W as WebhookVerifyConfig } from './kit-crypto-types-Cwj9bprV.js';
|
|
18
20
|
import '@h-ai/api-contract';
|
|
19
21
|
|
|
20
22
|
/**
|
|
@@ -26,6 +28,13 @@ import '@h-ai/api-contract';
|
|
|
26
28
|
|
|
27
29
|
/**
|
|
28
30
|
* 认证守卫配置
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import type { AuthGuardConfig } from '@h-ai/kit'
|
|
34
|
+
*
|
|
35
|
+
* const value: AuthGuardConfig = { apiMode: true }
|
|
36
|
+
* void value
|
|
37
|
+
* ```
|
|
29
38
|
*/
|
|
30
39
|
interface AuthGuardConfig {
|
|
31
40
|
/** 未登录时重定向 URL(默认 `'/login'`) */
|
|
@@ -35,6 +44,13 @@ interface AuthGuardConfig {
|
|
|
35
44
|
}
|
|
36
45
|
/**
|
|
37
46
|
* 会话守卫配置
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* import type { SessionGuardConfig } from '@h-ai/kit'
|
|
50
|
+
*
|
|
51
|
+
* const value: SessionGuardConfig = { validateSession: async () => null }
|
|
52
|
+
* void value
|
|
53
|
+
* ```
|
|
38
54
|
*/
|
|
39
55
|
interface SessionGuardConfig {
|
|
40
56
|
/** 会话校验函数(通常由应用注入 iam.auth.verifyToken 封装) */
|
|
@@ -44,122 +60,6 @@ interface SessionGuardConfig {
|
|
|
44
60
|
/** 为 `true` 时返回 JSON 401 而非重定向(适用于 API 路由) */
|
|
45
61
|
apiMode?: boolean;
|
|
46
62
|
}
|
|
47
|
-
/**
|
|
48
|
-
* 创建认证守卫
|
|
49
|
-
*
|
|
50
|
-
* 检查会话是否存在;未认证时:
|
|
51
|
-
* - 页面模式:重定向到 `loginUrl`,并携带 `returnUrl` 参数以便登录后返回。
|
|
52
|
-
* - API 模式:返回 JSON `{ allowed: false, status: 401 }`。
|
|
53
|
-
*
|
|
54
|
-
* @param config - 守卫配置
|
|
55
|
-
* @returns RouteGuard 实例
|
|
56
|
-
*
|
|
57
|
-
* @example
|
|
58
|
-
* ```ts
|
|
59
|
-
* // Hook 配置
|
|
60
|
-
* guards: [
|
|
61
|
-
* { guard: kit.guard.auth({ apiMode: true }), paths: ['/api/*'] },
|
|
62
|
-
* { guard: kit.guard.auth(), paths: ['/dashboard/*'] },
|
|
63
|
-
* ]
|
|
64
|
-
* ```
|
|
65
|
-
*/
|
|
66
|
-
declare function authGuard(config?: AuthGuardConfig): RouteGuard;
|
|
67
|
-
/**
|
|
68
|
-
* 创建会话守卫(支持 Bearer + 固定 Access Token Cookie 自动恢复)。
|
|
69
|
-
*
|
|
70
|
-
* 处理流程:
|
|
71
|
-
* 1. 若上游已注入 session,直接放行
|
|
72
|
-
* 2. 否则从 request/cookies 解析 token(Bearer 优先)
|
|
73
|
-
* 3. 调用 validateSession 恢复并写入 event.locals.session
|
|
74
|
-
* 4. 失败则按页面/API模式返回重定向或 401
|
|
75
|
-
*/
|
|
76
|
-
declare function sessionGuard(config: SessionGuardConfig): RouteGuard;
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* @h-ai/kit — 组合守卫
|
|
80
|
-
*
|
|
81
|
-
* 提供守卫组合器,支持 AND(allGuards)/ OR(anyGuard)/ NOT(notGuard)/ 条件分支(conditionalGuard)等逻辑组合,将多个 RouteGuard 聚合为一个。
|
|
82
|
-
* @module kit-compose
|
|
83
|
-
*/
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* 所有守卫都通过才允许访问(AND 逻辑)
|
|
87
|
-
*
|
|
88
|
-
* 短路求值:第一个拒绝的守卫结果会被直接返回。
|
|
89
|
-
*
|
|
90
|
-
* @param guards - 需要全部通过的守卫列表
|
|
91
|
-
* @returns 组合后的 RouteGuard
|
|
92
|
-
*
|
|
93
|
-
* @example
|
|
94
|
-
* ```ts
|
|
95
|
-
* const adminOnly = kit.guard.all(
|
|
96
|
-
* kit.guard.auth(),
|
|
97
|
-
* kit.guard.role({ roles: ['admin'] }),
|
|
98
|
-
* )
|
|
99
|
-
* ```
|
|
100
|
-
*/
|
|
101
|
-
declare function allGuards(...guards: RouteGuard[]): RouteGuard;
|
|
102
|
-
/**
|
|
103
|
-
* 任意一个守卫通过就允许访问(OR 逻辑)
|
|
104
|
-
*
|
|
105
|
-
* 短路求值:第一个允许的守卫结果会被直接返回。
|
|
106
|
-
* 全部拒绝时返回最后一个拒绝结果。
|
|
107
|
-
*
|
|
108
|
-
* @param guards - 守卫列表
|
|
109
|
-
* @returns 组合后的 RouteGuard
|
|
110
|
-
*
|
|
111
|
-
* @example
|
|
112
|
-
* ```ts
|
|
113
|
-
* const canView = kit.guard.any(
|
|
114
|
-
* kit.guard.role({ roles: ['admin'] }),
|
|
115
|
-
* kit.guard.permission({ permissions: ['article:read'] }),
|
|
116
|
-
* )
|
|
117
|
-
* ```
|
|
118
|
-
*/
|
|
119
|
-
declare function anyGuard(...guards: RouteGuard[]): RouteGuard;
|
|
120
|
-
/**
|
|
121
|
-
* 取反守卫
|
|
122
|
-
*
|
|
123
|
-
* 将原守卫的判定逻辑反转:原本放行变为拒绝,原本拒绝变为放行。
|
|
124
|
-
* 典型场景:「仅未登录用户可访问登录页」。
|
|
125
|
-
*
|
|
126
|
-
* @param guard - 需要取反的守卫
|
|
127
|
-
* @param options - 拒绝时的重定向/消息
|
|
128
|
-
* @param options.redirect - 拒绝时重定向 URL
|
|
129
|
-
* @param options.message - 拒绝时的提示消息
|
|
130
|
-
* @returns 取反后的 RouteGuard
|
|
131
|
-
*
|
|
132
|
-
* @example
|
|
133
|
-
* ```ts
|
|
134
|
-
* // 仅未登录用户可访问
|
|
135
|
-
* const guestOnly = kit.guard.not(kit.guard.auth(), { redirect: '/dashboard' })
|
|
136
|
-
* ```
|
|
137
|
-
*/
|
|
138
|
-
declare function notGuard(guard: RouteGuard, options?: {
|
|
139
|
-
redirect?: string;
|
|
140
|
-
message?: string;
|
|
141
|
-
}): RouteGuard;
|
|
142
|
-
/** 条件判断函数类型:返回 `true` 时才执行后续守卫 */
|
|
143
|
-
type ConditionFn = (event: RequestEvent, session: SessionData | undefined) => boolean | Promise<boolean>;
|
|
144
|
-
/**
|
|
145
|
-
* 条件守卫
|
|
146
|
-
*
|
|
147
|
-
* 仅当 `condition` 返回 `true` 时才执行 `guard`;否则直接放行。
|
|
148
|
-
*
|
|
149
|
-
* @param condition - 条件判断函数
|
|
150
|
-
* @param guard - 条件满足时执行的守卫
|
|
151
|
-
* @returns 包装后的 RouteGuard
|
|
152
|
-
*
|
|
153
|
-
* @example
|
|
154
|
-
* ```ts
|
|
155
|
-
* // 仅 POST 请求检查 CSRF
|
|
156
|
-
* kit.guard.conditional(
|
|
157
|
-
* (event) => event.request.method === 'POST',
|
|
158
|
-
* csrfGuard,
|
|
159
|
-
* )
|
|
160
|
-
* ```
|
|
161
|
-
*/
|
|
162
|
-
declare function conditionalGuard(condition: ConditionFn, guard: RouteGuard): RouteGuard;
|
|
163
63
|
|
|
164
64
|
/**
|
|
165
65
|
* @h-ai/kit — 权限守卫
|
|
@@ -170,6 +70,13 @@ declare function conditionalGuard(condition: ConditionFn, guard: RouteGuard): Ro
|
|
|
170
70
|
|
|
171
71
|
/**
|
|
172
72
|
* 权限守卫配置
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* import type { PermissionGuardConfig } from '@h-ai/kit'
|
|
76
|
+
*
|
|
77
|
+
* const value: PermissionGuardConfig = { permissions: ['article:read'] }
|
|
78
|
+
* void value
|
|
79
|
+
* ```
|
|
173
80
|
*/
|
|
174
81
|
interface PermissionGuardConfig {
|
|
175
82
|
/** 需要的权限列表(默认 OR 逻辑,满足任一即通过) */
|
|
@@ -181,24 +88,6 @@ interface PermissionGuardConfig {
|
|
|
181
88
|
/** 为 `true` 时返回 JSON 403 而非重定向 */
|
|
182
89
|
apiMode?: boolean;
|
|
183
90
|
}
|
|
184
|
-
/**
|
|
185
|
-
* 创建权限守卫
|
|
186
|
-
*
|
|
187
|
-
* 检查用户 `session.permissions` 是否满足配置中的权限要求。
|
|
188
|
-
* 支持通配符匹配:`admin:*` 可匹配 `admin:read`、`admin:write` 等。
|
|
189
|
-
* 未认证时返回 401;权限不匹配时根据 `apiMode` 返回 JSON 403 或重定向。
|
|
190
|
-
*
|
|
191
|
-
* @param config - 权限守卫配置
|
|
192
|
-
* @returns RouteGuard 实例
|
|
193
|
-
*
|
|
194
|
-
* @example
|
|
195
|
-
* ```ts
|
|
196
|
-
* guards: [
|
|
197
|
-
* { guard: kit.guard.permission({ permissions: ['user:read', 'user:write'], requireAll: true }), paths: ['/api/users/*'] },
|
|
198
|
-
* ]
|
|
199
|
-
* ```
|
|
200
|
-
*/
|
|
201
|
-
declare function permissionGuard(config: PermissionGuardConfig): RouteGuard;
|
|
202
91
|
/**
|
|
203
92
|
* 匹配权限
|
|
204
93
|
*
|
|
@@ -209,6 +98,13 @@ declare function permissionGuard(config: PermissionGuardConfig): RouteGuard;
|
|
|
209
98
|
* @param required - 需要的权限码
|
|
210
99
|
* @param userPermissions - 用户已有的权限列表
|
|
211
100
|
* @returns 是否匹配
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* import { matchPermission } from '@h-ai/kit'
|
|
104
|
+
*
|
|
105
|
+
* const allowed = matchPermission('article:read', ['article:*'])
|
|
106
|
+
* void allowed
|
|
107
|
+
* ```
|
|
212
108
|
*/
|
|
213
109
|
declare function matchPermission(required: string, userPermissions: string[]): boolean;
|
|
214
110
|
/**
|
|
@@ -219,35 +115,15 @@ declare function matchPermission(required: string, userPermissions: string[]): b
|
|
|
219
115
|
* @param session - 当前会话数据,null/undefined 视为无权限
|
|
220
116
|
* @param permission - 需要的权限码,如 `user:read`
|
|
221
117
|
* @returns 是否拥有权限
|
|
222
|
-
*
|
|
223
118
|
* @example
|
|
224
119
|
* ```ts
|
|
225
|
-
*
|
|
226
|
-
* // 有权限
|
|
227
|
-
* }
|
|
228
|
-
* ```
|
|
229
|
-
*/
|
|
230
|
-
declare function hasPermission(session: SessionLike | null | undefined, permission: string): boolean;
|
|
231
|
-
/**
|
|
232
|
-
* 断言会话具有指定权限,不满足时返回 403 Response
|
|
120
|
+
* import { kit } from '@h-ai/kit'
|
|
233
121
|
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
* @param session - 当前会话数据,null/undefined 视为未认证
|
|
238
|
-
* @param permission - 需要的权限码,如 `user:create`
|
|
239
|
-
* @returns 权限不足时返回 Response;有权限时返回 undefined
|
|
240
|
-
*
|
|
241
|
-
* @example
|
|
242
|
-
* ```ts
|
|
243
|
-
* export const POST: RequestHandler = async ({ locals }) => {
|
|
244
|
-
* const denied = kit.guard.assertPermission(locals.session, 'user:create')
|
|
245
|
-
* if (denied) return denied
|
|
246
|
-
* // ... 正常逻辑
|
|
247
|
-
* }
|
|
122
|
+
* const allowed = kit.guard.check({ userId: 'user_1', roles: ['editor'], permissions: ['article:read'] }, 'article:read')
|
|
123
|
+
* void allowed
|
|
248
124
|
* ```
|
|
249
125
|
*/
|
|
250
|
-
declare function
|
|
126
|
+
declare function hasPermission(session: SessionLike | null | undefined, permission: string): boolean;
|
|
251
127
|
/**
|
|
252
128
|
* 要求会话具有指定权限,不满足时 throw Response
|
|
253
129
|
*
|
|
@@ -257,47 +133,14 @@ declare function assertPermission(session: SessionLike | null | undefined, permi
|
|
|
257
133
|
* @param session - 当前会话数据,null/undefined 视为未认证
|
|
258
134
|
* @param permission - 需要的权限码,如 `user:create`
|
|
259
135
|
* @throws Response - 未认证 401 / 无权限 403
|
|
260
|
-
*
|
|
261
136
|
* @example
|
|
262
137
|
* ```ts
|
|
263
|
-
*
|
|
264
|
-
* kit.guard.require(locals.session, 'user:read')
|
|
265
|
-
* // 执行到这里说明权限已通过
|
|
266
|
-
* return kit.response.ok(data)
|
|
267
|
-
* })
|
|
268
|
-
* ```
|
|
269
|
-
*/
|
|
270
|
-
declare function requirePermission(session: SessionLike | null | undefined, permission: string): void;
|
|
271
|
-
/**
|
|
272
|
-
* 权限守卫高阶函数包装器
|
|
138
|
+
* import { kit } from '@h-ai/kit'
|
|
273
139
|
*
|
|
274
|
-
*
|
|
275
|
-
* 未认证返回 401、无权限返回 403(均通过 SvelteKit throw 控制流抛出)。
|
|
276
|
-
*
|
|
277
|
-
* 适用于 page load、API handler 等所有 SvelteKit server 函数。
|
|
278
|
-
*
|
|
279
|
-
* @param permission - 需要的权限码,如 `'user:read'`
|
|
280
|
-
* @param handler - 原始处理函数
|
|
281
|
-
* @returns 包装后的处理函数(签名不变)
|
|
282
|
-
*
|
|
283
|
-
* @example
|
|
284
|
-
* ```ts
|
|
285
|
-
* // +page.server.ts
|
|
286
|
-
* export const load = kit.guard.withPermission('user:read', async ({ locals }) => {
|
|
287
|
-
* const result = await iam.user.listUsers()
|
|
288
|
-
* return { users: result.success ? result.data.items : [] }
|
|
289
|
-
* })
|
|
290
|
-
*
|
|
291
|
-
* // +server.ts (API handler)
|
|
292
|
-
* export const GET = kit.handler(kit.guard.withPermission('user:list', async ({ locals }) => {
|
|
293
|
-
* const result = await iam.user.listUsers()
|
|
294
|
-
* return kit.response.ok(result.data)
|
|
295
|
-
* }))
|
|
140
|
+
* kit.guard.require({ userId: 'user_1', roles: ['editor'], permissions: ['article:read'] }, 'article:read') // 拒绝时抛出 Response,由框架传递。
|
|
296
141
|
* ```
|
|
297
142
|
*/
|
|
298
|
-
declare function
|
|
299
|
-
locals: Record<string, unknown>;
|
|
300
|
-
}, TReturn>(permission: string, handler: (event: TEvent) => Promise<TReturn>): (event: TEvent) => Promise<TReturn>;
|
|
143
|
+
declare function requirePermission(session: SessionLike | null | undefined, permission: string): void;
|
|
301
144
|
|
|
302
145
|
/**
|
|
303
146
|
* @h-ai/kit — 角色守卫
|
|
@@ -308,6 +151,13 @@ declare function withPermission<TEvent extends {
|
|
|
308
151
|
|
|
309
152
|
/**
|
|
310
153
|
* 角色守卫配置
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* import type { RoleGuardConfig } from '@h-ai/kit'
|
|
157
|
+
*
|
|
158
|
+
* const value: RoleGuardConfig = { roles: ['editor'] }
|
|
159
|
+
* void value
|
|
160
|
+
* ```
|
|
311
161
|
*/
|
|
312
162
|
interface RoleGuardConfig {
|
|
313
163
|
/** 需要的角色列表(默认 OR 逻辑,满足任一即通过) */
|
|
@@ -319,23 +169,6 @@ interface RoleGuardConfig {
|
|
|
319
169
|
/** 为 `true` 时返回 JSON 403 而非重定向 */
|
|
320
170
|
apiMode?: boolean;
|
|
321
171
|
}
|
|
322
|
-
/**
|
|
323
|
-
* 创建角色守卫
|
|
324
|
-
*
|
|
325
|
-
* 检查用户 `session.roles` 是否满足配置中的角色要求。
|
|
326
|
-
* 未认证时返回 401;角色不匹配时根据 `apiMode` 返回 JSON 403 或重定向。
|
|
327
|
-
*
|
|
328
|
-
* @param config - 角色守卫配置
|
|
329
|
-
* @returns RouteGuard 实例
|
|
330
|
-
*
|
|
331
|
-
* @example
|
|
332
|
-
* ```ts
|
|
333
|
-
* guards: [
|
|
334
|
-
* { guard: kit.guard.role({ roles: ['admin'], apiMode: true }), paths: ['/api/admin/*'] },
|
|
335
|
-
* ]
|
|
336
|
-
* ```
|
|
337
|
-
*/
|
|
338
|
-
declare function roleGuard(config: RoleGuardConfig): RouteGuard;
|
|
339
172
|
|
|
340
173
|
/**
|
|
341
174
|
* @h-ai/kit — Cookie 加密代理
|
|
@@ -346,6 +179,15 @@ declare function roleGuard(config: RoleGuardConfig): RouteGuard;
|
|
|
346
179
|
|
|
347
180
|
/**
|
|
348
181
|
* Cookie 加密代理配置
|
|
182
|
+
* @example
|
|
183
|
+
* ```ts
|
|
184
|
+
* import type { CookieProxyConfig } from '@h-ai/kit'
|
|
185
|
+
* import { crypto } from '@h-ai/crypto'
|
|
186
|
+
*
|
|
187
|
+
* function config(encryptionKey: string): CookieProxyConfig {
|
|
188
|
+
* return { names: new Set(['session']), symmetric: crypto.symmetric, encryptionKey }
|
|
189
|
+
* }
|
|
190
|
+
* ```
|
|
349
191
|
*/
|
|
350
192
|
interface CookieProxyConfig {
|
|
351
193
|
/** 需要加密的 Cookie 名称集合 */
|
|
@@ -355,20 +197,6 @@ interface CookieProxyConfig {
|
|
|
355
197
|
/** 加密密钥(32 字符十六进制) */
|
|
356
198
|
encryptionKey: string;
|
|
357
199
|
}
|
|
358
|
-
/**
|
|
359
|
-
* 创建加密 Cookie 代理
|
|
360
|
-
*
|
|
361
|
-
* 返回一个 Proxy 包装的 Cookies 对象:
|
|
362
|
-
* - `get(name)` —— 若 name 在加密列表中,自动解密后返回明文;格式或解密失败时返回 `undefined`
|
|
363
|
-
* - `set(name, value, opts)` —— 若 name 在加密列表中,自动加密后存储;加密失败时拒绝写入
|
|
364
|
-
* - `delete(name, opts)` —— 透传,无需解密
|
|
365
|
-
* - 其他方法原样透传
|
|
366
|
-
*
|
|
367
|
-
* @param cookies - SvelteKit 原始 Cookies 对象
|
|
368
|
-
* @param config - 加密配置
|
|
369
|
-
* @returns 代理后的 Cookies 对象(类型不变)
|
|
370
|
-
*/
|
|
371
|
-
declare function createEncryptedCookieProxy(cookies: Cookies, config: CookieProxyConfig): Cookies;
|
|
372
200
|
|
|
373
201
|
/**
|
|
374
202
|
* @h-ai/kit — SvelteKit Handle Hook
|
|
@@ -393,19 +221,12 @@ declare function createEncryptedCookieProxy(cookies: Cookies, config: CookieProx
|
|
|
393
221
|
*
|
|
394
222
|
* @param config - Handle 配置(均有合理默认值,可零配置使用)
|
|
395
223
|
* @returns SvelteKit Handle 函数
|
|
396
|
-
*
|
|
397
224
|
* @example
|
|
398
225
|
* ```ts
|
|
399
|
-
*
|
|
400
|
-
*
|
|
401
|
-
*
|
|
402
|
-
*
|
|
403
|
-
* protectedPaths: ['/admin/*', '/api/*'],
|
|
404
|
-
* publicPaths: ['/api/auth/*', '/api/public/*'],
|
|
405
|
-
* },
|
|
406
|
-
* rateLimit: { maxRequests: 100 },
|
|
407
|
-
* crypto: { crypto, transport: true },
|
|
408
|
-
* })
|
|
226
|
+
* import { kit } from '@h-ai/kit'
|
|
227
|
+
*
|
|
228
|
+
* const handle = kit.createHandle({ auth: { verifyToken: async () => null, protectedPaths: ['/admin/**'] } })
|
|
229
|
+
* void handle
|
|
409
230
|
* ```
|
|
410
231
|
*/
|
|
411
232
|
declare function createHandle(config?: HandleConfig): Handle;
|
|
@@ -416,16 +237,58 @@ declare function createHandle(config?: HandleConfig): Handle;
|
|
|
416
237
|
*
|
|
417
238
|
* @param handles - 待组合的 handle 函数列表
|
|
418
239
|
* @returns 组合后的单一 Handle 函数
|
|
419
|
-
*
|
|
420
240
|
* @example
|
|
421
241
|
* ```ts
|
|
422
|
-
*
|
|
423
|
-
*
|
|
242
|
+
* import { kit } from '@h-ai/kit'
|
|
243
|
+
*
|
|
244
|
+
* const handle = kit.sequence(kit.createHandle({ logging: true }), kit.createHandle())
|
|
245
|
+
* void handle
|
|
424
246
|
* ```
|
|
425
247
|
*/
|
|
426
248
|
declare function sequence(...handles: Handle[]): Handle;
|
|
427
249
|
|
|
428
|
-
/**
|
|
250
|
+
/** 可直接挂到 SvelteKit catch-all 路由的反馈适配,无 serv 或 Svelte UI 依赖。 */
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* 反馈服务及可信范围解析。resolveContext 必须使用已验证 session/宿主策略。
|
|
254
|
+
* @example
|
|
255
|
+
* ```ts
|
|
256
|
+
* import type { FeedbackHandlerOptions } from '@h-ai/kit'
|
|
257
|
+
* import { feedback } from '@h-ai/feedback'
|
|
258
|
+
* const options: FeedbackHandlerOptions = { feedback, resolveContext: () => null }
|
|
259
|
+
* // 应用实现真实 session 映射;null 拒绝访问,不能从 JSON 取得身份。
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
262
|
+
interface FeedbackHandlerOptions {
|
|
263
|
+
readonly feedback: FeedbackFunctions;
|
|
264
|
+
readonly resolveContext: (event: RequestEvent) => FeedbackContext | null | Promise<FeedbackContext | null>;
|
|
265
|
+
readonly basePath?: string;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* 装配反馈 REST 路由;默认路径 /api/feedback,由宿主 handle 保持 CSRF、认证和传输加密。
|
|
269
|
+
* @param options - 已初始化服务与可信范围映射。
|
|
270
|
+
* @returns 可赋值给 catch-all 路由 GET/POST/PATCH 的同一 handler。
|
|
271
|
+
* @example
|
|
272
|
+
* ```ts
|
|
273
|
+
* import { kit } from '@h-ai/kit'
|
|
274
|
+
* import { feedback } from '@h-ai/feedback'
|
|
275
|
+
* const handler = kit.createFeedbackHandler({ feedback, resolveContext: () => null })
|
|
276
|
+
* // 路由 export const GET = handler;真实应用从受信 session 返回范围,null 默认拒绝。
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
declare function createFeedbackHandler(options: FeedbackHandlerOptions): RequestHandler;
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* 应用路由直接导出对应 handler;认证 ops 仍由 createHandle 注入。
|
|
283
|
+
* @param iam - 已由应用初始化的 IAM 服务对象。
|
|
284
|
+
* @returns 标准认证端点的 RequestHandler 集合。
|
|
285
|
+
* @example 模块内部上下文
|
|
286
|
+
* ```ts
|
|
287
|
+
* function build(iam: IamFunctions) {
|
|
288
|
+
* return createAuthApi(iam) // 调用方负责初始化 IAM;工厂只装配 handler。
|
|
289
|
+
* }
|
|
290
|
+
* ```
|
|
291
|
+
*/
|
|
429
292
|
declare function createAuthApi(iam: IamFunctions): {
|
|
430
293
|
publicConfig: _sveltejs_kit.RequestHandler;
|
|
431
294
|
login: _sveltejs_kit.RequestHandler;
|
|
@@ -455,13 +318,12 @@ declare function createAuthApi(iam: IamFunctions): {
|
|
|
455
318
|
*
|
|
456
319
|
* @param fn - 业务处理函数
|
|
457
320
|
* @returns SvelteKit RequestHandler
|
|
458
|
-
*
|
|
459
321
|
* @example
|
|
460
322
|
* ```ts
|
|
461
|
-
*
|
|
462
|
-
*
|
|
463
|
-
*
|
|
464
|
-
*
|
|
323
|
+
* import { kit } from '@h-ai/kit'
|
|
324
|
+
*
|
|
325
|
+
* const GET = kit.handler(() => kit.response.ok({ healthy: true }))
|
|
326
|
+
* void GET
|
|
465
327
|
* ```
|
|
466
328
|
*/
|
|
467
329
|
declare function handler(fn: (event: RequestEvent) => Promise<Response> | Response): RequestHandler;
|
|
@@ -482,10 +344,12 @@ declare function handler(fn: (event: RequestEvent) => Promise<Response> | Respon
|
|
|
482
344
|
* @param data - 响应数据,序列化为 JSON
|
|
483
345
|
* @param requestId - 可选请求 ID,用于链路追踪
|
|
484
346
|
* @returns `{ success: true, data, requestId }` 格式的 JSON Response
|
|
485
|
-
*
|
|
486
347
|
* @example
|
|
487
348
|
* ```ts
|
|
488
|
-
*
|
|
349
|
+
* import { kit } from '@h-ai/kit'
|
|
350
|
+
*
|
|
351
|
+
* const response = kit.response.ok({ id: 'article_1' })
|
|
352
|
+
* void response.status
|
|
489
353
|
* ```
|
|
490
354
|
*/
|
|
491
355
|
declare function ok<T>(data: T, requestId?: string): Response;
|
|
@@ -495,10 +359,12 @@ declare function ok<T>(data: T, requestId?: string): Response;
|
|
|
495
359
|
* @param data - 新创建的资源数据
|
|
496
360
|
* @param requestId - 可选请求 ID
|
|
497
361
|
* @returns `{ success: true, data }` 格式的 JSON Response(status 201)
|
|
498
|
-
*
|
|
499
362
|
* @example
|
|
500
363
|
* ```ts
|
|
501
|
-
*
|
|
364
|
+
* import { kit } from '@h-ai/kit'
|
|
365
|
+
*
|
|
366
|
+
* const response = kit.response.created({ id: 'article_1' })
|
|
367
|
+
* void response.status
|
|
502
368
|
* ```
|
|
503
369
|
*/
|
|
504
370
|
declare function created<T>(data: T, requestId?: string): Response;
|
|
@@ -508,6 +374,13 @@ declare function created<T>(data: T, requestId?: string): Response;
|
|
|
508
374
|
* 通常用于 DELETE 成功或无返回值的更新操作。
|
|
509
375
|
*
|
|
510
376
|
* @returns 空 body、status 204 的 Response
|
|
377
|
+
* @example
|
|
378
|
+
* ```ts
|
|
379
|
+
* import { kit } from '@h-ai/kit'
|
|
380
|
+
*
|
|
381
|
+
* const response = kit.response.noContent()
|
|
382
|
+
* void response.status
|
|
383
|
+
* ```
|
|
511
384
|
*/
|
|
512
385
|
declare function noContent(): Response;
|
|
513
386
|
/**
|
|
@@ -519,10 +392,12 @@ declare function noContent(): Response;
|
|
|
519
392
|
* @param requestId - 可选请求 ID
|
|
520
393
|
* @param details - 可选额外详情
|
|
521
394
|
* @returns `{ success: false, error: { code, message, details } }` 格式的 JSON Response
|
|
522
|
-
*
|
|
523
395
|
* @example
|
|
524
396
|
* ```ts
|
|
525
|
-
*
|
|
397
|
+
* import { kit } from '@h-ai/kit'
|
|
398
|
+
*
|
|
399
|
+
* const response = kit.response.error('ARTICLE_MISSING', 'Article missing', 404)
|
|
400
|
+
* void response.status
|
|
526
401
|
* ```
|
|
527
402
|
*/
|
|
528
403
|
declare function error(code: string, message: string, status?: number, requestId?: string, details?: unknown): Response;
|
|
@@ -533,6 +408,13 @@ declare function error(code: string, message: string, status?: number, requestId
|
|
|
533
408
|
* @param requestId - 可选请求 ID
|
|
534
409
|
* @param details - 可选额外详情
|
|
535
410
|
* @returns error code 为 `'BAD_REQUEST'` 的 JSON Response
|
|
411
|
+
* @example
|
|
412
|
+
* ```ts
|
|
413
|
+
* import { kit } from '@h-ai/kit'
|
|
414
|
+
*
|
|
415
|
+
* const response = kit.response.badRequest('Invalid article')
|
|
416
|
+
* void response.status
|
|
417
|
+
* ```
|
|
536
418
|
*/
|
|
537
419
|
declare function badRequest(message: string, requestId?: string, details?: unknown): Response;
|
|
538
420
|
/**
|
|
@@ -541,6 +423,13 @@ declare function badRequest(message: string, requestId?: string, details?: unkno
|
|
|
541
423
|
* @param message - 错误消息,默认 `'Authentication required'`
|
|
542
424
|
* @param requestId - 可选请求 ID
|
|
543
425
|
* @returns error code 为 `'UNAUTHORIZED'` 的 JSON Response
|
|
426
|
+
* @example
|
|
427
|
+
* ```ts
|
|
428
|
+
* import { kit } from '@h-ai/kit'
|
|
429
|
+
*
|
|
430
|
+
* const response = kit.response.unauthorized()
|
|
431
|
+
* void response.status
|
|
432
|
+
* ```
|
|
544
433
|
*/
|
|
545
434
|
declare function unauthorized(message?: string, requestId?: string): Response;
|
|
546
435
|
/**
|
|
@@ -549,6 +438,13 @@ declare function unauthorized(message?: string, requestId?: string): Response;
|
|
|
549
438
|
* @param message - 错误消息,默认 `'Access denied'`
|
|
550
439
|
* @param requestId - 可选请求 ID
|
|
551
440
|
* @returns error code 为 `'FORBIDDEN'` 的 JSON Response
|
|
441
|
+
* @example
|
|
442
|
+
* ```ts
|
|
443
|
+
* import { kit } from '@h-ai/kit'
|
|
444
|
+
*
|
|
445
|
+
* const response = kit.response.forbidden()
|
|
446
|
+
* void response.status
|
|
447
|
+
* ```
|
|
552
448
|
*/
|
|
553
449
|
declare function forbidden(message?: string, requestId?: string): Response;
|
|
554
450
|
/**
|
|
@@ -557,6 +453,13 @@ declare function forbidden(message?: string, requestId?: string): Response;
|
|
|
557
453
|
* @param message - 错误消息,默认 `'Resource not found'`
|
|
558
454
|
* @param requestId - 可选请求 ID
|
|
559
455
|
* @returns error code 为 `'NOT_FOUND'` 的 JSON Response
|
|
456
|
+
* @example
|
|
457
|
+
* ```ts
|
|
458
|
+
* import { kit } from '@h-ai/kit'
|
|
459
|
+
*
|
|
460
|
+
* const response = kit.response.notFound()
|
|
461
|
+
* void response.status
|
|
462
|
+
* ```
|
|
560
463
|
*/
|
|
561
464
|
declare function notFound(message?: string, requestId?: string): Response;
|
|
562
465
|
/**
|
|
@@ -565,6 +468,13 @@ declare function notFound(message?: string, requestId?: string): Response;
|
|
|
565
468
|
* @param message - 冲突描述(如重复创建等)
|
|
566
469
|
* @param requestId - 可选请求 ID
|
|
567
470
|
* @returns error code 为 `'CONFLICT'` 的 JSON Response
|
|
471
|
+
* @example
|
|
472
|
+
* ```ts
|
|
473
|
+
* import { kit } from '@h-ai/kit'
|
|
474
|
+
*
|
|
475
|
+
* const response = kit.response.conflict('Already updated')
|
|
476
|
+
* void response.status
|
|
477
|
+
* ```
|
|
568
478
|
*/
|
|
569
479
|
declare function conflict(message: string, requestId?: string): Response;
|
|
570
480
|
/**
|
|
@@ -573,12 +483,12 @@ declare function conflict(message: string, requestId?: string): Response;
|
|
|
573
483
|
* @param errors - 字段级别的验证错误列表
|
|
574
484
|
* @param requestId - 可选请求 ID
|
|
575
485
|
* @returns error code 为 `'VALIDATION_ERROR'`,details 包含 `errors` 数组
|
|
576
|
-
*
|
|
577
486
|
* @example
|
|
578
487
|
* ```ts
|
|
579
|
-
*
|
|
580
|
-
*
|
|
581
|
-
* ])
|
|
488
|
+
* import { kit } from '@h-ai/kit'
|
|
489
|
+
*
|
|
490
|
+
* const response = kit.response.validationError([{ field: 'title', message: 'Title is required' }])
|
|
491
|
+
* void response.status
|
|
582
492
|
* ```
|
|
583
493
|
*/
|
|
584
494
|
declare function validationError(errors: Array<{
|
|
@@ -591,6 +501,13 @@ declare function validationError(errors: Array<{
|
|
|
591
501
|
* @param message - 错误消息,默认 `'Internal server error'`
|
|
592
502
|
* @param requestId - 可选请求 ID
|
|
593
503
|
* @returns error code 为 `'INTERNAL_ERROR'` 的 JSON Response
|
|
504
|
+
* @example
|
|
505
|
+
* ```ts
|
|
506
|
+
* import { kit } from '@h-ai/kit'
|
|
507
|
+
*
|
|
508
|
+
* const response = kit.response.internalError()
|
|
509
|
+
* void response.status
|
|
510
|
+
* ```
|
|
594
511
|
*/
|
|
595
512
|
declare function internalError(message?: string, requestId?: string): Response;
|
|
596
513
|
/**
|
|
@@ -599,10 +516,12 @@ declare function internalError(message?: string, requestId?: string): Response;
|
|
|
599
516
|
* @param url - 目标 URL
|
|
600
517
|
* @param status - HTTP 状态码,默认 302;常用 303(POST 后重定向)
|
|
601
518
|
* @returns 带 `Location` 头的空 body Response
|
|
602
|
-
*
|
|
603
519
|
* @example
|
|
604
520
|
* ```ts
|
|
605
|
-
*
|
|
521
|
+
* import { kit } from '@h-ai/kit'
|
|
522
|
+
*
|
|
523
|
+
* const response = kit.response.redirect('/articles', 303)
|
|
524
|
+
* void response.status
|
|
606
525
|
* ```
|
|
607
526
|
*/
|
|
608
527
|
declare function redirect(url: string, status?: 301 | 302 | 303 | 307 | 308): Response;
|
|
@@ -616,14 +535,12 @@ declare function redirect(url: string, status?: 301 | 302 | 303 | 307 | 308): Re
|
|
|
616
535
|
* @param httpStatusMap - 模块导出的错误码 → HTTP 状态码映射表(如 IamErrorHttpStatus)
|
|
617
536
|
* @param requestId - 可选请求 ID,用于链路追踪
|
|
618
537
|
* @returns 标准化 JSON Response
|
|
619
|
-
*
|
|
620
538
|
* @example
|
|
621
539
|
* ```ts
|
|
622
|
-
*
|
|
623
|
-
* return kit.response.fromResult(result)
|
|
540
|
+
* import { kit } from '@h-ai/kit'
|
|
624
541
|
*
|
|
625
|
-
*
|
|
626
|
-
*
|
|
542
|
+
* const response = kit.response.fromResult({ success: true, data: { id: 'article_1' } })
|
|
543
|
+
* void response.status
|
|
627
544
|
* ```
|
|
628
545
|
*/
|
|
629
546
|
declare function fromResult<T>(result: {
|
|
@@ -637,6 +554,19 @@ declare function fromResult<T>(result: {
|
|
|
637
554
|
[key: string]: unknown;
|
|
638
555
|
};
|
|
639
556
|
}, httpStatusMap?: Record<number | string, number>, requestId?: string): Response;
|
|
557
|
+
/**
|
|
558
|
+
* 按模块错误的 HTTP 状态构建安全的统一 API 错误响应。
|
|
559
|
+
* @param haiError - 包含 code、message 和可选 httpStatus 的模块错误。
|
|
560
|
+
* @param requestId - 可选的请求追踪标识。
|
|
561
|
+
* @returns 包含统一错误结构和对应 HTTP 状态的 Response。
|
|
562
|
+
* @example
|
|
563
|
+
* ```ts
|
|
564
|
+
* import { kit } from '@h-ai/kit'
|
|
565
|
+
*
|
|
566
|
+
* const response = kit.response.fromError({ code: 'app:articles:001', message: 'Article missing', httpStatus: 404 })
|
|
567
|
+
* void response.status
|
|
568
|
+
* ```
|
|
569
|
+
*/
|
|
640
570
|
declare function fromError(haiError: HaiError, requestId?: string): Response;
|
|
641
571
|
|
|
642
572
|
/**
|
|
@@ -662,13 +592,12 @@ declare function fromError(haiError: HaiError, requestId?: string): Response;
|
|
|
662
592
|
* @param schema - Zod Schema
|
|
663
593
|
* @returns 校验通过的数据(类型安全)
|
|
664
594
|
* @throws Response - 400 BadRequest(含首条错误消息)
|
|
665
|
-
*
|
|
666
|
-
* @example
|
|
595
|
+
* @example 模块内部上下文
|
|
667
596
|
* ```ts
|
|
668
|
-
*
|
|
669
|
-
*
|
|
670
|
-
*
|
|
671
|
-
*
|
|
597
|
+
* const schema = zod.object({ title: zod.string().min(1) })
|
|
598
|
+
* const result = await validateFormOrFail(new Request('https://app.example.com/api', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title: 'Example' }) }), schema)
|
|
599
|
+
* // 校验失败抛出 Response,由框架错误边界透传。
|
|
600
|
+
* void result.title
|
|
672
601
|
* ```
|
|
673
602
|
*/
|
|
674
603
|
declare function validateFormOrFail<T extends z.ZodType>(request: Request, schema: T): Promise<z.infer<T>>;
|
|
@@ -679,10 +608,12 @@ declare function validateFormOrFail<T extends z.ZodType>(request: Request, schem
|
|
|
679
608
|
* @param schema - Zod Schema
|
|
680
609
|
* @returns 校验通过的数据
|
|
681
610
|
* @throws Response - 400 BadRequest
|
|
682
|
-
*
|
|
683
|
-
* @example
|
|
611
|
+
* @example 模块内部上下文
|
|
684
612
|
* ```ts
|
|
685
|
-
* const
|
|
613
|
+
* const schema = zod.object({ title: zod.string().min(1) })
|
|
614
|
+
* const result = validateQueryOrFail(new URL('https://app.example.com/articles?title=Example'), schema)
|
|
615
|
+
* // 校验失败抛出 Response,由框架错误边界透传。
|
|
616
|
+
* void result.title
|
|
686
617
|
* ```
|
|
687
618
|
*/
|
|
688
619
|
declare function validateQueryOrFail<T extends z.ZodType>(url: URL, schema: T): z.infer<T>;
|
|
@@ -693,10 +624,12 @@ declare function validateQueryOrFail<T extends z.ZodType>(url: URL, schema: T):
|
|
|
693
624
|
* @param schema - Zod Schema
|
|
694
625
|
* @returns 校验通过的数据
|
|
695
626
|
* @throws Response - 400 BadRequest
|
|
696
|
-
*
|
|
697
|
-
* @example
|
|
627
|
+
* @example 模块内部上下文
|
|
698
628
|
* ```ts
|
|
699
|
-
* const
|
|
629
|
+
* const schema = zod.object({ title: zod.string().min(1) })
|
|
630
|
+
* const result = validateParamsOrFail({ title: 'Example' }, schema)
|
|
631
|
+
* // 校验失败抛出 Response,由框架错误边界透传。
|
|
632
|
+
* void result.title
|
|
700
633
|
* ```
|
|
701
634
|
*/
|
|
702
635
|
declare function validateParamsOrFail<T extends z.ZodType>(params: Record<string, string>, schema: T): z.infer<T>;
|
|
@@ -704,10 +637,12 @@ declare function validateParamsOrFail<T extends z.ZodType>(params: Record<string
|
|
|
704
637
|
* 路径参数 id 校验 Schema
|
|
705
638
|
*
|
|
706
639
|
* 验证 `event.params.id` 为非空字符串。
|
|
707
|
-
*
|
|
708
640
|
* @example
|
|
709
641
|
* ```ts
|
|
710
|
-
*
|
|
642
|
+
* import { IdParamSchema } from '@h-ai/kit'
|
|
643
|
+
*
|
|
644
|
+
* const value = IdParamSchema.parse({ id: 'article_1' })
|
|
645
|
+
* void value
|
|
711
646
|
* ```
|
|
712
647
|
*/
|
|
713
648
|
declare const IdParamSchema: z.ZodObject<{
|
|
@@ -718,16 +653,12 @@ declare const IdParamSchema: z.ZodObject<{
|
|
|
718
653
|
*
|
|
719
654
|
* 包含 page(默认 1)、pageSize(默认 20,上限 100)、search(可选)。
|
|
720
655
|
* 可通过 `.extend()` 扩展业务字段。
|
|
721
|
-
*
|
|
722
656
|
* @example
|
|
723
657
|
* ```ts
|
|
724
|
-
*
|
|
725
|
-
* const { page, pageSize, search } = kit.validate.query(url, PaginationQuerySchema)
|
|
658
|
+
* import { PaginationQuerySchema } from '@h-ai/kit'
|
|
726
659
|
*
|
|
727
|
-
*
|
|
728
|
-
*
|
|
729
|
-
* enabled: z.enum(['true', 'false']).transform(v => v === 'true').optional(),
|
|
730
|
-
* })
|
|
660
|
+
* const value = PaginationQuerySchema.parse({ page: '1', pageSize: '20' })
|
|
661
|
+
* void value
|
|
731
662
|
* ```
|
|
732
663
|
*/
|
|
733
664
|
declare const PaginationQuerySchema: z.ZodObject<{
|
|
@@ -740,9 +671,24 @@ declare const PaginationQuerySchema: z.ZodObject<{
|
|
|
740
671
|
* Kit 模块统一出口。
|
|
741
672
|
*
|
|
742
673
|
* 作为纯工具模块,kit 不需要 init/close 生命周期,
|
|
743
|
-
*
|
|
674
|
+
* 依赖服务、请求上下文和创建后句柄的状态由调用方按各自契约管理。
|
|
675
|
+
* @example
|
|
676
|
+
* ```ts
|
|
677
|
+
* import { kit } from '@h-ai/kit'
|
|
678
|
+
*
|
|
679
|
+
* const handle = kit.createHandle({ logging: true })
|
|
680
|
+
* void handle
|
|
681
|
+
* ```
|
|
744
682
|
*/
|
|
745
683
|
declare const kit: {
|
|
684
|
+
/** 创建反馈的受控 catch-all HTTP handler,身份范围由宿主解析。 */
|
|
685
|
+
createFeedbackHandler: typeof createFeedbackHandler;
|
|
686
|
+
/** 原子限流工厂;共享store生命周期由应用管理。 */
|
|
687
|
+
middleware: {
|
|
688
|
+
rateLimit: typeof rateLimitMiddleware;
|
|
689
|
+
createCacheRateLimitStore: typeof createCacheRateLimitStore;
|
|
690
|
+
createMemoryRateLimitStore: typeof createMemoryRateLimitStore;
|
|
691
|
+
};
|
|
746
692
|
/** 创建 SvelteKit Handle Hook(含 auth / logging / rateLimit 内置配置) */
|
|
747
693
|
createHandle: typeof createHandle;
|
|
748
694
|
/** 组合多个 Handle */
|
|
@@ -837,8 +783,6 @@ declare const kit: {
|
|
|
837
783
|
setBrowserToken: typeof setBrowserToken;
|
|
838
784
|
/** 清除浏览器端 Access Token(客户端 logout 用) */
|
|
839
785
|
clearBrowserToken: typeof clearBrowserToken;
|
|
840
|
-
/** 创建浏览器端 Token 存储器(自定义 key 时使用) */
|
|
841
|
-
createTokenStore: typeof createTokenStore;
|
|
842
786
|
};
|
|
843
787
|
crud: {
|
|
844
788
|
/** 定义 CRUD 资源(声明式配置 → 操作对象) */
|
|
@@ -847,38 +791,11 @@ declare const kit: {
|
|
|
847
791
|
i18n: {
|
|
848
792
|
/** 统一设置所有 hai 模块的默认语言 */
|
|
849
793
|
setLocale: typeof setAllModulesLocale;
|
|
794
|
+
/** 请求/SSR 语言隔离;异步处理使用同一请求上下文。 */
|
|
795
|
+
runWithLocale: <T>(locale: _h_ai_core.Locale, operation: () => T) => T;
|
|
850
796
|
};
|
|
851
797
|
};
|
|
852
798
|
|
|
853
|
-
/**
|
|
854
|
-
* @h-ai/kit — CORS 中间件
|
|
855
|
-
*
|
|
856
|
-
* 配置跨域资源共享(CORS)策略,自动处理 OPTIONS 预检请求与响应头注入。
|
|
857
|
-
* 支持精确 origin、数组白名单、通配符模式与函数匹配四种模式。
|
|
858
|
-
* 自动预置 Capacitor WebView origin(Android/iOS)。
|
|
859
|
-
* @module kit-cors
|
|
860
|
-
*/
|
|
861
|
-
|
|
862
|
-
/**
|
|
863
|
-
* 创建 CORS 中间件
|
|
864
|
-
*
|
|
865
|
-
* 处理跨域资源共享:
|
|
866
|
-
* - OPTIONS 预检请求返回 204 + CORS 头
|
|
867
|
-
* - 其他请求在业务响应上附加 CORS 头
|
|
868
|
-
* - 默认预置 Capacitor WebView origin
|
|
869
|
-
*
|
|
870
|
-
* @param config - CORS 配置(省略则允许所有 origin)
|
|
871
|
-
* @returns Middleware 实例
|
|
872
|
-
*
|
|
873
|
-
* @example
|
|
874
|
-
* ```ts
|
|
875
|
-
* middleware: [
|
|
876
|
-
* kit.middleware.cors({ origin: ['https://example.com'], credentials: true }),
|
|
877
|
-
* ]
|
|
878
|
-
* ```
|
|
879
|
-
*/
|
|
880
|
-
declare function corsMiddleware(config?: CorsConfig): Middleware;
|
|
881
|
-
|
|
882
799
|
/**
|
|
883
800
|
* @h-ai/kit — 日志中间件
|
|
884
801
|
*
|
|
@@ -889,6 +806,13 @@ declare function corsMiddleware(config?: CorsConfig): Middleware;
|
|
|
889
806
|
|
|
890
807
|
/**
|
|
891
808
|
* 日志中间件配置
|
|
809
|
+
* @example
|
|
810
|
+
* ```ts
|
|
811
|
+
* import type { LoggingMiddlewareConfig } from '@h-ai/kit'
|
|
812
|
+
*
|
|
813
|
+
* const value: LoggingMiddlewareConfig = { logBody: false }
|
|
814
|
+
* void value
|
|
815
|
+
* ```
|
|
892
816
|
*/
|
|
893
817
|
interface LoggingMiddlewareConfig {
|
|
894
818
|
/** 为 `true` 时记录非 GET 请求体(自动脱敏) */
|
|
@@ -898,25 +822,5 @@ interface LoggingMiddlewareConfig {
|
|
|
898
822
|
/** 指定需要脱敏的字段名(默认 `['password', 'token', 'secret']`) */
|
|
899
823
|
redactFields?: string[];
|
|
900
824
|
}
|
|
901
|
-
/**
|
|
902
|
-
* 创建日志中间件
|
|
903
|
-
*
|
|
904
|
-
* 以 `core.logger.trace` 级别记录请求进出信息,包含:
|
|
905
|
-
* - 请求:method / path / query / userAgent / ip
|
|
906
|
-
* - 响应:status / duration
|
|
907
|
-
*
|
|
908
|
-
* 敏感字段会被自动替换为 `[REDACTED]`。
|
|
909
|
-
*
|
|
910
|
-
* @param config - 日志配置
|
|
911
|
-
* @returns Middleware 实例
|
|
912
|
-
*
|
|
913
|
-
* @example
|
|
914
|
-
* ```ts
|
|
915
|
-
* middleware: [
|
|
916
|
-
* kit.middleware.logging({ logBody: true, redactFields: ['password', 'creditCard'] }),
|
|
917
|
-
* ]
|
|
918
|
-
* ```
|
|
919
|
-
*/
|
|
920
|
-
declare function loggingMiddleware(config?: LoggingMiddlewareConfig): Middleware;
|
|
921
825
|
|
|
922
|
-
export { type AuthGuardConfig, type CookieProxyConfig,
|
|
826
|
+
export { type AuthGuardConfig, type CookieProxyConfig, type FeedbackHandlerOptions, HandleConfig, IdParamSchema, type LoggingMiddlewareConfig, PaginationQuerySchema, type PermissionGuardConfig, type RoleGuardConfig, SessionData, type SessionGuardConfig, SessionLike, defineCrud, kit, matchPermission };
|