@unito/integration-api 4.1.6 → 4.2.1
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/src/compatibilities.d.ts +444 -0
- package/dist/src/compatibilities.js +345 -0
- package/dist/src/guards.d.ts +18 -0
- package/dist/src/guards.js +3 -3
- package/dist/src/index.cjs +512 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +2 -0
- package/dist/src/jsonPathHelpers.d.ts +5 -0
- package/dist/src/jsonPathHelpers.js +162 -0
- package/dist/src/types.d.ts +6 -0
- package/package.json +1 -1
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import * as Api from './index.js';
|
|
2
|
+
/**
|
|
3
|
+
* JSONPath parser that returns a relation that is guaranteed to have its schema populated.
|
|
4
|
+
*/
|
|
5
|
+
export function findRelationByJSONPath(item, query) {
|
|
6
|
+
const tokens = parseJSONPath(query);
|
|
7
|
+
const relations = [];
|
|
8
|
+
let current = item;
|
|
9
|
+
for (const token of tokens) {
|
|
10
|
+
if (current === '__self') {
|
|
11
|
+
const previousRelation = relations[relations.length - 1];
|
|
12
|
+
if (!previousRelation) {
|
|
13
|
+
throw new Error(`Invalid use of __self`);
|
|
14
|
+
}
|
|
15
|
+
current = previousRelation.schema;
|
|
16
|
+
}
|
|
17
|
+
const result = applyToken(current, token);
|
|
18
|
+
if (Api.isReferenceRelation(result) && Api.isRelationSchema(result.schema)) {
|
|
19
|
+
relations.push(result);
|
|
20
|
+
}
|
|
21
|
+
else if (Api.isRelationSummary(result) && Api.isRelationSchema(result.schema)) {
|
|
22
|
+
relations.push(result);
|
|
23
|
+
}
|
|
24
|
+
current = result;
|
|
25
|
+
}
|
|
26
|
+
if (Api.isReferenceRelation(current) || Api.isRelationSummary(current)) {
|
|
27
|
+
return relations[relations.length - 1];
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Parse JSONPath expression into tokens
|
|
33
|
+
*/
|
|
34
|
+
function parseJSONPath(query) {
|
|
35
|
+
const tokens = [];
|
|
36
|
+
let remaining = query;
|
|
37
|
+
// Remove root $ if present
|
|
38
|
+
if (remaining.startsWith('$')) {
|
|
39
|
+
remaining = remaining.substring(1);
|
|
40
|
+
}
|
|
41
|
+
while (remaining.length > 0) {
|
|
42
|
+
// Skip leading dots
|
|
43
|
+
if (remaining.startsWith('.')) {
|
|
44
|
+
remaining = remaining.substring(1);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
// Parse bracket notation [...]
|
|
48
|
+
if (remaining.startsWith('[')) {
|
|
49
|
+
const bracketMatch = remaining.match(/^\[([^\]]*)\]/);
|
|
50
|
+
if (!bracketMatch) {
|
|
51
|
+
throw new Error(`Unclosed bracket in JSONPath: ${query}`);
|
|
52
|
+
}
|
|
53
|
+
remaining = remaining.substring(bracketMatch[0].length);
|
|
54
|
+
tokens.push(parseBracketExpression(String(bracketMatch[1])));
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
// Parse property name (until . or [ or end)
|
|
58
|
+
const propertyMatch = remaining.match(/^([^.[]+)/);
|
|
59
|
+
if (propertyMatch) {
|
|
60
|
+
const propertyName = String(propertyMatch[1]);
|
|
61
|
+
remaining = remaining.substring(propertyName.length);
|
|
62
|
+
tokens.push({ type: 'property', name: propertyName });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return tokens;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Parse bracket expression into a token
|
|
69
|
+
*/
|
|
70
|
+
function parseBracketExpression(content) {
|
|
71
|
+
// Filter expression: ?(@.property == 'value')
|
|
72
|
+
if (content.startsWith('?(')) {
|
|
73
|
+
const filterExpr = content.substring(2, content.length - 1);
|
|
74
|
+
return { type: 'filter', expression: parseFilterExpression(filterExpr) };
|
|
75
|
+
}
|
|
76
|
+
// Array index: 0, 1, 2, etc.
|
|
77
|
+
const index = parseInt(content, 10);
|
|
78
|
+
if (!isNaN(index)) {
|
|
79
|
+
return { type: 'index', value: index };
|
|
80
|
+
}
|
|
81
|
+
throw new Error(`Unsupported bracket expression: ${content}`);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Parse filter expression like @.name == 'value'
|
|
85
|
+
*/
|
|
86
|
+
function parseFilterExpression(expr) {
|
|
87
|
+
const opIndex = expr.indexOf('==');
|
|
88
|
+
if (opIndex === -1) {
|
|
89
|
+
throw new Error(`Filter expression must use == operator: ${expr}`);
|
|
90
|
+
}
|
|
91
|
+
const left = expr.substring(0, opIndex).trim();
|
|
92
|
+
const right = expr.substring(opIndex + 2).trim();
|
|
93
|
+
// Parse left side (should be @.property)
|
|
94
|
+
if (!left.startsWith('@.')) {
|
|
95
|
+
throw new Error(`Filter expression must start with @.: ${expr}`);
|
|
96
|
+
}
|
|
97
|
+
const property = left.substring(2);
|
|
98
|
+
// Parse right side (value) using regex to extract quoted strings
|
|
99
|
+
const quotedMatch = right.match(/^(?<quote>['"])(?<content>.*?)\k<quote>$/);
|
|
100
|
+
if (!quotedMatch) {
|
|
101
|
+
throw new Error(`Filter expression value must be a quoted string: ${expr}`);
|
|
102
|
+
}
|
|
103
|
+
return { property, value: quotedMatch.groups['content'] };
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Apply a single token to the current value
|
|
107
|
+
*/
|
|
108
|
+
function applyToken(current, token) {
|
|
109
|
+
switch (token.type) {
|
|
110
|
+
case 'property':
|
|
111
|
+
return applyProperty(current, token.name);
|
|
112
|
+
case 'index':
|
|
113
|
+
return applyIndex(current, token.value);
|
|
114
|
+
case 'filter':
|
|
115
|
+
return applyFilter(current, token.expression);
|
|
116
|
+
default:
|
|
117
|
+
throw new Error(`Unsupported token type: ${token.type}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Apply property access
|
|
122
|
+
*/
|
|
123
|
+
function applyProperty(current, property) {
|
|
124
|
+
if (!Api.isObject(current) || !(property in current)) {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
return current[property];
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Apply array index access
|
|
131
|
+
*/
|
|
132
|
+
function applyIndex(current, index) {
|
|
133
|
+
if (!Array.isArray(current) || index < 0 || index >= current.length) {
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
return current[index];
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Apply filter expression
|
|
140
|
+
*
|
|
141
|
+
* This function returns the first item that matches the filter expression.
|
|
142
|
+
*/
|
|
143
|
+
function applyFilter(current, filter) {
|
|
144
|
+
if (!Array.isArray(current)) {
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
for (const item of current) {
|
|
148
|
+
if (Api.isObject(item) && matchesFilter(item, filter)) {
|
|
149
|
+
return item;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Check if an item matches a filter expression
|
|
156
|
+
*/
|
|
157
|
+
function matchesFilter(item, filter) {
|
|
158
|
+
if (!(filter.property in item)) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
return item[filter.property] === filter.value;
|
|
162
|
+
}
|
package/dist/src/types.d.ts
CHANGED
|
@@ -413,6 +413,12 @@ export interface RelationSchema {
|
|
|
413
413
|
*/
|
|
414
414
|
relations?: RelationSummary[];
|
|
415
415
|
}
|
|
416
|
+
/**
|
|
417
|
+
* A type that guarantees the presence of a populated schema by preventing the use of __self.
|
|
418
|
+
*/
|
|
419
|
+
export type RelationWithPopulatedSchema<T extends RelationSummary | ReferenceRelation> = Omit<T, 'schema'> & {
|
|
420
|
+
schema: Exclude<RelationSchema, '__self'>;
|
|
421
|
+
};
|
|
416
422
|
/**
|
|
417
423
|
* A CreateItemRequestPayload describes the shape of a request on an item creation endpoint.
|
|
418
424
|
*/
|