0.4.111 • Published 4 months ago

@api-buddy/plugin v0.4.111

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

@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

  1. Loading: Plugins are loaded from specified directories or directly
  2. Initialization: Each plugin's initialize method is called (if defined)
  3. Hooks: Plugins can register hooks for various lifecycle events
  4. Execution: Hooks are called at the appropriate times during execution
  5. Cleanup: When the application shuts down, each plugin's destroy method is called (if defined)

Available Hooks

  • beforeSchemaLoad: Called before the schema is loaded
  • afterSchemaLoad: Called after the schema is loaded
  • beforeCodegen: Called before code generation starts
  • afterCodegen: Called after code generation completes
  • beforeTypegen: Called before type generation starts
  • afterTypegen: Called after type generation completes
  • beforeMigrate: Called before database migration starts
  • afterMigrate: 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

0.4.111

4 months ago

0.4.109

4 months ago

0.4.108

4 months ago

0.4.107

4 months ago

0.4.106

4 months ago