0.4.111 • Published 4 months ago
@api-buddy/plugin-core v0.4.111
API Buddy Plugin Core
The plugin system allows extending API Buddy's functionality through a well-defined API. Plugins can hook into various lifecycle events and extend the core functionality.
Features
- Lifecycle Hooks: Plugins can subscribe to various lifecycle events
- Type Safety: Full TypeScript support with type definitions
- Plugin Discovery: Automatic loading from node_modules or custom directories
- Schema Extensions: Extend and modify the schema
- Type Generation: Customize TypeScript type generation
- Database Integration: Add support for different databases
- Flexible Loading: Support for both ESM and CommonJS modules
Installation
# Install the core plugin package
pnpm add @api-buddy/plugin-core
Usage
Basic Example
import { PluginManager, PluginHook } from '@api-buddy/plugin-core';
// Create a plugin manager
const pluginManager = new PluginManager({
debug: true,
cwd: process.cwd(), // Optional: specify the working directory
});
// Load a single plugin
await pluginManager.loadPlugin({
name: 'my-plugin',
version: '1.0.0',
hooks: {
[PluginHook.BeforeSchemaLoad]: [
async (context, schema) => {
console.log('Modifying schema...');
// Modify schema here
return schema;
}
]
}
});
// Or load multiple plugins from a directory
await pluginManager.loadPlugins('plugins'); // Load from ./plugins directory
// Or load from node_modules
await pluginManager.loadPlugins('node_modules', {
pattern: /^@my-org\/api-buddy-/ // Optional: filter by package name pattern
});
// Call a hook
await pluginManager.callHook(PluginHook.BeforeSchemaLoad, mySchema);
// Clean up
await pluginManager.destroy();
Available Hooks
Hook | Description | Parameters |
---|---|---|
BeforeSchemaLoad | Before schema is loaded | (context, schema) => Schema |
AfterSchemaLoad | After schema is loaded | (context, schema) => void |
BeforeCodegen | Before code generation | (context) => void |
AfterCodegen | After code generation | (context) => void |
BeforeTypegen | Before type generation | (context, schema) => void |
AfterTypegen | After type generation | (context, schema) => void |
BeforeMigrate | Before database migration | (context) => void |
AfterMigrate | After database migration | (context) => void |
Creating a Plugin
Create a plugin by implementing the Plugin
interface:
import { Plugin, PluginHook } from '@api-buddy/plugins';
const myPlugin: Plugin = {
name: 'my-plugin',
version: '1.0.0',
description: 'My awesome plugin',
hooks: {
[PluginHook.BeforeSchemaLoad]: [
async (context, schema) => {
// Modify schema here
return schema;
}
]
},
async initialize(context) {
console.log('Plugin initialized');
},
async destroy() {
console.log('Plugin destroyed');
}
};
export default myPlugin;
Plugin Extensions
Plugins can provide additional functionality through extensions:
Schema Extensions
const schemaPlugin: Plugin & SchemaPluginExtension = {
name: 'schema-plugin',
version: '1.0.0',
extendSchema(schema) {
// Add custom fields/types to the schema
return schema;
},
validateSchema(schema) {
// Validate the schema
return []; // Return array of error messages
}
};
Type Generation Extensions
const typegenPlugin: Plugin & TypegenPluginExtension = {
name: 'typegen-plugin',
version: '1.0.0',
getTypeMappings() {
return {
CustomType: 'string',
};
},
generateTypes(schema) {
// Generate custom type definitions
return '// Custom types';
}
};
License
MIT