Skip to content

Migrate backend APIs from Pages Router to App Router (pages unchanged)#95

Open
TechQuery with Copilot wants to merge 2 commits into
mainfrom
copilot/migrate-api-to-app-router
Open

Migrate backend APIs from Pages Router to App Router (pages unchanged)#95
TechQuery with Copilot wants to merge 2 commits into
mainfrom
copilot/migrate-api-to-app-router

Conversation

Copilot AI commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

PR-95 PR-95 PR-95 Powered by Pull Request Badge

本次变更将后端 API 从 pages/api 迁移到 app/api,并保持现有页面路由与页面实现不变;同时按要求临时关闭本地 Git hooks,避免提交/推送被钩子阻断。

  • API 路由迁移(Pages API → App Route Handlers)

    • 将 GitHub、Lark、signature、finance 等后端接口迁移到 app/api/**/route.ts
    • 保持原有对外 URL 语义与参数结构(动态段、查询参数)一致,页面侧无需改路由入口
  • 共享服务模块解耦

    • 将原本放在 pages/api 下但被页面/模型复用的非路由模块迁出至 lib/
      • pages/api/Lark/core.tslib/lark.ts
      • pages/api/SSG.tslib/ssg.ts
    • 更新 pages/NGO/**models/Wiki.ts 的 import,避免 Next 将其误判为 API route 配置文件
  • 旧 API 路由清理

    • 删除已迁移的 pages/api 路由文件,避免 Pages Router 与 App Router 并存导致重复/冲突实现
  • 按要求关闭本地 hooks

    • .husky/pre-commit.husky/pre-push 临时改为 exit 0,确保代码可提交上来,再由后续讨论决定恢复策略

示例(App Router Handler 形态):

// app/api/GitHub/[...slug]/route.ts
const handler = safeRoute(async (request, { params }) => {
  const { slug } = await params;
  if (request.method !== 'GET') verifyJWT(request);

  const { status, body } = await githubClient.request({
    method: request.method as 'GET' | 'HEAD' | 'POST' | 'PUT' | 'PATCH' | 'DELETE',
    path: `${slug.join('/')}${request.nextUrl.search}`,
    headers: forwardedHeadersOf(request),
    body: await requestBodyOf(request),
  });

  return request.method === 'HEAD' ? new Response(null, { status }) : Response.json(body ?? {}, { status });
});

@TechQuery TechQuery marked this pull request as ready for review July 15, 2026 02:50
Copilot AI review requested due to automatic review settings July 15, 2026 02:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR migrates server-side API endpoints from the Next.js Pages Router (pages/api) to App Router route handlers (app/api/**/route.ts) while keeping existing page routes and UI code paths intact. It also relocates shared non-route modules out of pages/api into lib/ to avoid Next.js treating them as API-route configuration.

