@api-buddy/core
v3.0.0
Published
Core functionality and plugin system for API Buddy
Readme
@api-buddy/plugin-core
Core plugin system for API Buddy, providing a robust foundation for building and managing plugins.
Features
- Plugin Lifecycle Management: Load, initialize, and unload plugins with proper lifecycle hooks
- Dependency Resolution: Handle plugin dependencies with automatic resolution
- Type Safety: Full TypeScript support with comprehensive type definitions
- Error Handling: Robust error handling and recovery options
- Hooks System: Extensible hook system for plugin communication
- Command Registration: Register and manage CLI commands from plugins
Installation
pnpm add @api-buddy/plugin-coreUsage
Basic Plugin Definition
import { ApiBuddyPlugin } from '@api-buddy/plugin-core';
const myPlugin: ApiBuddyPlugin = {
name: 'my-plugin',
version: '1.0.0',
description: 'My awesome plugin',
hooks: {
'before:load': async (context) => {
context.logger.info('Plugin is about to load');
},
'after:load': async (context) => {
context.logger.info('Plugin has been loaded');
},
'before:unload': async (context) => {
context.logger.info('Plugin is about to unload');
},
},
dependencies: ['other-plugin'],
metadata: {
// Any additional metadata
},
};
export default myPlugin;Using the Plugin Manager
import { PluginManager } from '@api-buddy/plugin-core';
import myPlugin from './my-plugin';
// Create a new plugin manager instance
const manager = new PluginManager({
// Optional context overrides
logger: {
info: console.log,
error: console.error,
debug: console.debug,
},
config: {
// Your configuration here
},
}, {
// Plugin manager options
hookTimeout: 5000, // Default timeout for hooks (ms)
continueOnError: false, // Whether to continue if a plugin fails to load
});
// Load a plugin
await manager.loadPlugin(myPlugin);
// Trigger a custom hook
await manager.triggerHook('custom:hook', { some: 'data' });
// Unload a plugin
await manager.unloadPlugin('my-plugin');Lifecycle Hooks
API Buddy plugins support the following lifecycle hooks:
before:load: Called before the plugin is loadedafter:load: Called after the plugin is loadedbefore:unload: Called before the plugin is unloaded
Error Handling
The plugin manager provides several options for error handling:
- hookTimeout: Automatically rejects hooks that take too long
- continueOnError: When true, the manager will continue loading other plugins if one fails
- Error Logging: All errors are logged through the provided logger
TypeScript Support
The package includes TypeScript type definitions for all public APIs.
License
MIT
