@api-client/core 0.20.2 → 0.20.4
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/build/src/modeling/Semantics.d.ts +0 -252
- package/build/src/modeling/Semantics.d.ts.map +1 -1
- package/build/src/modeling/Semantics.js +0 -356
- package/build/src/modeling/Semantics.js.map +1 -1
- package/build/src/modeling/ai/tools/Semantic.tools.d.ts +0 -3
- package/build/src/modeling/ai/tools/Semantic.tools.d.ts.map +1 -1
- package/build/src/modeling/types.d.ts +50 -0
- package/build/src/modeling/types.d.ts.map +1 -1
- package/build/src/modeling/types.js.map +1 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -4
- package/src/modeling/Semantics.ts +40 -520
- package/src/modeling/types.ts +52 -0
- package/tests/unit/modeling/client_ip_address_semantic.spec.ts +0 -22
- package/tests/unit/modeling/semantics.spec.ts +0 -65
- package/tests/unit/modeling/username_semantic.spec.ts +0 -32
- package/build/src/runtime/modeling/Semantics.d.ts +0 -84
- package/build/src/runtime/modeling/Semantics.d.ts.map +0 -1
- package/build/src/runtime/modeling/Semantics.js +0 -124
- package/build/src/runtime/modeling/Semantics.js.map +0 -1
- package/src/runtime/modeling/Semantics.ts +0 -196
- package/tests/unit/modeling/semantic_runtime.spec.ts +0 -113
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
import { Jexl } from '@pawel-up/jexl/Jexl.js'
|
|
2
|
-
import type { DomainEntity } from '../../modeling/DomainEntity.js'
|
|
3
|
-
import {
|
|
4
|
-
type AppliedDataSemantic,
|
|
5
|
-
DataSemantics,
|
|
6
|
-
type SemanticCondition,
|
|
7
|
-
SemanticOperation,
|
|
8
|
-
SemanticType,
|
|
9
|
-
} from '../../modeling/Semantics.js'
|
|
10
|
-
import type { TransformFunction } from '@pawel-up/jexl'
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Cache for JEXL instances to avoid recreation overhead
|
|
14
|
-
*/
|
|
15
|
-
const jexlCache = new Map<string, Jexl>()
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Get or create a JEXL instance with optional custom transforms
|
|
19
|
-
*/
|
|
20
|
-
export function getJexlInstance(transforms?: Record<string, TransformFunction>): Jexl {
|
|
21
|
-
const cacheKey = transforms ? JSON.stringify(Object.keys(transforms).sort()) : 'default'
|
|
22
|
-
|
|
23
|
-
if (!jexlCache.has(cacheKey)) {
|
|
24
|
-
const jexl = new Jexl()
|
|
25
|
-
if (transforms) {
|
|
26
|
-
Object.entries(transforms).forEach(([name, fn]) => {
|
|
27
|
-
jexl.addTransform(name, fn)
|
|
28
|
-
})
|
|
29
|
-
}
|
|
30
|
-
jexlCache.set(cacheKey, jexl)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return jexlCache.get(cacheKey) as Jexl
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Context object passed to semantic condition evaluation
|
|
38
|
-
*/
|
|
39
|
-
export interface SemanticExecutionContext {
|
|
40
|
-
/**
|
|
41
|
-
* The entity data being processed
|
|
42
|
-
*/
|
|
43
|
-
entity: Record<string, unknown>
|
|
44
|
-
/**
|
|
45
|
-
* Current user context (if authenticated)
|
|
46
|
-
*/
|
|
47
|
-
user?: {
|
|
48
|
-
id?: string
|
|
49
|
-
authenticated?: boolean
|
|
50
|
-
roles?: string[]
|
|
51
|
-
[key: string]: unknown
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* The current database operation
|
|
55
|
-
*/
|
|
56
|
-
operation: SemanticOperation
|
|
57
|
-
/**
|
|
58
|
-
* Applied semantics with their field mappings
|
|
59
|
-
*/
|
|
60
|
-
appliedSemantics: AppliedDataSemantic[]
|
|
61
|
-
/**
|
|
62
|
-
* Configuration for the current semantic being evaluated
|
|
63
|
-
*/
|
|
64
|
-
config?: Record<string, unknown>
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Builds a semantics object for JEXL evaluation that maps semantic types to actual field names.
|
|
69
|
-
*
|
|
70
|
-
* The resulting map should be used in JEXL expressions to reference semantic fields.
|
|
71
|
-
*
|
|
72
|
-
* @returns A map where keys are semantic type identifiers and values are the field names.
|
|
73
|
-
*
|
|
74
|
-
* @example
|
|
75
|
-
* const semanticFieldMap = buildSemanticFieldMap(entity);
|
|
76
|
-
* const result = await jexl.eval("semantics.CreatedTimestamp == null", {
|
|
77
|
-
* semantics: semanticFieldMap,
|
|
78
|
-
* ...
|
|
79
|
-
* }
|
|
80
|
-
*/
|
|
81
|
-
export function buildSemanticFieldMap(entity: DomainEntity): Record<string, string> {
|
|
82
|
-
const semanticFieldMap: Record<string, string> = {}
|
|
83
|
-
|
|
84
|
-
for (const property of entity.properties) {
|
|
85
|
-
if (!property.info.name) {
|
|
86
|
-
continue // Skip properties without a name
|
|
87
|
-
}
|
|
88
|
-
for (const semantic of property.semantics) {
|
|
89
|
-
// We use the truncated `semantic.id` as the key, e.g. 'Password' so that it is possible to do something like:
|
|
90
|
-
// `entity[semantics.Password] != null && !entity[semantics.Password].startsWith('$')`
|
|
91
|
-
// Where the `semantics` object is the object returned by this function and
|
|
92
|
-
// passed to the JEXL context.
|
|
93
|
-
const semanticKey = semantic.id.replace('Semantic#', '')
|
|
94
|
-
semanticFieldMap[semanticKey] = property.info.name
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return semanticFieldMap
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Evaluates a semantic condition against the execution context
|
|
102
|
-
* In a real implementation, this would use JEXL to evaluate the expression
|
|
103
|
-
*
|
|
104
|
-
* @param condition The semantic condition to evaluate
|
|
105
|
-
* @param context The execution context containing entity data, user info, etc.
|
|
106
|
-
* @param semanticFieldMap A map of semantic field names to their actual field names.
|
|
107
|
-
* Use the `buildSemanticFieldMap()` function to create this map.
|
|
108
|
-
* @param jexl Optional JEXL instance to use for evaluation, defaults to a cached instance.
|
|
109
|
-
* @returns A promise that resolves to true if the condition is met, false otherwise
|
|
110
|
-
*/
|
|
111
|
-
export function evaluateSemanticCondition(
|
|
112
|
-
condition: SemanticCondition,
|
|
113
|
-
context: SemanticExecutionContext,
|
|
114
|
-
semanticFieldMap: Record<string, string>,
|
|
115
|
-
jexl: Jexl = getJexlInstance()
|
|
116
|
-
): Promise<boolean> {
|
|
117
|
-
// Build the evaluation context
|
|
118
|
-
const evalContext = {
|
|
119
|
-
entity: context.entity,
|
|
120
|
-
user: context.user,
|
|
121
|
-
operation: context.operation,
|
|
122
|
-
config: context.config,
|
|
123
|
-
semantics: semanticFieldMap,
|
|
124
|
-
}
|
|
125
|
-
return jexl.eval(condition.expression, evalContext)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Check if semantic should execute based on all its conditions
|
|
130
|
-
*
|
|
131
|
-
* @param semantic The semantic to check
|
|
132
|
-
* @param context The execution context containing entity data, user info, etc.
|
|
133
|
-
* @param semanticFieldMap A map of semantic field names to their actual field names.
|
|
134
|
-
* Use the `buildSemanticFieldMap()` function to create this map.
|
|
135
|
-
* @return A promise that resolves to true if all conditions are met, false otherwise
|
|
136
|
-
*/
|
|
137
|
-
export async function shouldSemanticExecute(
|
|
138
|
-
semantic: AppliedDataSemantic,
|
|
139
|
-
context: SemanticExecutionContext,
|
|
140
|
-
semanticFieldMap: Record<string, string>
|
|
141
|
-
): Promise<boolean> {
|
|
142
|
-
const definition = DataSemantics[semantic.id]
|
|
143
|
-
const conditions = definition?.runtime?.conditions
|
|
144
|
-
|
|
145
|
-
if (!conditions || conditions.length === 0) {
|
|
146
|
-
return true
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
const results = await Promise.all(
|
|
150
|
-
conditions.map((condition) => evaluateSemanticCondition(condition, context, semanticFieldMap))
|
|
151
|
-
)
|
|
152
|
-
// All conditions must evaluate to true
|
|
153
|
-
return results.every((result) => result === true)
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Helper to get the actual field name for a semantic type
|
|
158
|
-
*/
|
|
159
|
-
export function getFieldNameForSemantic(
|
|
160
|
-
semanticType: SemanticType,
|
|
161
|
-
semanticFieldMap: Record<string, string>
|
|
162
|
-
): string | undefined {
|
|
163
|
-
const semanticKey = semanticType.replace('Semantic#', '')
|
|
164
|
-
return semanticFieldMap[semanticKey]
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Validates that all semantic references in conditions are available
|
|
169
|
-
*/
|
|
170
|
-
export function validateSemanticConditions(
|
|
171
|
-
semantic: AppliedDataSemantic,
|
|
172
|
-
semanticFieldMap: Record<string, string>
|
|
173
|
-
): string[] {
|
|
174
|
-
const definition = DataSemantics[semantic.id]
|
|
175
|
-
const conditions = definition?.runtime?.conditions || []
|
|
176
|
-
const errors: string[] = []
|
|
177
|
-
|
|
178
|
-
conditions.forEach((condition, index) => {
|
|
179
|
-
// Extract semantic references from the condition expression
|
|
180
|
-
// Look for patterns like "semantics.CreatedTimestamp" or "entity[semantics.Password]"
|
|
181
|
-
const semanticMatches = condition.expression.match(/semantics\.(\w+)/g)
|
|
182
|
-
|
|
183
|
-
if (semanticMatches) {
|
|
184
|
-
semanticMatches.forEach((match) => {
|
|
185
|
-
const semanticKey = match.replace('semantics.', '')
|
|
186
|
-
if (!semanticFieldMap[semanticKey]) {
|
|
187
|
-
errors.push(
|
|
188
|
-
`Condition ${index + 1} references semantic "${semanticKey}" but no field with that semantic was found`
|
|
189
|
-
)
|
|
190
|
-
}
|
|
191
|
-
})
|
|
192
|
-
}
|
|
193
|
-
})
|
|
194
|
-
|
|
195
|
-
return errors
|
|
196
|
-
}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { test } from '@japa/runner'
|
|
2
|
-
import {
|
|
3
|
-
SemanticType,
|
|
4
|
-
SemanticTiming,
|
|
5
|
-
SemanticOperation,
|
|
6
|
-
getSemanticsForOperation,
|
|
7
|
-
canSemanticBeDisabled,
|
|
8
|
-
type AppliedDataSemantic,
|
|
9
|
-
} from '../../../src/modeling/Semantics.js'
|
|
10
|
-
|
|
11
|
-
test.group('Semantic Runtime Control', () => {
|
|
12
|
-
test('getSemanticsForOperation should filter semantics by operation and timing', ({ assert }) => {
|
|
13
|
-
const semantics: AppliedDataSemantic[] = [
|
|
14
|
-
{ id: SemanticType.Password },
|
|
15
|
-
{ id: SemanticType.CreatedTimestamp },
|
|
16
|
-
{ id: SemanticType.UpdatedTimestamp },
|
|
17
|
-
{ id: SemanticType.Status },
|
|
18
|
-
{ id: SemanticType.Title },
|
|
19
|
-
]
|
|
20
|
-
|
|
21
|
-
// Test CREATE operation with BEFORE timing
|
|
22
|
-
const createBeforeSemantics = getSemanticsForOperation(semantics, SemanticOperation.Create, SemanticTiming.Before)
|
|
23
|
-
const createBeforeTypes = createBeforeSemantics.map((s) => s.id)
|
|
24
|
-
|
|
25
|
-
assert.includeMembers(createBeforeTypes, [SemanticType.Password, SemanticType.CreatedTimestamp])
|
|
26
|
-
assert.notInclude(createBeforeTypes, SemanticType.UpdatedTimestamp) // Only runs on UPDATE
|
|
27
|
-
assert.notInclude(createBeforeTypes, SemanticType.Title) // No runtime operations
|
|
28
|
-
|
|
29
|
-
// Test UPDATE operation with BEFORE timing
|
|
30
|
-
const updateBeforeSemantics = getSemanticsForOperation(semantics, SemanticOperation.Update, SemanticTiming.Before)
|
|
31
|
-
const updateBeforeTypes = updateBeforeSemantics.map((s) => s.id)
|
|
32
|
-
|
|
33
|
-
assert.includeMembers(updateBeforeTypes, [SemanticType.Password, SemanticType.UpdatedTimestamp])
|
|
34
|
-
assert.notInclude(updateBeforeTypes, SemanticType.CreatedTimestamp) // Only runs on CREATE
|
|
35
|
-
|
|
36
|
-
// Test Status semantic runs on both CREATE and UPDATE
|
|
37
|
-
assert.include(createBeforeTypes, SemanticType.Status)
|
|
38
|
-
assert.include(updateBeforeTypes, SemanticType.Status)
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
test('getSemanticsForOperation should sort semantics by priority', ({ assert }) => {
|
|
42
|
-
const semantics: AppliedDataSemantic[] = [
|
|
43
|
-
{ id: SemanticType.Password }, // priority: 10
|
|
44
|
-
{ id: SemanticType.ResourceOwnerIdentifier }, // priority: 5
|
|
45
|
-
{ id: SemanticType.UserRole }, // priority: 20
|
|
46
|
-
]
|
|
47
|
-
|
|
48
|
-
const createSemantics = getSemanticsForOperation(semantics, SemanticOperation.Create, SemanticTiming.Before)
|
|
49
|
-
|
|
50
|
-
// Should be sorted by priority: ResourceOwnerIdentifier (5), Password (10), UserRole (20)
|
|
51
|
-
assert.equal(createSemantics[0].id, SemanticType.ResourceOwnerIdentifier)
|
|
52
|
-
assert.equal(createSemantics[1].id, SemanticType.Password)
|
|
53
|
-
assert.equal(createSemantics[2].id, SemanticType.UserRole)
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
test('getSemanticsForOperation should handle BOTH timing correctly', ({ assert }) => {
|
|
57
|
-
const semantics: AppliedDataSemantic[] = [
|
|
58
|
-
{ id: SemanticType.Status }, // timing: Both
|
|
59
|
-
{ id: SemanticType.Password }, // timing: Before
|
|
60
|
-
]
|
|
61
|
-
|
|
62
|
-
const beforeSemantics = getSemanticsForOperation(semantics, SemanticOperation.Create, SemanticTiming.Before)
|
|
63
|
-
const afterSemantics = getSemanticsForOperation(semantics, SemanticOperation.Create, SemanticTiming.After)
|
|
64
|
-
|
|
65
|
-
// Status should appear in both BEFORE and AFTER
|
|
66
|
-
assert.equal(beforeSemantics.length, 2) // Status and Password
|
|
67
|
-
assert.equal(afterSemantics.length, 1) // Only Status
|
|
68
|
-
assert.equal(afterSemantics[0].id, SemanticType.Status)
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
test('canSemanticBeDisabled should check if semantic can be disabled', ({ assert }) => {
|
|
72
|
-
// Security semantics cannot be disabled
|
|
73
|
-
assert.isFalse(canSemanticBeDisabled(SemanticType.Password))
|
|
74
|
-
assert.isFalse(canSemanticBeDisabled(SemanticType.ResourceOwnerIdentifier))
|
|
75
|
-
|
|
76
|
-
// Other semantics can be disabled (default)
|
|
77
|
-
assert.isTrue(canSemanticBeDisabled(SemanticType.CreatedTimestamp))
|
|
78
|
-
assert.isTrue(canSemanticBeDisabled(SemanticType.Email))
|
|
79
|
-
assert.isTrue(canSemanticBeDisabled(SemanticType.Status))
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
test('getSemanticsForOperation should return empty array for semantics with no runtime', ({ assert }) => {
|
|
83
|
-
const semantics: AppliedDataSemantic[] = [
|
|
84
|
-
{ id: SemanticType.Title }, // No runtime operations
|
|
85
|
-
{ id: SemanticType.Description }, // No runtime operations
|
|
86
|
-
{ id: SemanticType.User }, // No runtime operations
|
|
87
|
-
]
|
|
88
|
-
|
|
89
|
-
const result = getSemanticsForOperation(semantics, SemanticOperation.Create, SemanticTiming.Before)
|
|
90
|
-
assert.lengthOf(result, 0)
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
test('getSemanticsForOperation should filter by specific operations', ({ assert }) => {
|
|
94
|
-
const semantics: AppliedDataSemantic[] = [
|
|
95
|
-
{ id: SemanticType.CreatedTimestamp }, // Only CREATE
|
|
96
|
-
{ id: SemanticType.UpdatedTimestamp }, // Only UPDATE
|
|
97
|
-
{ id: SemanticType.DeletedTimestamp }, // Only DELETE
|
|
98
|
-
]
|
|
99
|
-
|
|
100
|
-
const createSemantics = getSemanticsForOperation(semantics, SemanticOperation.Create, SemanticTiming.Before)
|
|
101
|
-
const updateSemantics = getSemanticsForOperation(semantics, SemanticOperation.Update, SemanticTiming.Before)
|
|
102
|
-
const deleteSemantics = getSemanticsForOperation(semantics, SemanticOperation.Delete, SemanticTiming.Before)
|
|
103
|
-
|
|
104
|
-
assert.lengthOf(createSemantics, 1)
|
|
105
|
-
assert.equal(createSemantics[0].id, SemanticType.CreatedTimestamp)
|
|
106
|
-
|
|
107
|
-
assert.lengthOf(updateSemantics, 1)
|
|
108
|
-
assert.equal(updateSemantics[0].id, SemanticType.UpdatedTimestamp)
|
|
109
|
-
|
|
110
|
-
assert.lengthOf(deleteSemantics, 1)
|
|
111
|
-
assert.equal(deleteSemantics[0].id, SemanticType.DeletedTimestamp)
|
|
112
|
-
})
|
|
113
|
-
})
|