0.4.111 • Published 4 months ago
@api-buddy/plugin v0.4.111
@api-buddy/plugin
Core plugin system for API Buddy, providing a flexible and extensible way to add functionality to the API Buddy ecosystem.
Features
- Plugin Management: Load, initialize, and manage plugins
- Lifecycle Hooks: Built-in hooks for different stages of the application lifecycle
- Type Safety: Fully typed plugin interfaces and extensions
- Multiple Plugin Types: Support for schema, type generation, and database plugins
- Singleton Pattern: Global plugin manager instance for easy access
Installation
npm install @api-buddy/plugin
# or
yarn add @api-buddy/plugin
Usage
Basic Plugin Creation
import { createPlugin, pluginManager } from '@api-buddy/plugin';
// Create a simple plugin
const myPlugin = createPlugin({
name: 'my-plugin',
version: '1.0.0',
description: 'My custom plugin',
hooks: {
beforeSchemaLoad: async (context) => {
console.log('Before schema load hook called');
},
},
});
// Load the plugin
await pluginManager.loadPlugin(myPlugin);
Schema Plugin Example
import { createSchemaPlugin, pluginManager } from '@api-buddy/plugin';
const schemaPlugin = createSchemaPlugin('my-schema-plugin', {
name: 'my-schema-plugin',
version: '1.0.0',
extendSchema: async (schema) => {
// Add custom fields or types to the schema
return {
...schema,
// Add your schema extensions here
};
},
validateSchema: async (schema) => {
const errors: string[] = [];
// Add validation logic here
return errors;
},
});
await pluginManager.loadPlugin(schemaPlugin);
Type Generation Plugin Example
import { createTypegenPlugin, pluginManager } from '@api-buddy/plugin';
const typegenPlugin = createTypegenPlugin('my-typegen-plugin', {
name: 'my-typegen-plugin',
version: '1.0.0',
getTypeMappings: () => ({
// Map custom scalar types to TypeScript types
'CustomScalar': 'string',
}),
generateTypes: async (schema) => {
// Generate custom type definitions
return '// Custom types';
},
generateImports: async () => [
"import { CustomType } from './custom-types';",
],
});
await pluginManager.loadPlugin(typegenPlugin);
Plugin Lifecycle
- Loading: Plugins are loaded from specified directories or directly
- Initialization: Each plugin's
initialize
method is called (if defined) - Hooks: Plugins can register hooks for various lifecycle events
- Execution: Hooks are called at the appropriate times during execution
- Cleanup: When the application shuts down, each plugin's
destroy
method is called (if defined)
Available Hooks
beforeSchemaLoad
: Called before the schema is loadedafterSchemaLoad
: Called after the schema is loadedbeforeCodegen
: Called before code generation startsafterCodegen
: Called after code generation completesbeforeTypegen
: Called before type generation startsafterTypegen
: Called after type generation completesbeforeMigrate
: Called before database migration startsafterMigrate
: Called after database migration completes
Plugin Types
Schema Plugins
Extend or validate the schema definition.
Type Generation Plugins
Customize TypeScript type generation.
Database Plugins
Handle database schema generation and migrations.
License
MIT