@ikonintegration/ikapi 3.1.33 → 3.1.35

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ikonintegration/ikapi",
3
- "version": "3.1.33",
3
+ "version": "3.1.35",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "module": "main.js",
@@ -59,7 +59,7 @@ export default class IKTransaction {
59
59
  if (this._resp && this._resp instanceof IKResponse) {
60
60
  await this._resp.build(this._context, this, this._syncReturn);
61
61
  executionFailed = !!(this._resp.getBody() && this._resp.getBody().rollback);
62
- } else {
62
+ } else if (!this._syncReturn) { //sync return can return other than ikresponse
63
63
  this._resp = this._getErrorResponse(IKGlobals.ErrorResponseInvalidServerResponse, IKGlobals.ErrorCode_APIError)
64
64
  await this._resp.build(this._context, this, this._syncReturn);
65
65
  this.logger.error("Invalid response object from main request code.");
@@ -3,15 +3,15 @@ import IKResponse, { IKBadRequestResponse, IKSuccessResponse } from './API/IKRes
3
3
  import IKGlobals from './IKGlobals';
4
4
  //
5
5
  export default class IKEventProcessor {
6
- constructor(event, context, config, isBatch) {
6
+ constructor(event, context, config, respondWithFailures) {
7
7
  this.event = event;
8
8
  this.context = context;
9
9
  this.apiConfig = config;
10
- this._syncReturn = isBatch;
10
+ this._respondWithFailures = respondWithFailures;
11
11
  }
12
12
  async processEvent(execution, doNotDecodeMessage) {
13
13
  const resp = await this._processRawEvent(execution, doNotDecodeMessage);
14
- if (resp && !(resp.getCode() >= 200 && resp.getCode() < 300)) throw new Error(JSON.stringify(resp.getBody()));
14
+ if (!this._respondWithFailures && resp && !(resp.getCode() >= 200 && resp.getCode() < 300)) throw new Error(JSON.stringify(resp.getBody()));
15
15
  else if (resp) return resp;
16
16
  }
17
17
  async _processRawEvent(execution, doNotDecodeMessage) {
@@ -20,17 +20,21 @@ export default class IKEventProcessor {
20
20
  return await (new IKTransaction(this.event, this.context, this.apiConfig, true /*batch processing*/)).execute(async (transaction) => {
21
21
  //Map records with decoded message when required
22
22
  const decodedRecords = this.event.Records.map((eventRecord) => (doNotDecodeMessage ? eventRecord.body : JSON.parse(eventRecord.body)));
23
- //If is batch, return execution
24
- if (this._syncReturn) return await execution(transaction, decodedRecords);
23
+
25
24
  //for each available event
25
+ const failureIDs = [];
26
26
  for (let eventRecord of decodedRecords) {
27
27
  //Call execution
28
28
  let resp = await execution(transaction, eventRecord);
29
- if (!(resp.getCode() >= 200 && resp.getCode() < 300)) { //if execution failed, respond and mark as answered
30
- return resp;
29
+ //check for failure
30
+ if (!(resp.getCode() >= 200 && resp.getCode() < 300)) {
31
+ //response with failures or fail hard at first
32
+ if (this._respondWithFailures) failureIDs.push(eventRecord.messageId);
33
+ else return resp;
31
34
  }
32
35
  }
33
- //not errored and loop ended - succeeded
36
+ //not errored and loop ended - succeeded (might have failures)
37
+ if (this._respondWithFailures) return { batchItemFailures: failureIDs.map((id) => ({ itemIdentifier: id })) };
34
38
  return IKSuccessResponse();
35
39
  });
36
40
  } else return IKBadRequestResponse(IKGlobals.ErrorResponseNoRecords, IKGlobals.ErrorCode_NoRecords); //no event to be processed?