Files
gitlab-instance-0a899031_salon/app/api/xiaohongshu/validate/route.ts
2026-03-18 00:53:28 -05:00

33 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { NextRequest, NextResponse } from "next/server";
/** 支持:主页 xiaohongshu.com/user/profile/xxx、xhslink.com/m/xxx;笔记 xhslink.com/o/xxx、xiaohongshu.com/explore/xxx */
const XH_PROFILE_PATTERN = /^https?:\/\/(www\.)?(xiaohongshu\.com|xhslink\.com)\/user\/profile\/[a-zA-Z0-9_-]+/i;
const XH_SHORT_PATTERN = /^https?:\/\/xhslink\.com\/m\/[a-zA-Z0-9_-]+/i;
const XH_NOTE_PATTERN = /^https?:\/\/(xhslink\.com\/o\/|(www\.)?xiaohongshu\.com\/explore\/)[a-zA-Z0-9_-]+/i;
function isValidUrl(url: string): boolean {
const s = url.trim();
if (!s) return false;
if (XH_PROFILE_PATTERN.test(s) || XH_SHORT_PATTERN.test(s) || XH_NOTE_PATTERN.test(s)) return true;
return false;
}
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const url = String(body?.url || body?.id || "").trim();
if (!url) {
return NextResponse.json({ valid: false, error: "请输入小红书链接" }, { status: 400 });
}
if (!isValidUrl(url)) {
return NextResponse.json({ valid: false, error: "请输入有效的主页链接或笔记链接(xhslink.com/m/xxx 或 xhslink.com/o/xxx)" }, { status: 200 });
}
return NextResponse.json({ valid: true, url });
} catch {
return NextResponse.json({ valid: false, error: "校验失败" }, { status: 500 });
}
}