UNPKG

@mulutime/plugin-sdk

Version:

SDK for developing MuluTime booking platform plugins

131 lines 4.15 kB
import { APIAccessScope, PluginContext } from '@mulutime/plugin-types'; import { APIAction, APIRequest, APIResponse } from '../types'; export interface APIHandlerOptions { enableValidation?: boolean; enablePermissionCheck?: boolean; timeout?: number; cors?: { enabled: boolean; origins?: string[]; methods?: string[]; headers?: string[]; }; } export interface APIExecutionLog { id: string; path: string; method: string; timestamp: Date; duration: number; statusCode: number; success: boolean; error?: string; userId?: string; organizationId?: string; } export declare class APIActionHandler { private actions; private executionLogs; private ajv; private options; constructor(options?: APIHandlerOptions); /** * Register an API action */ register(action: APIAction): void; /** * Register multiple API actions */ registerMany(actions: APIAction[]): void; /** * Unregister an API action */ unregister(method: string, path: string): void; /** * Handle an incoming API request */ handleRequest(method: string, path: string, request: APIRequest, context: PluginContext): Promise<APIResponse>; /** * Find a matching action for the given method and path */ private findMatchingAction; /** * Check if a request matches an action pattern */ private matchesPattern; /** * Extract parameters from a parameterized path */ private extractParams; /** * Check if the user has required permissions */ private checkPermissions; /** * Validate the request against the action's validation schema */ private validateRequest; /** * Validate data against JSON schema */ private validateSchema; /** * Get CORS headers */ private getCorsHeaders; /** * Generate action key for storage */ private getActionKey; /** * Log API execution */ private logExecution; /** * Get all registered actions */ getRegisteredActions(): APIAction[]; /** * Get execution logs */ getExecutionLogs(limit?: number): APIExecutionLog[]; /** * Get API statistics */ getStats(): { totalActions: number; totalExecutions: number; successfulExecutions: number; failedExecutions: number; averageResponseTime: number; responseTimeByStatus: Record<string, number>; }; /** * Clear all registered actions and logs */ clear(): void; } export declare class APIActionBuilder { access(access: APIAccessScope | APIAccessScope[]): APIActionBuilder; private action; static create(): APIActionBuilder; path(path: string): APIActionBuilder; method(method: APIAction['method']): APIActionBuilder; handler(handler: APIAction['handler']): APIActionBuilder; requirePermissions(permissions: APIAction['requiredPermissions']): APIActionBuilder; validateQuery(schema: object): APIActionBuilder; validateBody(schema: object): APIActionBuilder; validateParams(schema: object): APIActionBuilder; build(): APIAction; } export declare function Get(path: string, options?: Omit<APIAction, 'path' | 'method' | 'handler'>): any; export declare function Post(path: string, options?: Omit<APIAction, 'path' | 'method' | 'handler'>): any; export declare function Put(path: string, options?: Omit<APIAction, 'path' | 'method' | 'handler'>): any; export declare function Delete(path: string, options?: Omit<APIAction, 'path' | 'method' | 'handler'>): any; export declare function Patch(path: string, options?: Omit<APIAction, 'path' | 'method' | 'handler'>): any; export declare function createAPIAction(method: APIAction['method'], path: string, handler: APIAction['handler'], options?: { requiredPermissions?: APIAction['requiredPermissions']; validation?: APIAction['validation']; }): APIAction; export declare function extractAPIActions(pluginClass: any): APIAction[]; //# sourceMappingURL=api-handler.d.ts.map