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

View File

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

View File

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

View File

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