mcp-quiz-server
Version:
🧠AI-Powered Quiz Management via Model Context Protocol (MCP) - Create, manage, and take quizzes directly from VS Code, Claude, and other AI agents.
195 lines (194 loc) • 5.46 kB
JavaScript
"use strict";
/**
* @fileoverview Command Result Interface - Application Layer
* @version 1.0.0
* @since 2025-07-30
* @lastUpdated 2025-07-30
* @module CommandResult
* @description Command result interface for CQRS implementation in Clean Architecture.
* Standardizes command execution results across all handlers.
* @contributors Claude Code Agent
* @dependencies None (core application interface)
* @requirements REQ-ARCH-001 (Clean Architecture Application Layer)
* @testCoverage Unit tests for result validation and serialization
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandResultError = exports.CommandResultUtils = exports.CommandResultBuilder = void 0;
/**
* Command Result Builder
*
* @description Helper class for building consistent command results
*/
class CommandResultBuilder {
constructor(commandId) {
this.result = {
timestamp: new Date(),
events: [],
warnings: [],
};
this.result.commandId = commandId;
}
/**
* Mark result as successful with data
*/
success(data) {
this.result.success = true;
this.result.data = data;
return this;
}
/**
* Mark result as failed with error
*/
failure(error) {
this.result.success = false;
if (typeof error === 'string') {
this.result.error = {
code: 'COMMAND_EXECUTION_ERROR',
message: error,
};
}
else if (error instanceof Error) {
this.result.error = {
code: error.name || 'COMMAND_EXECUTION_ERROR',
message: error.message,
stack: error.stack,
};
}
else {
this.result.error = error;
}
return this;
}
/**
* Add execution time
*/
executionTime(timeMs) {
this.result.executionTime = timeMs;
return this;
}
/**
* Add domain events generated
*/
withEvents(events) {
this.result.events = [...(this.result.events || []), ...events];
return this;
}
/**
* Add single domain event
*/
withEvent(event) {
this.result.events = [...(this.result.events || []), event];
return this;
}
/**
* Add metadata
*/
withMetadata(metadata) {
this.result.metadata = { ...this.result.metadata, ...metadata };
return this;
}
/**
* Add warnings
*/
withWarnings(warnings) {
this.result.warnings = [...(this.result.warnings || []), ...warnings];
return this;
}
/**
* Add single warning
*/
withWarning(warning) {
this.result.warnings = [...(this.result.warnings || []), warning];
return this;
}
/**
* Build the final result
*/
build() {
if (this.result.success === undefined) {
throw new Error('Result success status must be set');
}
if (this.result.executionTime === undefined) {
this.result.executionTime = 0;
}
return {
success: this.result.success,
commandId: this.result.commandId,
data: this.result.data,
error: this.result.error,
executionTime: this.result.executionTime,
timestamp: this.result.timestamp,
events: this.result.events || [],
metadata: this.result.metadata,
warnings: this.result.warnings || [],
};
}
}
exports.CommandResultBuilder = CommandResultBuilder;
/**
* Utility functions for command results
*/
class CommandResultUtils {
/**
* Create successful result
*/
static success(commandId, data, executionTime = 0) {
return new CommandResultBuilder(commandId).success(data).executionTime(executionTime).build();
}
/**
* Create failure result
*/
static failure(commandId, error, executionTime = 0) {
return new CommandResultBuilder(commandId).failure(error).executionTime(executionTime).build();
}
/**
* Create validation failure result
*/
static validationFailure(commandId, validationErrors) {
return new CommandResultBuilder(commandId)
.failure({
code: 'VALIDATION_ERROR',
message: 'Command validation failed',
validationErrors,
})
.executionTime(0)
.build();
}
/**
* Check if result represents success
*/
static isSuccess(result) {
return result.success === true;
}
/**
* Check if result represents failure
*/
static isFailure(result) {
return result.success === false;
}
/**
* Extract error message from result
*/
static getErrorMessage(result) {
var _a;
return ((_a = result.error) === null || _a === void 0 ? void 0 : _a.message) || null;
}
/**
* Extract data from successful result
*/
static getData(result) {
return result.success ? result.data : null;
}
}
exports.CommandResultUtils = CommandResultUtils;
/**
* Command Result Validation Error
*/
class CommandResultError extends Error {
constructor(message, result) {
super(message);
this.result = result;
this.name = 'CommandResultError';
}
}
exports.CommandResultError = CommandResultError;