feat: strict data type

This commit is contained in:
DIYgod
2024-01-21 18:36:46 +08:00
parent 4591116733
commit 42b6b1c724
4 changed files with 8 additions and 4 deletions

View File

@@ -4,6 +4,7 @@ import logger from '@/utils/logger';
import * as path from 'node:path';
import render from '@/utils/render';
import { type MiddlewareHandler } from 'hono';
import { Data } from '@/types';
const templateRegex = /\${([^{}]+)}/g;
const allowedUrlProperties = new Set(['hash', 'host', 'hostname', 'href', 'origin', 'password', 'pathname', 'port', 'protocol', 'search', 'searchParams', 'username']);
@@ -130,7 +131,7 @@ const middleware: MiddlewareHandler = async (ctx, next) => {
// and here we will only check them in description.
// Use Cheerio to load the description as html and filter all
// image link
const data = ctx.get('data');
const data: Data = ctx.get('data');
if (data) {
if (data.description) {
data.description = process(data.description, image_hotlink_template, multimedia_hotlink_template, shouldWrapInIframe);

View File

@@ -4,6 +4,7 @@ import type { MiddlewareHandler } from 'hono';
import { config } from '@/config';
import { RequestInProgressError } from '@/errors';
import cacheModule from '@/utils/cache/index'
import { Data } from '@/types';
// only give cache string, as the `!` condition tricky
// md5 is used to shrink key size
@@ -48,7 +49,7 @@ const middleware: MiddlewareHandler = async (ctx, next) => {
throw error;
}
const data = ctx.get('data')
const data: Data = ctx.get('data');
if (ctx.res.headers.get('Cache-Control') !== 'no-cache' && data) {
data.lastBuildDate = new Date().toUTCString();
ctx.set('data', data)

View File

@@ -3,6 +3,7 @@ import { MiddlewareHandler } from "hono";
import etagCalculate from "etag";
import logger from "@/utils/logger";
import { config } from "@/config";
import { Data } from "@/types";
const headers: Record<string, string> = {
'Access-Control-Allow-Methods': 'GET',
@@ -29,7 +30,7 @@ const middleware: MiddlewareHandler = async (ctx, next) => {
await next();
const data = ctx.get('data');
const data: Data = ctx.get('data');
if (!data || ctx.res.headers.get('ETag')) {
return;
}

View File

@@ -41,4 +41,5 @@ export type Data = {
author?: string;
language?: string;
feedLink?: string;
lastBuildDate?: string;
};