refactor: 将Data到ViewModel的转换由App转移至Server端
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m15s
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m15s
- 将逻辑转移到Server端后,简化前端逻辑
This commit is contained in:
54
server/mappers/homepageMapper.ts
Normal file
54
server/mappers/homepageMapper.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import { isObject } from '../utils/object';
|
||||
|
||||
/**
|
||||
* 将 Directus 返回的 Homepage 数据转换为 HomepageView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 Homepage 数据
|
||||
* @returns 转换后的 HomepageView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toHomepageView(rawHomepage);
|
||||
*/
|
||||
export function toHomepageView(raw: Homepage): HomepageView {
|
||||
const carousel = (raw.carousel ?? [])
|
||||
.filter(isObject<HomepageFile>)
|
||||
.map((item) => item.directus_files_id)
|
||||
.filter(isObject<DirectusFile>)
|
||||
.map((file) => file.id);
|
||||
|
||||
const products = (raw.recommend_products ?? [])
|
||||
.filter(isObject<Product>)
|
||||
.map((item) => {
|
||||
const trans = item.translations?.[0];
|
||||
|
||||
const cover = isObject<DirectusFile>(item.cover) ? item.cover.id : '';
|
||||
|
||||
return {
|
||||
id: item.id,
|
||||
name: trans?.name ?? '',
|
||||
summary: trans?.summary ?? '',
|
||||
cover: cover,
|
||||
} satisfies HomepageProductView;
|
||||
});
|
||||
|
||||
const solutions = (raw.recommend_solutions ?? [])
|
||||
.filter(isObject<Solution>)
|
||||
.map((item) => {
|
||||
const trans = item.translations?.[0];
|
||||
const cover = isObject<DirectusFile>(item.cover) ? item.cover.id : '';
|
||||
|
||||
return {
|
||||
id: item.id,
|
||||
title: trans?.title ?? '',
|
||||
summary: trans?.summary ?? '',
|
||||
cover: cover,
|
||||
} satisfies HomepageSolutionView;
|
||||
});
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
carousel: carousel ?? [],
|
||||
recommendProducts: products ?? [],
|
||||
recommendSolutions: solutions ?? [],
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user