@multitenantkit/adapter-transport-supabase-edge
v0.2.11
Published
Supabase Edge Functions transport adapter for MultiTenantKit HTTP API
Maintainers
Readme
@multitenantkit/adapter-transport-supabase-edge
Supabase Edge Functions transport adapter for MultiTenantKit HTTP API.
This adapter enables running MultiTenantKit API endpoints as a single Supabase Edge Function with internal routing (the "fat function" pattern).
Features
- Single Edge Function: All endpoints run in one function, minimizing cold starts
- Native Deno: Uses
Deno.serve()directly without additional frameworks - Full Compatibility: Works with all
@multitenantkit/api-handlersHandlerPackages - Built-in CORS: Configurable CORS handling for browser clients
- Authentication: Integrates with
@multitenantkit/adapter-auth-supabasefor JWT validation - Request Validation: Zod-based validation for body, params, and query
Installation
npm install @multitenantkit/adapter-transport-supabase-edgeUsage
Basic Setup
// supabase/functions/multitenantkit/index.ts
import { buildEdgeFunction } from '@multitenantkit/adapter-transport-supabase-edge';
import { buildHandlers } from '@multitenantkit/api-handlers';
import { createUseCases } from '@multitenantkit/composition';
import { SupabaseAuthService } from '@multitenantkit/adapter-auth-supabase';
// Initialize your adapters and use cases
const useCases = createUseCases(adapters);
const handlers = buildHandlers(useCases);
// Create auth service
const authService = new SupabaseAuthService({
supabaseUrl: Deno.env.get('SUPABASE_URL')!,
supabaseServiceKey: Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!
});
// Build the Edge Function handler
const handler = buildEdgeFunction(handlers, authService, {
basePath: '/multitenantkit',
cors: {
allowOrigin: '*',
allowHeaders: ['authorization', 'x-client-info', 'apikey', 'content-type'],
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS']
},
debug: true // Enable for development
});
// Start the server
Deno.serve(handler);Configuration Options
interface EdgeFunctionOptions {
/** Base path prefix for all routes (usually the function name) */
basePath: string;
/** CORS configuration */
cors?: {
allowOrigin: string | string[];
allowHeaders: string[];
allowMethods: string[];
maxAge?: number;
};
/** Enable debug logging */
debug?: boolean;
}Project Structure
For a Supabase project using this adapter:
supabase/
├── functions/
│ ├── import_map.json
│ ├── _shared/
│ │ └── multitenantkit.ts # Shared configuration
│ └── multitenantkit/
│ └── index.ts # Entry point
├── migrations/
└── config.tomlSupabase Config (config.toml)
[functions.multitenantkit]
verify_jwt = false # We handle JWT verification ourselvesAvailable Endpoints
Once deployed, the following endpoints are available (prefixed with your function path):
Users
POST /users- Create userGET /users/me- Get current userPATCH /users/me- Update current userDELETE /users/me- Delete current userGET /users/me/organizations- List user's organizations
Organizations
POST /organizations- Create organizationGET /organizations/:id- Get organizationPATCH /organizations/:id- Update organizationDELETE /organizations/:id- Delete organization
Memberships
POST /organizations/:organizationId/memberships- Add memberGET /organizations/:organizationId/memberships- List membersGET /organizations/:organizationId/memberships/:memberId- Get memberPATCH /organizations/:organizationId/memberships/:memberId- Update memberDELETE /organizations/:organizationId/memberships/:memberId- Remove member
Health
GET /health- Health check endpoint (built-in)
API Reference
buildEdgeFunction(handlers, authService, options)
Creates a Deno.serve() compatible handler function.
Parameters:
handlers- Array of HandlerPackages from@multitenantkit/api-handlersauthService- AuthService implementation (e.g., SupabaseAuthService)options- Configuration options
Returns: (request: Request) => Promise<Response>
EdgeRouter
Low-level router class for advanced use cases.
import { EdgeRouter } from '@multitenantkit/adapter-transport-supabase-edge';
const router = new EdgeRouter('/api', handlers);
const match = router.match('GET', '/api/users/me');Middleware Utilities
For custom implementations:
import {
authenticateRequest,
buildCorsHeaders,
handleCorsPreflightRequest,
getRequestId,
validateRequest
} from '@multitenantkit/adapter-transport-supabase-edge';Differences from Express Adapter
| Aspect | Express | Supabase Edge |
|--------|---------|---------------|
| Runtime | Node.js | Deno |
| Deploy | Manual | Automatic (Supabase) |
| Scaling | Manual | Automatic (edge) |
| CORS | cors middleware | Manual headers |
| Body parsing | express.json() | Native request.json() |
| Path params | Express router | Regex matching |
License
MIT
