feat: directus视图与转换函数
- views: 用于前端渲染的视图模型 - mapper: 用于视图模型转换的转换函数 - utils: 相关工具函数
This commit is contained in:
29
app/models/mappers/documentMapper.ts
Normal file
29
app/models/mappers/documentMapper.ts
Normal file
@ -0,0 +1,29 @@
|
||||
/**
|
||||
* 将 Directus 返回的 Document 数据转换为 DocumentView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 Document 数据
|
||||
* @returns 转换后的 DocumentView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toDocumentView(rawDocument);
|
||||
*/
|
||||
export function toDocumentView(raw: ProductDocument): ProductDocumentView {
|
||||
const trans = raw.translations?.[0] ?? {
|
||||
title: '',
|
||||
};
|
||||
|
||||
const fileId = typeof raw.file === 'string' ? raw.file : raw.file?.id;
|
||||
const file = raw.file as DirectusFile;
|
||||
|
||||
const { getFileUrl } = useDirectusFiles();
|
||||
|
||||
const url = getFileUrl(fileId);
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
filename: file.filename_download,
|
||||
title: trans.title,
|
||||
url: url,
|
||||
size: file.filesize,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user