compat: support more kosync server implementations, closes #1862 (#1929)

This commit is contained in:
Huang Xin
2025-08-30 10:54:31 +08:00
committed by GitHub
parent 856ed4d3b3
commit 6003eeadba
2 changed files with 7 additions and 5 deletions
+6 -5
View File
@@ -2,6 +2,8 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import { corsAllMethods, runMiddleware } from '@/utils/cors';
import { KoSyncProxyPayload } from '@/types/kosync';
const validEndpoints = [/\/users\/create/, /\/users\/auth/, /\/syncs\/progress/];
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
await runMiddleware(req, res, corsAllMethods);
@@ -21,6 +23,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return res.status(400).json({ error: 'serverUrl and endpoint are required' });
}
if (!validEndpoints.some((regex) => regex.test(endpoint))) {
return res.status(400).json({ error: 'Invalid endpoint' });
}
const targetUrl = `${serverUrl.replace(/\/$/, '')}${endpoint}`;
try {
@@ -34,11 +40,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
body: clientBody ? JSON.stringify(clientBody) : null,
});
const contentType = response.headers.get('content-type');
if (!contentType || !contentType.includes('application/json')) {
throw new Error('Invalid sync server response: Unexpected Content-Type.');
}
const data = await response.text();
res.status(response.status);
try {
@@ -53,6 +53,7 @@ export class KOSyncClient {
method,
headers: {
accept: 'application/vnd.koreader.v1+json',
...(method === 'GET' ? {} : { 'Content-Type': 'application/json' }),
...Object.fromEntries(headers.entries()),
},
body,