@saasquatch/program-boilerplate 3.5.9-0 → 3.5.12-0

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/dist/index.d.ts CHANGED
@@ -8,9 +8,9 @@ import * as types from "./types";
8
8
  import { Program, ProgramRequirement, RequirementValidationResult, ValidationProgramField, ProgramTriggerBody } from "./types/rpc";
9
9
  import { timeboxExpression, safeJsonata } from "./jsonata";
10
10
  import { ProgramType } from "./types/saasquatch";
11
- import { inferType, getGoalAnalyticTimestamp, setRewardSchedule, numToEquality, getTriggerSchema } from "./utils";
11
+ import { inferType, getGoalAnalyticTimestamp, setRewardSchedule, numToEquality, getTriggerSchema, getUserCustomFieldsFromJsonata } from "./utils";
12
12
  export { types };
13
- export { Transaction, ProgramTriggerBody, Program, ProgramType, RequirementValidationResult, ProgramRequirement, ValidationProgramField, meetEventTriggerRules, meetCustomFieldRules, rewardEmailQuery, setRewardSchedule, getGoalAnalyticTimestamp, triggerProgram, inferType, numToEquality, getTriggerSchema, timeboxExpression, safeJsonata, getLogger, setLogLevel, };
13
+ export { Transaction, ProgramTriggerBody, Program, ProgramType, RequirementValidationResult, ProgramRequirement, ValidationProgramField, meetEventTriggerRules, meetCustomFieldRules, rewardEmailQuery, setRewardSchedule, getGoalAnalyticTimestamp, triggerProgram, inferType, numToEquality, getTriggerSchema, getUserCustomFieldsFromJsonata, timeboxExpression, safeJsonata, getLogger, setLogLevel, };
14
14
  /**
15
15
  * Returns an express server that serves the provided handlers
16
16
  * as a program
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.webtask = exports.setLogLevel = exports.getLogger = exports.safeJsonata = exports.timeboxExpression = exports.getTriggerSchema = exports.numToEquality = exports.inferType = exports.triggerProgram = exports.getGoalAnalyticTimestamp = exports.setRewardSchedule = exports.rewardEmailQuery = exports.meetCustomFieldRules = exports.meetEventTriggerRules = exports.Transaction = exports.types = void 0;
3
+ exports.webtask = exports.setLogLevel = exports.getLogger = exports.safeJsonata = exports.timeboxExpression = exports.getUserCustomFieldsFromJsonata = exports.getTriggerSchema = exports.numToEquality = exports.inferType = exports.triggerProgram = exports.getGoalAnalyticTimestamp = exports.setRewardSchedule = exports.rewardEmailQuery = exports.meetCustomFieldRules = exports.meetEventTriggerRules = exports.Transaction = exports.types = void 0;
4
4
  const express = require("express");
5
5
  const conversion_1 = require("./conversion");
6
6
  Object.defineProperty(exports, "meetCustomFieldRules", { enumerable: true, get: function () { return conversion_1.meetCustomFieldRules; } });
@@ -25,6 +25,7 @@ Object.defineProperty(exports, "getGoalAnalyticTimestamp", { enumerable: true, g
25
25
  Object.defineProperty(exports, "setRewardSchedule", { enumerable: true, get: function () { return utils_1.setRewardSchedule; } });
26
26
  Object.defineProperty(exports, "numToEquality", { enumerable: true, get: function () { return utils_1.numToEquality; } });
27
27
  Object.defineProperty(exports, "getTriggerSchema", { enumerable: true, get: function () { return utils_1.getTriggerSchema; } });
28
+ Object.defineProperty(exports, "getUserCustomFieldsFromJsonata", { enumerable: true, get: function () { return utils_1.getUserCustomFieldsFromJsonata; } });
28
29
  /**
29
30
  * Returns an express server that serves the provided handlers
30
31
  * as a program
@@ -34,10 +35,9 @@ Object.defineProperty(exports, "getTriggerSchema", { enumerable: true, get: func
34
35
  * @return {Object} The express server
35
36
  */
