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