@api-buddy/types
Version:
Shared types for API Buddy
166 lines • 5.1 kB
TypeScript
import type { Logger } from './logger.js';
import type { Schema } from './types.js';
export { fieldTypeSchema } from './types.js';
export type { Schema };
export declare enum PluginHook {
BeforeSchemaLoad = "beforeSchemaLoad",
AfterSchemaLoad = "afterSchemaLoad",
BeforeCodegen = "beforeCodegen",
AfterCodegen = "afterCodegen",
BeforeTypegen = "beforeTypegen",
AfterTypegen = "afterTypegen",
BeforeMigrate = "beforeMigrate",
AfterMigrate = "afterMigrate"
}
export interface PluginContext {
/** Current working directory */
cwd: string;
/** Plugin configuration */
config: Record<string, unknown>;
/** Logger instance */
logger: Logger;
/** Plugin data storage */
data: Map<string, unknown>;
/** Plugin utilities */
utils: {
/** Register a hook */
registerHook: (hookName: string, handler: (...args: any[]) => any) => void;
/** Unregister a hook */
unregisterHook: (hookName: string, handler: (...args: any[]) => any) => void;
/** Call a hook */
callHook: (hookName: string, ...args: any[]) => Promise<any>;
};
}
export interface Plugin {
/** Plugin name (must be unique) */
name: string;
/** Plugin version */
version: string;
/** Plugin description */
description?: string;
/**
* Initialize the plugin
* Called when the plugin is loaded
* @param context - The plugin context containing logger, config, etc.
*/
initialize?(context: PluginContext): Promise<void> | void;
/**
* Cleanup function
* Called when the plugin is unloaded
*/
destroy?(): Promise<void> | void;
/**
* Optional cleanup function (alias for destroy)
* Called when the plugin is unregistered
*/
cleanup?(context: PluginContext): Promise<void> | void;
/**
* Plugin hooks
* Keys are hook names, values are arrays of handler functions
*/
hooks?: {
[K in PluginHook]?: Array<(context: PluginContext, ...args: any[]) => Promise<void> | void>;
};
/**
* Plugin configuration schema (JSON Schema)
* Used to validate plugin configuration
*/
configSchema?: Record<string, unknown>;
}
/**
* Base interface for plugin manager options
*/
export interface PluginManagerOptions {
/** Current working directory */
cwd?: string;
/** Logger instance */
logger?: Partial<Logger>;
/** Enable debug mode */
debug?: boolean;
/** Plugin configurations */
plugins?: Record<string, unknown>;
/** Directories to search for plugins */
pluginsDir?: string | string[];
/** Node modules directories to search for plugins */
nodeModulesDirs?: string[];
/** Configuration object */
config?: Record<string, unknown>;
/** Callback when a plugin is loaded */
onPluginLoaded?: (plugin: Plugin) => void;
/** Error handler */
onError?: (error: Error, plugin?: string, phase?: string) => void;
}
/**
* A simple plugin manager that handles loading and managing plugins.
* Can be extended to add custom plugin functionality.
*/
export declare class SimplePluginManager {
/** Map of loaded plugins */
protected plugins: Map<string, Plugin>;
/** Plugin context */
protected context: PluginContext;
/** Plugin manager options */
protected options: {
pluginsDir: string[];
nodeModulesDirs: string[];
};
/** Default logger implementation */
private defaultLogger;
constructor(options?: PluginManagerOptions);
/**
* Register a plugin
*/
registerPlugin(plugin: Plugin): Promise<void>;
/**
* Get a registered plugin by name
*/
getPlugin<T extends Plugin = Plugin>(name: string): T | undefined;
/**
* Get all registered plugins
*/
getPlugins(): Plugin[];
/**
* Check if a plugin is registered
*/
hasPlugin(name: string): boolean;
/**
* Unregister a plugin
*/
unregisterPlugin(name: string): Promise<boolean>;
/**
* Load plugins from the specified directories
*/
loadPlugins(): Promise<void>;
}
export interface SchemaPluginExtension {
/**
* Extend the schema with custom fields and types
*/
extendSchema?(schema: Schema): Promise<Schema> | Schema;
/**
* Validate the schema
* Return an array of error messages if validation fails
*/
validateSchema?(schema: Schema): Promise<string[]> | string[];
}
export interface TypegenPluginExtension {
/**
* Custom type mappings for the Type Generator
*/
getTypeMappings?(): Record<string, string>;
/**
* Generate custom type definitions
*/
generateTypes?(schema: Schema): Promise<string> | string;
}
export interface DatabasePluginExtension {
/**
* Generate database schema
*/
generateSchema?(schema: Schema): Promise<string> | string;
/**
* Generate migration files
*/
generateMigrations?(oldSchema: Schema | null, newSchema: Schema): Promise<string[]> | string[];
}
//# sourceMappingURL=plugin-types.d.ts.map