Select Git revision

Petar Hristov authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
tree.ts 1.93 KiB
import { MapperConfiguration, MappingPair } from "@dynamic-mapper/mapper";
import { v4 as uuidv4 } from "uuid";
import type {
TreeDataType,
FileTreeDto,
} from "@coscine/api-client/dist/types/Coscine.Api";
import type {
FileInformation,
FolderInformation,
} from "@/modules/resource/types";
export const TreeDto2FileInformation = new MappingPair<
FileTreeDto,
FileInformation
>();
export const TreeDto2FolderInformation = new MappingPair<
FileTreeDto,
FolderInformation
>();
const configuration = new MapperConfiguration((cfg) => {
cfg.createMap(TreeDto2FileInformation, {
id: (opt) => opt.mapFrom((_) => uuidv4()),
name: (opt) => opt.mapFrom((dto) => dto.name ?? ""),
parentDirectory: (opt) => opt.mapFrom((dto) => dto.directory ?? ""),
path: (opt) => opt.mapFrom((dto) => dto.path ?? ""),
type: (opt) => opt.mapFrom((_) => "Leaf" as TreeDataType.Leaf),
isFolder: (opt) => opt.mapFrom((_) => false),
createdAt: (opt) => opt.mapFrom((dto) => dto.creationDate ?? undefined),
lastModified: (opt) => opt.mapFrom((dto) => dto.changeDate),
size: (opt) => opt.mapFrom((dto) => dto.size ?? 0),
version: (opt) => opt.mapFrom((_) => `${+new Date()}`),
metadata: (opt) => opt.mapFrom((_) => null), // Set outside of the mapper
});
cfg.createMap(TreeDto2FolderInformation, {
id: (opt) => opt.mapFrom((_) => uuidv4()),
name: (opt) => opt.mapFrom((dto) => dto.name ?? ""),
parentDirectory: (opt) => opt.mapFrom((dto) => dto.directory ?? ""),
path: (opt) => opt.mapFrom((dto) => dto.path ?? ""),
type: (opt) => opt.mapFrom((_) => "Tree" as TreeDataType.Tree),
isFolder: (opt) => opt.mapFrom((_) => true),
createdAt: (opt) => opt.mapFrom((dto) => dto.creationDate ?? undefined),
lastModified: (opt) => opt.mapFrom((dto) => dto.changeDate),
metadata: (opt) => opt.mapFrom((_) => null), // Set outside of the mapper
});
});
export const treeMapper = configuration.createMapper();