@creator.co/wapi 1.2.0-alpha7 → 1.2.0-alpha9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.cjs +1 -1
- package/dist/package.json +2 -1
- package/dist/src/API/Request.d.ts +1 -1
- package/dist/src/API/Response.d.ts +16 -13
- package/dist/src/API/Response.js +2 -9
- package/dist/src/API/Response.js.map +1 -1
- package/dist/src/BaseEvent/EventProcessor.js +1 -1
- package/dist/src/BaseEvent/EventProcessor.js.map +1 -1
- package/dist/src/BaseEvent/Transaction.d.ts +12 -12
- package/dist/src/BaseEvent/Transaction.js +28 -28
- package/dist/src/BaseEvent/Transaction.js.map +1 -1
- package/dist/src/Server/Router.d.ts +4 -7
- package/dist/src/Server/Router.js.map +1 -1
- package/dist/src/Server/lib/Server.js +3 -3
- package/dist/src/Server/lib/Server.js.map +1 -1
- package/package.json +1 -1
- package/src/API/Request.ts +1 -1
- package/src/API/Response.ts +33 -31
- package/src/BaseEvent/EventProcessor.ts +1 -1
- package/src/BaseEvent/Transaction.ts +37 -37
- package/src/Server/Router.ts +7 -7
- package/src/Server/lib/Server.ts +3 -3
package/.eslintrc.cjs
CHANGED
|
@@ -18,7 +18,7 @@ module.exports = {
|
|
|
18
18
|
},
|
|
19
19
|
],
|
|
20
20
|
"@typescript-eslint/ban-ts-comment": 0,
|
|
21
|
-
"@typescript-eslint/no-explicit-any":
|
|
21
|
+
"@typescript-eslint/no-explicit-any": 0,
|
|
22
22
|
// turn on errors for missing imports
|
|
23
23
|
"import/no-unresolved": "error",
|
|
24
24
|
// 'import/no-named-as-default-member': 'off',
|
package/dist/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@creator.co/wapi",
|
|
3
|
-
"version": "1.2.0-
|
|
3
|
+
"version": "1.2.0-alpha8",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"author": "",
|
|
14
14
|
"license": "ISC",
|
|
15
15
|
"dependencies": {
|
|
16
|
+
"@aws-sdk/client-kms": "^3.427.0",
|
|
16
17
|
"@aws-sdk/client-secrets-manager": "^3.427.0",
|
|
17
18
|
"@aws-sdk/client-ses": "^3.414.0",
|
|
18
19
|
"@aws-sdk/client-sns": "^3.398.0",
|
|
@@ -10,7 +10,7 @@ export default class Request<T> {
|
|
|
10
10
|
getContextParam(cxtParam: string): any | null;
|
|
11
11
|
containsPathParam(paramName: string): boolean;
|
|
12
12
|
getPathParam(paramName: string): string | null;
|
|
13
|
-
getBody(): T
|
|
13
|
+
getBody(): T;
|
|
14
14
|
getPath(): string;
|
|
15
15
|
getMethod(): string;
|
|
16
16
|
getPathParams(): object | null;
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { Context } from "aws-lambda";
|
|
2
2
|
import Transaction from "../BaseEvent/Transaction";
|
|
3
|
-
export
|
|
3
|
+
export type ResponseErrorType = {
|
|
4
|
+
err: string;
|
|
5
|
+
errCode?: string;
|
|
6
|
+
};
|
|
7
|
+
export default class Response<B = any | ResponseErrorType> {
|
|
4
8
|
private _statusCode;
|
|
5
9
|
private _body;
|
|
6
10
|
private _isPipingOut;
|
|
@@ -9,28 +13,27 @@ export default class Response {
|
|
|
9
13
|
readonly rawBody: boolean;
|
|
10
14
|
readonly throwOnErrors: boolean;
|
|
11
15
|
readonly disableTransactionID: boolean;
|
|
12
|
-
constructor(statusCode: number, body
|
|
16
|
+
constructor(statusCode: number, body: B, optBehaviour?: {
|
|
13
17
|
shouldStream?: boolean;
|
|
14
18
|
rawBody?: boolean;
|
|
15
19
|
throwOnErrors?: boolean;
|
|
16
20
|
disableTransactionID?: boolean;
|
|
17
21
|
} | undefined);
|
|
18
22
|
getCode(): number;
|
|
19
|
-
getBody():
|
|
23
|
+
getBody(): B;
|
|
20
24
|
appendIntoBody(key: string, value: any): void;
|
|
21
25
|
appendHeader(key: string, value: any): void;
|
|
22
26
|
build(context: Context, transaction: Transaction<any>, _optDoNotCallContext: boolean): Promise<void>;
|
|
23
27
|
private _pipe;
|
|
24
28
|
private _rawContext;
|
|
25
|
-
static MissingParamResponse(paramName: string): Response
|
|
26
|
-
static MissingQueryResponse(paramName: string): Response
|
|
27
|
-
static BadRequestResponse(msg?: string, errCode?: string, optBody?: any): Response
|
|
28
|
-
static BadRequestResponseWithRollback(msg: string, errCode: string, optBody?: any): Response
|
|
29
|
-
static UnauthorizedResponse(msg: string, errCode: string): Response
|
|
30
|
-
static SuccessResponse(body
|
|
31
|
-
static RedirectResponse(url: string): Response
|
|
32
|
-
static SuccessNoContentResponse(): Response
|
|
29
|
+
static MissingParamResponse(paramName: string): Response<ResponseErrorType>;
|
|
30
|
+
static MissingQueryResponse(paramName: string): Response<ResponseErrorType>;
|
|
31
|
+
static BadRequestResponse<T>(msg?: string, errCode?: string, optBody?: any): Response<T>;
|
|
32
|
+
static BadRequestResponseWithRollback(msg: string, errCode: string, optBody?: any): Response<ResponseErrorType>;
|
|
33
|
+
static UnauthorizedResponse(msg: string, errCode: string): Response<ResponseErrorType>;
|
|
34
|
+
static SuccessResponse<T>(body: T): Response<T>;
|
|
35
|
+
static RedirectResponse(url: string): Response<null>;
|
|
36
|
+
static SuccessNoContentResponse(): Response<null>;
|
|
33
37
|
static SuccessStreamResponse(stream: any, contentType: string): Response;
|
|
34
|
-
static
|
|
35
|
-
static SimpleResponse(body: any, optionalCode?: number): Response;
|
|
38
|
+
static SimpleResponse<T>(body: T, optionalCode?: number): Response<T>;
|
|
36
39
|
}
|
package/dist/src/API/Response.js
CHANGED
|
@@ -203,10 +203,10 @@ var Response = /** @class */ (function () {
|
|
|
203
203
|
return new Response(401, __assign({ err: msg }, (errCode ? { errCode: errCode } : {})));
|
|
204
204
|
};
|
|
205
205
|
Response.SuccessResponse = function (body) {
|
|
206
|
-
return new Response(200, body ? body : {});
|
|
206
|
+
return new Response(200, (body ? body : {}));
|
|
207
207
|
};
|
|
208
208
|
Response.RedirectResponse = function (url) {
|
|
209
|
-
var resp = new Response(302);
|
|
209
|
+
var resp = new Response(302, null);
|
|
210
210
|
resp.appendHeader("Location", url);
|
|
211
211
|
return resp;
|
|
212
212
|
};
|
|
@@ -222,13 +222,6 @@ var Response = /** @class */ (function () {
|
|
|
222
222
|
resp.appendHeader("Content-type", contentType);
|
|
223
223
|
return resp;
|
|
224
224
|
};
|
|
225
|
-
Response.StepFunctionResponse = function (body, optionalCode) {
|
|
226
|
-
var resp = new Response(optionalCode || 200, body, {
|
|
227
|
-
rawBody: true,
|
|
228
|
-
throwOnErrors: true,
|
|
229
|
-
});
|
|
230
|
-
return resp;
|
|
231
|
-
};
|
|
232
225
|
Response.SimpleResponse = function (body, optionalCode) {
|
|
233
226
|
var resp = new Response(optionalCode || 200, body);
|
|
234
227
|
return resp;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Response.js","sourceRoot":"","sources":["../../../src/API/Response.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,sCAAgC;AAChC,EAAE;AACF;IAA0B,+BAAK;IAC7B,qBAAY,IAAS;QAArB,iBAIC;QAHC,IAAM,GAAG,GAAG,IAAI,IAAI,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,gBAAgB,CAAA;gBAClE,kBAAM,GAAG,CAAC;QACV,KAAI,CAAC,IAAI,GAAG,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,cAAc,CAAA;;IAC9D,CAAC;IACH,kBAAC;AAAD,CAAC,AAND,CAA0B,KAAK,GAM9B;
|
|
1
|
+
{"version":3,"file":"Response.js","sourceRoot":"","sources":["../../../src/API/Response.ts"],"names":[],"mappings":";;;;;;;;;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AAC,CAAA;QACvD,OAAO,IAAI,CAAA;IACb,CAAC;IACH,eAAC;AAAD,CAAC,AA1LD,IA0LC"}
|
|
@@ -132,7 +132,7 @@ var EventProcessor = /** @class */ (function () {
|
|
|
132
132
|
return [2 /*return*/, {
|
|
133
133
|
batchItemFailures: failureIDs.map(function (id) { return ({ itemIdentifier: id }); }),
|
|
134
134
|
}];
|
|
135
|
-
return [2 /*return*/, Response_1.default.SuccessResponse()];
|
|
135
|
+
return [2 /*return*/, Response_1.default.SuccessResponse(null)];
|
|
136
136
|
}
|
|
137
137
|
});
|
|
138
138
|
}); })];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EventProcessor.js","sourceRoot":"","sources":["../../../src/BaseEvent/EventProcessor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,6CAA8D;AAC9D,4CAAsC;AACtC,sCAAgC;AAQhC;IAKE,wBACE,KAAe,EACf,OAAgB,EAChB,MAAyB,EACzB,YAAsB;QAEtB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAA;QACnB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAA;QACvB,IAAI,CAAC,UAAU,GAAG,MAAM,CAAA;QACxB,IAAI,CAAC,aAAa,GAAG,YAAY,CAAA;IACnC,CAAC;IACK,qCAAY,GAAlB,UACE,SAAkC,EAClC,kBAA4B;;;;;4BAEf,qBAAM,IAAI,CAAC,gBAAgB,CAAC,SAAS,EAAE,CAAC,CAAC,kBAAkB,CAAC,EAAA;;wBAAnE,IAAI,GAAG,SAA4D;wBACzE,IACE,CAAC,IAAI,CAAC,aAAa;4BACnB,IAAI;4BACJ,IAAI,YAAY,kBAAQ;4BACxB,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,GAAG,IAAI,IAAI,CAAC,OAAO,EAAE,GAAG,GAAG,CAAC;4BAEhD,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,SAAS,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AA8D;4CAC9D,IAAI,IAAI,CAAC,aAAa;gDACpB,sBAAO;wDACL,iBAAiB,EAAE,UAAU,CAAC,GAAG,CAAC,UAAC,EAAE,IAAK,OAAA,CAAC,EAAE,cAAc,EAAE,EAAE,EAAE,CAAC,EAAxB,CAAwB,CAAC;qDACpE,EAAA;4CACH,sBAAO,kBAAQ,CAAC,eAAe,CAAC,IAAI,CAAC,EAAA;;;iCACtC,CAAC,EAAA;;oBArCF,8BAA8B;oBAC9B,kCAAkC;oBAClC,sBAAO,SAmCL,EAAA;4BAEF,sBAAO,kBAAQ,CAAC,kBAAkB,CAChC,iBAAO,CAAC,sBAAsB,EAC9B,iBAAO,CAAC,mBAAmB,CAC5B,EAAA,CAAC,2BAA2B;;;;KAChC;IACH,qBAAC;AAAD,CAAC,AAhFD,IAgFC"}
|
|
@@ -3,27 +3,27 @@ import Request from "../API/Request";
|
|
|
3
3
|
import Response from "../API/Response";
|
|
4
4
|
import Logger, { LoggerConfig } from "../Logger/Logger";
|
|
5
5
|
import Publisher, { PublisherConfig } from "../Publisher/Publisher";
|
|
6
|
-
export type TransactionExecution<T> = (transaction: T) => Promise<Response | SQSBatchResponse>;
|
|
6
|
+
export type TransactionExecution<T, B = null> = (transaction: T) => Promise<Response<B> | SQSBatchResponse>;
|
|
7
7
|
export type TransactionConfig = {
|
|
8
8
|
throwOnErrors?: boolean;
|
|
9
9
|
syncReturn?: boolean;
|
|
10
10
|
logger?: LoggerConfig;
|
|
11
11
|
publisher?: PublisherConfig;
|
|
12
12
|
};
|
|
13
|
-
export default class Transaction<T = object> {
|
|
14
|
-
private
|
|
15
|
-
private
|
|
16
|
-
private
|
|
17
|
-
private
|
|
18
|
-
private
|
|
13
|
+
export default class Transaction<T = object, B = null> {
|
|
14
|
+
private event;
|
|
15
|
+
private context;
|
|
16
|
+
private response;
|
|
17
|
+
private syncReturn;
|
|
18
|
+
private retrowErrors;
|
|
19
19
|
readonly logger: Logger;
|
|
20
20
|
readonly request: Request<T>;
|
|
21
21
|
readonly publisher: Publisher;
|
|
22
22
|
responseProxy: (response: Response) => Promise<void>;
|
|
23
23
|
constructor(event: APIGatewayEvent | SQSEvent, context: Context, config?: TransactionConfig);
|
|
24
|
-
execute(executionFunc: TransactionExecution<Transaction<T>>): Promise<Response | null | SQSBatchResponse>;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
execute(executionFunc: TransactionExecution<Transaction<T, B>>): Promise<Response | null | SQSBatchResponse>;
|
|
25
|
+
private iexecute;
|
|
26
|
+
private executeDBTransactions;
|
|
27
|
+
private executeLoggerFlush;
|
|
28
|
+
private getErrorResponse;
|
|
29
29
|
}
|
|
@@ -50,17 +50,17 @@ var Transaction = /** @class */ (function () {
|
|
|
50
50
|
? event.requestContext.requestId
|
|
51
51
|
: "unknown";
|
|
52
52
|
// transaction ctx
|
|
53
|
-
this.
|
|
54
|
-
this.
|
|
55
|
-
this.
|
|
53
|
+
this.event = event;
|
|
54
|
+
this.context = context;
|
|
55
|
+
this.response = null;
|
|
56
56
|
// when set, this will be called with the response context right before calling the context suceed/fail - useful for writing the resp for example.
|
|
57
57
|
this.responseProxy = null;
|
|
58
58
|
// transaction flags
|
|
59
|
-
this.
|
|
60
|
-
this.
|
|
59
|
+
this.syncReturn = !!(config === null || config === void 0 ? void 0 : config.syncReturn);
|
|
60
|
+
this.retrowErrors = !!(config === null || config === void 0 ? void 0 : config.throwOnErrors); /* retrow internal errors */
|
|
61
61
|
// components
|
|
62
62
|
this.logger = new Logger_1.default(config === null || config === void 0 ? void 0 : config.logger, transactionId);
|
|
63
|
-
this.request = new Request_1.default(this.
|
|
63
|
+
this.request = new Request_1.default(this.event, this.context, this.logger);
|
|
64
64
|
this.publisher = new Publisher_1.default(config === null || config === void 0 ? void 0 : config.publisher);
|
|
65
65
|
}
|
|
66
66
|
//Main interface
|
|
@@ -69,14 +69,14 @@ var Transaction = /** @class */ (function () {
|
|
|
69
69
|
var _this = this;
|
|
70
70
|
return __generator(this, function (_a) {
|
|
71
71
|
switch (_a.label) {
|
|
72
|
-
case 0: return [4 /*yield*/, this.
|
|
72
|
+
case 0: return [4 /*yield*/, this.executeLoggerFlush(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
73
73
|
var _this = this;
|
|
74
74
|
return __generator(this, function (_a) {
|
|
75
75
|
switch (_a.label) {
|
|
76
|
-
case 0: return [4 /*yield*/, this.
|
|
76
|
+
case 0: return [4 /*yield*/, this.executeDBTransactions(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
77
77
|
return __generator(this, function (_a) {
|
|
78
78
|
switch (_a.label) {
|
|
79
|
-
case 0: return [4 /*yield*/, this.
|
|
79
|
+
case 0: return [4 /*yield*/, this.iexecute(executionFunc)];
|
|
80
80
|
case 1: return [2 /*return*/, _a.sent()];
|
|
81
81
|
}
|
|
82
82
|
});
|
|
@@ -92,8 +92,8 @@ var Transaction = /** @class */ (function () {
|
|
|
92
92
|
case 1:
|
|
93
93
|
_a.sent();
|
|
94
94
|
// return raw response if sync return is requested
|
|
95
|
-
if (this.
|
|
96
|
-
return [2 /*return*/, this.
|
|
95
|
+
if (this.syncReturn)
|
|
96
|
+
return [2 /*return*/, this.response
|
|
97
97
|
// allow request to async succeed through lambda context
|
|
98
98
|
];
|
|
99
99
|
// allow request to async succeed through lambda context
|
|
@@ -103,7 +103,7 @@ var Transaction = /** @class */ (function () {
|
|
|
103
103
|
});
|
|
104
104
|
};
|
|
105
105
|
//Executions
|
|
106
|
-
Transaction.prototype.
|
|
106
|
+
Transaction.prototype.iexecute = function (executionFunc) {
|
|
107
107
|
return __awaiter(this, void 0, void 0, function () {
|
|
108
108
|
var executionFailed, _a, e_1;
|
|
109
109
|
return __generator(this, function (_b) {
|
|
@@ -121,22 +121,22 @@ var Transaction = /** @class */ (function () {
|
|
|
121
121
|
//Answer client
|
|
122
122
|
];
|
|
123
123
|
case 2:
|
|
124
|
-
_a.
|
|
125
|
-
if (!(this.
|
|
126
|
-
return [4 /*yield*/, this.
|
|
124
|
+
_a.response = _b.sent();
|
|
125
|
+
if (!(this.response && this.response instanceof Response_1.default)) return [3 /*break*/, 4];
|
|
126
|
+
return [4 /*yield*/, this.response.build(this.context, this, this.syncReturn)];
|
|
127
127
|
case 3:
|
|
128
128
|
_b.sent();
|
|
129
|
-
executionFailed = !!(this.
|
|
129
|
+
executionFailed = !!(this.response.getBody() && this.response.getBody().rollback);
|
|
130
130
|
return [3 /*break*/, 7];
|
|
131
131
|
case 4:
|
|
132
|
-
if (!(this.
|
|
132
|
+
if (!(this.syncReturn && this.response)) return [3 /*break*/, 5];
|
|
133
133
|
this.logger.log("Sync return with different response object");
|
|
134
|
-
this.logger.debug(this.
|
|
134
|
+
this.logger.debug(this.response);
|
|
135
135
|
executionFailed = false;
|
|
136
136
|
return [3 /*break*/, 7];
|
|
137
137
|
case 5:
|
|
138
|
-
this.
|
|
139
|
-
return [4 /*yield*/, this.
|
|
138
|
+
this.response = this.getErrorResponse(Globals_1.default.ErrorResponseInvalidServerResponse, Globals_1.default.ErrorCode_APIError);
|
|
139
|
+
return [4 /*yield*/, this.response.build(this.context, this, this.syncReturn)];
|
|
140
140
|
case 6:
|
|
141
141
|
_b.sent();
|
|
142
142
|
this.logger.error("Invalid response object from main request code.");
|
|
@@ -148,11 +148,11 @@ var Transaction = /** @class */ (function () {
|
|
|
148
148
|
this.logger.error("Exception when executing main request code.");
|
|
149
149
|
this.logger.exception(e_1);
|
|
150
150
|
//retrow?
|
|
151
|
-
if (this.
|
|
151
|
+
if (this.retrowErrors)
|
|
152
152
|
throw e_1;
|
|
153
153
|
if (!executionFailed) return [3 /*break*/, 10];
|
|
154
|
-
this.
|
|
155
|
-
return [4 /*yield*/, this.
|
|
154
|
+
this.response = this.getErrorResponse(Globals_1.default.ErrorResponseUnhandledError, Globals_1.default.ErrorCode_APIError);
|
|
155
|
+
return [4 /*yield*/, this.response.build(this.context, this, this.syncReturn)];
|
|
156
156
|
case 9:
|
|
157
157
|
_b.sent();
|
|
158
158
|
_b.label = 10;
|
|
@@ -162,7 +162,7 @@ var Transaction = /** @class */ (function () {
|
|
|
162
162
|
});
|
|
163
163
|
});
|
|
164
164
|
};
|
|
165
|
-
Transaction.prototype.
|
|
165
|
+
Transaction.prototype.executeDBTransactions = function (safeExecution) {
|
|
166
166
|
return __awaiter(this, void 0, void 0, function () {
|
|
167
167
|
var e_2;
|
|
168
168
|
return __generator(this, function (_a) {
|
|
@@ -200,7 +200,7 @@ var Transaction = /** @class */ (function () {
|
|
|
200
200
|
this.logger.error("Exception when executing DB transactions.");
|
|
201
201
|
this.logger.log(e_2.stack);
|
|
202
202
|
//retrow?
|
|
203
|
-
if (this.
|
|
203
|
+
if (this.retrowErrors)
|
|
204
204
|
throw e_2;
|
|
205
205
|
return [3 /*break*/, 3];
|
|
206
206
|
case 3: return [2 /*return*/];
|
|
@@ -208,7 +208,7 @@ var Transaction = /** @class */ (function () {
|
|
|
208
208
|
});
|
|
209
209
|
});
|
|
210
210
|
};
|
|
211
|
-
Transaction.prototype.
|
|
211
|
+
Transaction.prototype.executeLoggerFlush = function (safeExecution) {
|
|
212
212
|
return __awaiter(this, void 0, void 0, function () {
|
|
213
213
|
var e_3;
|
|
214
214
|
return __generator(this, function (_a) {
|
|
@@ -227,7 +227,7 @@ var Transaction = /** @class */ (function () {
|
|
|
227
227
|
this.logger.error("Exception when flushing logs.");
|
|
228
228
|
this.logger.exception(e_3);
|
|
229
229
|
//retrow?
|
|
230
|
-
if (this.
|
|
230
|
+
if (this.retrowErrors)
|
|
231
231
|
throw e_3;
|
|
232
232
|
return [3 /*break*/, 5];
|
|
233
233
|
case 4:
|
|
@@ -239,7 +239,7 @@ var Transaction = /** @class */ (function () {
|
|
|
239
239
|
});
|
|
240
240
|
};
|
|
241
241
|
/* Response support */
|
|
242
|
-
Transaction.prototype.
|
|
242
|
+
Transaction.prototype.getErrorResponse = function (error, code) {
|
|
243
243
|
return Response_1.default.BadRequestResponseWithRollback(error, code);
|
|
244
244
|
};
|
|
245
245
|
return Transaction;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Transaction.js","sourceRoot":"","sources":["../../../src/BaseEvent/Transaction.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAOA,0CAAoC;AACpC,4CAAsC;AACtC,sCAAgC;AAChC,2CAAuD;AACvD,oDAAmE;AAenE;IAWE,EAAE;IACF,qBACE,KAAiC,EACjC,OAAgB,EAChB,MAA0B;QAE1B,IAAM,aAAa,GAAG,OAAO,CAAC,YAAY;YACxC,CAAC,CAAC,OAAO,CAAC,YAAY;YACtB,CAAC,CAAmB,KAAM,CAAC,cAAc;gBACzC,CAAC,CAAmB,KAAM,CAAC,cAAc,CAAC,SAAS;gBACnD,CAAC,CAAC,SAAS,CAAA;QACb,kBAAkB;QAClB,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"Transaction.js","sourceRoot":"","sources":["../../../src/BaseEvent/Transaction.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAOA,0CAAoC;AACpC,4CAAsC;AACtC,sCAAgC;AAChC,2CAAuD;AACvD,oDAAmE;AAenE;IAWE,EAAE;IACF,qBACE,KAAiC,EACjC,OAAgB,EAChB,MAA0B;QAE1B,IAAM,aAAa,GAAG,OAAO,CAAC,YAAY;YACxC,CAAC,CAAC,OAAO,CAAC,YAAY;YACtB,CAAC,CAAmB,KAAM,CAAC,cAAc;gBACzC,CAAC,CAAmB,KAAM,CAAC,cAAc,CAAC,SAAS;gBACnD,CAAC,CAAC,SAAS,CAAA;QACb,kBAAkB;QAClB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QAClB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAA;QACpB,kJAAkJ;QAClJ,IAAI,CAAC,aAAa,GAAG,IAAI,CAAA;QACzB,oBAAoB;QACpB,IAAI,C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AE7B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,+BAA+B,CAAC,CAAA;wBAClD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,GAAC,CAAC,CAAA;wBACxB,SAAS;wBACT,IAAI,IAAI,CAAC,YAAY;4BAAE,MAAM,GAAC,CAAA;;;wBAE9B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAA;;;;;;KAEzC;IACD,sBAAsB;IACd,sCAAgB,GAAxB,UAAyB,KAAa,EAAE,IAAY;QAClD,OAAO,kBAAQ,CAAC,8BAA8B,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;IAC7D,CAAC;IACH,kBAAC;AAAD,CAAC,AA1ID,IA0IC"}
|
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { ResponseErrorType } from "../API/Response";
|
|
2
3
|
import Transaction, { TransactionConfig, TransactionExecution } from "../BaseEvent/Transaction";
|
|
3
|
-
export interface Route<InputType = object> {
|
|
4
|
+
export interface Route<InputType = object, OutputType = any> {
|
|
4
5
|
path: string;
|
|
5
6
|
method: string;
|
|
6
|
-
handler: TransactionExecution<Transaction<InputType
|
|
7
|
-
|
|
8
|
-
outputValidation?: {
|
|
9
|
-
type: any;
|
|
10
|
-
validationSchema: z.ZodObject<any>;
|
|
11
|
-
};
|
|
7
|
+
handler: TransactionExecution<Transaction<InputType>, OutputType | ResponseErrorType>;
|
|
8
|
+
inputSchema?: z.ZodObject<any>;
|
|
12
9
|
}
|
|
13
10
|
export type RouterConfig = TransactionConfig & {
|
|
14
11
|
routes: Route[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Router.js","sourceRoot":"","sources":["../../../src/Server/Router.ts"],"names":[],"mappings":";;AAEA,yDAAmD;AACnD,uCAAiC;
|
|
1
|
+
{"version":3,"file":"Router.js","sourceRoot":"","sources":["../../../src/Server/Router.ts"],"names":[],"mappings":";;AAEA,yDAAmD;AACnD,uCAAiC;AAEjC,sCAAgC;AA+BhC;IAGE,gBAAY,MAAoB;QAC9B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,WAAW,EAAE;YAC9B,CAAC,CAAC,IAAI,yBAAe,CAAC,MAAM,CAAC;YAC7B,CAAC,CAAC,IAAI,gBAAM,CAAC,MAAM,CAAC,CAAA;IACxB,CAAC;IACM,0BAAS,GAAhB;QACE,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAA;IAChC,CAAC;IACO,4BAAW,GAAnB;QACE,OAAO,eAAK,CAAC,qBAAqB,EAAE,CAAA;IACtC,CAAC;IACH,aAAC;AAAD,CAAC,AAfD,IAeC"}
|
|
@@ -80,9 +80,9 @@ var Server = /** @class */ (function () {
|
|
|
80
80
|
handler = this.routeMatches(route.path, route.method, event);
|
|
81
81
|
if (!handler) return [3 /*break*/, 3];
|
|
82
82
|
transaction.logger.log("Router accepted route:", route);
|
|
83
|
-
// Validate input
|
|
84
|
-
if (route.
|
|
85
|
-
validationResp = Validator_1.default.validateSchema(transaction.request.getBody(), route.
|
|
83
|
+
// Validate input
|
|
84
|
+
if (route.inputSchema) {
|
|
85
|
+
validationResp = Validator_1.default.validateSchema(transaction.request.getBody(), route.inputSchema);
|
|
86
86
|
if (validationResp && validationResp instanceof Response_1.default)
|
|
87
87
|
return [2 /*return*/, validationResp];
|
|
88
88
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Server.js","sourceRoot":"","sources":["../../../../src/Server/lib/Server.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,6CAA8C;AAE9C,+CAAyC;AACzC,2DAAqD;AACrD,wDAAkD;AAGlD;IAEE,gBAAY,MAAoB;QAC9B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;IACtB,CAAC;IAEM,0BAAS,GAAhB;QACE,OAA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,7 +11,12 @@ class CustomError extends Error {
|
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
//
|
|
14
|
-
export
|
|
14
|
+
export type ResponseErrorType = {
|
|
15
|
+
err: string
|
|
16
|
+
errCode?: string
|
|
17
|
+
}
|
|
18
|
+
//
|
|
19
|
+
export default class Response<B = any | ResponseErrorType> {
|
|
15
20
|
private _statusCode: number
|
|
16
21
|
private _body: any
|
|
17
22
|
private _isPipingOut: boolean
|
|
@@ -23,7 +28,7 @@ export default class Response {
|
|
|
23
28
|
//
|
|
24
29
|
constructor(
|
|
25
30
|
statusCode: number,
|
|
26
|
-
body
|
|
31
|
+
body: B,
|
|
27
32
|
optBehaviour?:
|
|
28
33
|
| {
|
|
29
34
|
shouldStream?: boolean
|
|
@@ -52,7 +57,7 @@ export default class Response {
|
|
|
52
57
|
public getCode(): number {
|
|
53
58
|
return this._statusCode
|
|
54
59
|
}
|
|
55
|
-
public getBody():
|
|
60
|
+
public getBody(): B {
|
|
56
61
|
return this._body
|
|
57
62
|
}
|
|
58
63
|
public appendIntoBody(key: string, value: any): void {
|
|
@@ -118,25 +123,29 @@ export default class Response {
|
|
|
118
123
|
}
|
|
119
124
|
|
|
120
125
|
/* Shortcuts */
|
|
121
|
-
public static MissingParamResponse(
|
|
126
|
+
public static MissingParamResponse(
|
|
127
|
+
paramName: string,
|
|
128
|
+
): Response<ResponseErrorType> {
|
|
122
129
|
console.warn(`Invalid request - Path parameter ${paramName} is missing.`)
|
|
123
|
-
return new Response(400, {
|
|
130
|
+
return new Response<ResponseErrorType>(400, {
|
|
124
131
|
err: `Invalid request. Path parameter ${paramName} is missing.`,
|
|
125
132
|
errCode: Globals.ErrorCode_MissingParam,
|
|
126
133
|
})
|
|
127
134
|
}
|
|
128
|
-
public static MissingQueryResponse(
|
|
135
|
+
public static MissingQueryResponse(
|
|
136
|
+
paramName: string,
|
|
137
|
+
): Response<ResponseErrorType> {
|
|
129
138
|
console.warn(`Invalid request - Query parameter ${paramName} is missing.`)
|
|
130
|
-
return new Response(400, {
|
|
139
|
+
return new Response<ResponseErrorType>(400, {
|
|
131
140
|
err: `Invalid request. Query parameter ${paramName} is missing.`,
|
|
132
141
|
errCode: Globals.ErrorCode_MissingParam,
|
|
133
142
|
})
|
|
134
143
|
}
|
|
135
|
-
public static BadRequestResponse(
|
|
144
|
+
public static BadRequestResponse<T>(
|
|
136
145
|
msg?: string,
|
|
137
146
|
errCode?: string,
|
|
138
147
|
optBody?: any,
|
|
139
|
-
): Response {
|
|
148
|
+
): Response<T> {
|
|
140
149
|
console.warn(`Bad request - ${msg}`)
|
|
141
150
|
return new Response(400, {
|
|
142
151
|
err: msg,
|
|
@@ -148,32 +157,35 @@ export default class Response {
|
|
|
148
157
|
msg: string,
|
|
149
158
|
errCode: string,
|
|
150
159
|
optBody?: any,
|
|
151
|
-
): Response {
|
|
160
|
+
): Response<ResponseErrorType> {
|
|
152
161
|
console.warn(`Bad request - ${msg}`)
|
|
153
|
-
return new Response(400, {
|
|
162
|
+
return new Response<ResponseErrorType>(400, {
|
|
154
163
|
err: msg,
|
|
155
164
|
rollback: true,
|
|
156
165
|
...(errCode ? { errCode: errCode } : {}),
|
|
157
166
|
...(optBody || {}),
|
|
158
167
|
})
|
|
159
168
|
}
|
|
160
|
-
public static UnauthorizedResponse(
|
|
169
|
+
public static UnauthorizedResponse(
|
|
170
|
+
msg: string,
|
|
171
|
+
errCode: string,
|
|
172
|
+
): Response<ResponseErrorType> {
|
|
161
173
|
console.warn(`Denying request - ${msg}`)
|
|
162
|
-
return new Response(401, {
|
|
174
|
+
return new Response<ResponseErrorType>(401, {
|
|
163
175
|
err: msg,
|
|
164
176
|
...(errCode ? { errCode: errCode } : {}),
|
|
165
177
|
})
|
|
166
178
|
}
|
|
167
|
-
public static SuccessResponse(body
|
|
168
|
-
return new Response(200, body ? body : {})
|
|
179
|
+
public static SuccessResponse<T>(body: T): Response<T> {
|
|
180
|
+
return new Response<T>(200, (body ? body : {}) as T)
|
|
169
181
|
}
|
|
170
|
-
public static RedirectResponse(url: string): Response {
|
|
171
|
-
const resp = new Response(302)
|
|
182
|
+
public static RedirectResponse(url: string): Response<null> {
|
|
183
|
+
const resp = new Response<null>(302, null)
|
|
172
184
|
resp.appendHeader("Location", url)
|
|
173
185
|
return resp
|
|
174
186
|
}
|
|
175
|
-
public static SuccessNoContentResponse(): Response {
|
|
176
|
-
return new Response(204, null)
|
|
187
|
+
public static SuccessNoContentResponse(): Response<null> {
|
|
188
|
+
return new Response<null>(204, null)
|
|
177
189
|
}
|
|
178
190
|
public static SuccessStreamResponse(
|
|
179
191
|
stream: any,
|
|
@@ -186,18 +198,8 @@ export default class Response {
|
|
|
186
198
|
if (contentType) resp.appendHeader("Content-type", contentType)
|
|
187
199
|
return resp
|
|
188
200
|
}
|
|
189
|
-
public static
|
|
190
|
-
|
|
191
|
-
optionalCode?: number,
|
|
192
|
-
): Response {
|
|
193
|
-
const resp = new Response(optionalCode || 200, body, {
|
|
194
|
-
rawBody: true,
|
|
195
|
-
throwOnErrors: true,
|
|
196
|
-
})
|
|
197
|
-
return resp
|
|
198
|
-
}
|
|
199
|
-
public static SimpleResponse(body: any, optionalCode?: number): Response {
|
|
200
|
-
const resp = new Response(optionalCode || 200, body)
|
|
201
|
+
public static SimpleResponse<T>(body: T, optionalCode?: number): Response<T> {
|
|
202
|
+
const resp = new Response<T>(optionalCode || 200, body)
|
|
201
203
|
return resp
|
|
202
204
|
}
|
|
203
205
|
}
|
|
@@ -12,9 +12,9 @@ import Logger, { LoggerConfig } from "../Logger/Logger"
|
|
|
12
12
|
import Publisher, { PublisherConfig } from "../Publisher/Publisher"
|
|
13
13
|
|
|
14
14
|
// Request
|
|
15
|
-
export type TransactionExecution<T> = (
|
|
15
|
+
export type TransactionExecution<T, B = null> = (
|
|
16
16
|
transaction: T,
|
|
17
|
-
) => Promise<Response | SQSBatchResponse>
|
|
17
|
+
) => Promise<Response<B> | SQSBatchResponse>
|
|
18
18
|
// Config
|
|
19
19
|
export type TransactionConfig = {
|
|
20
20
|
throwOnErrors?: boolean
|
|
@@ -24,12 +24,12 @@ export type TransactionConfig = {
|
|
|
24
24
|
publisher?: PublisherConfig
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
export default class Transaction<T = object> {
|
|
28
|
-
private
|
|
29
|
-
private
|
|
30
|
-
private
|
|
31
|
-
private
|
|
32
|
-
private
|
|
27
|
+
export default class Transaction<T = object, B = null> {
|
|
28
|
+
private event: any
|
|
29
|
+
private context: Context
|
|
30
|
+
private response: Response | null | SQSBatchResponse
|
|
31
|
+
private syncReturn: boolean
|
|
32
|
+
private retrowErrors: boolean
|
|
33
33
|
//
|
|
34
34
|
public readonly logger: Logger
|
|
35
35
|
public readonly request: Request<T>
|
|
@@ -47,36 +47,36 @@ export default class Transaction<T = object> {
|
|
|
47
47
|
? (<APIGatewayEvent>event).requestContext.requestId
|
|
48
48
|
: "unknown"
|
|
49
49
|
// transaction ctx
|
|
50
|
-
this.
|
|
51
|
-
this.
|
|
52
|
-
this.
|
|
50
|
+
this.event = event
|
|
51
|
+
this.context = context
|
|
52
|
+
this.response = null
|
|
53
53
|
// when set, this will be called with the response context right before calling the context suceed/fail - useful for writing the resp for example.
|
|
54
54
|
this.responseProxy = null
|
|
55
55
|
// transaction flags
|
|
56
|
-
this.
|
|
57
|
-
this.
|
|
56
|
+
this.syncReturn = !!config?.syncReturn
|
|
57
|
+
this.retrowErrors = !!config?.throwOnErrors /* retrow internal errors */
|
|
58
58
|
// components
|
|
59
59
|
this.logger = new Logger(config?.logger, transactionId)
|
|
60
|
-
this.request = new Request<T>(this.
|
|
60
|
+
this.request = new Request<T>(this.event, this.context, this.logger)
|
|
61
61
|
this.publisher = new Publisher(config?.publisher)
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
//Main interface
|
|
65
|
-
async execute(
|
|
66
|
-
executionFunc: TransactionExecution<Transaction<T>>,
|
|
65
|
+
public async execute(
|
|
66
|
+
executionFunc: TransactionExecution<Transaction<T, B>>,
|
|
67
67
|
): Promise<Response | null | SQSBatchResponse> {
|
|
68
|
-
await this.
|
|
69
|
-
await this.
|
|
70
|
-
return await this.
|
|
68
|
+
await this.executeLoggerFlush(async () => {
|
|
69
|
+
await this.executeDBTransactions(async () => {
|
|
70
|
+
return await this.iexecute(executionFunc)
|
|
71
71
|
})
|
|
72
72
|
})
|
|
73
73
|
// return raw response if sync return is requested
|
|
74
|
-
if (this.
|
|
74
|
+
if (this.syncReturn) return this.response
|
|
75
75
|
// allow request to async succeed through lambda context
|
|
76
76
|
return null
|
|
77
77
|
}
|
|
78
78
|
//Executions
|
|
79
|
-
async
|
|
79
|
+
private async iexecute(
|
|
80
80
|
executionFunc: TransactionExecution<Transaction<T>>,
|
|
81
81
|
): Promise<boolean> {
|
|
82
82
|
let executionFailed = true //failed til we say no!
|
|
@@ -84,23 +84,23 @@ export default class Transaction<T = object> {
|
|
|
84
84
|
try {
|
|
85
85
|
//Execute
|
|
86
86
|
this.logger.debug("Starting main request code")
|
|
87
|
-
this.
|
|
87
|
+
this.response = await executionFunc(this)
|
|
88
88
|
//Answer client
|
|
89
|
-
if (this.
|
|
90
|
-
await this.
|
|
89
|
+
if (this.response && this.response instanceof Response) {
|
|
90
|
+
await this.response.build(this.context, this, this.syncReturn)
|
|
91
91
|
executionFailed = !!(
|
|
92
|
-
this.
|
|
92
|
+
this.response.getBody() && this.response.getBody().rollback
|
|
93
93
|
)
|
|
94
|
-
} else if (this.
|
|
94
|
+
} else if (this.syncReturn && this.response) {
|
|
95
95
|
this.logger.log("Sync return with different response object")
|
|
96
|
-
this.logger.debug(this.
|
|
96
|
+
this.logger.debug(this.response)
|
|
97
97
|
executionFailed = false
|
|
98
98
|
} else {
|
|
99
|
-
this.
|
|
99
|
+
this.response = this.getErrorResponse(
|
|
100
100
|
Globals.ErrorResponseInvalidServerResponse,
|
|
101
101
|
Globals.ErrorCode_APIError,
|
|
102
102
|
)
|
|
103
|
-
await this.
|
|
103
|
+
await this.response.build(this.context, this, this.syncReturn)
|
|
104
104
|
this.logger.error("Invalid response object from main request code.")
|
|
105
105
|
}
|
|
106
106
|
} catch (e) {
|
|
@@ -108,19 +108,19 @@ export default class Transaction<T = object> {
|
|
|
108
108
|
this.logger.error("Exception when executing main request code.")
|
|
109
109
|
this.logger.exception(e)
|
|
110
110
|
//retrow?
|
|
111
|
-
if (this.
|
|
111
|
+
if (this.retrowErrors) throw e
|
|
112
112
|
//envelope exception?
|
|
113
113
|
if (executionFailed) {
|
|
114
|
-
this.
|
|
114
|
+
this.response = this.getErrorResponse(
|
|
115
115
|
Globals.ErrorResponseUnhandledError,
|
|
116
116
|
Globals.ErrorCode_APIError,
|
|
117
117
|
)
|
|
118
|
-
await this.
|
|
118
|
+
await this.response.build(this.context, this, this.syncReturn)
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
return executionFailed
|
|
122
122
|
}
|
|
123
|
-
async
|
|
123
|
+
private async executeDBTransactions(safeExecution) {
|
|
124
124
|
try {
|
|
125
125
|
// //start DB
|
|
126
126
|
// if (this.db) await this.db.connect();
|
|
@@ -142,10 +142,10 @@ export default class Transaction<T = object> {
|
|
|
142
142
|
this.logger.error("Exception when executing DB transactions.")
|
|
143
143
|
this.logger.log(e.stack)
|
|
144
144
|
//retrow?
|
|
145
|
-
if (this.
|
|
145
|
+
if (this.retrowErrors) throw e
|
|
146
146
|
}
|
|
147
147
|
}
|
|
148
|
-
async
|
|
148
|
+
private async executeLoggerFlush(safeExecution) {
|
|
149
149
|
try {
|
|
150
150
|
await safeExecution()
|
|
151
151
|
await this.logger.flushLogs()
|
|
@@ -153,13 +153,13 @@ export default class Transaction<T = object> {
|
|
|
153
153
|
this.logger.error("Exception when flushing logs.")
|
|
154
154
|
this.logger.exception(e)
|
|
155
155
|
//retrow?
|
|
156
|
-
if (this.
|
|
156
|
+
if (this.retrowErrors) throw e
|
|
157
157
|
} finally {
|
|
158
158
|
this.logger.debug("Transaction ended")
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
161
|
/* Response support */
|
|
162
|
-
|
|
162
|
+
private getErrorResponse(error: string, code: string): Response {
|
|
163
163
|
return Response.BadRequestResponseWithRollback(error, code)
|
|
164
164
|
}
|
|
165
165
|
}
|
package/src/Server/Router.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { z } from "zod"
|
|
|
2
2
|
|
|
3
3
|
import ContainerServer from "./lib/ContainerServer"
|
|
4
4
|
import Server from "./lib/Server"
|
|
5
|
+
import { ResponseErrorType } from "../API/Response"
|
|
5
6
|
import Utils from "../API/Utils"
|
|
6
7
|
import Transaction, {
|
|
7
8
|
TransactionConfig,
|
|
@@ -9,15 +10,14 @@ import Transaction, {
|
|
|
9
10
|
} from "../BaseEvent/Transaction"
|
|
10
11
|
|
|
11
12
|
// Route
|
|
12
|
-
export interface Route<InputType = object> {
|
|
13
|
+
export interface Route<InputType = object, OutputType = any> {
|
|
13
14
|
path: string
|
|
14
15
|
method: string
|
|
15
|
-
handler: TransactionExecution<
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
16
|
+
handler: TransactionExecution<
|
|
17
|
+
Transaction<InputType>,
|
|
18
|
+
OutputType | ResponseErrorType
|
|
19
|
+
>
|
|
20
|
+
inputSchema?: z.ZodObject<any>
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
// Config
|
package/src/Server/lib/Server.ts
CHANGED
|
@@ -28,11 +28,11 @@ export default class Server {
|
|
|
28
28
|
const handler = this.routeMatches(route.path, route.method, event)
|
|
29
29
|
if (handler) {
|
|
30
30
|
transaction.logger.log("Router accepted route:", route)
|
|
31
|
-
// Validate input
|
|
32
|
-
if (route.
|
|
31
|
+
// Validate input
|
|
32
|
+
if (route.inputSchema) {
|
|
33
33
|
const validationResp = Validator.validateSchema(
|
|
34
34
|
transaction.request.getBody(),
|
|
35
|
-
route.
|
|
35
|
+
route.inputSchema,
|
|
36
36
|
)
|
|
37
37
|
if (validationResp && validationResp instanceof Response)
|
|
38
38
|
return validationResp
|