33 lines
1.3 KiB
TypeScript
33 lines
1.3 KiB
TypeScript
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 });
|
||
}
|
||
}
|