@ikonintegration/ikapi 2.6.1 → 2.6.2-alpha3
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/index.js +2 -0
- package/package.json +1 -1
- package/src/API/IKResponse.js +17 -15
- package/src/BaseEvent/IKTransaction.js +7 -5
package/index.js
CHANGED
|
@@ -29,6 +29,8 @@ module.exports = {
|
|
|
29
29
|
IKSuccessNoContentResponse: require("./src/API/IKResponse.js").IKSuccessNoContentResponse,
|
|
30
30
|
IKBadRequestResponseWithRollback: require("./src/API/IKResponse.js").IKBadRequestResponseWithRollback,
|
|
31
31
|
IKStepFunctionResponse: require("./src/API/IKResponse.js").IKStepFunctionResponse,
|
|
32
|
+
IKSimpleResponse: require("./src/API/IKResponse.js").IKSimpleResponse,
|
|
33
|
+
|
|
32
34
|
//Helpers
|
|
33
35
|
Utils: require("./src/API/IKUtils.js").default,
|
|
34
36
|
|
package/package.json
CHANGED
package/src/API/IKResponse.js
CHANGED
|
@@ -21,40 +21,38 @@ export default class IKResponse {
|
|
|
21
21
|
getBody() { return this._body; }
|
|
22
22
|
appendIntoBody(key, value) { this._body[key] = value; }
|
|
23
23
|
appendHeader(key, value) { this._headers[key] = value; }
|
|
24
|
-
build(context,
|
|
24
|
+
async build(context, transaction, isBatch) {
|
|
25
25
|
//Stream support
|
|
26
26
|
if (this._streamingOut) return;
|
|
27
|
-
if (this._isStream) return this._pipe(context
|
|
28
|
-
//Raw response support
|
|
29
|
-
if (this._contextRawBody) return this.rawContext(context, requestID, transaction);
|
|
27
|
+
if (this._isStream) return this._pipe(context);
|
|
30
28
|
|
|
31
29
|
//append default fields
|
|
32
|
-
if (
|
|
30
|
+
if (transaction.request.getRequestID() && this._body) this.appendIntoBody('transactionID', transaction.request.getRequestID()); //append transaction ID
|
|
31
|
+
//Raw response support
|
|
32
|
+
if (this._contextRawBody) return this.rawContext(context, transaction);
|
|
33
|
+
|
|
33
34
|
//build response
|
|
34
35
|
let b = {
|
|
35
|
-
statusCode: this._statusCode,
|
|
36
|
+
statusCode: this._statusCode, headers: this._headers,
|
|
36
37
|
...(this._body ? { body: JSON.stringify(this._body) } : {}),
|
|
37
|
-
headers: this._headers
|
|
38
38
|
};
|
|
39
39
|
//log response and respond to context
|
|
40
40
|
transaction.logger.debug(b)
|
|
41
|
+
//Check for transaction response proxy
|
|
42
|
+
if (transaction.responseProxy) await transaction.responseProxy(b);
|
|
43
|
+
//Batch does not succeed directly just on upper transaction (which will should be a batch)
|
|
41
44
|
if (!isBatch) context.succeed(b);
|
|
42
45
|
}
|
|
43
|
-
_pipe(context
|
|
46
|
+
_pipe(context) {
|
|
44
47
|
//Check if not streaming
|
|
45
48
|
if (this._streamingOut) return;
|
|
46
49
|
this._streamingOut = true;
|
|
47
50
|
//build response
|
|
48
|
-
let b = {
|
|
49
|
-
statusCode: this._statusCode,
|
|
50
|
-
body: this._body, headers: this._headers
|
|
51
|
-
};
|
|
51
|
+
let b = { statusCode: this._statusCode, body: this._body, headers: this._headers };
|
|
52
52
|
//log response and respond to context
|
|
53
53
|
context.succeed(b);
|
|
54
54
|
}
|
|
55
|
-
rawContext(context,
|
|
56
|
-
//append default fields
|
|
57
|
-
if(requestID && this._body) this.appendIntoBody('transactionID', requestID); //append transaction ID
|
|
55
|
+
rawContext(context, transaction) {
|
|
58
56
|
//log response and respond to context
|
|
59
57
|
transaction.logger.debug(this._body)
|
|
60
58
|
if (this.getCode() <= 200 && this.getCode() <= 299) context.succeed(this._body);
|
|
@@ -114,3 +112,7 @@ export function IKStepFunctionResponse(body, optionalCode) {
|
|
|
114
112
|
resp._throwOnErrors = true;
|
|
115
113
|
return resp;
|
|
116
114
|
}
|
|
115
|
+
export function IKSimpleResponse(body, optionalCode) {
|
|
116
|
+
const resp = new IKResponse(optionalCode || 200, body);
|
|
117
|
+
return resp;
|
|
118
|
+
}
|
|
@@ -23,8 +23,10 @@ export default class IKTransaction {
|
|
|
23
23
|
//queue support
|
|
24
24
|
this._isBatch = _isBatch;
|
|
25
25
|
this._resp = null;
|
|
26
|
-
//
|
|
27
|
-
this._retrowErrors = _retrowErrors;
|
|
26
|
+
//Step function support
|
|
27
|
+
this._retrowErrors = _retrowErrors; /* retrow internal errors */
|
|
28
|
+
//When set, this will be called with the response context right before calling the context suceed/fail
|
|
29
|
+
this.responseProxy = null;
|
|
28
30
|
//
|
|
29
31
|
this.logger = new IKLogger({/*COFIG S3/SQS HERE*/}, Utils.logLevel(), (context.awsRequestId ? context.awsRequestId : (event.requestContext ? event.requestContext.requestId : 'unknown')));
|
|
30
32
|
this.request = new IKRequest(this._event, this._context, this);
|
|
@@ -52,11 +54,11 @@ export default class IKTransaction {
|
|
|
52
54
|
this._resp = await executionFunc(this);
|
|
53
55
|
//Answer client
|
|
54
56
|
if (this._resp && this._resp instanceof IKResponse) {
|
|
55
|
-
this._resp.build(this._context, this
|
|
57
|
+
await this._resp.build(this._context, this, this._isBatch);
|
|
56
58
|
executionFailed = !!(this._resp.getBody() && this._resp.getBody().rollback);
|
|
57
59
|
} else {
|
|
58
60
|
this._resp = this._getErrorResponse(IKGlobals.ErrorResponseInvalidServerResponse, IKGlobals.ErrorCode_APIError)
|
|
59
|
-
this._resp.build(this._context, this
|
|
61
|
+
await this._resp.build(this._context, this, this._isBatch);
|
|
60
62
|
this.logger.error("Invalid response object from main request code.");
|
|
61
63
|
}
|
|
62
64
|
} catch (e) { /*EXECUTION FAIL*/
|
|
@@ -67,7 +69,7 @@ export default class IKTransaction {
|
|
|
67
69
|
//envelope exception?
|
|
68
70
|
if (executionFailed) {
|
|
69
71
|
this._resp = this._getErrorResponse(IKGlobals.ErrorResponseUnhandledError, IKGlobals.ErrorCode_APIError);
|
|
70
|
-
this._resp.build(this._context, this
|
|
72
|
+
await this._resp.build(this._context, this, this._isBatch);
|
|
71
73
|
}
|
|
72
74
|
} return executionFailed;
|
|
73
75
|
}
|