@lark-apaas/fullstack-nestjs-core 1.1.2-alpha.10 → 1.1.2-alpha.11

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/dist/index.d.cts CHANGED
@@ -97,22 +97,26 @@ declare class ViewContextMiddleware implements NestMiddleware {
97
97
  }
98
98
 
99
99
  /**
100
- * API 404 兜底 Controller
100
+ * API 404 响应格式
101
+ */
102
+ interface ApiNotFoundResponse {
103
+ statusCode: number;
104
+ message: string;
105
+ error: string;
106
+ path: string;
107
+ timestamp: string;
108
+ }
109
+ /**
110
+ * API 响应拦截中间件
101
111
  *
102
- * 处理所有未匹配的 /api/* 路径,返回标准的 JSON 404 响应
112
+ * 拦截 ViewModule 的 /* 路由,当其尝试返回 HTML
113
+ * 如果请求路径是 /api/*,改为返回 JSON 404
103
114
  *
104
115
  * 工作原理:
105
- * NestJS 会根据路由的具体程度自动排序:
106
- * 1. 具体路径 (/api/users) - 最高优先级
107
- * 2. 路径参数 (/api/:id)
108
- * 3. 通配符 (/api/*) - 本 Controller
109
- * 4. 更宽泛的通配符 (/*) - ViewModule
110
- *
111
- * 因此即使 PlatformModule 在其他业务模块之前导入,
112
- * 具体的 API 路由仍然会优先匹配,不会被拦截。
116
+ * 1. 包装 res.render() 和 res.send()
117
+ * 2. render() 直接返回 JSON(肯定是要渲染 HTML)
118
+ * 3. send() 检查 Content-Type 判断是否是 HTML
113
119
  */
114
- declare class ApiFallbackController {
115
- handleNotFound(req: Request, res: Response): Response<any, Record<string, any>>;
116
- }
120
+ declare function apiResponseInterceptor(req: Request, res: Response, next: NextFunction): void;
117
121
 
118
- export { ApiFallbackController, CsrfMiddleware, CsrfTokenMiddleware, PlatformModule, type PlatformModuleOptions, UserContextMiddleware, ViewContextMiddleware, configureApp };
122
+ export { type ApiNotFoundResponse, CsrfMiddleware, CsrfTokenMiddleware, PlatformModule, type PlatformModuleOptions, UserContextMiddleware, ViewContextMiddleware, apiResponseInterceptor, configureApp };
package/dist/index.d.ts CHANGED
@@ -97,22 +97,26 @@ declare class ViewContextMiddleware implements NestMiddleware {
97
97
  }
98
98
 
99
99
  /**
100
- * API 404 兜底 Controller
100
+ * API 404 响应格式
101
+ */
102
+ interface ApiNotFoundResponse {
103
+ statusCode: number;
104
+ message: string;
105
+ error: string;
106
+ path: string;
107
+ timestamp: string;
108
+ }
109
+ /**
110
+ * API 响应拦截中间件
101
111
  *
102
- * 处理所有未匹配的 /api/* 路径,返回标准的 JSON 404 响应
112
+ * 拦截 ViewModule 的 /* 路由,当其尝试返回 HTML
113
+ * 如果请求路径是 /api/*,改为返回 JSON 404
103
114
  *
104
115
  * 工作原理:
105
- * NestJS 会根据路由的具体程度自动排序:
106
- * 1. 具体路径 (/api/users) - 最高优先级
107
- * 2. 路径参数 (/api/:id)
108
- * 3. 通配符 (/api/*) - 本 Controller
109
- * 4. 更宽泛的通配符 (/*) - ViewModule
110
- *
111
- * 因此即使 PlatformModule 在其他业务模块之前导入,
112
- * 具体的 API 路由仍然会优先匹配,不会被拦截。
116
+ * 1. 包装 res.render() 和 res.send()
117
+ * 2. render() 直接返回 JSON(肯定是要渲染 HTML)
118
+ * 3. send() 检查 Content-Type 判断是否是 HTML
113
119
  */
114
- declare class ApiFallbackController {
115
- handleNotFound(req: Request, res: Response): Response<any, Record<string, any>>;
116
- }
120
+ declare function apiResponseInterceptor(req: Request, res: Response, next: NextFunction): void;
117
121
 
118
- export { ApiFallbackController, CsrfMiddleware, CsrfTokenMiddleware, PlatformModule, type PlatformModuleOptions, UserContextMiddleware, ViewContextMiddleware, configureApp };
122
+ export { type ApiNotFoundResponse, CsrfMiddleware, CsrfTokenMiddleware, PlatformModule, type PlatformModuleOptions, UserContextMiddleware, ViewContextMiddleware, apiResponseInterceptor, configureApp };