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AC,CAAC,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC;SAC/F;IACL,CAAC;IAED,WAAW,CAAC,IAAU,EAAE,aAAqB,GAAG,EAAE,aAAsB,qBAAS,CAAC,mBAAmB;QAEjG,IAAG,IAAI,CAAC,GAAG,CAAC,WAAW,EACvB;YACI,OAAO;SACV;QAED,IAAI,MAAM,GAAG,IAAI,CAAC;QAElB,IAAI,UAAU,EAAE;YACZ,MAAM,GAAG;gBACL,MAAM,EAAE,WAAW;gBACnB,MAAM,EAAE,IAAI;aACf,CAAA;SACJ;QAED,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC7C,CAAC;CACJ;AA9FD,kCA8FC","sourcesContent":["import {APIConfig} from \"./APIConfig\";\nimport {APIError} from \"./APIError\";\nimport {get, isNil} from \"lodash\";\nimport {URL} from \"url\";\n\nexport class APIResponse {\n req;\n res;\n next;\n\n constructor(req?, res?, next?) {\n this.req = req;\n this.res = res;\n this.next = next;\n }\n\n static withError(req, res, error: any, hapiOutput: boolean = APIConfig.OUTPUT_HAPI_RESULTS) {\n return new APIResponse(req, res).withError(error, hapiOutput);\n }\n\n processHandlerFunction(target: any, handlerFunction: Function, handlerArgs: any[] = [], successResponseHandler?: (responseData: any, res) => void) {\n // Add the req, and res to the end arguments if the function wants it\n handlerArgs = handlerArgs.concat([this.req, this.res]);\n\n let handlerPromise = handlerFunction.apply(target, handlerArgs);\n if (!(handlerPromise instanceof Promise)) {\n throw new Error(`API function named '${handlerFunction.name}' doesn't return a promise.`);\n }\n else {\n handlerPromise.then((data: any) => {\n\n // If the data is a URL, consider this a redirect.\n if (data instanceof URL) {\n this.res.redirect((<URL>data).toString());\n return;\n }\n\n if (!isNil(successResponseHandler)) {\n successResponseHandler(data, this.res);\n }\n else {\n this.withSuccess(data, 200);\n }\n }).catch((error: any) => {\n this.withError(error);\n });\n }\n }\n\n withError(error: any, hapiOutput: boolean = APIConfig.OUTPUT_HAPI_RESULTS) {\n\n if(this.res.headersSent || isNil(error))\n {\n return;\n }\n\n let apiError: APIError;\n\n if (error instanceof APIError) {\n apiError = error;\n }\n else {\n apiError = new APIError(\"unknown\", error);\n }\n\n if ((apiError.statusCode >= 500 && apiError.statusCode <= 599 && APIConfig.LOG_500_ERRORS) ||\n (apiError.statusCode >= 400 && apiError.statusCode <= 499 && APIConfig.LOG_400_ERRORS)) {\n console.error(JSON.stringify(apiError.out(true)));\n }\n\n if(this.next)\n {\n // Let the standard error handler handle it\n this.next(apiError);\n }\n else\n {\n this.res.status(apiError.statusCode).send(hapiOutput ? apiError.hapiOut() : apiError.out());\n }\n }\n\n withSuccess(data?: any, statusCode: number = 200, hapiOutput: boolean = APIConfig.OUTPUT_HAPI_RESULTS) {\n\n if(this.res.headersSent)\n {\n return;\n }\n\n let output = data;\n\n if (hapiOutput) {\n output = {\n \"this\": \"succeeded\",\n \"with\": data\n }\n }\n\n this.res.status(statusCode).send(output);\n }\n}"]}
\No newline at end of file