@mono-lit/utility 0.0.0-stage → 0.0.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/LICENSE.md +54 -0
- package/README.md +156 -2
- package/dist/app-roots-BJsIxzjk.d.ts +91 -0
- package/dist/app-roots-CvJnmXcs.js +128 -0
- package/dist/config-node-UzJrXg0Z.js +76 -0
- package/dist/config-node.d.ts +977 -0
- package/dist/config-node.js +7 -0
- package/dist/config.d.ts +2 -0
- package/dist/config.js +3 -0
- package/dist/create-config-D3m6xTaQ.d.ts +733 -0
- package/dist/create-config-DdL3Fh6T.js +357 -0
- package/dist/fetching.d.ts +239 -0
- package/dist/fetching.js +867 -0
- package/dist/generate-CJ7MtcDd.js +291 -0
- package/dist/index-C2hZeNkq.d.ts +365 -0
- package/dist/index-ClrZbU7n.d.ts +518 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +939 -0
- package/dist/merge-file-C5nzi_MY.d.ts +122 -0
- package/dist/merge-file-CtyKTu5L.js +133 -0
- package/dist/mock-db-5mJTObSC.js +1282 -0
- package/dist/mock-db.d.ts +2 -0
- package/dist/mock-db.js +4 -0
- package/dist/mono-alias-DNDm-jB_.js +508 -0
- package/dist/mono-clone.mjs +958 -0
- package/dist/mono-db.d.ts +1 -0
- package/dist/mono-db.js +151 -0
- package/dist/mono-env.mjs +231 -0
- package/dist/mono-git.mjs +798 -0
- package/dist/mono-link-YlOQ2PVS.js +488 -0
- package/dist/mono-prepare.d.ts +1 -0
- package/dist/mono-prepare.js +27 -0
- package/dist/mono-skills.d.ts +1 -0
- package/dist/mono-skills.js +830 -0
- package/dist/mono-tsconfig-D2qRL9_R.js +204 -0
- package/dist/mono.mjs +34 -0
- package/dist/nuxt-layers-DP1m7f1-.js +176 -0
- package/dist/nuxt-layers.d.ts +34 -0
- package/dist/nuxt-layers.js +3 -0
- package/dist/nuxt-runtime.d.ts +18 -0
- package/dist/nuxt-runtime.js +102 -0
- package/dist/nuxt.d.ts +386 -0
- package/dist/nuxt.js +590 -0
- package/dist/runtime-C_IWNrAG.js +5817 -0
- package/dist/runtime.d.ts +2583 -0
- package/dist/runtime.js +4 -0
- package/dist/session-Bk985AIM.js +1721 -0
- package/dist/types-B6FdB7LJ.d.ts +57 -0
- package/dist/universal-B2eD9xho.d.ts +97 -0
- package/dist/universal-BNTo_83u.js +599 -0
- package/dist/virtual-mono-apps.d.ts +16 -0
- package/dist/vite.d.ts +940 -0
- package/dist/vite.js +1132 -0
- package/package.json +141 -3
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
//#region pkg/mock-db/schema.ts
|
|
2
|
+
const FIELD_TYPES = [
|
|
3
|
+
"number",
|
|
4
|
+
"string",
|
|
5
|
+
"boolean",
|
|
6
|
+
"date",
|
|
7
|
+
"object",
|
|
8
|
+
"array"
|
|
9
|
+
];
|
|
10
|
+
/** `object|userId->users:id` -> the relation half, or null when it isn't one. */
|
|
11
|
+
function parseRelationModifier(modifier, kind) {
|
|
12
|
+
const match = /^([A-Za-z0-9_$]+)\s*->\s*([A-Za-z0-9_$]+)\s*:\s*([A-Za-z0-9_$]+)$/.exec(modifier);
|
|
13
|
+
if (!match) return null;
|
|
14
|
+
if (kind !== "object" && kind !== "array") return null;
|
|
15
|
+
return {
|
|
16
|
+
localField: match[1],
|
|
17
|
+
targetEntity: match[2],
|
|
18
|
+
targetKey: match[3],
|
|
19
|
+
kind
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Parse one `'<type>|<modifier>'` declaration. Throws with the field name attached. */
|
|
23
|
+
function parseField(name, declaration) {
|
|
24
|
+
const raw = String(declaration ?? "").trim();
|
|
25
|
+
if (!raw) throw new Error(`field "${name}": empty declaration`);
|
|
26
|
+
const [typePart, ...modifierParts] = raw.split("|");
|
|
27
|
+
const type = String(typePart ?? "").trim();
|
|
28
|
+
if (!FIELD_TYPES.includes(type)) throw new Error(`field "${name}": unknown type "${type}" (expected ${FIELD_TYPES.join(" | ")})`);
|
|
29
|
+
const field = {
|
|
30
|
+
name,
|
|
31
|
+
type,
|
|
32
|
+
primary: false,
|
|
33
|
+
foreign: false
|
|
34
|
+
};
|
|
35
|
+
for (const part of modifierParts) {
|
|
36
|
+
const modifier = part.trim();
|
|
37
|
+
if (!modifier) continue;
|
|
38
|
+
if (modifier === "primary") {
|
|
39
|
+
field.primary = true;
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (modifier === "foreign") {
|
|
43
|
+
field.foreign = true;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const relation = parseRelationModifier(modifier, type);
|
|
47
|
+
if (!relation) throw new Error(`field "${name}": bad modifier "${modifier}" — expected primary, foreign, or a relation like "object|userId->users:id" (relations need type object or array)`);
|
|
48
|
+
field.relation = relation;
|
|
49
|
+
}
|
|
50
|
+
if (field.relation && field.primary) throw new Error(`field "${name}": a relation cannot also be the primary key`);
|
|
51
|
+
return field;
|
|
52
|
+
}
|
|
53
|
+
function parseEntity(name, entity) {
|
|
54
|
+
const declarations = entity?.fields ?? {};
|
|
55
|
+
const fields = Object.entries(declarations).map(([fieldName, declaration]) => parseField(fieldName, declaration));
|
|
56
|
+
const primaries = fields.filter((f) => f.primary);
|
|
57
|
+
if (primaries.length === 0) throw new Error(`entity "${name}": no primary key (mark one field "…|primary")`);
|
|
58
|
+
if (primaries.length > 1) throw new Error(`entity "${name}": ${primaries.length} primary keys (${primaries.map((f) => f.name).join(", ")}) — exactly one is required`);
|
|
59
|
+
const relations = fields.filter((f) => f.relation);
|
|
60
|
+
return {
|
|
61
|
+
name,
|
|
62
|
+
fields,
|
|
63
|
+
columns: fields.filter((f) => !f.relation),
|
|
64
|
+
relations,
|
|
65
|
+
primaryKey: primaries[0].name,
|
|
66
|
+
seed: entity?.seed
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Parse every base-url group. Collects errors instead of throwing on the first
|
|
71
|
+
* one, so `mono db validate` can report them all in a single pass.
|
|
72
|
+
*/
|
|
73
|
+
function parseMockSchema(config) {
|
|
74
|
+
const schemas = [];
|
|
75
|
+
const errors = [];
|
|
76
|
+
for (const [baseUrl, entityMap] of Object.entries(config?.schema ?? {})) {
|
|
77
|
+
const entities = {};
|
|
78
|
+
for (const [entityName, entity] of Object.entries(entityMap ?? {})) try {
|
|
79
|
+
entities[entityName] = parseEntity(entityName, entity);
|
|
80
|
+
} catch (error) {
|
|
81
|
+
errors.push({
|
|
82
|
+
baseUrl,
|
|
83
|
+
entity: entityName,
|
|
84
|
+
message: error?.message ?? String(error)
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
for (const entity of Object.values(entities)) for (const field of entity.relations) {
|
|
88
|
+
const relation = field.relation;
|
|
89
|
+
const target = entities[relation.targetEntity];
|
|
90
|
+
if (!target) {
|
|
91
|
+
errors.push({
|
|
92
|
+
baseUrl,
|
|
93
|
+
entity: entity.name,
|
|
94
|
+
field: field.name,
|
|
95
|
+
message: `relation points at unknown entity "${relation.targetEntity}"`
|
|
96
|
+
});
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (!entity.fields.some((f) => f.name === relation.localField)) errors.push({
|
|
100
|
+
baseUrl,
|
|
101
|
+
entity: entity.name,
|
|
102
|
+
field: field.name,
|
|
103
|
+
message: `relation reads local field "${relation.localField}", which this entity does not declare`
|
|
104
|
+
});
|
|
105
|
+
if (!target.fields.some((f) => f.name === relation.targetKey)) errors.push({
|
|
106
|
+
baseUrl,
|
|
107
|
+
entity: entity.name,
|
|
108
|
+
field: field.name,
|
|
109
|
+
message: `relation reads "${relation.targetEntity}.${relation.targetKey}", which that entity does not declare`
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
schemas.push({
|
|
113
|
+
baseUrl,
|
|
114
|
+
entities
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
schemas,
|
|
119
|
+
errors
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** Strip leading/trailing slashes so `/a/` and `a` compare equal. */
|
|
123
|
+
function normalizeSegment(value) {
|
|
124
|
+
return String(value ?? "").replace(/^\/+|\/+$/g, "");
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Find the schema whose base-url this request url belongs to, and the entity
|
|
128
|
+
* within it. Matches on path segments (never a bare substring), so a base-url of
|
|
129
|
+
* `flow` cannot swallow `/flowers/1`.
|
|
130
|
+
*
|
|
131
|
+
* Accepts absolute urls, and urls where the base-url is only part of the path
|
|
132
|
+
* (e.g. `https://host/api/my-mock/users`).
|
|
133
|
+
*/
|
|
134
|
+
function matchMockRoute(schemas, url) {
|
|
135
|
+
let path = String(url ?? "");
|
|
136
|
+
path = path.replace(/^[a-z][a-z0-9+.-]*:\/\/[^/]+/i, "");
|
|
137
|
+
path = path.split("?")[0].split("#")[0];
|
|
138
|
+
const segments = normalizeSegment(path).split("/").filter(Boolean);
|
|
139
|
+
if (!segments.length) return null;
|
|
140
|
+
for (const schema of schemas) {
|
|
141
|
+
const baseSegments = normalizeSegment(schema.baseUrl).split("/").filter(Boolean);
|
|
142
|
+
if (!baseSegments.length) continue;
|
|
143
|
+
const at = indexOfSequence(segments, baseSegments);
|
|
144
|
+
if (at < 0) continue;
|
|
145
|
+
const after = segments.slice(at + baseSegments.length);
|
|
146
|
+
if (!after.length) continue;
|
|
147
|
+
const entityName = after[0].replace(/\(.*\)$/, "");
|
|
148
|
+
const entity = schema.entities[entityName];
|
|
149
|
+
if (!entity) continue;
|
|
150
|
+
return {
|
|
151
|
+
schema,
|
|
152
|
+
entity,
|
|
153
|
+
rest: after.join("/")
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
/** Index of `needle` inside `haystack`, comparing whole segments. */
|
|
159
|
+
function indexOfSequence(haystack, needle) {
|
|
160
|
+
outer: for (let i = 0; i + needle.length <= haystack.length; i++) {
|
|
161
|
+
for (let j = 0; j < needle.length; j++) if (haystack[i + j] !== needle[j]) continue outer;
|
|
162
|
+
return i;
|
|
163
|
+
}
|
|
164
|
+
return -1;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region pkg/mock-db/generate.ts
|
|
169
|
+
/** mulberry32 — small, fast, seedable. We need repeatability, not cryptography. */
|
|
170
|
+
function createRandom(seed) {
|
|
171
|
+
let state = seed >>> 0 || 1;
|
|
172
|
+
return function random() {
|
|
173
|
+
state |= 0;
|
|
174
|
+
state = state + 1831565813 | 0;
|
|
175
|
+
let t = Math.imul(state ^ state >>> 15, 1 | state);
|
|
176
|
+
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
|
|
177
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
const WORDS = [
|
|
181
|
+
"alpha",
|
|
182
|
+
"bravo",
|
|
183
|
+
"charlie",
|
|
184
|
+
"delta",
|
|
185
|
+
"echo",
|
|
186
|
+
"foxtrot",
|
|
187
|
+
"golf",
|
|
188
|
+
"hotel",
|
|
189
|
+
"india",
|
|
190
|
+
"juliet",
|
|
191
|
+
"kilo",
|
|
192
|
+
"lima",
|
|
193
|
+
"mike",
|
|
194
|
+
"november",
|
|
195
|
+
"oscar",
|
|
196
|
+
"papa"
|
|
197
|
+
];
|
|
198
|
+
function titleCase(value) {
|
|
199
|
+
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* A value for one column. Name-aware: a field called `email` should look like an
|
|
203
|
+
* email, otherwise generated data is useless for eyeballing a UI.
|
|
204
|
+
*/
|
|
205
|
+
function generateValue(field, index, random) {
|
|
206
|
+
const name = field.name.toLowerCase();
|
|
207
|
+
const word = WORDS[Math.floor(random() * WORDS.length)] ?? "alpha";
|
|
208
|
+
switch (field.type) {
|
|
209
|
+
case "number": return field.foreign ? 0 : Math.floor(random() * 1e3);
|
|
210
|
+
case "boolean": return random() > .5;
|
|
211
|
+
case "date": return new Date(Date.UTC(2025, 0, 1) + index * 864e5).toISOString();
|
|
212
|
+
case "string":
|
|
213
|
+
if (name.includes("email")) return `${word}${index + 1}@example.com`;
|
|
214
|
+
if (name.includes("name") || name.includes("title")) return `${titleCase(word)} ${index + 1}`;
|
|
215
|
+
if (name.includes("phone")) return `08${Math.floor(random() * 1e10).toString().padStart(10, "0")}`;
|
|
216
|
+
if (name.includes("url") || name.includes("link")) return `https://example.com/${word}/${index + 1}`;
|
|
217
|
+
if (name.includes("icon")) return `i-mdi-${word}`;
|
|
218
|
+
return `${titleCase(word)} ${index + 1}`;
|
|
219
|
+
case "object": return {};
|
|
220
|
+
case "array": return [];
|
|
221
|
+
default: return null;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Topologically order entities so every parent is generated before the children
|
|
226
|
+
* that point at it — otherwise a foreign key would reference rows that don't
|
|
227
|
+
* exist yet. Cycles fall back to declaration order (a self-referencing FK just
|
|
228
|
+
* gets a key from an already-generated row of the same entity).
|
|
229
|
+
*/
|
|
230
|
+
function orderEntitiesByDependency(entities) {
|
|
231
|
+
const byName = new Map(entities.map((entity) => [entity.name, entity]));
|
|
232
|
+
const ordered = [];
|
|
233
|
+
const done = /* @__PURE__ */ new Set();
|
|
234
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
235
|
+
const visit = (entity) => {
|
|
236
|
+
if (done.has(entity.name) || visiting.has(entity.name)) return;
|
|
237
|
+
visiting.add(entity.name);
|
|
238
|
+
for (const field of entity.relations) {
|
|
239
|
+
const relation = field.relation;
|
|
240
|
+
if (relation.kind !== "object") continue;
|
|
241
|
+
const parent = byName.get(relation.targetEntity);
|
|
242
|
+
if (parent && parent.name !== entity.name) visit(parent);
|
|
243
|
+
}
|
|
244
|
+
visiting.delete(entity.name);
|
|
245
|
+
done.add(entity.name);
|
|
246
|
+
ordered.push(entity);
|
|
247
|
+
};
|
|
248
|
+
for (const entity of entities) visit(entity);
|
|
249
|
+
return ordered;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Build the seed for one base-url group.
|
|
253
|
+
*
|
|
254
|
+
* An entity's explicit `seed` wins outright: a generator cannot invent
|
|
255
|
+
* enum-like columns or a serialized graph, so anything an app parses must be
|
|
256
|
+
* supplied as fixtures.
|
|
257
|
+
*/
|
|
258
|
+
function generateSeed(schema, options = {}) {
|
|
259
|
+
const count = Math.max(0, options.count ?? 10);
|
|
260
|
+
const random = createRandom(options.random ?? 1);
|
|
261
|
+
const tables = {};
|
|
262
|
+
const ordered = orderEntitiesByDependency(Object.values(schema.entities));
|
|
263
|
+
for (const entity of ordered) {
|
|
264
|
+
if (entity.seed) {
|
|
265
|
+
tables[entity.name] = entity.seed.map((row) => ({ ...row }));
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
const rows = [];
|
|
269
|
+
for (let index = 0; index < count; index++) {
|
|
270
|
+
const row = {};
|
|
271
|
+
for (const field of entity.columns) row[field.name] = field.primary ? index + 1 : generateValue(field, index, random);
|
|
272
|
+
rows.push(row);
|
|
273
|
+
}
|
|
274
|
+
for (const field of entity.relations) {
|
|
275
|
+
const relation = field.relation;
|
|
276
|
+
if (relation.kind !== "object") continue;
|
|
277
|
+
const parentRows = tables[relation.targetEntity];
|
|
278
|
+
if (!parentRows?.length) continue;
|
|
279
|
+
if (!entity.columns.find((column) => column.name === relation.localField)) continue;
|
|
280
|
+
rows.forEach((row, index) => {
|
|
281
|
+
const parent = parentRows[index % parentRows.length];
|
|
282
|
+
row[relation.localField] = parent[relation.targetKey];
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
tables[entity.name] = rows;
|
|
286
|
+
}
|
|
287
|
+
return tables;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
//#endregion
|
|
291
|
+
export { normalizeSegment as a, matchMockRoute as i, generateSeed as n, parseField as o, orderEntitiesByDependency as r, parseMockSchema as s, createRandom as t };
|
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
import { v as MonoMockDbConfig } from "./create-config-D3m6xTaQ.js";
|
|
2
|
+
|
|
3
|
+
//#region pkg/mock-db/schema.d.ts
|
|
4
|
+
type MonoMockFieldType = 'number' | 'string' | 'boolean' | 'date' | 'object' | 'array';
|
|
5
|
+
/** `A->B:C` — "rows of B where `B[C] === row[A]`". */
|
|
6
|
+
interface MonoMockRelation {
|
|
7
|
+
/** Field on THIS entity whose value is matched. */
|
|
8
|
+
localField: string;
|
|
9
|
+
/** Entity being pointed at. */
|
|
10
|
+
targetEntity: string;
|
|
11
|
+
/** Field on the target entity that must equal `row[localField]`. */
|
|
12
|
+
targetKey: string;
|
|
13
|
+
/** `object` -> first match, `array` -> every match. */
|
|
14
|
+
kind: 'object' | 'array';
|
|
15
|
+
}
|
|
16
|
+
interface MonoMockField {
|
|
17
|
+
name: string;
|
|
18
|
+
type: MonoMockFieldType;
|
|
19
|
+
primary: boolean;
|
|
20
|
+
foreign: boolean;
|
|
21
|
+
/** Set when the modifier is a relation. Virtual: never persisted. */
|
|
22
|
+
relation?: MonoMockRelation;
|
|
23
|
+
}
|
|
24
|
+
interface MonoMockParsedEntity {
|
|
25
|
+
name: string;
|
|
26
|
+
/** Every declared field, including virtual relation fields. */
|
|
27
|
+
fields: MonoMockField[];
|
|
28
|
+
/** Fields actually written to the store (relations excluded). */
|
|
29
|
+
columns: MonoMockField[];
|
|
30
|
+
relations: MonoMockField[];
|
|
31
|
+
primaryKey: string;
|
|
32
|
+
seed?: Record<string, any>[];
|
|
33
|
+
}
|
|
34
|
+
interface MonoMockParsedSchema {
|
|
35
|
+
/** The base-url this group of entities is served under. */
|
|
36
|
+
baseUrl: string;
|
|
37
|
+
entities: Record<string, MonoMockParsedEntity>;
|
|
38
|
+
}
|
|
39
|
+
interface MonoMockSchemaError {
|
|
40
|
+
baseUrl: string;
|
|
41
|
+
entity: string;
|
|
42
|
+
field?: string;
|
|
43
|
+
message: string;
|
|
44
|
+
}
|
|
45
|
+
/** Parse one `'<type>|<modifier>'` declaration. Throws with the field name attached. */
|
|
46
|
+
declare function parseField(name: string, declaration: string): MonoMockField;
|
|
47
|
+
/**
|
|
48
|
+
* Parse every base-url group. Collects errors instead of throwing on the first
|
|
49
|
+
* one, so `mono db validate` can report them all in a single pass.
|
|
50
|
+
*/
|
|
51
|
+
declare function parseMockSchema(config: MonoMockDbConfig): {
|
|
52
|
+
schemas: MonoMockParsedSchema[];
|
|
53
|
+
errors: MonoMockSchemaError[];
|
|
54
|
+
};
|
|
55
|
+
/** Strip leading/trailing slashes so `/a/` and `a` compare equal. */
|
|
56
|
+
declare function normalizeSegment(value: string): string;
|
|
57
|
+
/**
|
|
58
|
+
* Find the schema whose base-url this request url belongs to, and the entity
|
|
59
|
+
* within it. Matches on path segments (never a bare substring), so a base-url of
|
|
60
|
+
* `flow` cannot swallow `/flowers/1`.
|
|
61
|
+
*
|
|
62
|
+
* Accepts absolute urls, and urls where the base-url is only part of the path
|
|
63
|
+
* (e.g. `https://host/api/my-mock/users`).
|
|
64
|
+
*/
|
|
65
|
+
declare function matchMockRoute(schemas: MonoMockParsedSchema[], url: string): {
|
|
66
|
+
schema: MonoMockParsedSchema;
|
|
67
|
+
entity: MonoMockParsedEntity;
|
|
68
|
+
rest: string;
|
|
69
|
+
} | null;
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region pkg/mock-db/odata.d.ts
|
|
72
|
+
type TokenType = 'ident' | 'string' | 'number' | 'date' | 'op' | 'paren' | 'comma' | 'path' | 'colon' | 'eof';
|
|
73
|
+
interface Token {
|
|
74
|
+
type: TokenType;
|
|
75
|
+
value: string;
|
|
76
|
+
}
|
|
77
|
+
declare function tokenize(input: string): Token[];
|
|
78
|
+
type FilterNode = {
|
|
79
|
+
kind: 'literal';
|
|
80
|
+
value: unknown;
|
|
81
|
+
} | {
|
|
82
|
+
kind: 'field';
|
|
83
|
+
name: string;
|
|
84
|
+
} /** `A/B/C` — a navigation path, or a lambda range variable (`d/Bulan`). */ | {
|
|
85
|
+
kind: 'path';
|
|
86
|
+
segments: string[];
|
|
87
|
+
} /** `Nav/any(d: <predicate>)` — `nav` may be empty for a lambda on the row itself. */ | {
|
|
88
|
+
kind: 'lambda';
|
|
89
|
+
op: 'any' | 'all';
|
|
90
|
+
nav: string[];
|
|
91
|
+
variable: string;
|
|
92
|
+
predicate: FilterNode;
|
|
93
|
+
} /** `Field in (1,2,3)` */ | {
|
|
94
|
+
kind: 'in';
|
|
95
|
+
left: FilterNode;
|
|
96
|
+
values: FilterNode[];
|
|
97
|
+
} | {
|
|
98
|
+
kind: 'call';
|
|
99
|
+
name: string;
|
|
100
|
+
args: FilterNode[];
|
|
101
|
+
} | {
|
|
102
|
+
kind: 'compare';
|
|
103
|
+
op: 'eq' | 'ne' | 'gt' | 'ge' | 'lt' | 'le';
|
|
104
|
+
left: FilterNode;
|
|
105
|
+
right: FilterNode;
|
|
106
|
+
} | {
|
|
107
|
+
kind: 'logical';
|
|
108
|
+
op: 'and' | 'or';
|
|
109
|
+
left: FilterNode;
|
|
110
|
+
right: FilterNode;
|
|
111
|
+
} | {
|
|
112
|
+
kind: 'not';
|
|
113
|
+
operand: FilterNode;
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Recursive descent, lowest precedence first:
|
|
117
|
+
* or -> and -> not -> comparison -> primary
|
|
118
|
+
* so `a eq 1 and b eq 2 or c eq 3` parses as `((a eq 1 and b eq 2) or c eq 3)`.
|
|
119
|
+
*/
|
|
120
|
+
declare function parseFilter(input: string): FilterNode;
|
|
121
|
+
/**
|
|
122
|
+
* What the evaluator needs beyond the row itself.
|
|
123
|
+
*
|
|
124
|
+
* `entity` + `readEntity` let a filter reach a NAVIGATION property. Relations here
|
|
125
|
+
* are virtual — they only materialise under `$expand` — so without this a lambda
|
|
126
|
+
* like `Detail/any(d: d/Bulan eq 3)` would see an empty collection and silently
|
|
127
|
+
* match nothing.
|
|
128
|
+
*
|
|
129
|
+
* `scope` binds lambda range variables (`d` -> the item currently under test).
|
|
130
|
+
*/
|
|
131
|
+
interface FilterContext {
|
|
132
|
+
entity?: MonoMockParsedEntity;
|
|
133
|
+
readEntity?: (name: string) => Record<string, any>[];
|
|
134
|
+
scope?: Record<string, Record<string, any>>;
|
|
135
|
+
}
|
|
136
|
+
declare function evaluateFilter(node: FilterNode, row: Record<string, any>, context?: FilterContext): boolean;
|
|
137
|
+
/**
|
|
138
|
+
* One `$expand` item, with the options that can live inside its parens:
|
|
139
|
+
* `Approval($select=Id;$expand=Step($select=X);$filter=Status eq 'Menunggu';$top=1)`.
|
|
140
|
+
*
|
|
141
|
+
* These are not decoration — the app relies on the nested `$filter`/`$top` to cut a
|
|
142
|
+
* child collection down to the row it wants. Ignoring them returns every child.
|
|
143
|
+
*/
|
|
144
|
+
interface ExpandItem {
|
|
145
|
+
name: string;
|
|
146
|
+
select?: string[];
|
|
147
|
+
filter?: string;
|
|
148
|
+
orderby?: {
|
|
149
|
+
field: string;
|
|
150
|
+
desc: boolean;
|
|
151
|
+
}[];
|
|
152
|
+
top?: number;
|
|
153
|
+
skip?: number;
|
|
154
|
+
expand?: ExpandItem[];
|
|
155
|
+
}
|
|
156
|
+
interface ODataQuery {
|
|
157
|
+
filter?: string;
|
|
158
|
+
select?: string[];
|
|
159
|
+
orderby?: {
|
|
160
|
+
field: string;
|
|
161
|
+
desc: boolean;
|
|
162
|
+
}[];
|
|
163
|
+
top?: number;
|
|
164
|
+
skip?: number;
|
|
165
|
+
count?: boolean;
|
|
166
|
+
/**
|
|
167
|
+
* Either bare relation names (`['cars']`) or parsed items with nested options.
|
|
168
|
+
* A raw string is normalised on the way in, so both call styles work.
|
|
169
|
+
*/
|
|
170
|
+
expand?: (string | ExpandItem)[];
|
|
171
|
+
/** Raw `$apply` pipeline, e.g. `filter(X gt 1)/groupby((A),aggregate(B with sum as T))`. */
|
|
172
|
+
apply?: string;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Parse an `$expand` value into a tree.
|
|
176
|
+
*
|
|
177
|
+
* Tolerates everything the real app emits: embedded newlines/tabs, spaces after
|
|
178
|
+
* commas (`Feature($select=Id, Nama)`), a stray trailing `;`
|
|
179
|
+
* (`BudgetAlokasi($select=…;)`), and a single string holding several comma-separated
|
|
180
|
+
* expands.
|
|
181
|
+
*/
|
|
182
|
+
declare function parseExpand(input: string | string[]): ExpandItem[];
|
|
183
|
+
/** Read OData options off a query string or a param object (DevExtreme sends both shapes). */
|
|
184
|
+
declare function parseQuery(input: string | Record<string, any>): ODataQuery;
|
|
185
|
+
interface ODataResult {
|
|
186
|
+
/** Rows after filter -> order -> page -> expand -> select. */
|
|
187
|
+
rows: Record<string, any>[];
|
|
188
|
+
/** Total AFTER filtering but BEFORE paging — what @odata.count must report. */
|
|
189
|
+
total: number;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Run a query over an entity's rows.
|
|
193
|
+
*
|
|
194
|
+
* Order matters and is the usual source of off-by-one bugs: filter first, take
|
|
195
|
+
* the count from the FILTERED set, then page, then expand, then select. Counting
|
|
196
|
+
* before filtering (or after paging) is the classic way to break a grid's pager.
|
|
197
|
+
*/
|
|
198
|
+
declare function executeQuery(rows: Record<string, any>[], query: ODataQuery, context?: {
|
|
199
|
+
entity: MonoMockParsedEntity;
|
|
200
|
+
readEntity: (name: string) => Record<string, any>[]; /** Resolves a child entity's schema — needed to recurse into a nested $expand. */
|
|
201
|
+
entityOf?: (name: string) => MonoMockParsedEntity | undefined;
|
|
202
|
+
}): ODataResult;
|
|
203
|
+
/** `Entity(1)` / `Entity('abc')` -> the key, or null for a collection request. */
|
|
204
|
+
declare function parseKeySegment(segment: string): string | number | null;
|
|
205
|
+
/** The `{ value, @odata.count }` envelope DevExtreme's ODataStore expects. */
|
|
206
|
+
declare function toODataEnvelope(result: ODataResult, count: boolean): {
|
|
207
|
+
value: Record<string, any>[];
|
|
208
|
+
'@odata.count'?: number | undefined;
|
|
209
|
+
};
|
|
210
|
+
//#endregion
|
|
211
|
+
//#region pkg/mock-db/apply.d.ts
|
|
212
|
+
/** Compiles a `$filter` expression into a row predicate. Injected to avoid an import cycle. */
|
|
213
|
+
type CompileFilter = (expression: string) => (row: Record<string, any>) => boolean;
|
|
214
|
+
/**
|
|
215
|
+
* Reads a property that may be a NAVIGATION PATH (`PostBudget/ParentName`).
|
|
216
|
+
*
|
|
217
|
+
* Injected for the same reason as `compileFilter`: the path resolver lives in
|
|
218
|
+
* odata.ts, which imports this module. Without it `groupby((PostBudget/ParentName))`
|
|
219
|
+
* read a flat field, found nothing, and silently collapsed every row into one
|
|
220
|
+
* `null` group — the aggregate looked plausible and was wrong.
|
|
221
|
+
*/
|
|
222
|
+
type ReadValue = (row: Record<string, any>, field: string) => unknown;
|
|
223
|
+
type AggregateMethod = 'sum' | 'average' | 'min' | 'max' | 'count' | 'countdistinct';
|
|
224
|
+
interface AggregateSpec {
|
|
225
|
+
/** Source field. Absent for `$count as X`. */
|
|
226
|
+
field?: string;
|
|
227
|
+
method: AggregateMethod;
|
|
228
|
+
/** Output property name (`... as Alias`). */
|
|
229
|
+
alias: string;
|
|
230
|
+
}
|
|
231
|
+
type ApplyTransform = {
|
|
232
|
+
kind: 'filter';
|
|
233
|
+
expression: string;
|
|
234
|
+
} | {
|
|
235
|
+
kind: 'groupby';
|
|
236
|
+
fields: string[];
|
|
237
|
+
aggregates: AggregateSpec[];
|
|
238
|
+
} | {
|
|
239
|
+
kind: 'aggregate';
|
|
240
|
+
aggregates: AggregateSpec[];
|
|
241
|
+
} | {
|
|
242
|
+
kind: 'identity';
|
|
243
|
+
};
|
|
244
|
+
declare function parseApply(input: string): ApplyTransform[];
|
|
245
|
+
/** Run the pipeline. Each transform's output feeds the next. */
|
|
246
|
+
declare function applyTransforms(rows: Record<string, any>[], transforms: ApplyTransform[], compileFilter: CompileFilter, readValue?: ReadValue): Record<string, any>[];
|
|
247
|
+
//#endregion
|
|
248
|
+
//#region pkg/mock-db/generate.d.ts
|
|
249
|
+
/** mulberry32 — small, fast, seedable. We need repeatability, not cryptography. */
|
|
250
|
+
declare function createRandom(seed: number): () => number;
|
|
251
|
+
/**
|
|
252
|
+
* Topologically order entities so every parent is generated before the children
|
|
253
|
+
* that point at it — otherwise a foreign key would reference rows that don't
|
|
254
|
+
* exist yet. Cycles fall back to declaration order (a self-referencing FK just
|
|
255
|
+
* gets a key from an already-generated row of the same entity).
|
|
256
|
+
*/
|
|
257
|
+
declare function orderEntitiesByDependency(entities: MonoMockParsedEntity[]): MonoMockParsedEntity[];
|
|
258
|
+
interface GenerateOptions {
|
|
259
|
+
/** Rows per entity that has no explicit `seed`. */
|
|
260
|
+
count?: number;
|
|
261
|
+
/** RNG seed — same seed, same rows. */
|
|
262
|
+
random?: number;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Build the seed for one base-url group.
|
|
266
|
+
*
|
|
267
|
+
* An entity's explicit `seed` wins outright: a generator cannot invent
|
|
268
|
+
* enum-like columns or a serialized graph, so anything an app parses must be
|
|
269
|
+
* supplied as fixtures.
|
|
270
|
+
*/
|
|
271
|
+
declare function generateSeed(schema: MonoMockParsedSchema, options?: GenerateOptions): Record<string, Record<string, any>[]>;
|
|
272
|
+
//#endregion
|
|
273
|
+
//#region pkg/mock-db/store.d.ts
|
|
274
|
+
declare function isIndexedDbAvailable(): boolean;
|
|
275
|
+
interface MonoMockStore {
|
|
276
|
+
read(baseUrl: string, entity: string): Promise<Record<string, any>[]>;
|
|
277
|
+
write(baseUrl: string, entity: string, rows: Record<string, any>[]): Promise<void>;
|
|
278
|
+
insert(baseUrl: string, entity: string, row: Record<string, any>): Promise<Record<string, any>>;
|
|
279
|
+
/**
|
|
280
|
+
* `merge: true` (PATCH) keeps fields absent from `changes`.
|
|
281
|
+
* `merge: false` (PUT) REPLACES the row, carrying over only the primary key —
|
|
282
|
+
* which is what a real OData PUT does. A merging PUT would hide the production
|
|
283
|
+
* bug where a partial PUT nulls out the fields you omitted.
|
|
284
|
+
*/
|
|
285
|
+
update(baseUrl: string, entity: string, key: any, changes: Record<string, any>, options?: {
|
|
286
|
+
merge?: boolean;
|
|
287
|
+
}): Promise<Record<string, any> | null>;
|
|
288
|
+
remove(baseUrl: string, entity: string, key: any): Promise<boolean>;
|
|
289
|
+
clear(): Promise<void>;
|
|
290
|
+
close(): void;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Open (and upgrade) the database. Every entity across every base-url becomes an
|
|
294
|
+
* object store keyed on its declared primary key, with an index per foreign key.
|
|
295
|
+
*
|
|
296
|
+
* `version` comes from the config: bumping it triggers `onupgradeneeded`, which
|
|
297
|
+
* drops and recreates the stores — i.e. a schema change WIPES the user's data.
|
|
298
|
+
* That's correct for a mock, but it is destructive and therefore logged.
|
|
299
|
+
*/
|
|
300
|
+
declare function openMockDb(dbName: string, version: number, schemas: MonoMockParsedSchema[]): Promise<IDBDatabase>;
|
|
301
|
+
declare function createMockStore(db: IDBDatabase): MonoMockStore;
|
|
302
|
+
//#endregion
|
|
303
|
+
//#region pkg/mock-db/index.d.ts
|
|
304
|
+
interface MonoMockRequest {
|
|
305
|
+
url: string;
|
|
306
|
+
method?: string;
|
|
307
|
+
/** Query string or param object — DevExtreme sends either. */
|
|
308
|
+
params?: string | Record<string, any>;
|
|
309
|
+
/**
|
|
310
|
+
* A prebuilt query, which wins over `params`.
|
|
311
|
+
*
|
|
312
|
+
* The fetch wrapper uses this to merge the core's `options` (the DevExtreme
|
|
313
|
+
* DataSourceOptions shape: select/filter/sort/expand/take/pageSize) with any raw
|
|
314
|
+
* `$`-params, since only the caller knows both.
|
|
315
|
+
*/
|
|
316
|
+
query?: ODataQuery;
|
|
317
|
+
/**
|
|
318
|
+
* Row key for PUT/PATCH/DELETE, when the url carries none.
|
|
319
|
+
*
|
|
320
|
+
* The core sends it in the payload envelope (`payload.keyValue`) and leaves the url
|
|
321
|
+
* keyless — `store.update(payload.keyValue, payload.data)`. A url key still wins,
|
|
322
|
+
* so the DevExtreme CustomStore's `Entity(key)` urls keep working.
|
|
323
|
+
*/
|
|
324
|
+
key?: string | number;
|
|
325
|
+
/** Body for POST/PUT/PATCH. */
|
|
326
|
+
payload?: Record<string, any> | null;
|
|
327
|
+
}
|
|
328
|
+
interface MonoMockResponse<T = any> {
|
|
329
|
+
data: T | null;
|
|
330
|
+
statusCode: number;
|
|
331
|
+
error: {
|
|
332
|
+
message: string;
|
|
333
|
+
stack: string;
|
|
334
|
+
response: any;
|
|
335
|
+
} | null;
|
|
336
|
+
}
|
|
337
|
+
interface MonoMockDb {
|
|
338
|
+
/** True once the schema is parsed and IndexedDB is hydrated. */
|
|
339
|
+
ready: Promise<void>;
|
|
340
|
+
schemas: MonoMockParsedSchema[];
|
|
341
|
+
/** Does this url belong to a mock schema? */
|
|
342
|
+
matches(url: string): boolean;
|
|
343
|
+
request<T = any>(request: MonoMockRequest): Promise<MonoMockResponse<T>>;
|
|
344
|
+
/** Wipe and re-hydrate from the seed. */
|
|
345
|
+
reset(): Promise<void>;
|
|
346
|
+
/** Dump every table (for promoting generated rows into an explicit `seed`). */
|
|
347
|
+
export(): Promise<Record<string, Record<string, Record<string, any>[]>>>;
|
|
348
|
+
/** Replace tables with the given rows. */
|
|
349
|
+
import(data: Record<string, Record<string, Record<string, any>[]>>): Promise<void>;
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Build the runtime for a config. Idempotent per page — `monoMockDb()` caches it,
|
|
353
|
+
* so the schema is parsed and IndexedDB opened once.
|
|
354
|
+
*/
|
|
355
|
+
declare function createMockDb(config: MonoMockDbConfig): MonoMockDb;
|
|
356
|
+
/**
|
|
357
|
+
* The page-wide mock backend, built from `mono.config.ts` `mockIndexedDB`.
|
|
358
|
+
* Returns null when the app declares no mock — every caller must treat that as
|
|
359
|
+
* "go to the network".
|
|
360
|
+
*/
|
|
361
|
+
declare function monoMockDb(config?: MonoMockDbConfig): MonoMockDb | null;
|
|
362
|
+
/** Test seam — drops the cached instance. */
|
|
363
|
+
declare function resetMonoMockDb(): void;
|
|
364
|
+
//#endregion
|
|
365
|
+
export { parseFilter as A, MonoMockSchemaError as B, FilterContext as C, evaluateFilter as D, ODataResult as E, MonoMockField as F, normalizeSegment as H, MonoMockFieldType as I, MonoMockParsedEntity as L, parseQuery as M, toODataEnvelope as N, executeQuery as O, tokenize as P, MonoMockParsedSchema as R, ExpandItem as S, ODataQuery as T, parseField as U, matchMockRoute as V, parseMockSchema as W, ApplyTransform as _, monoMockDb as a, applyTransforms as b, createMockStore as c, GenerateOptions as d, createRandom as f, AggregateSpec as g, AggregateMethod as h, createMockDb as i, parseKeySegment as j, parseExpand as k, isIndexedDbAvailable as l, orderEntitiesByDependency as m, MonoMockRequest as n, resetMonoMockDb as o, generateSeed as p, MonoMockResponse as r, MonoMockStore as s, MonoMockDb as t, openMockDb as u, CompileFilter as v, FilterNode as w, parseApply as x, ReadValue as y, MonoMockRelation as z };
|