otomato-sdk 2.0.19 → 2.0.21

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.
@@ -1,4 +1,4 @@
1
- export const SDK_VERSION = '2.0.18';
1
+ export const SDK_VERSION = '2.0.20';
2
2
  export function compareVersions(v1, v2) {
3
3
  // Split the version strings into parts
4
4
  const v1Parts = v1.split('.').map(Number);
@@ -97,3 +97,38 @@ export const findTriggerByBlockId = (blockId) => {
97
97
  }
98
98
  throw new Error(`Trigger with id ${blockId} not found`);
99
99
  };
100
+ /**
101
+ * Searches through the TRIGGERS object to find a sub-object
102
+ * whose `prototype` property matches `prototypeToMatch`.
103
+ *
104
+ * @param prototypeToMatch - The prototype string to find (e.g. "priceMovementAgainstCurrency").
105
+ * @returns An object of shape { [blockKey]: any } if found, otherwise null.
106
+ */
107
+ export function findBlockByPrototype(prototypeToMatch) {
108
+ // Top-level keys are categories like "CORE", "TOKENS", "YIELD", etc.
109
+ for (const categoryKey in TRIGGERS) {
110
+ const category = TRIGGERS[categoryKey];
111
+ // Each category may have multiple blocks: e.g. "EVERY_PERIOD", "TRANSFER", "BALANCE", etc.
112
+ for (const blockKey in category) {
113
+ const blockValue = category[blockKey];
114
+ // If blockValue is not an object, skip it (it might be `description`, `image`, etc.)
115
+ if (typeof blockValue !== 'object' || blockValue === null) {
116
+ continue;
117
+ }
118
+ // Inside each block, we often see an object with the same name:
119
+ // e.g. category["TRANSFER"]["TRANSFER"]
120
+ // We iterate all keys inside blockValue to find the actual block that has `prototype`.
121
+ for (const subKey in blockValue) {
122
+ const subValue = blockValue[subKey];
123
+ if (typeof subValue === 'object' &&
124
+ subValue !== null &&
125
+ subValue.prototype === prototypeToMatch) {
126
+ // Return the found block in the shape { [subKey]: subValue }
127
+ return subValue;
128
+ }
129
+ }
130
+ }
131
+ }
132
+ // If we never find a matching prototype, return null
133
+ return null;
134
+ }
@@ -1,4 +1,49 @@
1
+ import { findBlockByPrototype } from "../models/Trigger";
1
2
  export function getExternalVariable(prototype, args) {
2
3
  return `{{external.functions.${prototype}(${args})}}`;
3
4
  }
4
5
  ;
6
+ /**
7
+ * Creates a string of the form:
8
+ * {{external.functions.[prototype](param1,param2,...)}}
9
+ *
10
+ * - It looks up the parameter definitions for the given prototype.
11
+ * - For each parameter definition (in order), it finds a matching user parameter by key.
12
+ * - If not found, uses the parameter's default `value` if it exists, otherwise an empty placeholder.
13
+ * - Removes any trailing empty placeholders, but preserves empty placeholders in the middle.
14
+ */
15
+ export function getExternalVariableFromParameters(prototype, parameters) {
16
+ // 1. Find the block in TRIGGERS
17
+ const block = findBlockByPrototype(prototype);
18
+ if (!block) {
19
+ // If we can't find the block definition, just call with user-supplied parameters
20
+ return `{{external.functions.${prototype}(${parameters.map((p) => p.value).join(",")})}}`;
21
+ }
22
+ // 2. Grab the ordered list of parameter definitions
23
+ const blockParams = block.parameters || [];
24
+ // 3. Build an array of final values in the same order as blockParams
25
+ const finalValues = blockParams.map((bp) => {
26
+ var _a, _b, _c, _d;
27
+ // 3a. Find a matching user parameter by exact key
28
+ const userParam = parameters.find((up) => up.key === bp.key);
29
+ if (userParam) {
30
+ // Use the user-supplied value
31
+ return (_b = (_a = userParam.value) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : "";
32
+ }
33
+ else if (bp.value != null) {
34
+ // Use the block's default value, if defined
35
+ return (_d = (_c = bp.value) === null || _c === void 0 ? void 0 : _c.toString()) !== null && _d !== void 0 ? _d : "";
36
+ }
37
+ else {
38
+ // No user-supplied or default => empty placeholder
39
+ return "";
40
+ }
41
+ });
42
+ // 4. Remove trailing empty placeholders (e.g., `,,` at the end)
43
+ while (finalValues.length && finalValues[finalValues.length - 1] === "") {
44
+ finalValues.pop();
45
+ }
46
+ // 5. Join with commas to preserve empty slots in the middle
47
+ const paramString = finalValues.join(",");
48
+ return `{{external.functions.${prototype}(${paramString})}}`;
49
+ }
@@ -1,2 +1,2 @@
1
- export declare const SDK_VERSION = "2.0.18";
1
+ export declare const SDK_VERSION = "2.0.20";
2
2
  export declare function compareVersions(v1: string, v2: string): number;
@@ -29,3 +29,13 @@ export declare const findTriggerByBlockId: (blockId: number) => {
29
29
  parentInfo: ParentInfo;
30
30
  block: any;
31
31
  };
32
+ /**
33
+ * Searches through the TRIGGERS object to find a sub-object
34
+ * whose `prototype` property matches `prototypeToMatch`.
35
+ *
36
+ * @param prototypeToMatch - The prototype string to find (e.g. "priceMovementAgainstCurrency").
37
+ * @returns An object of shape { [blockKey]: any } if found, otherwise null.
38
+ */
39
+ export declare function findBlockByPrototype(prototypeToMatch: string): {
40
+ [blockKey: string]: any;
41
+ } | null;
@@ -1 +1,14 @@
1
1
  export declare function getExternalVariable(prototype: String, args: Array<any>): string;
2
+ /**
3
+ * Creates a string of the form:
4
+ * {{external.functions.[prototype](param1,param2,...)}}
5
+ *
6
+ * - It looks up the parameter definitions for the given prototype.
7
+ * - For each parameter definition (in order), it finds a matching user parameter by key.
8
+ * - If not found, uses the parameter's default `value` if it exists, otherwise an empty placeholder.
9
+ * - Removes any trailing empty placeholders, but preserves empty placeholders in the middle.
10
+ */
11
+ export declare function getExternalVariableFromParameters(prototype: string, parameters: Array<{
12
+ key: string;
13
+ value: any;
14
+ }>): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "otomato-sdk",
3
- "version": "2.0.19",
3
+ "version": "2.0.21",
4
4
  "description": "An SDK for building and managing automations on Otomato",
5
5
  "main": "dist/src/index.js",
6
6
  "types": "dist/types/src/index.d.ts",