@orgloop/transform-enrich 0.1.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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 OrgLoop contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Enrich transform — add, copy, and compute fields on events.
3
+ *
4
+ * Three operations:
5
+ * - set: Static key-value pairs added to event (dot-path → value)
6
+ * - copy: Copy a field from one dot-path to another
7
+ * - compute: Simple boolean expression evaluated against event fields
8
+ */
9
+ import type { OrgLoopEvent, Transform, TransformContext } from '@orgloop/sdk';
10
+ export declare class EnrichTransform implements Transform {
11
+ readonly id = "enrich";
12
+ private config;
13
+ init(config: Record<string, unknown>): Promise<void>;
14
+ execute(event: OrgLoopEvent, _context: TransformContext): Promise<OrgLoopEvent | null>;
15
+ shutdown(): Promise<void>;
16
+ }
17
+ //# sourceMappingURL=enrich.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enrich.d.ts","sourceRoot":"","sources":["../src/enrich.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAkH9E,qBAAa,eAAgB,YAAW,SAAS;IAChD,QAAQ,CAAC,EAAE,YAAY;IACvB,OAAO,CAAC,MAAM,CAAoB;IAE5B,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IASpD,OAAO,CAAC,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,gBAAgB,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC;IAmCtF,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAG/B"}
package/dist/enrich.js ADDED
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Enrich transform — add, copy, and compute fields on events.
3
+ *
4
+ * Three operations:
5
+ * - set: Static key-value pairs added to event (dot-path → value)
6
+ * - copy: Copy a field from one dot-path to another
7
+ * - compute: Simple boolean expression evaluated against event fields
8
+ */
9
+ /**
10
+ * Get a value from a nested object using a dot-separated path.
11
+ * Returns undefined for missing paths without throwing.
12
+ */
13
+ function getByPath(obj, path) {
14
+ const segments = path.split('.');
15
+ let current = obj;
16
+ for (const segment of segments) {
17
+ if (current === null || current === undefined || typeof current !== 'object') {
18
+ return undefined;
19
+ }
20
+ current = current[segment];
21
+ }
22
+ return current;
23
+ }
24
+ /**
25
+ * Set a value on a nested object using a dot-separated path.
26
+ * Creates intermediate objects as needed.
27
+ */
28
+ function setByPath(obj, path, value) {
29
+ const segments = path.split('.');
30
+ let current = obj;
31
+ for (let i = 0; i < segments.length - 1; i++) {
32
+ const segment = segments[i];
33
+ const next = current[segment];
34
+ if (next === null || next === undefined || typeof next !== 'object') {
35
+ current[segment] = {};
36
+ }
37
+ current = current[segment];
38
+ }
39
+ current[segments[segments.length - 1]] = value;
40
+ }
41
+ /**
42
+ * Parse and evaluate a simple comparison expression against an event.
43
+ *
44
+ * Supported formats:
45
+ * field === 'value' (string equality)
46
+ * field !== 'value' (string inequality)
47
+ * field === value (unquoted — tries numeric then string)
48
+ * field !== value
49
+ * field > N (numeric comparison)
50
+ * field < N
51
+ * field >= N
52
+ * field <= N
53
+ *
54
+ * No eval() — just string parsing.
55
+ */
56
+ function evaluateExpression(expression, event) {
57
+ const trimmed = expression.trim();
58
+ // Match: field operator value
59
+ // Operators: ===, !==, >=, <=, >, <
60
+ const match = trimmed.match(/^([a-zA-Z_][\w.]*)\s*(===|!==|>=|<=|>|<)\s*(.+)$/);
61
+ if (!match) {
62
+ return undefined;
63
+ }
64
+ const [, fieldPath, operator, rawValue] = match;
65
+ const fieldValue = getByPath(event, fieldPath);
66
+ // Parse the comparison value
67
+ let compareValue;
68
+ const trimmedValue = rawValue.trim();
69
+ // String literal: 'value' or "value"
70
+ const stringMatch = trimmedValue.match(/^(['"])(.*)(\1)$/);
71
+ if (stringMatch) {
72
+ compareValue = stringMatch[2];
73
+ }
74
+ else {
75
+ // Try numeric
76
+ const num = Number(trimmedValue);
77
+ if (!Number.isNaN(num)) {
78
+ compareValue = num;
79
+ }
80
+ else {
81
+ compareValue = trimmedValue;
82
+ }
83
+ }
84
+ switch (operator) {
85
+ case '===':
86
+ return fieldValue === compareValue || String(fieldValue) === String(compareValue);
87
+ case '!==':
88
+ return fieldValue !== compareValue && String(fieldValue) !== String(compareValue);
89
+ case '>':
90
+ return typeof fieldValue === 'number' && typeof compareValue === 'number'
91
+ ? fieldValue > compareValue
92
+ : false;
93
+ case '<':
94
+ return typeof fieldValue === 'number' && typeof compareValue === 'number'
95
+ ? fieldValue < compareValue
96
+ : false;
97
+ case '>=':
98
+ return typeof fieldValue === 'number' && typeof compareValue === 'number'
99
+ ? fieldValue >= compareValue
100
+ : false;
101
+ case '<=':
102
+ return typeof fieldValue === 'number' && typeof compareValue === 'number'
103
+ ? fieldValue <= compareValue
104
+ : false;
105
+ default:
106
+ return undefined;
107
+ }
108
+ }
109
+ export class EnrichTransform {
110
+ id = 'enrich';
111
+ config = {};
112
+ async init(config) {
113
+ const c = config;
114
+ this.config = {
115
+ set: c.set,
116
+ copy: c.copy,
117
+ compute: c.compute,
118
+ };
119
+ }
120
+ async execute(event, _context) {
121
+ // Shallow clone to avoid mutating the input
122
+ const result = { ...event, payload: { ...event.payload } };
123
+ const resultObj = result;
124
+ // 1. Static field setting
125
+ if (this.config.set) {
126
+ for (const [path, value] of Object.entries(this.config.set)) {
127
+ setByPath(resultObj, path, value);
128
+ }
129
+ }
130
+ // 2. Field copying
131
+ if (this.config.copy) {
132
+ for (const [targetPath, sourcePath] of Object.entries(this.config.copy)) {
133
+ const value = getByPath(resultObj, sourcePath);
134
+ if (value !== undefined) {
135
+ setByPath(resultObj, targetPath, value);
136
+ }
137
+ }
138
+ }
139
+ // 3. Computed fields
140
+ if (this.config.compute) {
141
+ for (const [targetPath, expression] of Object.entries(this.config.compute)) {
142
+ const value = evaluateExpression(expression, resultObj);
143
+ if (value !== undefined) {
144
+ setByPath(resultObj, targetPath, value);
145
+ }
146
+ }
147
+ }
148
+ return result;
149
+ }
150
+ async shutdown() {
151
+ // No resources to clean up
152
+ }
153
+ }
154
+ //# sourceMappingURL=enrich.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enrich.js","sourceRoot":"","sources":["../src/enrich.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAUH;;;GAGG;AACH,SAAS,SAAS,CAAC,GAAY,EAAE,IAAY;IAC5C,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACjC,IAAI,OAAO,GAAY,GAAG,CAAC;IAC3B,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;QAChC,IAAI,OAAO,KAAK,I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AAC;YACtB,KAAK,MAAM,CAAC,UAAU,EAAE,UAAU,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;gBACzE,MAAM,KAAK,GAAG,SAAS,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;gBAC/C,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;oBACzB,SAAS,CAAC,SAAS,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;gBACzC,CAAC;YACF,CAAC;QACF,CAAC;QAED,qBAAqB;QACrB,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YACzB,KAAK,MAAM,CAAC,UAAU,EAAE,UAAU,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC5E,MAAM,KAAK,GAAG,kBAAkB,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;gBACxD,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;oBACzB,SAAS,CAAC,SAAS,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;gBACzC,CAAC;YACF,CAAC;QACF,CAAC;QAED,OAAO,MAAM,CAAC;IACf,CAAC;IAED,KAAK,CAAC,QAAQ;QACb,2BAA2B;IAC5B,CAAC;CACD"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @orgloop/transform-enrich — registration entry point.
3
+ */
4
+ import type { TransformRegistration } from '@orgloop/sdk';
5
+ export declare function register(): TransformRegistration;
6
+ export { EnrichTransform } from './enrich.js';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAG1D,wBAAgB,QAAQ,IAAI,qBAAqB,CA2BhD;AAED,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,32 @@
1
+ /**
2
+ * @orgloop/transform-enrich — registration entry point.
3
+ */
4
+ import { EnrichTransform } from './enrich.js';
5
+ export function register() {
6
+ return {
7
+ id: 'enrich',
8
+ transform: EnrichTransform,
9
+ configSchema: {
10
+ type: 'object',
11
+ properties: {
12
+ set: {
13
+ type: 'object',
14
+ description: 'Static key-value pairs to set on the event (dot-path → value).',
15
+ },
16
+ copy: {
17
+ type: 'object',
18
+ description: 'Copy fields from one location to another (target dot-path → source dot-path).',
19
+ additionalProperties: { type: 'string' },
20
+ },
21
+ compute: {
22
+ type: 'object',
23
+ description: 'Computed boolean fields from simple expressions (target dot-path → expression).',
24
+ additionalProperties: { type: 'string' },
25
+ },
26
+ },
27
+ additionalProperties: false,
28
+ },
29
+ };
30
+ }
31
+ export { EnrichTransform } from './enrich.js';
32
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C,MAAM,UAAU,QAAQ;IACvB,OAAO;QACN,EAAE,EAAE,QAAQ;QACZ,SAAS,EAAE,eAAe;QAC1B,YAAY,EAAE;YACb,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE;gBACX,GAAG,EAAE;oBACJ,IAAI,EAAE,QAAQ;oBACd,WAAW,EAAE,gEAAgE;iBAC7E;gBACD,IAAI,EAAE;oBACL,IAAI,EAAE,QAAQ;oBACd,WAAW,EACV,+EAA+E;oBAChF,oBAAoB,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;iBACxC;gBACD,OAAO,EAAE;oBACR,IAAI,EAAE,QAAQ;oBACd,WAAW,EACV,iFAAiF;oBAClF,oBAAoB,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;iBACxC;aACD;YACD,oBAAoB,EAAE,KAAK;SAC3B;KACD,CAAC;AACH,CAAC;AAED,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC"}
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@orgloop/transform-enrich",
3
+ "version": "0.1.0",
4
+ "description": "OrgLoop enrich transform — add, copy, and compute fields on events",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "dependencies": {
9
+ "@orgloop/sdk": "0.1.0"
10
+ },
11
+ "orgloop": {
12
+ "type": "transform",
13
+ "id": "enrich"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "license": "MIT",
22
+ "scripts": {
23
+ "build": "tsc",
24
+ "clean": "rm -rf dist",
25
+ "typecheck": "tsc --noEmit",
26
+ "test": "vitest run"
27
+ }
28
+ }