fix(plugin-tools): add missing types and utils for the "loader" plugins

This commit is contained in:
Joakim Carlstein 2023-11-16 10:53:09 +01:00
parent a1debba2e1
commit 1799b6e399
3 changed files with 19 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'@emigrate/plugin-tools': patch
---
Add missing types and utility methods related to the new "loader" plugins

View file

@ -5,6 +5,7 @@ import {
type GeneratorPlugin, type GeneratorPlugin,
type StoragePlugin, type StoragePlugin,
type Plugin, type Plugin,
type LoaderPlugin,
} from './types.js'; } from './types.js';
export const isGeneratorPlugin = (plugin: any): plugin is GeneratorPlugin => { export const isGeneratorPlugin = (plugin: any): plugin is GeneratorPlugin => {
@ -23,6 +24,14 @@ export const isStoragePlugin = (plugin: any): plugin is StoragePlugin => {
return typeof plugin.initializeStorage === 'function'; return typeof plugin.initializeStorage === 'function';
}; };
export const isLoaderPlugin = (plugin: any): plugin is LoaderPlugin => {
if (!plugin || typeof plugin !== 'object') {
return false;
}
return typeof plugin.loadMigration === 'function' && Array.isArray(plugin.loadableExtensions);
};
export const isPluginOfType = <T extends PluginType>(type: T, plugin: any): plugin is PluginFromType<T> => { export const isPluginOfType = <T extends PluginType>(type: T, plugin: any): plugin is PluginFromType<T> => {
if (type === 'generator') { if (type === 'generator') {
return isGeneratorPlugin(plugin); return isGeneratorPlugin(plugin);
@ -32,6 +41,10 @@ export const isPluginOfType = <T extends PluginType>(type: T, plugin: any): plug
return isStoragePlugin(plugin); return isStoragePlugin(plugin);
} }
if (type === 'loader') {
return isLoaderPlugin(plugin);
}
throw new Error(`Unknown plugin type: ${type}`); throw new Error(`Unknown plugin type: ${type}`);
}; };

View file

@ -109,7 +109,7 @@ export type LoaderPlugin = {
loadMigration(migration: MigrationMetadata): Promise<MigrationFunction>; loadMigration(migration: MigrationMetadata): Promise<MigrationFunction>;
}; };
export type Plugin = StoragePlugin | GeneratorPlugin; export type Plugin = StoragePlugin | GeneratorPlugin | LoaderPlugin;
export type PluginType = 'storage' | 'generator' | 'loader'; export type PluginType = 'storage' | 'generator' | 'loader';