Skip to main content

Adapter Plugins Refactoring

This document summarizes the refactoring work done to implement adapters as true plugins in the secure_project library.

Changes Madeโ€‹

  1. Created a Plugin Architecture

    • Defined AdapterPlugin interface to standardize adapter implementations
    • Implemented AdapterManager to handle registration and management of plugins
    • Created a unified context model with AdapterContext interface
  2. Migrated Existing Adapters

    • Reimplemented Express, Koa, and Drupal adapters using the new plugin architecture
    • Maintained backward compatibility with existing API
    • Enhanced adapters with more consistent interfaces and better error handling
  3. Added New Adapters

    • Implemented Fastify adapter for the Fastify web framework
    • Implemented NestJS adapter for the NestJS framework
    • Each adapter follows the same interface, making it easy to add more in the future
  4. Documentation

    • Created comprehensive documentation for the adapter plugin system
    • Added example usage for all supported frameworks
    • Documented the adapter lifecycle and API
  5. Test Coverage

    • Implemented TDD tests for the adapter plugin system
    • Ensured adapter loading and middleware execution is properly tested

Directory Structureโ€‹

The new directory structure for adapters is:

src/
plugins/
AdapterPlugin.ts # Interface definition
AdapterManager.ts # Plugin manager
adapters/
index.ts # Exports all adapters
ExpressAdapter.ts # Express framework adapter
KoaAdapter.ts # Koa framework adapter
DrupalAdapter.ts # Drupal CMS adapter
FastifyAdapter.ts # Fastify framework adapter
NestJSAdapter.ts # NestJS framework adapter
adapters/ # Legacy adapters (kept for backward compatibility)
express.ts
koa.ts
drupal.ts

Usage Exampleโ€‹

Here's a simple example of using the new adapter plugin system:

import { AdapterManager, ExpressAdapter } from 'secure_project';
import express from 'express';

// Create adapter manager
const adapterManager = new AdapterManager();

// Register Express adapter
await adapterManager.registerAdapter(new ExpressAdapter());

// Create middleware
const expressMiddleware = adapterManager.createMiddleware('express', {
audit: true,
accessControl: true
});

// Use with Express
const app = express();
app.use(expressMiddleware);

Adding New Adaptersโ€‹

To add a new adapter for another framework, follow these steps:

  1. Create a new file in src/plugins/adapters/ for your adapter
  2. Implement the AdapterPlugin interface
  3. Export the adapter class and a factory function
  4. Add the adapter to src/plugins/adapters/index.ts

Future Improvementsโ€‹

  1. Dynamic Loading: Enhance the AdapterManager to support dynamic loading of adapters from npm packages
  2. Adapter Configuration: Add more comprehensive configuration options for adapters
  3. Adapter Events: Implement an event system for adapters to communicate with each other
  4. More Adapters: Add support for more frameworks like Hapi, Next.js, etc.
  5. Adapter Versioning: Add versioning support for adapters to ensure compatibility

Conclusionโ€‹

The new adapter plugin system makes it easy to integrate secure_project with various frameworks and platforms. It provides a standardized way to create and use adapters, making the library more flexible and extensible.