@longzai-intelligence-transport/http-core 0.1.2 → 0.1.3

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 (2) hide show
  1. package/dist/index.d.ts +196 -110
  2. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -2007,7 +2007,7 @@ declare function buildQuery(query?: Record<string, string | number | boolean | u
2007
2007
  */
2008
2008
  declare function concatPath<T extends string, U extends string>(a: T, b: U): `${T}${U}`;
2009
2009
  //#endregion
2010
- //#region src/routes/route-group.utils.d.ts
2010
+ //#region src/routes/route-group.types.d.ts
2011
2011
  /**
2012
2012
  * API 版本号类型
2013
2013
  *
@@ -2029,134 +2029,76 @@ type DefaultAuthStrategy = 'public' | 'user' | 'admin' | (string & {});
2029
2029
  */
2030
2030
  type VersionedPrefix<TVersion extends ApiVersion | undefined, TPrefix extends string> = TVersion extends ApiVersion ? TPrefix extends `/${string}` ? `/${TVersion}${TPrefix}` : TPrefix extends '' ? `/${TVersion}` : `/${TVersion}/${TPrefix}` : TPrefix;
2031
2031
  /**
2032
- * 路由分组配置
2032
+ * 路由定义配置(用于分组内定义)
2033
2033
  *
2034
- * @typeParam TPrefix - 路径前缀类型
2035
- * @typeParam TVersion - API 版本号类型
2036
- * @typeParam TAuthStrategy - 认证策略类型
2034
+ * @typeParam TMethod - HTTP 方法类型
2035
+ * @typeParam TPath - 路径模板字符串
2036
+ * @typeParam TParams - 路径参数 Schema 类型
2037
+ * @typeParam TQuery - 查询参数 Schema 类型
2038
+ * @typeParam TBody - 请求体 Schema 类型
2039
+ * @typeParam TResponse - 响应 Schema 类型
2037
2040
  */
2038
- type RouteGroupConfig<TPrefix extends string = '', TVersion extends ApiVersion | undefined = undefined, TAuthStrategy extends string = DefaultAuthStrategy> = {
2041
+ type RouteDefineConfig<TMethod extends HttpMethod = HttpMethod, TPath extends string = string, TParams extends ZodType | undefined = ZodType | undefined, TQuery extends ZodType | undefined = ZodType | undefined, TBody extends ZodType | undefined = ZodType | undefined, TResponse extends ZodType = ZodType> = {
2039
2042
  /**
2040
- * 父路由分组(可选)
2041
- *
2042
- * 指定父分组时,当前分组会继承父分组的配置并追加路径
2043
- * - prefix 会追加到父分组的 prefix 后面
2044
- * - auth 和 tags 会继承父分组的值(如果未指定)
2043
+ * HTTP 方法
2045
2044
  */
2046
- parent?: RouteGroup<string, ApiVersion | undefined, TAuthStrategy>;
2045
+ method: TMethod;
2047
2046
  /**
2048
- * 路径前缀(可选)
2049
- *
2050
- * 所有子路由的路径都会自动拼接此前缀
2051
- * 如果指定了 parent,此前缀会追加到父分组的 prefix 后面
2052
- * 如果指定了 version,版本前缀会自动添加到 prefix 前面
2053
- * 如果未指定,默认为空字符串
2047
+ * 路径模板,会自动拼接分组前缀
2054
2048
  */
2055
- prefix?: TPrefix;
2049
+ path: TPath;
2056
2050
  /**
2057
- * API 版本号
2058
- *
2059
- * 版本前缀会自动添加到 prefix 前面,格式为 /{version}
2060
- * 例如:version: 'v1', prefix: 'client' -> 实际前缀: /v1/client
2061
- * 例如:version: 'v1', prefix: '' -> 实际前缀: /v1
2062
- *
2063
- * 注意:如果指定了 parent,version 应该在父分组中指定,子分组会自动继承
2051
+ * 路径参数 Schema
2064
2052
  */
2065
- version?: TVersion;
2053
+ params?: TParams;
2066
2054
  /**
2067
- * 认证策略
2068
- *
2069
- * 标识分组内路由的认证要求,可由适配器读取并应用相应的中间件
2070
- * 如果指定了 parent 且未设置此字段,会继承父分组的 auth
2055
+ * 查询参数 Schema
2071
2056
  */
2072
- auth?: TAuthStrategy;
2057
+ query?: TQuery;
2073
2058
  /**
2074
- * API 标签(用于 Swagger 分组和 TypedController)
2075
- *
2076
- * TypedController 装饰器会使用这些标签设置 @ApiTags()
2077
- * 子路由会继承这些标签,并可追加自己的标签
2078
- * 如果指定了 parent 且未设置此字段,会继承父分组的 tags
2059
+ * 请求体 Schema
2079
2060
  */
2080
- tags?: string[];
2061
+ body?: TBody;
2081
2062
  /**
2082
- * 路由定义集合(可选)
2083
- *
2084
- * 提供 routes 时,defineRouteGroup 返回 RouteGroup & { routes },
2085
- * 可直接用于 TypedController 和 TypedRoute。
2086
- * 不提供 routes 时,返回纯 RouteGroup,可用于 defineRoute 拆分定义。
2087
- *
2088
- * 接受两种输入:
2089
- * - 内联原始配置对象(简单场景,少量路由)
2090
- * - group.defineRoute() 的返回值(复杂场景,拆分定义)
2063
+ * 响应 Schema
2091
2064
  */
2092
- routes?: Record<string, RouteDefineConfig | ProcessedRoute<RouteDefinition>>;
2093
- };
2094
- /**
2095
- * 路由分组
2096
- *
2097
- * 提供路由分组功能,子路由自动继承分组的配置
2098
- *
2099
- * @typeParam TPrefix - 路径前缀类型
2100
- * @typeParam TVersion - API 版本号类型
2101
- * @typeParam TAuthStrategy - 认证策略类型
2102
- */
2103
- type RouteGroup<TPrefix extends string = string, TVersion extends ApiVersion | undefined = undefined, TAuthStrategy extends string = DefaultAuthStrategy> = {
2065
+ response: TResponse;
2104
2066
  /**
2105
- * 完整路径前缀(包含版本)
2106
- *
2107
- * 如果指定了 version,格式为 /{version}/{prefix}
2108
- * 如果未指定 version,则为原始 prefix
2067
+ * 接口摘要描述
2109
2068
  */
2110
- readonly prefix: VersionedPrefix<TVersion, TPrefix>;
2069
+ summary?: string;
2111
2070
  /**
2112
- * API 版本号
2071
+ * 接口标签,会与分组标签合并
2113
2072
  */
2114
- readonly version?: TVersion;
2073
+ tags?: string[];
2115
2074
  /**
2116
- * 认证策略
2117
- *
2118
- * 标识分组内路由的认证要求
2075
+ * 是否需要 CSRF 令牌保护
2119
2076
  */
2120
- readonly auth?: TAuthStrategy;
2077
+ csrf?: boolean;
2121
2078
  /**
2122
- * API 标签(用于 Swagger 分组和 TypedController)
2123
- *
2124
- * 用于接口分组和文档生成
2079
+ * 请求内容类型
2125
2080
  */
2126
- readonly tags?: string[];
2081
+ contentType?: 'json' | 'formData';
2127
2082
  /**
2128
- * 定义单条路由
2129
- *
2130
- * 语法糖,用于需要单独引用某条路由或拆分复杂路由定义的场景。
2131
- * 子路由的路径会自动拼接分组前缀,并继承分组的认证策略和标签。
2083
+ * 覆盖分组的认证策略
2132
2084
  *
2133
- * @typeParam TMethod - HTTP 方法类型
2134
- * @typeParam TPath - 路径模板字符串
2135
- * @typeParam TParams - 路径参数 Zod Schema
2136
- * @typeParam TQuery - 查询参数 Zod Schema
2137
- * @typeParam TBody - 请求体 Zod Schema
2138
- * @typeParam TResponse - 响应 Zod Schema
2139
- * @param config - 路由配置对象
2140
- * @returns 类型安全的路由定义,路径已拼接前缀
2141
- */
2142
- defineRoute: <TMethod extends HttpMethod = HttpMethod, TPath extends string = string, TParams extends ZodType | undefined = undefined, TQuery extends ZodType | undefined = undefined, TBody extends ZodType | undefined = undefined, TResponse extends ZodType = ZodType>(config: RouteDefineConfig<TMethod, TPath, TParams, TQuery, TBody, TResponse>) => RouteDefinition<TMethod, `${VersionedPrefix<TVersion, TPrefix>}${TPath}`, TParams, TQuery, TBody, TResponse> & {
2143
- /**
2144
- * 认证策略
2145
- */
2146
- auth?: TAuthStrategy;
2147
- };
2085
+ * 用于覆盖父路由分组的认证设置
2086
+ */
2087
+ auth?: string;
2148
2088
  };
2149
2089
  /**
2150
- * 路由定义配置(用于分组内定义)
2090
+ * 流式路由分组定义配置
2091
+ *
2092
+ * 用于 RouteGroup.defineRoute 创建流式响应路由,responseType 必填 'stream',
2093
+ * stream 字段描述响应元数据。
2151
2094
  *
2152
2095
  * @typeParam TMethod - HTTP 方法类型
2153
2096
  * @typeParam TPath - 路径模板字符串
2154
2097
  * @typeParam TParams - 路径参数 Schema 类型
2155
2098
  * @typeParam TQuery - 查询参数 Schema 类型
2156
2099
  * @typeParam TBody - 请求体 Schema 类型
2157
- * @typeParam TResponse - 响应 Schema 类型
2158
2100
  */
2159
- type RouteDefineConfig<TMethod extends HttpMethod = HttpMethod, TPath extends string = string, TParams extends ZodType | undefined = ZodType | undefined, TQuery extends ZodType | undefined = ZodType | undefined, TBody extends ZodType | undefined = ZodType | undefined, TResponse extends ZodType = ZodType> = {
2101
+ type StreamRouteGroupDefineConfig<TMethod extends HttpMethod = HttpMethod, TPath extends string = string, TParams extends ZodType | undefined = ZodType | undefined, TQuery extends ZodType | undefined = ZodType | undefined, TBody extends ZodType | undefined = ZodType | undefined> = {
2160
2102
  /**
2161
2103
  * HTTP 方法
2162
2104
  */
@@ -2178,9 +2120,13 @@ type RouteDefineConfig<TMethod extends HttpMethod = HttpMethod, TPath extends st
2178
2120
  */
2179
2121
  body?: TBody;
2180
2122
  /**
2181
- * 响应 Schema
2123
+ * 响应类型标记:二进制流,显式必填
2182
2124
  */
2183
- response: TResponse;
2125
+ responseType: 'stream';
2126
+ /**
2127
+ * 流式响应元数据
2128
+ */
2129
+ stream: StreamResponseMeta;
2184
2130
  /**
2185
2131
  * 接口摘要描述
2186
2132
  */
@@ -2199,30 +2145,77 @@ type RouteDefineConfig<TMethod extends HttpMethod = HttpMethod, TPath extends st
2199
2145
  contentType?: 'json' | 'formData';
2200
2146
  /**
2201
2147
  * 覆盖分组的认证策略
2202
- *
2203
- * 用于覆盖父路由分组的认证设置
2204
2148
  */
2205
2149
  auth?: string;
2206
2150
  };
2207
2151
  /**
2208
- * 路由定义结果类型
2152
+ * defineRoute 重载类型
2209
2153
  *
2210
- * defineRoute 返回的类型,包含完整的路由定义和可选的 auth 属性
2154
+ * stream config 返回精确的 StreamResponseRoute,
2155
+ * JSON config 返回精确的 JsonResponseRoute。
2211
2156
  *
2212
- * @typeParam TMethod - HTTP 方法类型
2213
2157
  * @typeParam TVersion - API 版本号类型
2214
2158
  * @typeParam TPrefix - 路径前缀类型
2215
- * @typeParam TPath - 路径模板字符串
2216
- * @typeParam TParams - 路径参数 Schema 类型
2217
- * @typeParam TQuery - 查询参数 Schema 类型
2218
- * @typeParam TBody - 请求体 Schema 类型
2219
- * @typeParam TResponse - 响应 Schema 类型
2159
+ * @typeParam TAuthStrategy - 认证策略类型
2220
2160
  */
2221
- type RouteDefineResult<TMethod extends HttpMethod, TVersion extends ApiVersion | undefined, TPrefix extends string, TPath extends string, TParams extends ZodType | undefined, TQuery extends ZodType | undefined, TBody extends ZodType | undefined, TResponse extends ZodType> = RouteDefinition<TMethod, `${VersionedPrefix<TVersion, TPrefix>}${TPath}`, TParams, TQuery, TBody, TResponse> & {
2161
+ type DefineRouteOverloads<TVersion extends ApiVersion | undefined, TPrefix extends string, TAuthStrategy extends string> = {
2162
+ /**
2163
+ * 定义单条路由(流式响应)
2164
+ */
2165
+ <TMethod extends HttpMethod, TPath extends string, TParams extends ZodType | undefined, TQuery extends ZodType | undefined, TBody extends ZodType | undefined, TLocalAuth extends string | undefined = undefined>(config: StreamRouteGroupDefineConfig<TMethod, TPath, TParams, TQuery, TBody> & {
2166
+ auth?: TLocalAuth;
2167
+ }): StreamResponseRoute<TMethod, `${VersionedPrefix<TVersion, TPrefix>}${TPath}`, TParams, TQuery, TBody> & {
2168
+ auth?: TLocalAuth extends string ? TLocalAuth : TAuthStrategy;
2169
+ };
2170
+ /**
2171
+ * 定义单条路由(JSON 响应)
2172
+ */
2173
+ <TMethod extends HttpMethod = HttpMethod, TPath extends string = string, TParams extends ZodType | undefined = undefined, TQuery extends ZodType | undefined = undefined, TBody extends ZodType | undefined = undefined, TResponse extends ZodType = ZodType, TLocalAuth extends string | undefined = undefined>(config: RouteDefineConfig<TMethod, TPath, TParams, TQuery, TBody, TResponse> & {
2174
+ auth?: TLocalAuth;
2175
+ }): JsonResponseRoute<TMethod, `${VersionedPrefix<TVersion, TPrefix>}${TPath}`, TParams, TQuery, TBody, TResponse> & {
2176
+ auth?: TLocalAuth extends string ? TLocalAuth : TAuthStrategy;
2177
+ };
2178
+ };
2179
+ /**
2180
+ * 路由分组
2181
+ *
2182
+ * 提供路由分组功能,子路由自动继承分组的配置
2183
+ *
2184
+ * @typeParam TPrefix - 路径前缀类型
2185
+ * @typeParam TVersion - API 版本号类型
2186
+ * @typeParam TAuthStrategy - 认证策略类型
2187
+ */
2188
+ type RouteGroup<TPrefix extends string = string, TVersion extends ApiVersion | undefined = undefined, TAuthStrategy extends string = DefaultAuthStrategy> = {
2189
+ /**
2190
+ * 完整路径前缀(包含版本)
2191
+ *
2192
+ * 如果指定了 version,格式为 /{version}/{prefix}
2193
+ * 如果未指定 version,则为原始 prefix
2194
+ */
2195
+ readonly prefix: VersionedPrefix<TVersion, TPrefix>;
2196
+ /**
2197
+ * API 版本号
2198
+ */
2199
+ readonly version?: TVersion;
2222
2200
  /**
2223
2201
  * 认证策略
2202
+ *
2203
+ * 标识分组内路由的认证要求
2224
2204
  */
2225
- auth?: string;
2205
+ readonly auth?: TAuthStrategy;
2206
+ /**
2207
+ * API 标签(用于 Swagger 分组和 TypedController)
2208
+ *
2209
+ * 用于接口分组和文档生成
2210
+ */
2211
+ readonly tags?: string[];
2212
+ /**
2213
+ * 定义单条路由(支持 JSON 响应和流式响应)
2214
+ *
2215
+ * stream config → 精确的 StreamResponseRoute
2216
+ * JSON config → 精确的 JsonResponseRoute
2217
+ */
2218
+ defineRoute: DefineRouteOverloads<TVersion, TPrefix, TAuthStrategy>;
2226
2219
  };
2227
2220
  /**
2228
2221
  * 已处理的路由类型
@@ -2332,6 +2325,99 @@ T extends {
2332
2325
  * @typeParam TRoutes - 路由定义集合类型
2333
2326
  */
2334
2327
  type ProcessRoutes<TRoutes extends Record<string, RouteDefineConfig | ProcessedRoute<RouteDefinition>>> = { [K in keyof TRoutes]: ProcessedRoute<InferRouteDefinitionFromValue<TRoutes[K]>> };
2328
+ /**
2329
+ * 路由分组配置
2330
+ *
2331
+ * @typeParam TPrefix - 路径前缀类型
2332
+ * @typeParam TVersion - API 版本号类型
2333
+ * @typeParam TAuthStrategy - 认证策略类型
2334
+ */
2335
+ type RouteGroupConfig<TPrefix extends string = '', TVersion extends ApiVersion | undefined = undefined, TAuthStrategy extends string = DefaultAuthStrategy> = {
2336
+ /**
2337
+ * 父路由分组(可选)
2338
+ *
2339
+ * 指定父分组时,当前分组会继承父分组的配置并追加路径
2340
+ * - prefix 会追加到父分组的 prefix 后面
2341
+ * - auth 和 tags 会继承父分组的值(如果未指定)
2342
+ */
2343
+ parent?: RouteGroup<string, ApiVersion | undefined, TAuthStrategy>;
2344
+ /**
2345
+ * 路径前缀(可选)
2346
+ *
2347
+ * 所有子路由的路径都会自动拼接此前缀
2348
+ * 如果指定了 parent,此前缀会追加到父分组的 prefix 后面
2349
+ * 如果指定了 version,版本前缀会自动添加到 prefix 前面
2350
+ * 如果未指定,默认为空字符串
2351
+ */
2352
+ prefix?: TPrefix;
2353
+ /**
2354
+ * API 版本号
2355
+ *
2356
+ * 版本前缀会自动添加到 prefix 前面,格式为 /{version}
2357
+ * 例如:version: 'v1', prefix: 'client' -> 实际前缀: /v1/client
2358
+ * 例如:version: 'v1', prefix: '' -> 实际前缀: /v1
2359
+ *
2360
+ * 注意:如果指定了 parent,version 应该在父分组中指定,子分组会自动继承
2361
+ */
2362
+ version?: TVersion;
2363
+ /**
2364
+ * 认证策略
2365
+ *
2366
+ * 标识分组内路由的认证要求,可由适配器读取并应用相应的中间件
2367
+ * 如果指定了 parent 且未设置此字段,会继承父分组的 auth
2368
+ */
2369
+ auth?: TAuthStrategy;
2370
+ /**
2371
+ * API 标签(用于 Swagger 分组和 TypedController)
2372
+ *
2373
+ * TypedController 装饰器会使用这些标签设置 @ApiTags()
2374
+ * 子路由会继承这些标签,并可追加自己的标签
2375
+ * 如果指定了 parent 且未设置此字段,会继承父分组的 tags
2376
+ */
2377
+ tags?: string[];
2378
+ /**
2379
+ * 路由定义集合(可选)
2380
+ *
2381
+ * 提供 routes 时,defineRouteGroup 返回 RouteGroup & { routes },
2382
+ * 可直接用于 TypedController 和 TypedRoute。
2383
+ * 不提供 routes 时,返回纯 RouteGroup,可用于 defineRoute 拆分定义。
2384
+ *
2385
+ * 接受两种输入:
2386
+ * - 内联原始配置对象(简单场景,少量路由)
2387
+ * - group.defineRoute() 的返回值(复杂场景,拆分定义)
2388
+ */
2389
+ routes?: Record<string, RouteDefineConfig | ProcessedRoute<RouteDefinition>>;
2390
+ };
2391
+ /**
2392
+ * defineRouteGroup 函数配置类型
2393
+ *
2394
+ * @typeParam TPrefix - 路径前缀类型
2395
+ * @typeParam TVersion - API 版本号类型
2396
+ * @typeParam TAuthStrategy - 认证策略类型
2397
+ * @typeParam TRoutes - 路由定义集合类型
2398
+ */
2399
+ /**
2400
+ * 路由定义结果类型
2401
+ *
2402
+ * defineRoute 返回的类型,包含完整的路由定义和可选的 auth 属性
2403
+ *
2404
+ * @typeParam TMethod - HTTP 方法类型
2405
+ * @typeParam TVersion - API 版本号类型
2406
+ * @typeParam TPrefix - 路径前缀类型
2407
+ * @typeParam TPath - 路径模板字符串
2408
+ * @typeParam TParams - 路径参数 Schema 类型
2409
+ * @typeParam TQuery - 查询参数 Schema 类型
2410
+ * @typeParam TBody - 请求体 Schema 类型
2411
+ * @typeParam TResponse - 响应 Schema 类型
2412
+ */
2413
+ type RouteDefineResult<TMethod extends HttpMethod, TVersion extends ApiVersion | undefined, TPrefix extends string, TPath extends string, TParams extends ZodType | undefined, TQuery extends ZodType | undefined, TBody extends ZodType | undefined, TResponse extends ZodType> = RouteDefinition<TMethod, `${VersionedPrefix<TVersion, TPrefix>}${TPath}`, TParams, TQuery, TBody, TResponse> & {
2414
+ /**
2415
+ * 认证策略
2416
+ */
2417
+ auth?: string;
2418
+ };
2419
+ //#endregion
2420
+ //#region src/routes/route-group.utils.d.ts
2335
2421
  /**
2336
2422
  * 定义路由分组(含 routes,保留具体键类型)
2337
2423
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@longzai-intelligence-transport/http-core",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "keywords": [
5
5
  "api",
6
6
  "contract",