chore(api): p1a final-review hygiene items

- contracts: add NOT_FOUND error code (framework-level 404 for unknown
  routes); Error enum + regen committed
- api: 404 default mapping in AllExceptionsFilter now NOT_FOUND (was
  ALBUM_NOT_FOUND); business 404s unchanged via explicit ApiException codes
- api: extract configureApp (prefix/CORS/x-powered-by) shared by main-cn
  bootstrap and e2e; disable x-powered-by, asserted in e2e
- api: move parseCursor/parseLimit (+page-size bounds) from
  albums.controller to shared/pagination; members/uploads/public re-pointed
- infra: bind compose published ports to 127.0.0.1 (mysql/redis/minio);
  .env.example endpoints follow suit
- e2e: unknown-route 404 NOT_FOUND + x-powered-by absent cases added
  (api e2e 120 -> 122)

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
浅念
2026-09-18 08:18:30 +08:00
parent 62382eca3c
commit 47fb81ad31
16 changed files with 137 additions and 83 deletions
+18
View File
@@ -0,0 +1,18 @@
// services/api/src/shared/configure-app.ts —— 应用级配置(全局前缀 / CORS /
// 响应头),main-cn.ts 生产引导与 e2e 测试装配共用同一函数:此前 e2e 各 spec
// 手工镜像 setGlobalPrefix,两处漂移无从守护;收敛为单一来源后,e2e 对
// x-powered-by 头的断言才真正落在生产引导路径上(P1a 终审修缮②)。
// 参数类型取 NestExpressApplication:disable() 是 Express 适配器特有方法。
import type { NestExpressApplication } from '@nestjs/platform-express';
import { API_PREFIX } from './constants';
export function configureApp(app: NestExpressApplication): void {
app.setGlobalPrefix(API_PREFIX);
// 隐藏 Express x-powered-by 指纹响应头(终审修缮②,e2e 断言之)。
app.disable('x-powered-by');
const corsOrigins = (process.env.CORS_ORIGINS ?? 'http://localhost:5173')
.split(',')
.map((origin) => origin.trim())
.filter((origin) => origin !== '');
app.enableCors({ origin: corsOrigins });
}
+57
View File
@@ -0,0 +1,57 @@
// services/api/src/shared/pagination.ts —— 列表查询参数规整(limit/cursor)。
// P1a 终审修缮③:自 albums.controller 迁入 shared——members/uploads/public 与
// albums 共用同一契约 bounds,工具函数不再寄居相册控制器导出。语义零变化:
// · limit:缺省 20,须为 1..100 的十进制整数,非法 → 400 VALIDATION_FAILED
// (与 body 校验同一出口);
// · cursor:仅长度上限规整(≤512,契约 CursorQuery.maxLength),语义校验在
// 服务层解码处(shared/cursor.ts)。
// 公开照片列表 bounds 不同(1..60 缺省 30),其 parsePublicPhotoLimit 仍在
// public.controller 本地维护,勿混用。
import { HttpStatus } from '@nestjs/common';
import { ErrorCodes } from '@photo/contracts';
import { ApiException } from '../api-exception';
import { MAX_CURSOR_LENGTH } from './cursor';
/** 列表 limit 缺省值(albums/members/uploads 各列表契约共用 bounds)。 */
export const DEFAULT_PAGE_SIZE = 20;
/** 列表 limit 上界(1..100,同上)。 */
export const MAX_PAGE_SIZE = 100;
/** limit 查询参数:缺省 20,须为 1..100 的十进制整数(契约 bounds)。 */
export function parseLimit(raw: string | undefined): number {
if (raw === undefined || raw === '') {
return DEFAULT_PAGE_SIZE;
}
if (!/^\d+$/.test(raw)) {
throw new ApiException(
ErrorCodes.VALIDATION_FAILED,
'limit 必须是 1-100 的整数',
HttpStatus.BAD_REQUEST,
);
}
const limit = Number.parseInt(raw, 10);
if (limit < 1 || limit > MAX_PAGE_SIZE) {
throw new ApiException(
ErrorCodes.VALIDATION_FAILED,
'limit 必须是 1-100 的整数',
HttpStatus.BAD_REQUEST,
);
}
return limit;
}
/** cursor 查询参数:仅做长度规整(语义校验在服务层解码时)。 */
export function parseCursor(raw: string | undefined): string | undefined {
if (raw === undefined || raw === '') {
return undefined;
}
if (raw.length > MAX_CURSOR_LENGTH) {
throw new ApiException(
ErrorCodes.VALIDATION_FAILED,
'cursor 不合法',
HttpStatus.BAD_REQUEST,
);
}
return raw;
}