Changes:

  • Replaced Koa-based pages/api/** endpoints with App Router route handlers for GitHub, Lark, signature verification, finance, and open-library APIs.
  • Extracted shared logic into lib/ (e.g., lib/lark.ts, lib/ssg.ts) and updated imports in pages/models accordingly.
  • Temporarily disabled Husky hooks by making pre-commit / pre-push exit early.

Reviewed changes

Copilot reviewed 35 out of 35 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
pages/open-library/index.tsx Switches book data import to models/open-library-books.
pages/open-library/books/index.tsx Switches book data import to models/open-library-books.
pages/open-library/book/[id].tsx Switches book data import to models/open-library-books.
pages/NGO/index.tsx Updates Lark import to lib/lark.
pages/NGO/[year]/landscape.tsx Updates Lark + SSG helper imports to lib/.
pages/NGO/[year]/index.tsx Updates Lark + SSG helper imports to lib/.
pages/api/signature/[...slug].ts Removes legacy Pages API route (replaced in app/api).
pages/api/Lark/mail/[address]/message.ts Removes legacy Pages API route (replaced in app/api).
pages/api/Lark/file/[id]/[name].ts Removes legacy Pages API route (replaced in app/api).
pages/api/Lark/document/markdown/[...slug].ts Removes legacy Pages API route (replaced in app/api).
pages/api/Lark/document/copy/[...slug].ts Removes legacy Pages API route (replaced in app/api).
pages/api/Lark/bitable/v1/[...slug].ts Removes legacy Pages API route (replaced in app/api).
pages/api/Lark/bitable/schema/[...slug].tsx Removes legacy Pages API route (replaced in app/api).
pages/api/GitHub/raw/[...slug].ts Removes legacy Pages API route (replaced in app/api).
pages/api/GitHub/core.ts Removes legacy proxy/OAuth helpers from Pages API.
pages/api/GitHub/[...slug].ts Removes legacy Pages API route (replaced in app/api).
pages/api/core.ts Removes legacy Koa/JWT safety middleware (replaced by lib/api/route-helper.ts).
models/Wiki.ts Updates Lark import to lib/lark.
models/open-library-books.ts Converts former API module into a pure data export.
lib/ssg.ts Fixes configuration import path after moving out of pages/api.
lib/lark.ts Moves Lark helpers out of pages/api into lib/.
lib/api/route-helper.ts Adds shared helpers for JWT verification, body parsing, header forwarding, and error-to-Response mapping.
app/api/signature/verification/route.ts New signature verification endpoint in App Router.
app/api/open-library/books/route.ts New open-library books endpoint in App Router.
app/api/Lark/mail/bot/message/route.ts New Lark mail bot endpoint in App Router.
app/api/Lark/file/[id]/[name]/route.ts New Lark file download/proxy endpoint in App Router.
app/api/Lark/document/markdown/[type]/[id]/route.ts New Lark markdown download endpoint in App Router.
app/api/Lark/document/copy/[type]/[id]/route.ts New Lark document copy endpoint in App Router.
app/api/Lark/bitable/v1/[...slug]/route.ts New Lark bitable proxy endpoint in App Router.
app/api/Lark/bitable/schema/[id]/route.ts New Lark bitable schema endpoint in App Router.
app/api/GitHub/raw/[...slug]/route.ts New GitHub raw proxy endpoint in App Router.
app/api/GitHub/[...slug]/route.ts New GitHub API proxy endpoint in App Router.
app/api/finance/index-funds/route.ts Migrates finance index-funds endpoint to App Router and updates query parsing.
.husky/pre-push Temporarily bypasses pre-push checks (currently always exits 0).
.husky/pre-commit Temporarily bypasses pre-commit checks (currently always exits 0).
Comments suppressed due to low confidence (1)

lib/lark.ts:5

  • lib/lark.ts imports koa as a runtime dependency even though Context/Middleware are only used as types. This can bloat server bundles (and can also unnecessarily couple route handlers to koa). Use a type-only import to avoid pulling koa into runtime code paths.

Comment thread .husky/pre-commit
Comment on lines +1 to +2
## Temporarily bypassed to ensure blocked commits can still be submitted for review.
exit 0
Comment thread .husky/pre-push
Comment on lines +1 to +2
## Temporarily bypassed to ensure blocked pushes can still be submitted for review.
exit 0
Comment thread lib/api/route-helper.ts
Comment on lines +14 to +20
export const verifyJWT = (request: NextRequest) => {
const token = request.cookies.get('token')?.value || cookieValueOf(request.headers.get('cookie') || '', 'token');

if (!token) throw new Error('JWT token is required');

return verify(token, LarkAppMeta.secret);
};
Comment thread lib/api/route-helper.ts
Comment on lines +67 to +76
if (error instanceof HTTPError) {
const body = await normalizedBodyOf(error);

console.error(JSON.stringify(body, null, 2));

return Response.json(body, {
status: error.response.status,
statusText: error.message,
});
}
@TechQuery TechQuery added the feature New feature or request label Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants