feat: use hono context json and text

This commit is contained in:
DIYgod 2024-03-01 20:50:17 +08:00
parent 1e1b726ed7
commit ba1bdf9bdc
No known key found for this signature in database
6 changed files with 9 additions and 17 deletions

View File

@ -41,8 +41,6 @@ export const errorHandler: ErrorHandler = (error, ctx) => {
},
});
} else {
ctx.header('Content-Type', 'text/html; charset=UTF-8');
if (error instanceof RequestInProgressError) {
ctx.header('Cache-Control', `public, max-age=${config.requestTimeout / 1000}`);
ctx.status(503);
@ -59,7 +57,7 @@ export const errorHandler: ErrorHandler = (error, ctx) => {
const requestPath = ctx.req.path;
return ctx.body(
return ctx.html(
art(path.resolve(__dirname, '../views/error.art'), {
requestPath,
message,

View File

@ -14,15 +14,12 @@ const middleware: MiddlewareHandler = async (ctx, next) => {
// only enable when debugInfo=true
if (config.debugInfo) {
if (outputType === 'debug.json') {
ctx.header('Content-Type', 'application/json; charset=UTF-8');
return ctx.body(ctx.get('json') ? JSON.stringify(ctx.get('json'), null, 4) : JSON.stringify({ message: 'plugin does not set debug json' }));
return ctx.json(ctx.get('json') || { message: 'plugin does not set debug json' });
}
if (/(\d+)\.debug\.html$/.test(outputType)) {
ctx.header('Content-Type', 'text/html; charset=UTF-8');
const index = Number.parseInt(outputType.match(/(\d+)\.debug\.html$/)?.[1] || '0');
return ctx.body(data?.item?.[index]?.description || `data.item[${index}].description not found`);
return ctx.text(data?.item?.[index]?.description || `data.item[${index}].description not found`);
}
}
@ -87,8 +84,7 @@ const middleware: MiddlewareHandler = async (ctx, next) => {
}
if (outputType === 'ums') {
ctx.header('Content-Type', 'application/json; charset=UTF-8');
return ctx.body(rss3Ums(result));
return ctx.json(rss3Ums(result));
} else if (outputType === 'json') {
ctx.header('Content-Type', 'application/feed+json; charset=UTF-8');
return ctx.body(json(result));

View File

@ -8,7 +8,6 @@ import { getDebugInfo } from '@/utils/debug-info';
const startTime = Date.now();
const handler: Handler = (ctx) => {
ctx.header('Content-Type', 'text/html; charset=UTF-8');
ctx.header('Cache-Control', 'no-cache');
const debug = getDebugInfo();
@ -18,7 +17,7 @@ const handler: Handler = (ctx) => {
const duration = Date.now() - startTime;
return ctx.body(
return ctx.html(
art(path.resolve(__dirname, '../views/welcome.art'), {
showDebug,
disallowRobot,

View File

@ -3,11 +3,10 @@ import { config } from '@/config';
const handler: Handler = (ctx) => {
if (config.disallowRobot) {
ctx.header('Content-Type', 'text/plain');
return ctx.body('User-agent: *\nDisallow: /');
return ctx.text('User-agent: *\nDisallow: /');
} else {
ctx.status(404);
return ctx.body('');
return ctx.text('');
}
};

View File

@ -42,7 +42,7 @@ const json = (data: Data) => {
_extra: item._extra || undefined,
})),
};
return JSON.stringify(jsonFeed, null, 4);
return JSON.stringify(jsonFeed);
};
export default json;

View File

@ -46,7 +46,7 @@ const rss3Ums = (data) => {
};
}),
};
return JSON.stringify(umsResult, null, 4);
return umsResult;
};
// we treat the domain as the owner of the content