- 空值处理与类型控制:为相关关系型字段的数据处理添加空值处理与类型控制 - 调整目录结构:将文件目录按照实际查询划分为Product和ProductList两个文件
55 lines
1.3 KiB
TypeScript
55 lines
1.3 KiB
TypeScript
import { isObject } from '../../server/utils/object';
|
|
|
|
/**
|
|
* 将Directus返回的ProductType数据转换为ProductTypeView视图模型
|
|
*
|
|
* @param raw: 原始的ProductType数据
|
|
* @returns 转换后的ProductTypeView对象
|
|
*
|
|
* @example
|
|
* const view = toProductTypeView(rawProductType);
|
|
*/
|
|
export function toProductTypeView(
|
|
raw: ProductType | string | null
|
|
): ProductTypeView {
|
|
if (typeof raw === 'string' || raw === null) {
|
|
return {
|
|
id: '-1',
|
|
name: '',
|
|
sort: 999,
|
|
} satisfies ProductTypeView;
|
|
}
|
|
const trans = raw.translations?.[0] ?? { name: '' };
|
|
|
|
return {
|
|
id: raw.id.toString(),
|
|
name: trans.name,
|
|
sort: raw?.sort ?? 999,
|
|
} satisfies ProductTypeView;
|
|
}
|
|
|
|
/**
|
|
* 将 Directus返回的 Product 数据转换为 ProductListView 视图模型
|
|
*
|
|
* @param raw: 原始的 Product 数据
|
|
* @returns 转换后的 ProductListView 对象
|
|
*
|
|
* @example
|
|
* const view = toProductListView(rawProduct);
|
|
*/
|
|
export function toProductListView(raw: Product): ProductListView {
|
|
const trans = raw.translations?.[0];
|
|
|
|
const type = toProductTypeView(raw.product_type ?? null);
|
|
|
|
const cover = isObject<DirectusFile>(raw.cover) ? raw.cover.id : '';
|
|
|
|
return {
|
|
id: raw.id.toString(),
|
|
product_type: type,
|
|
name: trans?.name ?? '',
|
|
summary: trans?.summary ?? '',
|
|
cover: cover,
|
|
} satisfies ProductListView;
|
|
}
|