Integration API
Integration types
Section titled “Integration types”There are two ways to create integrations in Gaman:
1. Direct Integration
Section titled “1. Direct Integration”For simple integrations without configuration options:
const myIntegration = defineIntegration((app) => ({ name: "my-integration", priority: "normal",}));
// Usageintegrations: [myIntegration];
2. Factory Function Integration
Section titled “2. Factory Function Integration”For integrations that need customizable options:
export function myIntegration(options: MyOptions) { return defineIntegration((app) => ({ name: "my-integration", priority: "normal", }));}
// Usageintegrations: [myIntegration({ option: "value" })];
Integration Structure
Section titled “Integration Structure”An integration is defined using the IntegrationFactory
interface which includes the following properties:
- name (required): The unique identifier for your integration
- priority (required): Determines the execution order of integrations
- onLoad (optional): Called when the integration is initialized
- onDisabled (optional): Called when the integration is disabled
- onRequest (optional): Called on each incoming request
- onResponse (optional): Called before sending response to client
Priority Levels
Section titled “Priority Levels”The priority system determines the order in which integrations are executed:
high
- Executes firstnormal
- Standard execution orderlow
- Executes last
Creating an Integration
Section titled “Creating an Integration”Basic Integration
Section titled “Basic Integration”Create a new file with the .integration.ts
suffix:
export default defineIntegration((app) => ({ name: "example-integration", priority: "normal", onLoad: () => { console.log("Integration loaded!"); }, onRequest: (ctx) => { // Handle incoming requests console.log("Request received:", ctx.request.url); }, onResponse: (ctx, res) => { // Modify response before sending console.log("Response being sent"); return res; },}));
Integration with Configuration Options
Section titled “Integration with Configuration Options”For integrations that need customizable options:
interface AuthOptions { secret?: string; enabled?: boolean; protectedRoutes?: string[];}
export function authIntegration(options: AuthOptions = {}) { return defineIntegration((app) => ({ name: "auth-middleware", priority: "high", onLoad: () => { // Initialize authentication system with options app.config.auth = { enabled: options.enabled ?? true, secret: options.secret ?? process.env.JWT_SECRET, protectedRoutes: options.protectedRoutes ?? ["/api/protected"], }; }, onRequest: (ctx) => { const token = ctx.request.headers.get("Authorization"); const isProtected = app.config.auth.protectedRoutes.some((route) => ctx.request.url.includes(route) );
if (!token && isProtected) { return new Response("Unauthorized", { status: 401 }); } }, onDisabled: () => { delete app.config.auth; }, }));}
Using Integrations
Section titled “Using Integrations”Registering Integrations
Section titled “Registering Integrations”In your main application file (index.ts
), register your integrations:
defineBootstrap(..., (app) => { app.registerIntegration( authIntegration({ secret: "my-secret", protectedRoutes: ["/api/admin", "/api/user"], }), exampleIntegration );})
Multiple Integrations
Section titled “Multiple Integrations”You can register multiple integrations, and they will execute based on their priority:
defineBootstrap(..., (app) => { app.registerIntegration( corsIntegration(), // with options rateLimitIntegration({ maxRequests: 100 }), // with options loggingIntegration, // direct integration )})
Integration Lifecycle
Section titled “Integration Lifecycle”Execution Order
Section titled “Execution Order”- onLoad: Called during application startup (ordered by priority)
- onRequest: Called for each incoming request (ordered by priority)
- onResponse: Called before sending response (ordered by priority)
- onDisabled: Called when integration is disabled
Context Access
Section titled “Context Access”Integrations receive the application context and request context, allowing you to:
- Access application configuration
- Modify request/response data
- Share data between integrations
- Implement middleware logic
Best Practices
Section titled “Best Practices”Naming Convention
Section titled “Naming Convention”- Use descriptive names with kebab-case:
auth-middleware
,rate-limiter
- Add
.integration.ts
suffix to integration files - Keep integration names unique across your application
This documentation provides a complete guide for creating and using integrations in the Gaman framework. The modular approach allows for flexible and maintainable application architecture.