65 lines
1.7 KiB
JavaScript
65 lines
1.7 KiB
JavaScript
function assertRouter(api) {
|
|
if (!api || typeof api.get !== 'function') {
|
|
throw new Error('attachPortalPlazaDiscoveryRoutes requires an Express-compatible router');
|
|
}
|
|
}
|
|
|
|
function defaultSendData(res, _req, data) {
|
|
return res.json(data);
|
|
}
|
|
|
|
function defaultSendError(res, _req, status, code, message) {
|
|
return res.status(status).json({ error: { code, message } });
|
|
}
|
|
|
|
function defaultRouteError(_res, _req, error) {
|
|
throw error;
|
|
}
|
|
|
|
export function attachPortalPlazaDiscoveryRoutes(
|
|
api,
|
|
{
|
|
getPlazaPosts = () => null,
|
|
getPlazaSeo = () => null,
|
|
sendData = defaultSendData,
|
|
sendError = defaultSendError,
|
|
handleRouteError = defaultRouteError,
|
|
} = {},
|
|
) {
|
|
assertRouter(api);
|
|
|
|
api.get('/plaza/v1/categories', async (req, res) => {
|
|
const plazaPosts = getPlazaPosts();
|
|
if (!plazaPosts) {
|
|
sendError(res, req, 503, 'plaza_unavailable', 'Plaza 未启用');
|
|
return;
|
|
}
|
|
try {
|
|
return sendData(res, req, { categories: await plazaPosts.listCategories() });
|
|
} catch (error) {
|
|
return handleRouteError(res, req, error);
|
|
}
|
|
});
|
|
|
|
api.get('/plaza/v1/seo/sitemap', async (req, res) => {
|
|
const plazaPosts = getPlazaPosts();
|
|
if (!plazaPosts) {
|
|
sendError(res, req, 503, 'plaza_unavailable', 'Plaza 未启用');
|
|
return;
|
|
}
|
|
const plazaSeo = getPlazaSeo();
|
|
if (!plazaSeo) {
|
|
return sendError(res, req, 503, 'plaza_unavailable', 'Plaza SEO 未启用');
|
|
}
|
|
try {
|
|
const data = await plazaSeo.listSitemapData({
|
|
postLimit: req.query.post_limit,
|
|
userLimit: req.query.user_limit,
|
|
});
|
|
return sendData(res, req, data);
|
|
} catch (error) {
|
|
return handleRouteError(res, req, error);
|
|
}
|
|
});
|
|
}
|