@terreno/admin-backend 57.0.0 → 57.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/README.md +3 -0
- package/dist/adminApp.d.ts +11 -4
- package/dist/adminApp.js +859 -263
- package/dist/adminConfigIdentity.d.ts +16 -0
- package/dist/adminConfigIdentity.js +100 -0
- package/dist/adminTextSearch.d.ts +17 -0
- package/dist/adminTextSearch.js +117 -0
- package/dist/adminUiV2.js +1 -1
- package/dist/aggregateAdmin.d.ts +20 -0
- package/dist/aggregateAdmin.js +275 -0
- package/dist/documentStorageApp.d.ts +3 -1
- package/dist/documentStorageApp.js +20 -42
- package/dist/filterParser.d.ts +11 -0
- package/dist/filterParser.js +260 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +15 -1
- package/dist/legacy.d.ts +9 -0
- package/dist/legacy.js +61 -0
- package/dist/resolvedAdminModel.d.ts +23 -0
- package/dist/resolvedAdminModel.js +130 -0
- package/dist/routePath.d.ts +2 -0
- package/dist/routePath.js +18 -0
- package/package.json +4 -3
- package/src/adminApp.models.test.ts +894 -1
- package/src/adminApp.test.ts +35 -1
- package/src/adminApp.ts +599 -98
- package/src/adminConfigIdentity.test.ts +56 -0
- package/src/adminConfigIdentity.ts +87 -0
- package/src/adminTextSearch.test.ts +73 -0
- package/src/adminTextSearch.ts +74 -0
- package/src/adminUiV2.test.ts +1 -0
- package/src/adminUiV2.ts +1 -1
- package/src/aggregateAdmin.test.ts +220 -0
- package/src/aggregateAdmin.ts +206 -0
- package/src/documentStorageApp.test.ts +11 -0
- package/src/documentStorageApp.ts +20 -39
- package/src/filterParser.test.ts +68 -0
- package/src/filterParser.ts +229 -0
- package/src/index.ts +35 -0
- package/src/legacy.test.ts +34 -0
- package/src/legacy.ts +63 -0
- package/src/resolvedAdminModel.ts +114 -0
- package/src/routePath.ts +14 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AdminContribution,
|
|
3
|
+
AdminCustomScreen,
|
|
4
|
+
AdminHomeWidgetContribution,
|
|
5
|
+
AdminScriptContribution,
|
|
6
|
+
ModelRouterRegistration,
|
|
7
|
+
TerrenoApp,
|
|
8
|
+
TerrenoPlugin,
|
|
9
|
+
} from "@terreno/api";
|
|
10
|
+
import {logger} from "@terreno/api";
|
|
11
|
+
|
|
12
|
+
import type {AdminCustomScreenConfig, AdminModelConfig, AdminScriptConfig} from "./adminApp";
|
|
13
|
+
import {convertLegacyModelConfig} from "./legacy";
|
|
14
|
+
import {type ResolvedAdminModel, resolvedModelFromAdminConfig} from "./resolvedAdminModel";
|
|
15
|
+
import {normalizeAdminRoutePath} from "./routePath";
|
|
16
|
+
|
|
17
|
+
const isModelRouterRegistration = (
|
|
18
|
+
registration: ModelRouterRegistration | TerrenoPlugin
|
|
19
|
+
): registration is ModelRouterRegistration => {
|
|
20
|
+
return (registration as ModelRouterRegistration).__type === "modelRouter";
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export interface AggregatedAdminContributions {
|
|
24
|
+
customScreens: AdminCustomScreenConfig[];
|
|
25
|
+
models: ResolvedAdminModel[];
|
|
26
|
+
scripts: AdminScriptConfig[];
|
|
27
|
+
widgetIds: string[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const contributionScriptToConfig = (script: AdminScriptContribution): AdminScriptConfig => ({
|
|
31
|
+
args: script.args,
|
|
32
|
+
description: script.description,
|
|
33
|
+
name: script.name,
|
|
34
|
+
runner: script.runner,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const contributionScreenToConfig = (screen: AdminCustomScreen): AdminCustomScreenConfig => ({
|
|
38
|
+
displayName: screen.displayName,
|
|
39
|
+
name: screen.name,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const mergeUniqueByName = <T extends {name: string}>(existing: T[], incoming: T[]): T[] => {
|
|
43
|
+
const seen = new Set(existing.map((item) => item.name));
|
|
44
|
+
const merged = [...existing];
|
|
45
|
+
for (const item of incoming) {
|
|
46
|
+
if (seen.has(item.name)) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
seen.add(item.name);
|
|
50
|
+
merged.push(item);
|
|
51
|
+
}
|
|
52
|
+
return merged;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const mergeWidgetIds = (existing: string[], incoming: AdminHomeWidgetContribution[]): string[] => {
|
|
56
|
+
const seen = new Set(existing);
|
|
57
|
+
const merged = [...existing];
|
|
58
|
+
for (const widget of incoming) {
|
|
59
|
+
if (seen.has(widget.id)) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
seen.add(widget.id);
|
|
63
|
+
merged.push(widget.id);
|
|
64
|
+
}
|
|
65
|
+
return merged;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const warnShadowed = (
|
|
69
|
+
path: string,
|
|
70
|
+
incoming: ResolvedAdminModel,
|
|
71
|
+
existing: ResolvedAdminModel
|
|
72
|
+
): void => {
|
|
73
|
+
logger.warn(
|
|
74
|
+
`[admin] routePath "${path}" from ${incoming.source} (${incoming.sourceLabel}) overrides ${existing.source} (${existing.sourceLabel})`
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const aggregateAdminContributions = ({
|
|
79
|
+
legacyModels = [],
|
|
80
|
+
pluginContributions = [],
|
|
81
|
+
registeredModels = [],
|
|
82
|
+
}: {
|
|
83
|
+
legacyModels?: AdminModelConfig[];
|
|
84
|
+
pluginContributions?: AdminContribution[];
|
|
85
|
+
registeredModels?: ResolvedAdminModel[];
|
|
86
|
+
}): AggregatedAdminContributions => {
|
|
87
|
+
const modelsByPath = new Map<string, ResolvedAdminModel>();
|
|
88
|
+
const registeredByPath = new Map<string, ResolvedAdminModel>();
|
|
89
|
+
|
|
90
|
+
for (const legacy of legacyModels) {
|
|
91
|
+
const resolved = convertLegacyModelConfig(legacy);
|
|
92
|
+
modelsByPath.set(resolved.routePath, resolved);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let customScreens: AdminCustomScreenConfig[] = [];
|
|
96
|
+
let scripts: AdminScriptConfig[] = [];
|
|
97
|
+
let widgetIds: string[] = [];
|
|
98
|
+
|
|
99
|
+
for (const contribution of pluginContributions) {
|
|
100
|
+
if (contribution.homeWidgets) {
|
|
101
|
+
widgetIds = mergeWidgetIds(widgetIds, contribution.homeWidgets);
|
|
102
|
+
}
|
|
103
|
+
if (contribution.customScreens) {
|
|
104
|
+
customScreens = mergeUniqueByName(
|
|
105
|
+
customScreens,
|
|
106
|
+
contribution.customScreens.map(contributionScreenToConfig)
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
if (contribution.scripts) {
|
|
110
|
+
scripts = mergeUniqueByName(scripts, contribution.scripts.map(contributionScriptToConfig));
|
|
111
|
+
}
|
|
112
|
+
for (const modelContribution of contribution.models ?? []) {
|
|
113
|
+
const path = normalizeAdminRoutePath(modelContribution.routePath);
|
|
114
|
+
const resolved = resolvedModelFromAdminConfig({
|
|
115
|
+
admin: modelContribution.admin,
|
|
116
|
+
model: modelContribution.model,
|
|
117
|
+
populatePaths: modelContribution.populatePaths,
|
|
118
|
+
queryFilter: undefined,
|
|
119
|
+
routePath: path,
|
|
120
|
+
source: "plugin",
|
|
121
|
+
sourceLabel: modelContribution.admin.displayName,
|
|
122
|
+
});
|
|
123
|
+
const existing = modelsByPath.get(path);
|
|
124
|
+
if (existing) {
|
|
125
|
+
warnShadowed(path, resolved, existing);
|
|
126
|
+
}
|
|
127
|
+
modelsByPath.set(path, resolved);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
for (const registered of registeredModels) {
|
|
132
|
+
const path = registered.routePath;
|
|
133
|
+
const existingRegistered = registeredByPath.get(path);
|
|
134
|
+
if (existingRegistered) {
|
|
135
|
+
throw new Error(
|
|
136
|
+
`Duplicate admin modelRouter routePath "${path}" (${existingRegistered.sourceLabel} and ${registered.sourceLabel})`
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
registeredByPath.set(path, registered);
|
|
140
|
+
const existing = modelsByPath.get(path);
|
|
141
|
+
if (existing) {
|
|
142
|
+
warnShadowed(path, registered, existing);
|
|
143
|
+
}
|
|
144
|
+
modelsByPath.set(path, registered);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
customScreens,
|
|
149
|
+
models: [...modelsByPath.values()],
|
|
150
|
+
scripts,
|
|
151
|
+
widgetIds,
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
export const collectRegisteredAdminModels = (terrenoApp?: TerrenoApp): ResolvedAdminModel[] => {
|
|
156
|
+
if (!terrenoApp) {
|
|
157
|
+
return [];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const models: ResolvedAdminModel[] = [];
|
|
161
|
+
for (const registration of terrenoApp.getRegistrations()) {
|
|
162
|
+
if (!isModelRouterRegistration(registration) || !registration.options.admin) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
models.push(
|
|
166
|
+
resolvedModelFromAdminConfig({
|
|
167
|
+
admin: registration.options.admin,
|
|
168
|
+
model: registration.model,
|
|
169
|
+
populatePaths: registration.options.populatePaths,
|
|
170
|
+
registrationPath: registration.path,
|
|
171
|
+
routePath: registration.path,
|
|
172
|
+
source: "registered",
|
|
173
|
+
sourceLabel: registration.model.modelName,
|
|
174
|
+
})
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
return models;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
export const collectPluginAdminContributions = (terrenoApp?: TerrenoApp): AdminContribution[] => {
|
|
181
|
+
if (!terrenoApp) {
|
|
182
|
+
return [];
|
|
183
|
+
}
|
|
184
|
+
const contributions: AdminContribution[] = [];
|
|
185
|
+
for (const plugin of terrenoApp.getPlugins()) {
|
|
186
|
+
const contribution = plugin.adminContribution?.();
|
|
187
|
+
if (contribution) {
|
|
188
|
+
contributions.push(contribution);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return contributions;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
export const aggregateFromTerrenoApp = ({
|
|
195
|
+
legacyModels = [],
|
|
196
|
+
terrenoApp,
|
|
197
|
+
}: {
|
|
198
|
+
legacyModels?: AdminModelConfig[];
|
|
199
|
+
terrenoApp?: TerrenoApp;
|
|
200
|
+
}): AggregatedAdminContributions => {
|
|
201
|
+
return aggregateAdminContributions({
|
|
202
|
+
legacyModels,
|
|
203
|
+
pluginContributions: collectPluginAdminContributions(terrenoApp),
|
|
204
|
+
registeredModels: collectRegisteredAdminModels(terrenoApp),
|
|
205
|
+
});
|
|
206
|
+
};
|
|
@@ -78,6 +78,16 @@ const buildApp = (options: DocumentStorageOptions): express.Application => {
|
|
|
78
78
|
return app;
|
|
79
79
|
};
|
|
80
80
|
|
|
81
|
+
describe("DocumentStorageApp admin contribution", () => {
|
|
82
|
+
it("contributes the documents custom screen", () => {
|
|
83
|
+
const contribution = new DocumentStorageApp({bucketName: "test-bucket"}).adminContribution();
|
|
84
|
+
|
|
85
|
+
expect(contribution).toEqual({
|
|
86
|
+
customScreens: [{displayName: "Documents", icon: "folder", name: "documents"}],
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
81
91
|
describe("DocumentStorageApp", () => {
|
|
82
92
|
let app: express.Application;
|
|
83
93
|
let adminAgent: TestAgent;
|
|
@@ -314,6 +324,7 @@ describe("DocumentStorageApp", () => {
|
|
|
314
324
|
|
|
315
325
|
const res = await adminAgent.get("/documents/download/boom.pdf").expect(500);
|
|
316
326
|
expect(res.body.title).toInclude("Failed to access file");
|
|
327
|
+
expect(JSON.stringify(res.body)).not.toInclude("broken");
|
|
317
328
|
});
|
|
318
329
|
|
|
319
330
|
it("logs and swallows stream errors once headers have been flushed", async () => {
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import {pipeline} from "node:stream/promises";
|
|
2
2
|
import {Storage} from "@google-cloud/storage";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
type AdminContribution,
|
|
5
|
+
APIError,
|
|
6
|
+
asyncHandler,
|
|
7
|
+
authenticateMiddleware,
|
|
8
|
+
logger,
|
|
9
|
+
type TerrenoPlugin,
|
|
10
|
+
} from "@terreno/api";
|
|
4
11
|
import type express from "express";
|
|
5
12
|
import {DateTime} from "luxon";
|
|
6
13
|
import multer from "multer";
|
|
@@ -66,7 +73,7 @@ const isAdmin = (req: express.Request): boolean => {
|
|
|
66
73
|
return user?.admin === true;
|
|
67
74
|
};
|
|
68
75
|
|
|
69
|
-
export class DocumentStorageApp {
|
|
76
|
+
export class DocumentStorageApp implements TerrenoPlugin {
|
|
70
77
|
private options: DocumentStorageOptions;
|
|
71
78
|
private storage: Storage;
|
|
72
79
|
|
|
@@ -75,6 +82,12 @@ export class DocumentStorageApp {
|
|
|
75
82
|
this.storage = new Storage();
|
|
76
83
|
}
|
|
77
84
|
|
|
85
|
+
adminContribution(): AdminContribution {
|
|
86
|
+
return {
|
|
87
|
+
customScreens: [{displayName: "Documents", icon: "folder", name: "documents"}],
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
78
91
|
private get bucket() {
|
|
79
92
|
const bucketName = this.options.bucketName || process.env.GCS_BUCKET;
|
|
80
93
|
if (!bucketName) {
|
|
@@ -223,50 +236,26 @@ export class DocumentStorageApp {
|
|
|
223
236
|
const gcsPath = `${this.prefix}${filePath}`;
|
|
224
237
|
const gcsFile = this.bucket.file(gcsPath);
|
|
225
238
|
|
|
226
|
-
console.info("[documentStorage] download attempt", {filePath, gcsPath});
|
|
227
|
-
|
|
228
239
|
let metadata: Record<string, unknown>;
|
|
229
240
|
try {
|
|
230
241
|
const [meta] = await gcsFile.getMetadata();
|
|
231
242
|
metadata = meta as Record<string, unknown>;
|
|
232
|
-
console.info("[documentStorage] getMetadata success", {
|
|
233
|
-
contentType: metadata.contentType,
|
|
234
|
-
etag: metadata.etag,
|
|
235
|
-
size: metadata.size,
|
|
236
|
-
});
|
|
237
243
|
} catch (err: unknown) {
|
|
238
244
|
const storageErr = err as {
|
|
239
245
|
code?: number;
|
|
240
|
-
message?: string;
|
|
241
|
-
stack?: string;
|
|
242
|
-
errors?: unknown[];
|
|
243
|
-
response?: unknown;
|
|
244
246
|
status?: number;
|
|
245
247
|
};
|
|
246
248
|
if (storageErr?.code === 404) {
|
|
247
249
|
throw new APIError({
|
|
248
|
-
detail: filePath,
|
|
249
250
|
disableExternalErrorTracking: true,
|
|
250
251
|
status: 404,
|
|
251
252
|
title: "File not found",
|
|
252
253
|
});
|
|
253
254
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
errors: storageErr?.errors,
|
|
257
|
-
message: storageErr?.message,
|
|
258
|
-
response: storageErr?.response,
|
|
259
|
-
stack: storageErr?.stack,
|
|
260
|
-
status: storageErr?.status,
|
|
261
|
-
});
|
|
262
|
-
logger.error("[documentStorage] getMetadata error", {
|
|
263
|
-
code: storageErr?.code,
|
|
264
|
-
errors: storageErr?.errors,
|
|
265
|
-
message: storageErr?.message,
|
|
266
|
-
status: storageErr?.status,
|
|
255
|
+
logger.error("Document download metadata failed", {
|
|
256
|
+
status: storageErr?.code ?? storageErr?.status,
|
|
267
257
|
});
|
|
268
258
|
throw new APIError({
|
|
269
|
-
detail: storageErr?.message ?? String(err),
|
|
270
259
|
status: 500,
|
|
271
260
|
title: "Failed to access file",
|
|
272
261
|
});
|
|
@@ -285,20 +274,12 @@ export class DocumentStorageApp {
|
|
|
285
274
|
try {
|
|
286
275
|
await pipeline(gcsFile.createReadStream(), res);
|
|
287
276
|
} catch (err: unknown) {
|
|
288
|
-
const pipelineErr = err as {code?: number
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
message: pipelineErr?.message,
|
|
292
|
-
stack: pipelineErr?.stack,
|
|
293
|
-
});
|
|
294
|
-
logger.error("[documentStorage] pipeline error", {
|
|
295
|
-
code: pipelineErr?.code,
|
|
296
|
-
message: pipelineErr?.message,
|
|
297
|
-
stack: pipelineErr?.stack,
|
|
277
|
+
const pipelineErr = err as {code?: number};
|
|
278
|
+
logger.error("Document download stream failed", {
|
|
279
|
+
status: pipelineErr?.code,
|
|
298
280
|
});
|
|
299
281
|
if (!res.headersSent) {
|
|
300
282
|
throw new APIError({
|
|
301
|
-
detail: pipelineErr?.message ?? String(err),
|
|
302
283
|
status: 500,
|
|
303
284
|
title: "Failed to stream file",
|
|
304
285
|
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import type {AdminListFilter} from "./adminUiV2";
|
|
4
|
+
import {parseAdminListFilters} from "./filterParser";
|
|
5
|
+
|
|
6
|
+
const filters: AdminListFilter[] = [
|
|
7
|
+
{field: "admin", kind: "boolean"},
|
|
8
|
+
{choices: [{label: "Staff", value: "staff"}], field: "role", kind: "choice"},
|
|
9
|
+
{field: "name", kind: "text"},
|
|
10
|
+
{field: "ownerId", kind: "ref"},
|
|
11
|
+
{field: "created", kind: "dateRange"},
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
describe("parseAdminListFilters", () => {
|
|
15
|
+
it("parses v2-compatible boolean, choice, text, ref, and dateRange params", () => {
|
|
16
|
+
const {errors, filter, consumedKeys} = parseAdminListFilters(
|
|
17
|
+
{
|
|
18
|
+
admin: "true",
|
|
19
|
+
created_gte: "2024-01-01T00:00:00.000Z",
|
|
20
|
+
created_lte: "2024-12-31T23:59:59.999Z",
|
|
21
|
+
name: "alice",
|
|
22
|
+
ownerId: "507f1f77bcf86cd799439011",
|
|
23
|
+
role: "staff",
|
|
24
|
+
},
|
|
25
|
+
filters
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
expect(errors).toEqual({});
|
|
29
|
+
expect(consumedKeys.has("admin")).toBe(true);
|
|
30
|
+
expect(consumedKeys.has("created_gte")).toBe(true);
|
|
31
|
+
expect(filter.admin).toBe(true);
|
|
32
|
+
expect(filter.role).toBe("staff");
|
|
33
|
+
expect(filter.name).toBe("alice");
|
|
34
|
+
expect(filter.ownerId).toBe("507f1f77bcf86cd799439011");
|
|
35
|
+
expect(filter.created).toEqual({
|
|
36
|
+
$gte: new Date("2024-01-01T00:00:00.000Z"),
|
|
37
|
+
$lte: new Date("2024-12-31T23:59:59.999Z"),
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("rejects invalid shapes and mongo operator keys", () => {
|
|
42
|
+
const {errors} = parseAdminListFilters(
|
|
43
|
+
{
|
|
44
|
+
$where: "1",
|
|
45
|
+
admin: ["true"],
|
|
46
|
+
role: "missing",
|
|
47
|
+
},
|
|
48
|
+
filters
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
expect(errors.admin).toBeDefined();
|
|
52
|
+
expect(errors.role).toBeDefined();
|
|
53
|
+
expect(errors.$where).toBeDefined();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("drops prototype pollution keys without surfacing them as filter errors", () => {
|
|
57
|
+
const {errors, filter} = parseAdminListFilters(
|
|
58
|
+
{
|
|
59
|
+
__proto__: {polluted: true},
|
|
60
|
+
admin: "true",
|
|
61
|
+
},
|
|
62
|
+
filters
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
expect(errors).toEqual({});
|
|
66
|
+
expect(filter.admin).toBe(true);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import type {AdminFilter} from "@terreno/api";
|
|
2
|
+
import {logger} from "@terreno/api";
|
|
3
|
+
import {DateTime} from "luxon";
|
|
4
|
+
import mongoose from "mongoose";
|
|
5
|
+
|
|
6
|
+
const POLLUTION_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
7
|
+
|
|
8
|
+
const isOperatorKey = (key: string): boolean => {
|
|
9
|
+
return key.startsWith("$");
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const dropPollutionKeys = (query: Record<string, unknown>): Record<string, unknown> => {
|
|
13
|
+
const next: Record<string, unknown> = {};
|
|
14
|
+
for (const [key, value] of Object.entries(query)) {
|
|
15
|
+
if (POLLUTION_KEYS.has(key)) {
|
|
16
|
+
logger.warn(`[admin] Dropping prototype-pollution query key: ${key}`);
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
next[key] = value;
|
|
20
|
+
}
|
|
21
|
+
return next;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const scalarString = (value: unknown): string | undefined => {
|
|
25
|
+
if (typeof value === "string") {
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
29
|
+
return String(value);
|
|
30
|
+
}
|
|
31
|
+
return undefined;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const parseBooleanFilter = (
|
|
35
|
+
field: string,
|
|
36
|
+
value: unknown
|
|
37
|
+
): {ok: true; value: boolean} | {ok: false; error: string} => {
|
|
38
|
+
const scalar = scalarString(value);
|
|
39
|
+
if (scalar === "true") {
|
|
40
|
+
return {ok: true, value: true};
|
|
41
|
+
}
|
|
42
|
+
if (scalar === "false") {
|
|
43
|
+
return {ok: true, value: false};
|
|
44
|
+
}
|
|
45
|
+
return {error: `${field} must be true or false`, ok: false};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const parseTextFilter = (
|
|
49
|
+
field: string,
|
|
50
|
+
value: unknown
|
|
51
|
+
): {ok: true; value: string} | {ok: false; error: string} => {
|
|
52
|
+
const scalar = scalarString(value);
|
|
53
|
+
if (
|
|
54
|
+
scalar === undefined ||
|
|
55
|
+
Array.isArray(value) ||
|
|
56
|
+
(value != null && typeof value === "object")
|
|
57
|
+
) {
|
|
58
|
+
return {error: `${field} must be a string`, ok: false};
|
|
59
|
+
}
|
|
60
|
+
return {ok: true, value: scalar};
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const parseChoiceFilter = (
|
|
64
|
+
field: string,
|
|
65
|
+
value: unknown,
|
|
66
|
+
choices: {label: string; value: string}[]
|
|
67
|
+
): {ok: true; value: string} | {ok: false; error: string} => {
|
|
68
|
+
const parsed = parseTextFilter(field, value);
|
|
69
|
+
if (!parsed.ok) {
|
|
70
|
+
return parsed;
|
|
71
|
+
}
|
|
72
|
+
const allowed = new Set(choices.map((choice) => choice.value));
|
|
73
|
+
if (!allowed.has(parsed.value)) {
|
|
74
|
+
return {error: `${field} must be one of: ${[...allowed].join(", ")}`, ok: false};
|
|
75
|
+
}
|
|
76
|
+
return parsed;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const parseRefFilter = (
|
|
80
|
+
field: string,
|
|
81
|
+
value: unknown
|
|
82
|
+
): {ok: true; value: string} | {ok: false; error: string} => {
|
|
83
|
+
const parsed = parseTextFilter(field, value);
|
|
84
|
+
if (!parsed.ok) {
|
|
85
|
+
return parsed;
|
|
86
|
+
}
|
|
87
|
+
if (!mongoose.isValidObjectId(parsed.value)) {
|
|
88
|
+
return {error: `${field} must be a valid id`, ok: false};
|
|
89
|
+
}
|
|
90
|
+
return parsed;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const parseIsoDate = (
|
|
94
|
+
field: string,
|
|
95
|
+
value: unknown
|
|
96
|
+
): {ok: true; value: Date} | {ok: false; error: string} => {
|
|
97
|
+
const scalar = scalarString(value);
|
|
98
|
+
if (
|
|
99
|
+
scalar === undefined ||
|
|
100
|
+
Array.isArray(value) ||
|
|
101
|
+
(value != null && typeof value === "object")
|
|
102
|
+
) {
|
|
103
|
+
return {error: `${field} must be an ISO date string`, ok: false};
|
|
104
|
+
}
|
|
105
|
+
const parsed = DateTime.fromISO(scalar, {zone: "utc"});
|
|
106
|
+
if (!parsed.isValid) {
|
|
107
|
+
return {error: `${field} must be an ISO date string`, ok: false};
|
|
108
|
+
}
|
|
109
|
+
return {ok: true, value: parsed.toJSDate()};
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export interface ParseAdminListFiltersResult {
|
|
113
|
+
consumedKeys: Set<string>;
|
|
114
|
+
errors: Record<string, string>;
|
|
115
|
+
filter: Record<string, unknown>;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Parses v2-compatible list query params for declared admin filters.
|
|
120
|
+
* Undeclared keys are ignored; Mongo operator keys are rejected.
|
|
121
|
+
*/
|
|
122
|
+
export const parseAdminListFilters = (
|
|
123
|
+
query: Record<string, unknown>,
|
|
124
|
+
filters: AdminFilter[] = []
|
|
125
|
+
): ParseAdminListFiltersResult => {
|
|
126
|
+
const safeQuery = dropPollutionKeys(query);
|
|
127
|
+
const errors: Record<string, string> = {};
|
|
128
|
+
const filter: Record<string, unknown> = {};
|
|
129
|
+
const consumedKeys = new Set<string>();
|
|
130
|
+
|
|
131
|
+
for (const declared of filters) {
|
|
132
|
+
const field = declared.field;
|
|
133
|
+
if (declared.kind === "boolean") {
|
|
134
|
+
if (!(field in safeQuery)) {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
consumedKeys.add(field);
|
|
138
|
+
const parsed = parseBooleanFilter(field, safeQuery[field]);
|
|
139
|
+
if (!parsed.ok) {
|
|
140
|
+
errors[field] = parsed.error;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
filter[field] = parsed.value;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (declared.kind === "text") {
|
|
148
|
+
if (!(field in safeQuery)) {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
consumedKeys.add(field);
|
|
152
|
+
const parsed = parseTextFilter(field, safeQuery[field]);
|
|
153
|
+
if (!parsed.ok) {
|
|
154
|
+
errors[field] = parsed.error;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
filter[field] = parsed.value;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (declared.kind === "choice") {
|
|
162
|
+
if (!(field in safeQuery)) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
consumedKeys.add(field);
|
|
166
|
+
const parsed = parseChoiceFilter(field, safeQuery[field], declared.choices);
|
|
167
|
+
if (!parsed.ok) {
|
|
168
|
+
errors[field] = parsed.error;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
filter[field] = parsed.value;
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (declared.kind === "ref") {
|
|
176
|
+
if (!(field in safeQuery)) {
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
consumedKeys.add(field);
|
|
180
|
+
const parsed = parseRefFilter(field, safeQuery[field]);
|
|
181
|
+
if (!parsed.ok) {
|
|
182
|
+
errors[field] = parsed.error;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
filter[field] = parsed.value;
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (declared.kind === "dateRange") {
|
|
190
|
+
const gteKey = `${field}_gte`;
|
|
191
|
+
const lteKey = `${field}_lte`;
|
|
192
|
+
const hasGte = gteKey in safeQuery;
|
|
193
|
+
const hasLte = lteKey in safeQuery;
|
|
194
|
+
if (!hasGte && !hasLte) {
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
const range: {$gte?: Date; $lte?: Date} = {};
|
|
198
|
+
if (hasGte) {
|
|
199
|
+
consumedKeys.add(gteKey);
|
|
200
|
+
const parsed = parseIsoDate(gteKey, safeQuery[gteKey]);
|
|
201
|
+
if (!parsed.ok) {
|
|
202
|
+
errors[gteKey] = parsed.error;
|
|
203
|
+
} else {
|
|
204
|
+
range.$gte = parsed.value;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (hasLte) {
|
|
208
|
+
consumedKeys.add(lteKey);
|
|
209
|
+
const parsed = parseIsoDate(lteKey, safeQuery[lteKey]);
|
|
210
|
+
if (!parsed.ok) {
|
|
211
|
+
errors[lteKey] = parsed.error;
|
|
212
|
+
} else {
|
|
213
|
+
range.$lte = parsed.value;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (Object.keys(range).length > 0) {
|
|
217
|
+
filter[field] = range;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
for (const key of Object.keys(safeQuery)) {
|
|
223
|
+
if (isOperatorKey(key)) {
|
|
224
|
+
errors[key] = "Mongo operators are not allowed in admin list filters";
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return {consumedKeys, errors, filter};
|
|
229
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
AdminChangeEvent,
|
|
3
|
+
AdminChangeType,
|
|
4
|
+
AdminConfig,
|
|
5
|
+
AdminContribution,
|
|
6
|
+
AdminCustomScreen,
|
|
7
|
+
AdminFieldset,
|
|
8
|
+
AdminFilter,
|
|
9
|
+
AdminFilterBoolean,
|
|
10
|
+
AdminFilterChoice,
|
|
11
|
+
AdminFilterDateRange,
|
|
12
|
+
AdminFilterKind,
|
|
13
|
+
AdminFilterRef,
|
|
14
|
+
AdminFilterText,
|
|
15
|
+
AdminHomeConfig,
|
|
16
|
+
AdminHomeSlots,
|
|
17
|
+
AdminHomeWidgetContribution,
|
|
18
|
+
AdminModelContribution,
|
|
19
|
+
AdminModelPermissions,
|
|
20
|
+
AdminScriptContribution,
|
|
21
|
+
TerrenoAppAdminEvent,
|
|
22
|
+
} from "@terreno/api";
|
|
1
23
|
export type {
|
|
2
24
|
AdminAuditEvent,
|
|
3
25
|
AdminCustomScreenConfig,
|
|
@@ -8,11 +30,24 @@ export type {
|
|
|
8
30
|
} from "./adminApp";
|
|
9
31
|
export {AdminApp} from "./adminApp";
|
|
10
32
|
export * from "./adminUiV2";
|
|
33
|
+
export type {AggregatedAdminContributions} from "./aggregateAdmin";
|
|
34
|
+
export {
|
|
35
|
+
aggregateAdminContributions,
|
|
36
|
+
aggregateFromTerrenoApp,
|
|
37
|
+
collectPluginAdminContributions,
|
|
38
|
+
collectRegisteredAdminModels,
|
|
39
|
+
} from "./aggregateAdmin";
|
|
11
40
|
export type {
|
|
12
41
|
DocumentFile,
|
|
13
42
|
DocumentListResponse,
|
|
14
43
|
DocumentStorageOptions,
|
|
15
44
|
} from "./documentStorageApp";
|
|
16
45
|
export {DocumentStorageApp} from "./documentStorageApp";
|
|
46
|
+
export type {ParseAdminListFiltersResult} from "./filterParser";
|
|
47
|
+
export {parseAdminListFilters} from "./filterParser";
|
|
48
|
+
export {convertLegacyModelConfig, resetLegacyDeprecationWarningsForTests} from "./legacy";
|
|
49
|
+
export type {AdminModelSource, ResolvedAdminModel} from "./resolvedAdminModel";
|
|
50
|
+
export {resolvedModelFromAdminConfig} from "./resolvedAdminModel";
|
|
51
|
+
export {normalizeAdminRoutePath} from "./routePath";
|
|
17
52
|
export type {RunScriptCliOptions, RunScriptCliResult} from "./scriptCli";
|
|
18
53
|
export {runScriptCli} from "./scriptCli";
|