1
0
mirror of https://github.com/koloml/furbooru-tagging-assistant.git synced 2025-12-24 07:12:57 +00:00

Converting the exporters to TypeScript, fixing type errors

This commit is contained in:
2024-11-28 00:45:41 +04:00
parent c7f40e99b7
commit b3a92653ad
5 changed files with 30 additions and 32 deletions

View File

@@ -1,5 +1,5 @@
import {validateImportedEntity} from "$lib/extension/transporting/validators.js";
import {exportEntityToObject} from "$lib/extension/transporting/exporters.js";
import {exportEntityToObject} from "$lib/extension/transporting/exporters.ts";
import StorageEntity from "$lib/extension/base/StorageEntity.ts";
import {compressToEncodedURIComponent, decompressFromEncodedURIComponent} from "lz-string";
@@ -55,6 +55,10 @@ export default class EntitiesTransporter<EntityType> {
throw new TypeError('Transporter should be connected to the same entity to export!');
}
if (!(entityObject instanceof StorageEntity)) {
throw new TypeError('Only storage entities could be exported!');
}
const exportableObject = exportEntityToObject(
entityObject,
this.#entityName

View File

@@ -1,26 +0,0 @@
/**
* @type {Map<string, ((entity: import('../base/StorageEntity.ts').default) => Record<string, any>)>}
*/
const entitiesExporters = new Map([
['profiles', /** @param {import('../entities/MaintenanceProfile.ts').default} entity */entity => {
return {
v: 1,
id: entity.id,
name: entity.settings.name,
tags: entity.settings.tags,
}
}]
])
/**
* @param entityInstance
* @param {string} entityName
* @returns {Record<string, *>}
*/
export function exportEntityToObject(entityInstance, entityName) {
if (!entitiesExporters.has(entityName)) {
throw new Error(`Missing exporter for entity: ${entityName}`);
}
return entitiesExporters.get(entityName).call(null, entityInstance);
}

View File

@@ -0,0 +1,24 @@
import StorageEntity from "$lib/extension/base/StorageEntity.ts";
type ExportersMap = {
[EntityName in keyof App.EntityNamesMap]: (entity: App.EntityNamesMap[EntityName]) => Record<string, any>
};
const entitiesExporters: ExportersMap = {
profiles: entity => {
return {
v: 1,
id: entity.id,
name: entity.settings.name,
tags: entity.settings.tags,
}
},
};
export function exportEntityToObject(entityInstance: StorageEntity<any>, entityName: string): Record<string, any> {
if (!(entityName in entitiesExporters) || !entitiesExporters.hasOwnProperty(entityName)) {
throw new Error(`Missing exporter for entity: ${entityName}`);
}
return entitiesExporters[entityName as keyof App.EntityNamesMap].call(null, entityInstance);
}

View File

@@ -1,6 +1,6 @@
/**
* Map of validators for each entity. Function should throw the error if validation failed.
* @type {Map<string, ((importedObject: Object) => void)>}
* @type {Map<keyof App.EntityNamesMap|string, ((importedObject: Object) => void)>}
*/
const entitiesValidators = new Map([
['profiles', importedObject => {