36
37
  function webtask(program = {}) {
37
- const bodyParser = require("body-parser");
38
38
  const compression = require("compression");
39
39
  const app = express();
40
- app.use(bodyParser.json());
40
+ app.use(express.json({ limit: process.env.MAX_PAYLOAD_SIZE || "1mb" }));
41
41
  app.use(compression());
42
42
  // Enforce HTTPS. The server does not redirect http -> https
43
43
  // because OWASP advises not to
package/dist/utils.d.ts CHANGED
@@ -67,3 +67,10 @@ export declare function numToEquality(num: number): string;
67
67
  * @return object[] The tranformed data that is relavent for the trigger type
68
68
  */
69
69
  export declare function getTriggerSchema(body: ProgramTriggerBody): object[];
70
+ /**
71
+ * Parses JSONata expressions and finds user custom fields used in the expression(s)
72
+ *
73
+ * @param jsonataExpressions string | string[] input JSONata expression(s)
74
+ * @returns string[] a deduplicated list of user custom fields found in the input expression(s)
75
+ */
76
+ export declare function getUserCustomFieldsFromJsonata(jsonataExpressions: string | string[]): string[];
package/dist/utils.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getTriggerSchema = exports.numToEquality = exports.inferType = exports.getGoalAnalyticTimestamp = exports.setRewardSchedule = void 0;
3
+ exports.getUserCustomFieldsFromJsonata = exports.getTriggerSchema = exports.numToEquality = exports.inferType = exports.getGoalAnalyticTimestamp = exports.setRewardSchedule = void 0;
4
4
  const queries_1 = require("./queries");
5
+ const jsonata = require("jsonata");
6
+ const jsonata_paths_extractor_1 = require("@saasquatch/jsonata-paths-extractor");
5
7
  /**
6
8
  * Append a reward schedule to the template and return the new template
7
9
  *
@@ -182,3 +184,47 @@ function getTriggerSchema(body) {
182
184
  }
183
185
  }
184
186
  exports.getTriggerSchema = getTriggerSchema;
187
+ /**
188
+ * Parses JSONata expressions and finds user custom fields used in the expression(s)
189
+ *
190
+ * @param jsonataExpressions string | string[] input JSONata expression(s)
191
+ * @returns string[] a deduplicated list of user custom fields found in the input expression(s)
192
+ */
193
+ function getUserCustomFieldsFromJsonata(jsonataExpressions) {
194
+ let userCustomFields = [];
195
+ const getJsonataASTSafe = (expression) => {
196
+ try {
197
+ return jsonata(expression).ast();
198
+ }
199
+ catch (e) { }
200
+ };
201
+ if (typeof jsonataExpressions === "string") {
202
+ jsonataExpressions = [jsonataExpressions];
203
+ }
204
+ for (const expression of jsonataExpressions) {
205
+ const ast = getJsonataASTSafe(expression);
206
+ if (!ast)
207
+ continue;
208
+ const allPaths = jsonata_paths_extractor_1.default(ast);
209
+ for (const path of allPaths) {
210
+ if (path.startsWith("/user/customFields/")) {
211
+ const key = path.split("/")[3];
212
+ if (key)
213
+ userCustomFields.push(key);
214
+ }
215
+ if (path.startsWith("/user/referredByReferral/referrerUser/customFields/")) {
216
+ const key = path.split("/")[5];
217
+ if (key)
218
+ userCustomFields.push(key);
219
+ }
220
+ if (path.startsWith("/referral/referrerUser/customFields/")) {
221
+ const key = path.split("/")[4];
222
+ if (key)
223
+ userCustomFields.push(key);
224
+ }
225
+ }
226
+ }
227
+ //dedup
228
+ return Array.from(new Set(userCustomFields));
229
+ }
230
+ exports.getUserCustomFieldsFromJsonata = getUserCustomFieldsFromJsonata;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saasquatch/program-boilerplate",
3
- "version": "3.5.9-0",
3
+ "version": "3.5.12-0",
4
4
  "description": "Boilerplate for writing programs",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -32,7 +32,7 @@
32
32
  "typescript": "^3.9.9"
33
33
  },
34
34
  "dependencies": {
35
- "body-parser": "^1.19.0",
35
+ "@saasquatch/jsonata-paths-extractor": "^0.1.0-1",
36
36
  "bson-objectid": "^1.3.1",
37
37
  "compression": "^1.7.4",
38
38
  "express": "^4.17.1",