UNPKG

3.98 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const APIConfig_1 = require("./APIConfig");
13const APIError_1 = require("./APIError");
14const lodash_1 = require("lodash");
15const url_1 = require("url");
16class APIResponse {
17 constructor(req, res, next) {
18 this.req = req;
19 this.res = res;
20 this.next = next;
21 }
22 static withError(req, res, error, hapiOutput = APIConfig_1.APIConfig.OUTPUT_HAPI_RESULTS) {
23 return new APIResponse(req, res).withError(error, hapiOutput);
24 }
25 processHandlerFunction(target, handlerFunction, handlerArgs = [], disableFriendlyResponse, successResponseHandler) {
26 return __awaiter(this, void 0, void 0, function* () {
27 // Add the req, and res to the end arguments if the function wants it
28 handlerArgs = handlerArgs.concat([this.req, this.res]);
29 let handlerData;
30 try {
31 const handlerOutput = handlerFunction.apply(target, handlerArgs);
32 if (!(handlerOutput instanceof Promise)) {
33 handlerData = handlerOutput;
34 }
35 else {
36 handlerData = yield handlerOutput;
37 }
38 }
39 catch (error) {
40 this.withError(error);
41 return;
42 }
43 // If the data is a URL, consider this a redirect.
44 if (handlerData instanceof url_1.URL) {
45 this.res.redirect(handlerData.toString());
46 return;
47 }
48 if (disableFriendlyResponse) {
49 this.res.json(handlerData);
50 }
51 else if (!lodash_1.isNil(successResponseHandler)) {
52 successResponseHandler(handlerData, this.res);
53 }
54 else {
55 this.withSuccess(handlerData, 200);
56 }
57 });
58 }
59 withError(error, hapiOutput = APIConfig_1.APIConfig.OUTPUT_HAPI_RESULTS) {
60 if (this.res.headersSent || lodash_1.isNil(error)) {
61 return;
62 }
63 let apiError;
64 if (error instanceof APIError_1.APIError) {
65 apiError = error;
66 }
67 else {
68 apiError = new APIError_1.APIError("unknown", error);
69 }
70 if ((apiError.statusCode >= 500 && apiError.statusCode <= 599 && APIConfig_1.APIConfig.LOG_500_ERRORS) ||
71 (apiError.statusCode >= 400 && apiError.statusCode <= 499 && APIConfig_1.APIConfig.LOG_400_ERRORS)) {
72 console.error(JSON.stringify(apiError.out(true)));
73 }
74 if (this.next) {
75 // Let the standard error handler handle it
76 this.next(apiError);
77 }
78 else {
79 this.res.status(apiError.statusCode).send(hapiOutput ? apiError.hapiOut() : apiError.out());
80 }
81 }
82 withSuccess(data, statusCode = 200, hapiOutput = APIConfig_1.APIConfig.OUTPUT_HAPI_RESULTS) {
83 if (this.res.headersSent) {
84 return;
85 }
86 let output = data;
87 if (hapiOutput) {
88 output = {
89 "this": "succeeded",
90 "with": data
91 };
92 }
93 this.res.status(statusCode).send(output);
94 }
95}
96exports.APIResponse = APIResponse;
97//# sourceMappingURL=APIResponse.js.map
\No newline at end of file