@ikonintegration/ikapi 2.4.12 → 2.5.2
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 +1 -0
- package/package.json +1 -1
- package/src/API/IKResponse.js +22 -8
- package/src/BaseEvent/IKTransaction.js +14 -10
- package/src/IKStepTransaction.js +13 -0
- package/src/Mailer/IKMailer.js +0 -3
package/index.js
CHANGED
|
@@ -12,6 +12,7 @@ module.exports = {
|
|
|
12
12
|
IKRouter: require("./src/IKRouter.js").default,
|
|
13
13
|
IKEventProcessor: require('./src/IKEventProcessor.js').default,
|
|
14
14
|
IKDynamoStream: require('./src/IKDynamoStream.js').default,
|
|
15
|
+
IKStepTransaction: require('./src/IKStepTransaction.js').default,
|
|
15
16
|
|
|
16
17
|
//Extra components
|
|
17
18
|
IKMailer: require('./src/Mailer/IKMailer.js').default,
|
package/package.json
CHANGED
package/src/API/IKResponse.js
CHANGED
|
@@ -4,9 +4,11 @@ export default class IKResponse {
|
|
|
4
4
|
constructor(statusCode, body) {
|
|
5
5
|
this._statusCode = statusCode;
|
|
6
6
|
this._body = body;
|
|
7
|
+
//
|
|
7
8
|
this._streamingOut = false;
|
|
8
9
|
this._isStream = false;
|
|
9
|
-
this.
|
|
10
|
+
this._contextRawBody = false;
|
|
11
|
+
//
|
|
10
12
|
this._headers = {
|
|
11
13
|
"Access-Control-Allow-Origin": "*",
|
|
12
14
|
"Access-Control-Allow-Credentials": true,
|
|
@@ -18,10 +20,12 @@ export default class IKResponse {
|
|
|
18
20
|
appendIntoBody(key, value) { this._body[key] = value; }
|
|
19
21
|
appendHeader(key, value) { this._headers[key] = value; }
|
|
20
22
|
build(context, requestID, onQueue, transaction) {
|
|
21
|
-
//
|
|
23
|
+
//Stream support
|
|
22
24
|
if (this._streamingOut) return;
|
|
23
25
|
if (this._isStream) return this._pipe(context, requestID);
|
|
24
|
-
|
|
26
|
+
//Raw response support
|
|
27
|
+
if (this._contextRawBody) return this.rawContext(context, requestID, transaction);
|
|
28
|
+
|
|
25
29
|
//append default fields
|
|
26
30
|
if (requestID && this._body) this.appendIntoBody('transactionID', requestID); //append transaction ID
|
|
27
31
|
//build response
|
|
@@ -46,12 +50,22 @@ export default class IKResponse {
|
|
|
46
50
|
//log response and respond to context
|
|
47
51
|
context.succeed(b);
|
|
48
52
|
}
|
|
49
|
-
|
|
53
|
+
rawContext(context, requestID, transaction) {
|
|
50
54
|
//append default fields
|
|
51
55
|
if(requestID && this._body) this.appendIntoBody('transactionID', requestID); //append transaction ID
|
|
52
56
|
//log response and respond to context
|
|
53
57
|
transaction.logger.debug(this._body)
|
|
54
|
-
context.succeed(this._body);
|
|
58
|
+
if (this.getCode() <= 200 && this.getCode() <= 299) context.succeed(this._body);
|
|
59
|
+
else {
|
|
60
|
+
//
|
|
61
|
+
function CustomError(message) {
|
|
62
|
+
this.name = this._body && this._body.error ? this._body.error : 'UnknownError';
|
|
63
|
+
this.message = message;
|
|
64
|
+
}
|
|
65
|
+
CustomError.prototype = new Error();
|
|
66
|
+
//
|
|
67
|
+
context.fail(new CustomError(this.body));
|
|
68
|
+
}
|
|
55
69
|
}
|
|
56
70
|
}
|
|
57
71
|
|
|
@@ -91,8 +105,8 @@ export function IKSuccessStreamResponse(stream, contentType) {
|
|
|
91
105
|
if (contentType) resp.appendHeader('Content-type', contentType);
|
|
92
106
|
return resp;
|
|
93
107
|
}
|
|
94
|
-
export function IKStepFunctionResponse(body) {
|
|
95
|
-
const resp = new IKResponse(200, body);
|
|
96
|
-
resp.
|
|
108
|
+
export function IKStepFunctionResponse(body, optionalCode) {
|
|
109
|
+
const resp = new IKResponse(optionalCode || 200, body);
|
|
110
|
+
resp._contextRawBody = true;
|
|
97
111
|
return resp;
|
|
98
112
|
}
|
|
@@ -22,7 +22,7 @@ export default class IKTransaction {
|
|
|
22
22
|
this._config = config;
|
|
23
23
|
//queue support
|
|
24
24
|
this._onQueue = _isQueue;
|
|
25
|
-
this.
|
|
25
|
+
this._resp = null;
|
|
26
26
|
//
|
|
27
27
|
this.logger = new IKLogger({/*COFIG S3/SQS HERE*/}, Utils.logLevel(), (context.awsRequestId ? context.awsRequestId : (event.requestContext ? event.requestContext.requestId : 'unknown')));
|
|
28
28
|
this.request = new IKRequest(this._event, this._context, this);
|
|
@@ -39,7 +39,7 @@ export default class IKTransaction {
|
|
|
39
39
|
return await this._execute(executionFunc);
|
|
40
40
|
});
|
|
41
41
|
});
|
|
42
|
-
if (this._onQueue) return this.
|
|
42
|
+
if (this._onQueue) return this._resp;
|
|
43
43
|
}
|
|
44
44
|
//Executions
|
|
45
45
|
async _execute(executionFunc) {
|
|
@@ -47,22 +47,22 @@ export default class IKTransaction {
|
|
|
47
47
|
//safe execution handler
|
|
48
48
|
try { //Execute
|
|
49
49
|
this.logger.debug("Starting main request code");
|
|
50
|
-
this.
|
|
50
|
+
this._resp = await executionFunc(this);
|
|
51
51
|
//Answer client
|
|
52
|
-
if (this.
|
|
53
|
-
this.
|
|
54
|
-
executionFailed = !!(this.
|
|
52
|
+
if (this._resp && this._resp instanceof IKResponse) {
|
|
53
|
+
this._resp.build(this._context, this.request.getRequestID(), this._onQueue, this);
|
|
54
|
+
executionFailed = !!(this._resp.getBody() && this._resp.getBody().rollback);
|
|
55
55
|
} else {
|
|
56
|
-
this.
|
|
57
|
-
this.
|
|
56
|
+
this._resp = this._getErrorResponse(IKGlobals.ErrorResponseInvalidServerResponse, IKGlobals.ErrorCode_APIError)
|
|
57
|
+
this._resp.build(this._context, this.request.getRequestID(), this._onQueue, this);
|
|
58
58
|
this.logger.error("Invalid response object from main request code.");
|
|
59
59
|
}
|
|
60
60
|
} catch (e) { /*EXECUTION FAIL*/
|
|
61
61
|
this.logger.error('Exception when executing main request code.');
|
|
62
62
|
this.logger.exception(e);
|
|
63
63
|
if (executionFailed) {
|
|
64
|
-
this.
|
|
65
|
-
this.
|
|
64
|
+
this._resp = this._getErrorResponse(IKGlobals.ErrorResponseUnhandledError, IKGlobals.ErrorCode_APIError);
|
|
65
|
+
this._resp.build(this._context, this.request.getRequestID(), this._onQueue, this);
|
|
66
66
|
}
|
|
67
67
|
} return executionFailed;
|
|
68
68
|
}
|
|
@@ -112,4 +112,8 @@ export default class IKTransaction {
|
|
|
112
112
|
if (this._config.cache.type == IKGlobals.CacheDrivers.REDIS) return new IKCacheRedis(this._config.cache, this);
|
|
113
113
|
} return null;
|
|
114
114
|
}
|
|
115
|
+
/* Response support */
|
|
116
|
+
_getErrorResponse(error, code) {
|
|
117
|
+
return IKBadRequestResponseWithRollback(error, code);
|
|
118
|
+
}
|
|
115
119
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//
|
|
2
|
+
import IKTransaction from './BaseEvent/IKTransaction';
|
|
3
|
+
import { IKStepFunctionResponse } from './API/IKResponse';
|
|
4
|
+
//
|
|
5
|
+
export default class IKStepTransaction extends IKTransaction {
|
|
6
|
+
constructor(event, context, config) {
|
|
7
|
+
super(event, context, config, false);
|
|
8
|
+
}
|
|
9
|
+
/* Response support */
|
|
10
|
+
_getErrorResponse(error, code) {
|
|
11
|
+
return IKStepFunctionResponse({ err: error, errCode: code }, 400);
|
|
12
|
+
}
|
|
13
|
+
}
|