feat: add truncate for post list

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-04-24 12:07:43 +08:00
parent 99ecd3cde6
commit 45e7cf83a8
4 changed files with 26 additions and 8 deletions

View File

@@ -53,9 +53,11 @@ export class HttpCacheInterceptor implements NestInterceptor {
}
const request = this.getRequest(context)
const res = context.switchToHttp().getResponse<FastifyReply>()
// 如果请求通过认证,跳过缓存因为,认证后的请求可能会有敏感数据
if (request.isAuthenticated) {
this.setPrivateCacheHeader(res)
return call$
}
@@ -85,8 +87,6 @@ export class HttpCacheInterceptor implements NestInterceptor {
const metaTTL = this.reflector.get(META.HTTP_CACHE_TTL_METADATA, handler)
const ttl = metaTTL || HTTP_CACHE.ttl
const res = context.switchToHttp().getResponse<FastifyReply>()
try {
const value = await this.cacheManager.get(key)
@@ -111,6 +111,14 @@ export class HttpCacheInterceptor implements NestInterceptor {
}
}
setPrivateCacheHeader(res: FastifyReply) {
if (res.raw.statusCode !== 200) return
const cacheValue = 'private, max-age=0, no-cache, no-store, must-revalidate'
res.header('cdn-cache-control', cacheValue)
res.header('cache-control', cacheValue)
res.header('cloudflare-cdn-cache-control', cacheValue)
}
setCacheHeader(res: FastifyReply, ttl: number) {
if (res.raw.statusCode !== 200) return
if (HTTP_CACHE.enableCDNHeader) {

View File

@@ -19,10 +19,9 @@ import { IpLocation, IpRecord } from '~/common/decorators/ip.decorator'
import { CannotFindException } from '~/common/exceptions/cant-find.exception'
import { CountingService } from '~/processors/helper/helper.counting.service'
import { MongoIdDto } from '~/shared/dto/id.dto'
import { PagerDto } from '~/shared/dto/pager.dto'
import { addYearCondition } from '~/transformers/db-query.transformer'
import { CategoryAndSlugDto } from './post.dto'
import { CategoryAndSlugDto, PostPagerDto } from './post.dto'
import { PartialPostModel, PostModel } from './post.model'
import { PostService } from './post.service'
@@ -35,8 +34,8 @@ export class PostController {
@Get('/')
@Paginator
async getPaginate(@Query() query: PagerDto) {
const { size, select, page, year, sortBy, sortOrder } = query
async getPaginate(@Query() query: PostPagerDto) {
const { size, select, page, year, sortBy, sortOrder, truncate } = query
return this.postService.model
.aggregatePaginate(
@@ -112,6 +111,7 @@ export class PostController {
)
.then((res) => {
res.docs = res.docs.map((doc: PostModel) => {
doc.text = truncate ? doc.text.slice(0, truncate) : doc.text
if (doc.meta && typeof doc.meta === 'string') {
doc.meta = JSON.safeParse(doc.meta as string) || doc.meta
}

View File

@@ -1,5 +1,7 @@
import { Transform } from 'class-transformer'
import { IsString } from 'class-validator'
import { Transform, Type } from 'class-transformer'
import { IsInt, IsOptional, IsString } from 'class-validator'
import { PagerDto } from '~/shared/dto/pager.dto'
export class CategoryAndSlugDto {
@IsString()
@@ -9,3 +11,10 @@ export class CategoryAndSlugDto {
@Transform(({ value: v }) => decodeURI(v))
readonly slug: string
}
export class PostPagerDto extends PagerDto {
@IsOptional()
@IsInt()
@Type(() => Number)
truncate?: number
}

View File

@@ -22,6 +22,7 @@ export type PostListOptions = {
year?: number
sortBy?: 'categoryId' | 'title' | 'created' | 'modified'
sortOrder?: 1 | -1
truncate?: number
}
export class PostController<ResponseWrapper> implements IController {