@ikonintegration/ikapi 4.0.0-alpha2 → 4.0.0-alpha20

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/README.md CHANGED
@@ -14,9 +14,10 @@ Ikon nodejs API foundation
14
14
  - Router
15
15
 
16
16
  ````
17
+ import * as Config from './routes/tenant/config.js';
17
18
 
18
19
  const routes = [
19
- { route: '/tenant/config', method: 'GET', handler: require('./routes/tenant/config').get },
20
+ { route: '/tenant/config', method: 'GET', handler: Config.get },
20
21
  ];
21
22
  //Main handler
22
23
  export const handler = async (event, context) => {
package/index.js CHANGED
@@ -13,7 +13,7 @@ import IKStepTransaction from "./src/IKStepTransaction.js";
13
13
  import IKMailer from "./src/Mailer/IKMailer.js";
14
14
  import IKValidation from "./src/Validation/IKValidation.js";
15
15
  //Responses
16
- import IKResponse from "./src/API/IKResponse.js";
16
+ import * as IKResponse from "./src/API/IKResponse.js";
17
17
  //Helpers
18
18
  import Utils from "./src/API/IKUtils.js";
19
19
  //Cache
@@ -46,7 +46,7 @@ export default {
46
46
  IKMailer,
47
47
  IKValidation,
48
48
  //Responses
49
- IKResponse,
49
+ IKResponse: IKResponse.default,
50
50
  IKBadRequestResponse: IKResponse.IKBadRequestResponse,
51
51
  IKUnauthorizedResponse: IKResponse.IKUnauthorizedResponse,
52
52
  IKMissingParamResponse: IKResponse.IKMissingParamResponse,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ikonintegration/ikapi",
3
- "version": "4.0.0-alpha2",
3
+ "version": "4.0.0-alpha20",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "module": "main.js",
@@ -32,6 +32,6 @@
32
32
  "pg": "^8.11.1",
33
33
  "sha1": "^1.1.1",
34
34
  "stack-trace": "0.0.10",
35
- "superstruct": "0.8.4"
35
+ "superstruct": "1.0.3"
36
36
  }
37
37
  }
@@ -99,7 +99,7 @@ export default class IKTransaction {
99
99
  if (this.db) await this.db.cleanup();
100
100
  } catch (e) {
101
101
  this.logger.error('Exception when executing DB transactions.');
102
- this.logger.log(e);
102
+ this.logger.log(e.stack);
103
103
  //retrow?
104
104
  if (this._retrowErrors) throw e;
105
105
  }
@@ -73,15 +73,18 @@ export default class IKCache_Redis extends IKCache {
73
73
  });
74
74
  }
75
75
  /* Private */
