UNPKG

@iota-big3/sdk-gateway

Version:

Universal API Gateway with protocol translation, intelligent routing, rate limiting, health checking, and caching

292 lines 9.36 kB
"use strict"; /** * Runtime Utilities for SDK Types * Following Phase 2g principles: Incremental implementation with backward compatibility * These utilities provide runtime type checking and validation for compile-time types */ Object.defineProperty(exports, "__esModule", { value: true }); exports.isAuthUser = isAuthUser; exports.isComplianceViolation = isComplianceViolation; exports.isApiError = isApiError; exports.isValidJsonValue = isValidJsonValue; exports.createApiResponse = createApiResponse; exports.createAuthUser = createAuthUser; exports.createComplianceViolation = createComplianceViolation; exports.createValidationContext = createValidationContext; exports.validateSDKConfig = validateSDKConfig; exports.validateEnvironment = validateEnvironment; exports.validateComplianceFramework = validateComplianceFramework; const tslib_1 = require("tslib"); const crypto = tslib_1.__importStar(require("crypto")); // ============================================ // Type Guard Functions // ============================================ /** * Type guard to check if a value is a valid AuthUser */ function isAuthUser(value) { if (!value || typeof value !== 'object') { return false; } const obj = value; // Required fields if (typeof obj.id !== 'string' || !obj.id) { return false; } // Optional fields with type checking if (obj.email !== undefined && typeof obj.email !== 'string') { return false; } if (obj.name !== undefined && typeof obj.name !== 'string') { return false; } if (obj.roles !== undefined && !Array.isArray(obj.roles)) { return false; } if (obj.permissions !== undefined && !Array.isArray(obj.permissions)) { return false; } if (obj.organizationId !== undefined && typeof obj.organizationId !== 'string') { return false; } if (obj.metadata !== undefined && typeof obj.metadata !== 'object') { return false; } return true; } /** * Type guard to check if a value is a valid ComplianceViolation */ function isComplianceViolation(value) { if (!value || typeof value !== 'object') { return false; } const obj = value; const validSeverities = ['low', 'medium', 'high', 'critical']; // Required fields if (typeof obj.rule !== 'string' || !obj.rule) { return false; } if (!validSeverities.includes(obj.severity)) { return false; } if (typeof obj.message !== 'string' || !obj.message) { return false; } // Optional fields if (obj.remediation !== undefined && typeof obj.remediation !== 'string') { return false; } if (obj.context !== undefined && typeof obj.context !== 'object') { return false; } return true; } /** * Type guard to check if a value is a valid ApiError */ function isApiError(value) { if (!value || typeof value !== 'object') { return false; } const obj = value; // Required fields if (typeof obj.code !== 'string' || !obj.code) { return false; } if (typeof obj.message !== 'string' || !obj.message) { return false; } // Optional field can be any type return true; } /** * Type guard to check if a value is valid JSON */ function isValidJsonValue(value) { // Helper to recursively check nested values function checkNestedValues(obj, visited = new WeakSet()) { // Prevent infinite recursion on circular references if (typeof obj === 'object' && obj !== null) { if (visited.has(obj)) return false; visited.add(obj); } // Check the value itself if (obj === null) return true; if (typeof obj === 'string') return true; if (typeof obj === 'number') return true; if (typeof obj === 'boolean') return true; if (obj === undefined) return false; if (typeof obj === 'function') return false; if (typeof obj === 'symbol') return false; if (typeof obj === 'bigint') return false; if (typeof obj === 'object' && obj !== null) { if (obj instanceof Date) return false; if (obj instanceof RegExp) return false; if (obj instanceof Error) return false; if (obj instanceof Map) return false; if (obj instanceof Set) return false; if (obj instanceof WeakMap) return false; if (obj instanceof WeakSet) return false; // Check all properties/elements if (Array.isArray(obj)) { return obj.every(item => checkNestedValues(item, visited)); } else { // For plain objects, check all enumerable properties const keys = Object.keys(obj); for (const key of keys) { if (!checkNestedValues(obj[key], visited)) { return false; } } return true; } } return false; } try { return checkNestedValues(value); } catch { // Catch any unexpected errors including stack overflow return false; } } // ============================================ // Factory Functions // ============================================ /** * Generate a unique request ID */ function generateRequestId() { return `req-${Date.now()}-${crypto.randomInt(0, Number.MAX_SAFE_INTEGER).toString(36).substring(2, 9)}`; } /** * Create a properly typed ApiResponse */ function createApiResponse(options) { const response = {}; if (options.data !== undefined) { response.data = options.data; } if (options.error !== undefined) { response.error = options.error; } // Generate default meta if not provided response.meta = options.meta || { timestamp: Date.now(), version: '1.0.0', requestId: generateRequestId() }; return response; } /** * Create a properly typed AuthUser */ function createAuthUser(data) { return { id: data.id, ...(data.email !== undefined && { email: data.email }), ...(data.name !== undefined && { name: data.name }), ...(data.roles !== undefined && { roles: data.roles }), ...(data.permissions !== undefined && { permissions: data.permissions }), ...(data.organizationId !== undefined && { organizationId: data.organizationId }), ...(data.metadata !== undefined && { metadata: data.metadata }) }; } /** * Create a properly typed ComplianceViolation */ function createComplianceViolation(data) { return { rule: data.rule, severity: data.severity, message: data.message, ...(data.remediation !== undefined && { remediation: data.remediation }), ...(data.context !== undefined && { context: data.context }) }; } /** * Create a properly typed ValidationContext */ function createValidationContext(data = {}) { return { ...data }; } /** * Validate an SDKConfig object */ function validateSDKConfig(config) { const errors = []; if (!config || typeof config !== 'object') { return { valid: false, errors: ['Config must be an object'] }; } const obj = config; // Required fields if (!obj.serviceName || typeof obj.serviceName !== 'string') { errors.push('Service name is required'); } if (!obj.version || typeof obj.version !== 'string') { errors.push('Version is required'); } if (!obj.industry || typeof obj.industry !== 'object') { errors.push('Industry configuration is required'); } else { const industry = obj.industry; if (!industry.name || typeof industry.name !== 'string') { errors.push('Industry name is required'); } if (!industry.displayName || typeof industry.displayName !== 'string') { errors.push('Industry display name is required'); } } // Optional fields validation if (obj.serviceUrl !== undefined && typeof obj.serviceUrl !== 'string') { errors.push('Service URL must be a string'); } if (obj.compliance !== undefined && !Array.isArray(obj.compliance)) { errors.push('Compliance must be an array'); } if (obj.environment !== undefined && !validateEnvironment(obj.environment)) { errors.push('Invalid environment value'); } return { valid: errors.length === 0, errors }; } /** * Validate an Environment value */ function validateEnvironment(value) { return value === 'development' || value === 'staging' || value === 'production'; } /** * Validate a ComplianceFramework value */ function validateComplianceFramework(value) { const validFrameworks = [ 'FERPA', 'COPPA', 'GDPR', 'HIPAA', 'HITECH', 'PCI-DSS', 'SOX', 'FedRAMP', 'StateRAMP', 'CCPA', 'ISO-28000', 'CTPAT', 'TAPA', 'IRS-501c3', 'GAAP-NFP', 'OMB-A133' ]; return typeof value === 'string' && validFrameworks.includes(value); } //# sourceMappingURL=runtime-utilities.js.map