import { Request , Response } from 'express' ; import { parse , stringify } from 'flatted' ; import { IExecutionDb , IExecutionFlatted , IExecutionFlattedDb , IExecutionResponse , IWorkflowDb , } from '.' ; export class extends Error export class ResponseError extends Error { httpStatusCode ? : number ; errorCode ? : number ; hint ? : string ; param { string } message param { number } errorCode param { number } httpStatusCode param { string } hint memberof constructor ( message , errorCode ? , httpStatusCode ? , hint ? ) { super ( message ) ; this . name = 'ResponseError' ; if ( errorCode ) { this . errorCode = errorCode ; } if ( httpStatusCode ) { this . httpStatusCode = httpStatusCode ; } if ( hint ) { this . hint = hint ; } } } export function basicAuthAuthorizationError ( resp , realm , message ? ) { resp . statusCode = 401 ; resp . setHeader ( 'WWW-Authenticate' , `Basic realm="${ realm }"` ) ; resp . json ( { code : resp . statusCode , message } ) ; } export function jwtAuthAuthorizationError ( resp , message ? ) { resp . statusCode = 403 ; resp . json ( { code : resp . statusCode , message } ) ; } export function sendSuccessResponse ( res , data , raw ? , responseCode ? , responseHeader ? ) { if ( responseCode !== undefined ) { res . status ( responseCode ) ; } if ( responseHeader ) { res . header ( responseHeader ) ; } if ( raw === true ) { if ( typeof data === 'string' ) { res . send ( data ) ; } else { res . json ( data ) ; } } else { res . json ( { data , } ) ; } } export function sendErrorResponse ( res , error , shouldLog = true ) { let httpStatusCode = 500 ; if ( error . httpStatusCode ) { httpStatusCode = error . httpStatusCode ; } if ( process . env . NODE_ENV !== 'production' && shouldLog ) { console . error ( 'ERROR RESPONSE' ) ; console . error ( error ) ; } const response = { code : 0 , message : 'Unknown error' , hint : '' , } ; if ( error . name === 'NodeApiError' ) { Object . assign ( response , error ) ; } if ( error . errorCode ) { response . code = error . errorCode ; } if ( error . message ) { response . message = error . message ; } if ( error . hint ) { response . hint = error . hint ; } if ( error . stack && process . env . NODE_ENV !== 'production' ) { response . stack = error . stack ; } res . status ( httpStatusCode ) . json ( response ) ; } export param { ( req : Request , res : Response ) => Promise < any > } processFunction returns export function send ( processFunction ) { return async ( req , res ) => { try { const data = await processFunction ( req , res ) ; sendSuccessResponse ( res , data ) ; } catch ( error ) { sendErrorResponse ( res , error ) ; } } ; } export param { IExecutionDb } fullExecutionData returns { IExecutionFlatted } export function flattenExecutionData ( fullExecutionData ) { const returnData = { data : stringify ( fullExecutionData . data ) , mode : fullExecutionData . mode , waitTill : fullExecutionData . waitTill , startedAt : fullExecutionData . startedAt , stoppedAt : fullExecutionData . stoppedAt , finished : fullExecutionData . finished ? fullExecutionData . finished : false , workflowId : fullExecutionData . workflowId , workflowData : fullExecutionData . workflowData ! , } ; if ( fullExecutionData . id !== undefined ) { returnData . id = fullExecutionData . id . toString ( ) ; } if ( fullExecutionData . retryOf !== undefined ) { returnData . retryOf = fullExecutionData . retryOf . toString ( ) ; } if ( fullExecutionData . retrySuccessId !== undefined ) { returnData . retrySuccessId = fullExecutionData . retrySuccessId . toString ( ) ; } return returnData ; } export param { IExecutionFlattedDb } fullExecutionData returns { IExecutionResponse } export function unflattenExecutionData ( fullExecutionData ) { const returnData = { id : fullExecutionData . id . toString ( ) , workflowData : fullExecutionData . workflowData as IWorkflowDb , data : parse ( fullExecutionData . data ) , mode : fullExecutionData . mode , waitTill : fullExecutionData . waitTill ? fullExecutionData . waitTill : undefined , startedAt : fullExecutionData . startedAt , stoppedAt : fullExecutionData . stoppedAt , finished : fullExecutionData . finished ? fullExecutionData . finished : false , workflowId : fullExecutionData . workflowId , } ; return returnData ; }