76
- _isRedisClientAvailable() {
76
+ async _isRedisClientAvailable() {
77
77
  let response = true;
78
- try { const test = require('redis'); }
78
+ try { const test = await import('redis'); }
79
79
  catch (e) { response = false; }
80
80
  return response;
81
81
  }
82
- _isRedisClientV4() {
82
+ async _isRedisClientV4() {
83
83
  let isV4 = false;
84
- try { isV4 = !!require('redis').createCluster; }
84
+ try {
85
+ const test = await import('redis');
86
+ isV4 = !!test.createCluster;
87
+ }
85
88
  catch (e) { }
86
89
  return isV4;
87
90
  }
@@ -93,7 +96,7 @@ export default class IKCache_Redis extends IKCache {
93
96
  } else if (this.connection) return this.connection; //already connected
94
97
 
95
98
  //Check if redis client is available
96
- if (!this._isRedisClientAvailable()) throw new Error('Redis client is not available! Please, run "npm i -S redis".');
99
+ if (!(await this._isRedisClientAvailable())) throw new Error('Redis client is not available! Please, run "npm i -S redis".');
97
100
 
98
101
  //Prepare
99
102
  const localConsole = (this.transaction ? this.transaction.logger : console);
@@ -102,8 +105,8 @@ export default class IKCache_Redis extends IKCache {
102
105
 
103
106
  //Instantiate client and initiate connection
104
107
  return new Promise(async (resolve, reject) => {
105
- const redis = require("redis");
106
- const isV4 = this._isRedisClientV4();
108
+ const redis = await import("redis");
109
+ const isV4 = await this._isRedisClientV4();
107
110
  //
108
111
  const connection = redis.createClient({
109
112
  ...(isV4 ? {
@@ -1,5 +1,5 @@
1
- import IKDBBaseQuery from './IKDBBaseQuery.js';
2
1
  import Utils from './../../API/IKUtils.js';
2
+ import IKDBBaseQuery from './IKDBBaseQuery.js';
3
3
  //
4
4
  export default class IKDBQueryScan extends IKDBBaseQuery {
5
5
  constructor(optIndexName) {
@@ -12,7 +12,7 @@ export default class IKDBQueryScan extends IKDBBaseQuery {
12
12
  const resp = await this.rawRun(dbManager, appendingItems);
13
13
  if (resp.Items) {
14
14
  let unmarshalled = Utils.decapsulateForDB(resp.Items);
15
- localConsole.debug('Parsed result: ', unmarshalled);
15
+ localConsole.debug('Parsed result: ', JSON.stringify(unmarshalled).substring(0,4000));
16
16
  //Check for appending items from previous query
17
17
  if (appendingItems && appendingItems.length > 0) {
18
18
  unmarshalled = unmarshalled.concat(appendingItems);
@@ -154,16 +154,18 @@ export default class IKDBQueryUpdate extends IKDBBaseQuery {
154
154
  if (!expression.ExpressionAttributeNames) expression.ExpressionAttributeNames = {};
155
155
  //build update expression
156
156
  for (let dbKey of Object.keys(this.incrementValues)) {
157
+ let initialKey = ':initial';
157
158
  let appendingKey = (':' + dbKey);
158
159
  let appendingValueKey = ('#' + dbKey);
159
160
  let val = this.incrementValues[dbKey];
160
161
  if (val != undefined) {
161
162
  let last = Object.keys(this.incrementValues).indexOf(dbKey) == (Object.keys(this.incrementValues).length - 1);
162
163
  //last
163
- if (last) expression.UpdateExpression += appendingValueKey + ' =' + appendingValueKey + '+' + appendingKey;
164
- else expression.UpdateExpression += appendingValueKey + ' =' + appendingValueKey + '+' + appendingKey + ', ';
164
+ if (last) expression.UpdateExpression += appendingValueKey + ` = if_not_exists(${appendingValueKey}, :initial) + ${appendingKey}`;
165
+ else expression.UpdateExpression += appendingValueKey + ` = if_not_exists(${appendingValueKey}, :initial) + ${appendingKey}` + ', ';
165
166
  //
166
167
  expression.ExpressionAttributeValues[appendingKey] = Utils.encapsulateForDB(val);
168
+ expression.ExpressionAttributeValues[initialKey] = Utils.encapsulateForDB(0);
167
169
  expression.ExpressionAttributeNames[appendingValueKey] = dbKey;
168
170
  }
169
171
  }
@@ -21,12 +21,13 @@ export default class IKMailer {
21
21
  });
22
22
  }
23
23
 
24
- //to can be an array or just one address
25
- async sendTemplatedEmail(_to, _templates, _data, _optionalCC, _optionalAttachments) {
24
+ //to and cc can be an array, just one address or a comma separated string containing multiple addresses
25
+ async sendTemplatedEmail(_to, _templates, _data, _optionalCC, _optionalAttachments, _optionalHeaders, _optionalFrom) {
26
26
  //Generate emails
27
27
  const email = new Email({
28
28
  message: {
29
- from: this.from,
29
+ ...(_optionalHeaders ? {headers: _optionalHeaders} : {}),
30
+ from: _optionalFrom ? _optionalFrom : this.from,
30
31
  to: _to,
31
32
  ...(_optionalAttachments ? {attachments: _optionalAttachments} : {}),
32
33
  ...(_optionalCC ? {cc: _optionalCC} : {})
@@ -2,29 +2,31 @@ import IKGlobals from './../IKGlobals.js';
2
2
  import Utils from './../API/IKUtils.js';
3
3
  import IKResponse, { IKBadRequestResponse } from './../API/IKResponse.js';
4
4
  //https://www.npmjs.com/package/superstruct
5
- import { struct, superstruct } from 'superstruct';
5
+ import * as struct from 'superstruct';
6
6
  //validation libs
7
7
  import isUuid from 'is-uuid';
8
8
  import isEmail from 'is-email';
9
9
  //
10
10
  export default class IKValidation {
11
11
  constructor(config) {
12
+ this.customStruct = {};
12
13
  this._buildInternalTypes((config && config.additionalTypes ? config.additionalTypes : {}));
13
14
  }
14
15
  _buildInternalTypes(additionalTypes) {
15
- this.structor = superstruct({
16
- types: {
17
- ...IKValidation.internalTypes(),
18
- ...additionalTypes
19
- }
20
- });
16
+ for (const key in IKValidation.internalTypes()) {
17
+ this.customStruct[key] = struct.define(key, IKValidation.internalTypes()[key]);
18
+ }
19
+ for (const key in additionalTypes) {
20
+ this.customStruct[key] = struct.define(key, additionalTypes[key]);
21
+ }
21
22
  }
22
23
  async validate(spec, obj, request) {
23
- let parsedObj = null;
24
+ let assertion = null;
24
25
  let err = null;
25
26
  try {
26
27
  let parsedRawObj = obj;
27
- parsedObj = this.structor(spec)(parsedRawObj);
28
+ if (typeof spec == 'function') spec = spec(this)
29
+ assertion = struct.assert(parsedRawObj, spec);
28
30
  } catch (e) {
29
31
  console.debug(e);
30
32
  const { path, value, type } = e;
@@ -38,17 +40,14 @@ export default class IKValidation {
38
40
  err = IKBadRequestResponse(IKGlobals.ErrorResponseValidationFail + errorMessage, IKGlobals.ErrorCode_InvalidInput);
39
41
  }
40
42
  //Catch error as response
41
- if (!parsedObj && err) return err;
43
+ if (err) return err;
42
44
  //run normal code out of this try/catch scope
43
- return await request(parsedObj);
45
+ return await request(obj);
44
46
  }
45
47
  }
46
48
  //
47
49
  IKValidation.internalTypes = function() {
48
50
  return {
49
- optionalNumber: value => (value == undefined || !isNaN(parseInt(value))),
50
- optionalValidString: value => { return (!value || Utils.isValidString(value) == true); },
51
- validString: value => { return (Utils.isValidString(value) == true); },
52
51
  email: value => { return (isEmail(value) && value.length < 256); },
53
52
  fullAddress: value => {
54
53
  if (!value) return false;