@lark-apaas/fullstack-nestjs-core 1.1.2-alpha.0 → 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.cjs CHANGED
@@ -39,6 +39,7 @@ __export(index_exports, {
39
39
  PlatformModule: () => PlatformModule,
40
40
  UserContextMiddleware: () => UserContextMiddleware,
41
41
  ViewContextMiddleware: () => ViewContextMiddleware,
42
+ apiResponseInterceptor: () => apiResponseInterceptor,
42
43
  configureApp: () => configureApp
43
44
  });
44
45
  module.exports = __toCommonJS(index_exports);
@@ -91,7 +92,11 @@ var UserContextMiddleware = class {
91
92
  userId: webUser?.user_id,
92
93
  tenantId: webUser?.tenant_id,
93
94
  appId: webUser?.app_id ?? "",
94
- loginUrl: webUser?.login_url ?? ""
95
+ loginUrl: webUser?.login_url ?? "",
96
+ env: webUser?.env ?? "runtime",
97
+ userName: webUser?.user_name?.zh_cn ?? "",
98
+ userNameEn: webUser?.user_name?.en_us ?? "",
99
+ userNameI18n: webUser?.user_name ?? {}
95
100
  };
96
101
  next();
97
102
  }
@@ -244,7 +249,7 @@ var CsrfTokenMiddleware = class _CsrfTokenMiddleware {
244
249
  res.cookie(cookieKey, token, {
245
250
  maxAge: cookieMaxAge,
246
251
  path: cookiePath,
247
- httpOnly: true,
252
+ httpOnly: false,
248
253
  secure: true,
249
254
  sameSite: "none",
250
255
  partitioned: true
@@ -257,6 +262,28 @@ CsrfTokenMiddleware = _ts_decorate4([
257
262
  (0, import_common4.Injectable)()
258
263
  ], CsrfTokenMiddleware);
259
264
 
265
+ // src/middlewares/api-fallback/index.ts
266
+ function createApiNotFoundResponse(req) {
267
+ return {
268
+ statusCode: 404,
269
+ message: `Cannot ${req.method} ${req.path}`,
270
+ error: "Not Found",
271
+ path: req.path,
272
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
273
+ };
274
+ }
275
+ __name(createApiNotFoundResponse, "createApiNotFoundResponse");
276
+ function apiResponseInterceptor(req, res, next) {
277
+ if (!req.path.startsWith("/api/")) {
278
+ return next();
279
+ }
280
+ res.render = function() {
281
+ return res.status(404).json(createApiNotFoundResponse(req));
282
+ };
283
+ next();
284
+ }
285
+ __name(apiResponseInterceptor, "apiResponseInterceptor");
286
+
260
287
  // src/modules/platform/config/app.config.ts
261
288
  var import_config = require("@nestjs/config");
262
289
  var NAMESPACE = "app";
@@ -353,6 +380,7 @@ var PlatformModule = class _PlatformModule {
353
380
  */
354
381
  configure(consumer) {
355
382
  const options = _PlatformModule.moduleOptions;
383
+ consumer.apply(apiResponseInterceptor).forRoutes("/api/*");
356
384
  consumer.apply(UserContextMiddleware, import_nestjs_logger.LoggerContextMiddleware, import_nestjs_datapaas.SqlExecutionContextMiddleware).forRoutes("/*");
357
385
  consumer.apply(CsrfTokenMiddleware, ViewContextMiddleware).exclude("/api/(.*)").forRoutes("*");
358
386
  if (options.enableCsrf !== false) {
@@ -411,6 +439,7 @@ __reExport(index_exports, require("@lark-apaas/nestjs-datapaas"), module.exports
411
439
  PlatformModule,
412
440
  UserContextMiddleware,
413
441
  ViewContextMiddleware,
442
+ apiResponseInterceptor,
414
443
  configureApp,
415
444
  ...require("@lark-apaas/nestjs-authnpaas"),
416
445
  ...require("@lark-apaas/nestjs-datapaas")
package/dist/index.d.cts CHANGED
@@ -19,6 +19,10 @@ declare global {
19
19
  tenantId?: number;
20
20
  appId?: string;
21
21
  loginUrl?: string;
22
+ env?: 'preview' | 'runtime';
23
+ userName?: string; // 默认中文名
24
+ userNameEn?: string; // 冗余一份英文名
25
+ userNameI18n?: Record<string, string>; // 带多语的用户名
22
26
  },
23
27
  csrfToken?: string;
24
28
  }
@@ -92,4 +96,27 @@ declare class ViewContextMiddleware implements NestMiddleware {
92
96
  use(req: Request, res: Response, next: NextFunction): void;
93
97
  }
94
98
 
95
- export { CsrfMiddleware, CsrfTokenMiddleware, PlatformModule, type PlatformModuleOptions, UserContextMiddleware, ViewContextMiddleware, configureApp };
99
+ /**
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 响应拦截中间件
111
+ *
112
+ * 拦截 ViewModule 的 /* 路由,当其尝试返回 HTML 时
113
+ * 如果请求路径是 /api/*,改为返回 JSON 404
114
+ *
115
+ * 工作原理:
116
+ * 1. 包装 res.render() 和 res.send()
117
+ * 2. render() 直接返回 JSON(肯定是要渲染 HTML)
118
+ * 3. send() 检查 Content-Type 判断是否是 HTML
119
+ */
120
+ declare function apiResponseInterceptor(req: Request, res: Response, next: NextFunction): void;
121
+
122
+ export { type ApiNotFoundResponse, CsrfMiddleware, CsrfTokenMiddleware, PlatformModule, type PlatformModuleOptions, UserContextMiddleware, ViewContextMiddleware, apiResponseInterceptor, configureApp };
package/dist/index.d.ts CHANGED
@@ -19,6 +19,10 @@ declare global {
19
19
  tenantId?: number;
20
20
  appId?: string;
21
21
  loginUrl?: string;
22
+ env?: 'preview' | 'runtime';
23
+ userName?: string; // 默认中文名
24
+ userNameEn?: string; // 冗余一份英文名
25
+ userNameI18n?: Record<string, string>; // 带多语的用户名
22
26
  },
23
27
  csrfToken?: string;
24
28
  }
@@ -92,4 +96,27 @@ declare class ViewContextMiddleware implements NestMiddleware {
92
96
  use(req: Request, res: Response, next: NextFunction): void;
93
97
  }
94
98
 
95
- export { CsrfMiddleware, CsrfTokenMiddleware, PlatformModule, type PlatformModuleOptions, UserContextMiddleware, ViewContextMiddleware, configureApp };
99
+ /**
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 响应拦截中间件
111
+ *
112
+ * 拦截 ViewModule 的 /* 路由,当其尝试返回 HTML 时
113
+ * 如果请求路径是 /api/*,改为返回 JSON 404
114
+ *
115
+ * 工作原理:
116
+ * 1. 包装 res.render() 和 res.send()
117
+ * 2. render() 直接返回 JSON(肯定是要渲染 HTML)
118
+ * 3. send() 检查 Content-Type 判断是否是 HTML
119
+ */
120
+ declare function apiResponseInterceptor(req: Request, res: Response, next: NextFunction): void;
121
+
122
+ export { type ApiNotFoundResponse, CsrfMiddleware, CsrfTokenMiddleware, PlatformModule, type PlatformModuleOptions, UserContextMiddleware, ViewContextMiddleware, apiResponseInterceptor, configureApp };
package/dist/index.js CHANGED
@@ -49,7 +49,11 @@ var UserContextMiddleware = class {
49
49
  userId: webUser?.user_id,
50
50
  tenantId: webUser?.tenant_id,
51
51
  appId: webUser?.app_id ?? "",
52
- loginUrl: webUser?.login_url ?? ""
52
+ loginUrl: webUser?.login_url ?? "",
53
+ env: webUser?.env ?? "runtime",
54
+ userName: webUser?.user_name?.zh_cn ?? "",
55
+ userNameEn: webUser?.user_name?.en_us ?? "",
56
+ userNameI18n: webUser?.user_name ?? {}
53
57
  };
54
58
  next();
55
59
  }
@@ -202,7 +206,7 @@ var CsrfTokenMiddleware = class _CsrfTokenMiddleware {
202
206
  res.cookie(cookieKey, token, {
203
207
  maxAge: cookieMaxAge,
204
208
  path: cookiePath,
205
- httpOnly: true,
209
+ httpOnly: false,
206
210
  secure: true,
207
211
  sameSite: "none",
208
212
  partitioned: true
@@ -215,6 +219,28 @@ CsrfTokenMiddleware = _ts_decorate4([
215
219
  Injectable4()
216
220
  ], CsrfTokenMiddleware);
217
221
 
222
+ // src/middlewares/api-fallback/index.ts
223
+ function createApiNotFoundResponse(req) {
224
+ return {
225
+ statusCode: 404,
226
+ message: `Cannot ${req.method} ${req.path}`,
227
+ error: "Not Found",
228
+ path: req.path,
229
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
230
+ };
231
+ }
232
+ __name(createApiNotFoundResponse, "createApiNotFoundResponse");
233
+ function apiResponseInterceptor(req, res, next) {
234
+ if (!req.path.startsWith("/api/")) {
235
+ return next();
236
+ }
237
+ res.render = function() {
238
+ return res.status(404).json(createApiNotFoundResponse(req));
239
+ };
240
+ next();
241
+ }
242
+ __name(apiResponseInterceptor, "apiResponseInterceptor");
243
+
218
244
  // src/modules/platform/config/app.config.ts
219
245
  import { registerAs } from "@nestjs/config";
220
246
  var NAMESPACE = "app";
@@ -311,6 +337,7 @@ var PlatformModule = class _PlatformModule {
311
337
  */
312
338
  configure(consumer) {
313
339
  const options = _PlatformModule.moduleOptions;
340
+ consumer.apply(apiResponseInterceptor).forRoutes("/api/*");
314
341
  consumer.apply(UserContextMiddleware, LoggerContextMiddleware, SqlExecutionContextMiddleware).forRoutes("/*");
315
342
  consumer.apply(CsrfTokenMiddleware, ViewContextMiddleware).exclude("/api/(.*)").forRoutes("*");
316
343
  if (options.enableCsrf !== false) {
@@ -368,5 +395,6 @@ export {
368
395
  PlatformModule,
369
396
  UserContextMiddleware,
370
397
  ViewContextMiddleware,
398
+ apiResponseInterceptor,
371
399
  configureApp
372
400
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/fullstack-nestjs-core",
3
- "version": "1.1.2-alpha.0",
3
+ "version": "1.1.2-alpha.11",
4
4
  "description": "FullStack Nestjs Core",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -39,7 +39,7 @@
39
39
  "prepublishOnly": "npm run build"
40
40
  },
41
41
  "dependencies": {
42
- "@lark-apaas/nestjs-authnpaas": "1.0.2-alpha.0",
42
+ "@lark-apaas/nestjs-authnpaas": "^1.0.1",
43
43
  "@lark-apaas/nestjs-datapaas": "^1.0.5",
44
44
  "@lark-apaas/nestjs-logger": "^1.0.2",
45
45
  "@lark-apaas/nestjs-openapi-devtools": "^1.0.9",