Adapter Plugins Refactoring
This document summarizes the refactoring work done to implement adapters as true plugins in the secure_project library.
Changes Madeโ
-
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
- Defined
-
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
-
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
-
Documentation
- Created comprehensive documentation for the adapter plugin system
- Added example usage for all supported frameworks
- Documented the adapter lifecycle and API
-
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:
- Create a new file in
src/plugins/adapters/
for your adapter - Implement the
AdapterPlugin
interface - Export the adapter class and a factory function
- Add the adapter to
src/plugins/adapters/index.ts
Future Improvementsโ
- Dynamic Loading: Enhance the
AdapterManager
to support dynamic loading of adapters from npm packages - Adapter Configuration: Add more comprehensive configuration options for adapters
- Adapter Events: Implement an event system for adapters to communicate with each other
- More Adapters: Add support for more frameworks like Hapi, Next.js, etc.
- 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.