1
0
mirror of https://github.com/koloml/furbooru-tagging-assistant.git synced 2025-12-23 23:02:58 +00:00

Introducing lists of importable entities, renaming some interfaces

This commit is contained in:
2025-02-19 03:42:20 +04:00
parent c93c3c7bd5
commit 5dc41700b8
3 changed files with 25 additions and 9 deletions

View File

@@ -1,7 +1,7 @@
import StorageEntity from "$lib/extension/base/StorageEntity";
import type { ImportableObject } from "$lib/extension/transporting/importables";
import type { ImportableEntityObject } from "$lib/extension/transporting/importables";
type ExporterFunction<EntityType extends StorageEntity> = (entity: EntityType) => ImportableObject<EntityType>;
type ExporterFunction<EntityType extends StorageEntity> = (entity: EntityType) => ImportableEntityObject<EntityType>;
type ExportersMap = {
[EntityName in keyof App.EntityNamesMap]: ExporterFunction<App.EntityNamesMap[EntityName]>;
@@ -10,7 +10,8 @@ type ExportersMap = {
const entitiesExporters: ExportersMap = {
profiles: entity => {
return {
v: 1,
$type: "profiles",
v: 2,
id: entity.id,
name: entity.settings.name,
tags: entity.settings.tags,
@@ -20,7 +21,8 @@ const entitiesExporters: ExportersMap = {
},
groups: entity => {
return {
v: 1,
$type: "groups",
v: 2,
id: entity.id,
name: entity.settings.name,
tags: entity.settings.tags,
@@ -33,7 +35,7 @@ const entitiesExporters: ExportersMap = {
export function exportEntityToObject<EntityName extends keyof App.EntityNamesMap>(
entityName: EntityName,
entityInstance: App.EntityNamesMap[EntityName]
): ImportableObject<App.EntityNamesMap[EntityName]> {
): ImportableEntityObject<App.EntityNamesMap[EntityName]> {
if (!(entityName in entitiesExporters) || !entitiesExporters.hasOwnProperty(entityName)) {
throw new Error(`Missing exporter for entity: ${entityName}`);
}

View File

@@ -1,9 +1,23 @@
import type StorageEntity from "$lib/extension/base/StorageEntity";
export interface ImportableElement<Type extends string = string> {
/**
* Type of importable. Should be unique to properly import everything.
*/
$type: Type;
}
export interface ImportableElementsList<ElementsType extends ImportableElement = ImportableElement> extends ImportableElement<"list"> {
/**
* List of elements inside. Elements could be of any type and should be checked and mapped.
*/
elements: ElementsType[];
}
/**
* Base information on the object which should be present on every entity.
*/
export interface BaseImportableObject {
export interface BaseImportableEntity extends ImportableElement<keyof App.EntityNamesMap> {
/**
* Numeric version of the entity for upgrading.
*/
@@ -18,5 +32,5 @@ export interface BaseImportableObject {
* Utility type which combines base importable object and the entity type interfaces together. It strips away any types
* defined for the properties, since imported object can not be trusted and should be type-checked by the validators.
*/
export type ImportableObject<EntityType extends StorageEntity> = { [ObjectKey in keyof BaseImportableObject]: any }
export type ImportableEntityObject<EntityType extends StorageEntity> = { [ObjectKey in keyof BaseImportableEntity]: any }
& { [SettingKey in keyof EntityType["settings"]]: any };

View File

@@ -1,12 +1,12 @@
import type StorageEntity from "$lib/extension/base/StorageEntity";
import type { ImportableObject } from "$lib/extension/transporting/importables";
import type { ImportableEntityObject } from "$lib/extension/transporting/importables";
/**
* Function for validating the entities.
* @todo Probably would be better to replace the throw-catch method with some kind of result-error returning type.
* Errors are only properly definable in the JSDoc.
*/
type ValidationFunction<EntityType extends StorageEntity> = (importedObject: ImportableObject<EntityType>) => void;
type ValidationFunction<EntityType extends StorageEntity> = (importedObject: ImportableEntityObject<EntityType>) => void;
/**
* Mapping of validation functions for all entities present in the extension. Key is a name of entity and value is a