30 lines
705 B
TypeScript
30 lines
705 B
TypeScript
/**
|
|
* 将 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,
|
|
};
|
|
}
|