UNPKG

aem-mcp-server

Version:
2 lines (1 loc) 3.75 kB
"use strict";export class AEMOperationError extends Error{code;details;recoverable;retryAfter;constructor(t){super(t.message),this.name="AEMOperationError",this.code=t.code,this.details=t.details,this.recoverable=t.recoverable,this.retryAfter=t.retryAfter}}export const AEM_ERROR_CODES={CONNECTION_FAILED:"CONNECTION_FAILED",TIMEOUT:"TIMEOUT",AUTHENTICATION_FAILED:"AUTHENTICATION_FAILED",UNAUTHORIZED:"UNAUTHORIZED",INVALID_PATH:"INVALID_PATH",INVALID_COMPONENT_TYPE:"INVALID_COMPONENT_TYPE",INVALID_LOCALE:"INVALID_LOCALE",INVALID_PARAMETERS:"INVALID_PARAMETERS",RESOURCE_NOT_FOUND:"RESOURCE_NOT_FOUND",COMPONENT_NOT_FOUND:"COMPONENT_NOT_FOUND",PAGE_NOT_FOUND:"PAGE_NOT_FOUND",UPDATE_FAILED:"UPDATE_FAILED",VALIDATION_FAILED:"VALIDATION_FAILED",REPLICATION_FAILED:"REPLICATION_FAILED",QUERY_FAILED:"QUERY_FAILED",INSUFFICIENT_PERMISSIONS:"INSUFFICIENT_PERMISSIONS",SYSTEM_ERROR:"SYSTEM_ERROR",RATE_LIMITED:"RATE_LIMITED"};export function createAEMError(e,t,n,r=!1,s){return new AEMOperationError({code:e,message:t,details:n,recoverable:r,retryAfter:s})}export function handleAEMHttpError(e,t){if(e.response){const n=e.response.status,r=e.response.data;switch(n){case 401:return createAEMError(AEM_ERROR_CODES.AUTHENTICATION_FAILED,"Authentication failed. Check AEM credentials.",{status:n,data:r});case 403:return createAEMError(AEM_ERROR_CODES.INSUFFICIENT_PERMISSIONS,"Insufficient permissions for this operation.",{status:n,data:r,operation:t});case 404:return createAEMError(AEM_ERROR_CODES.RESOURCE_NOT_FOUND,"Resource not found in AEM.",{status:n,data:r,operation:t});case 429:const s=e.response.headers["retry-after"];return createAEMError(AEM_ERROR_CODES.RATE_LIMITED,"Rate limit exceeded. Please try again later.",{status:n,data:r},!0,s?parseInt(s)*1e3:6e4);case 500:case 502:case 503:return createAEMError(AEM_ERROR_CODES.SYSTEM_ERROR,"AEM system error. Please try again later.",{status:n,data:r},!0,3e4);default:return createAEMError(AEM_ERROR_CODES.SYSTEM_ERROR,`HTTP ${n}: ${r?.message||"Unknown error"}`,{status:n,data:r,operation:t})}}else return e.code==="ECONNREFUSED"||e.code==="ENOTFOUND"?createAEMError(AEM_ERROR_CODES.CONNECTION_FAILED,"Cannot connect to AEM instance. Check host and network.",{originalError:e.message},!0,5e3):e.code==="ETIMEDOUT"?createAEMError(AEM_ERROR_CODES.TIMEOUT,"Request to AEM timed out.",{originalError:e.message},!0,1e4):createAEMError(AEM_ERROR_CODES.SYSTEM_ERROR,`Unexpected error during ${t}: ${e.message}`,{originalError:e.message})}export async function safeExecute(e,t,n=3){let r;for(let s=1;s<=n;s++)try{return await e()}catch(E){if(r=E instanceof AEMOperationError?E:handleAEMHttpError(E,t),!r.recoverable||s===n)break;const a=r.retryAfter||Math.pow(2,s)*1e3;console.warn(`[${t}] Attempt ${s} failed, retrying in ${a}ms:`,r.message),await new Promise(i=>setTimeout(i,a))}throw r}export function validateComponentOperation(e,t,n,r){const s=[];if((!e||typeof e!="string")&&s.push("Locale is required and must be a string"),!t||typeof t!="string"?s.push("Page path is required and must be a string"):t.startsWith("/content")||s.push("Page path must start with /content"),(!n||typeof n!="string")&&s.push("Component type is required and must be a string"),(!r||typeof r!="object")&&s.push("Component properties are required and must be an object"),s.length>0)throw createAEMError(AEM_ERROR_CODES.INVALID_PARAMETERS,"Invalid component operation parameters",{errors:s})}export function createSuccessResponse(e,t){return{success:!0,operation:t,timestamp:new Date().toISOString(),data:e}}export function createErrorResponse(e,t){return{success:!1,operation:t,timestamp:new Date().toISOString(),error:{code:e.code,message:e.message,details:e.details,recoverable:e.recoverable,retryAfter:e.retryAfter}}}