@terreno/admin-backend 0.7.2 → 0.8.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/biome.jsonc CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "$schema": "https://biomejs.dev/schemas/2.3.10/schema.json",
2
+ "$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
3
3
  "extends": ["../biome.jsonc"],
4
4
  "files": {
5
5
  "includes": [
@@ -21,6 +21,10 @@ export interface AdminModelConfig {
21
21
  defaultSort?: string;
22
22
  /** Per-field overrides for widget type and other display options */
23
23
  fieldOverrides?: Record<string, AdminFieldOverride>;
24
+ /** Ordered list of field names for the form. Fields not listed are appended at the end. */
25
+ fieldOrder?: string[];
26
+ /** Fields to hide from admin forms/responses (e.g., password hash fields). */
27
+ hiddenFields?: string[];
24
28
  }
25
29
  /**
26
30
  * Configuration for a script that can be run from the admin panel.
@@ -108,6 +112,6 @@ export declare class AdminApp {
108
112
  *
109
113
  * @param app - The Express application instance to register with
110
114
  */
111
- register(app: express.Application): void;
115
+ register(app: express.Application, openApi?: unknown): void;
112
116
  private mountScriptRoutes;
113
117
  }
package/dist/adminApp.js CHANGED
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -70,13 +81,46 @@ exports.AdminApp = void 0;
70
81
  var api_1 = require("@terreno/api");
71
82
  var express_1 = __importDefault(require("express"));
72
83
  var luxon_1 = require("luxon");
73
- var extractFieldMeta = function (properties, required) {
84
+ var toPlainObject = function (value) {
85
+ if (value && typeof value === "object" && typeof value.toObject === "function") {
86
+ return value.toObject();
87
+ }
88
+ return value;
89
+ };
90
+ var removeHiddenFields = function (value, hiddenFieldSet) {
74
91
  var e_1, _a;
75
- var _b;
92
+ if (Array.isArray(value)) {
93
+ return value.map(function (item) { return removeHiddenFields(item, hiddenFieldSet); });
94
+ }
95
+ if (!value || typeof value !== "object") {
96
+ return value;
97
+ }
98
+ var plainValue = toPlainObject(value);
99
+ var nextValue = {};
100
+ try {
101
+ for (var _b = __values(Object.entries(plainValue)), _c = _b.next(); !_c.done; _c = _b.next()) {
102
+ var _d = __read(_c.value, 2), key = _d[0], fieldValue = _d[1];
103
+ if (!hiddenFieldSet.has(key)) {
104
+ nextValue[key] = fieldValue;
105
+ }
106
+ }
107
+ }
108
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
109
+ finally {
110
+ try {
111
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
112
+ }
113
+ finally { if (e_1) throw e_1.error; }
114
+ }
115
+ return nextValue;
116
+ };
117
+ var extractFieldMeta = function (properties, required) {
118
+ var e_2, _a;
119
+ var _b, _c, _d;
76
120
  var fields = {};
77
121
  try {
78
- for (var _c = __values(Object.entries(properties)), _d = _c.next(); !_d.done; _d = _c.next()) {
79
- var _e = __read(_d.value, 2), key = _e[0], prop = _e[1];
122
+ for (var _e = __values(Object.entries(properties)), _f = _e.next(); !_f.done; _f = _e.next()) {
123
+ var _g = __read(_f.value, 2), key = _g[0], prop = _g[1];
80
124
  fields[key] = {
81
125
  default: prop.default,
82
126
  description: prop.description,
@@ -85,18 +129,23 @@ var extractFieldMeta = function (properties, required) {
85
129
  required: required.includes(key),
86
130
  type: (_b = prop.type) !== null && _b !== void 0 ? _b : "string",
87
131
  };
132
+ // For array fields, extract item sub-field metadata
133
+ if (prop.type === "array" && ((_c = prop.items) === null || _c === void 0 ? void 0 : _c.properties)) {
134
+ var itemRequired = (_d = prop.items.required) !== null && _d !== void 0 ? _d : [];
135
+ fields[key].items = extractFieldMeta(prop.items.properties, itemRequired);
136
+ }
88
137
  // Check for ObjectId references in the raw property
89
138
  if (!fields[key].ref && prop.type === "string" && prop.format === "objectid") {
90
139
  // mongoose-to-swagger may not preserve ref directly; we'll handle this in register()
91
140
  }
92
141
  }
93
142
  }
94
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
143
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
95
144
  finally {
96
145
  try {
97
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
146
+ if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
98
147
  }
99
- finally { if (e_1) throw e_1.error; }
148
+ finally { if (e_2) throw e_2.error; }
100
149
  }
101
150
  return fields;
102
151
  };
@@ -165,22 +214,28 @@ var AdminApp = /** @class */ (function () {
165
214
  *
166
215
  * @param app - The Express application instance to register with
167
216
  */
168
- AdminApp.prototype.register = function (app) {
169
- var e_2, _a;
217
+ AdminApp.prototype.register = function (app, openApi) {
218
+ var e_3, _a;
170
219
  var _this = this;
171
- var _b, _c, _d;
220
+ var _b, _c, _d, _e;
172
221
  var basePath = (_b = this.options.basePath) !== null && _b !== void 0 ? _b : "/admin";
173
222
  var modelConfigs = this.options.models;
174
223
  // Build config response with field metadata from Mongoose schemas
175
224
  var configModels = modelConfigs.map(function (config) {
176
- var e_3, _a, e_4, _b;
177
- var _c, _d;
178
- var _e = (0, api_1.getOpenApiSpecForModel)(config.model), properties = _e.properties, required = _e.required;
225
+ var e_4, _a, e_5, _b;
226
+ var _c, _d, _e;
227
+ var _f = (0, api_1.getOpenApiSpecForModel)(config.model), properties = _f.properties, required = _f.required;
228
+ var hiddenFieldSet = new Set((_c = config.hiddenFields) !== null && _c !== void 0 ? _c : []);
229
+ var filteredProperties = Object.fromEntries(Object.entries(properties).filter(function (_a) {
230
+ var _b = __read(_a, 1), key = _b[0];
231
+ return !hiddenFieldSet.has(key);
232
+ }));
233
+ var filteredRequired = required.filter(function (key) { return !hiddenFieldSet.has(key); });
179
234
  // Extract ref information directly from the Mongoose schema
180
- var fields = extractFieldMeta(properties, required);
235
+ var fields = extractFieldMeta(filteredProperties, filteredRequired);
181
236
  try {
182
- for (var _f = __values(Object.entries(fields)), _g = _f.next(); !_g.done; _g = _f.next()) {
183
- var _h = __read(_g.value, 2), key = _h[0], field = _h[1];
237
+ for (var _g = __values(Object.entries(fields)), _h = _g.next(); !_h.done; _h = _g.next()) {
238
+ var _j = __read(_h.value, 2), key = _j[0], field = _j[1];
184
239
  var schemaPath = config.model.schema.path(key);
185
240
  if (schemaPath) {
186
241
  var pathOptions = schemaPath.options;
@@ -188,42 +243,43 @@ var AdminApp = /** @class */ (function () {
188
243
  field.ref = pathOptions.ref;
189
244
  }
190
245
  // Handle array of refs
191
- if (Array.isArray(pathOptions === null || pathOptions === void 0 ? void 0 : pathOptions.type) && ((_c = pathOptions.type[0]) === null || _c === void 0 ? void 0 : _c.ref)) {
246
+ if (Array.isArray(pathOptions === null || pathOptions === void 0 ? void 0 : pathOptions.type) && ((_d = pathOptions.type[0]) === null || _d === void 0 ? void 0 : _d.ref)) {
192
247
  field.ref = pathOptions.type[0].ref;
193
248
  }
194
249
  }
195
250
  }
196
251
  }
197
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
252
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
198
253
  finally {
199
254
  try {
200
- if (_g && !_g.done && (_a = _f.return)) _a.call(_f);
255
+ if (_h && !_h.done && (_a = _g.return)) _a.call(_g);
201
256
  }
202
- finally { if (e_3) throw e_3.error; }
257
+ finally { if (e_4) throw e_4.error; }
203
258
  }
204
259
  // Apply field overrides (e.g., widget: "markdown")
205
260
  if (config.fieldOverrides) {
206
261
  try {
207
- for (var _j = __values(Object.entries(config.fieldOverrides)), _k = _j.next(); !_k.done; _k = _j.next()) {
208
- var _l = __read(_k.value, 2), key = _l[0], override = _l[1];
262
+ for (var _k = __values(Object.entries(config.fieldOverrides)), _l = _k.next(); !_l.done; _l = _k.next()) {
263
+ var _m = __read(_l.value, 2), key = _m[0], override = _m[1];
209
264
  if (fields[key] && override.widget) {
210
265
  fields[key].widget = override.widget;
211
266
  }
212
267
  }
213
268
  }
214
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
269
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
215
270
  finally {
216
271
  try {
217
- if (_k && !_k.done && (_b = _j.return)) _b.call(_j);
272
+ if (_l && !_l.done && (_b = _k.return)) _b.call(_k);
218
273
  }
219
- finally { if (e_4) throw e_4.error; }
274
+ finally { if (e_5) throw e_5.error; }
220
275
  }
221
276
  }
222
277
  return {
223
- defaultSort: (_d = config.defaultSort) !== null && _d !== void 0 ? _d : "-created",
278
+ defaultSort: (_e = config.defaultSort) !== null && _e !== void 0 ? _e : "-created",
224
279
  displayName: config.displayName,
280
+ fieldOrder: config.fieldOrder,
225
281
  fields: fields,
226
- listFields: config.listFields,
282
+ listFields: config.listFields.filter(function (field) { return !hiddenFieldSet.has(field); }),
227
283
  name: config.model.modelName,
228
284
  routePath: "".concat(basePath).concat(config.routePath),
229
285
  };
@@ -286,7 +342,7 @@ var AdminApp = /** @class */ (function () {
286
342
  }); }));
287
343
  app.put(versionConfigPath, (0, api_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
288
344
  var raw, allowedFields, setFields, unsetFields, allowedFields_1, allowedFields_1_1, field, updateOp, doc;
289
- var e_5, _a;
345
+ var e_6, _a;
290
346
  return __generator(this, function (_b) {
291
347
  switch (_b.label) {
292
348
  case 0: return [4 /*yield*/, (0, api_1.checkPermissions)("update", [api_1.Permissions.IsAdmin], req.user)];
@@ -317,12 +373,12 @@ var AdminApp = /** @class */ (function () {
317
373
  }
318
374
  }
319
375
  }
320
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
376
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
321
377
  finally {
322
378
  try {
323
379
  if (allowedFields_1_1 && !allowedFields_1_1.done && (_a = allowedFields_1.return)) _a.call(allowedFields_1);
324
380
  }
325
- finally { if (e_5) throw e_5.error; }
381
+ finally { if (e_6) throw e_6.error; }
326
382
  }
327
383
  updateOp = {};
328
384
  if (Object.keys(setFields).length > 0) {
@@ -343,29 +399,34 @@ var AdminApp = /** @class */ (function () {
343
399
  }
344
400
  });
345
401
  }); }));
402
+ var _loop_1 = function (config) {
403
+ var hiddenFieldSet = new Set((_d = config.hiddenFields) !== null && _d !== void 0 ? _d : []);
404
+ var routerOptions = __assign(__assign({}, (openApi ? { openApi: openApi } : {})), { permissions: {
405
+ create: [api_1.Permissions.IsAdmin],
406
+ delete: [api_1.Permissions.IsAdmin],
407
+ list: [api_1.Permissions.IsAdmin],
408
+ read: [api_1.Permissions.IsAdmin],
409
+ update: [api_1.Permissions.IsAdmin],
410
+ }, responseHandler: hiddenFieldSet.size > 0
411
+ ? function (value, _method, _request, _options) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
412
+ return [2 /*return*/, removeHiddenFields(value, hiddenFieldSet)];
413
+ }); }); }
414
+ : undefined, sort: (_e = config.defaultSort) !== null && _e !== void 0 ? _e : "-created" });
415
+ app.use("".concat(basePath).concat(config.routePath), (0, api_1.modelRouter)(config.model, routerOptions));
416
+ };
346
417
  try {
347
418
  // Mount modelRouter for each model with IsAdmin permissions
348
419
  for (var modelConfigs_1 = __values(modelConfigs), modelConfigs_1_1 = modelConfigs_1.next(); !modelConfigs_1_1.done; modelConfigs_1_1 = modelConfigs_1.next()) {
349
420
  var config = modelConfigs_1_1.value;
350
- var routerOptions = {
351
- permissions: {
352
- create: [api_1.Permissions.IsAdmin],
353
- delete: [api_1.Permissions.IsAdmin],
354
- list: [api_1.Permissions.IsAdmin],
355
- read: [api_1.Permissions.IsAdmin],
356
- update: [api_1.Permissions.IsAdmin],
357
- },
358
- sort: (_d = config.defaultSort) !== null && _d !== void 0 ? _d : "-created",
359
- };
360
- app.use("".concat(basePath).concat(config.routePath), (0, api_1.modelRouter)(config.model, routerOptions));
421
+ _loop_1(config);
361
422
  }
362
423
  }
363
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
424
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
364
425
  finally {
365
426
  try {
366
427
  if (modelConfigs_1_1 && !modelConfigs_1_1.done && (_a = modelConfigs_1.return)) _a.call(modelConfigs_1);
367
428
  }
368
- finally { if (e_2) throw e_2.error; }
429
+ finally { if (e_3) throw e_3.error; }
369
430
  }
370
431
  // Mount script routes
371
432
  if (scriptConfigs.length > 0) {
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  },
6
6
  "dependencies": {
7
7
  "@google-cloud/storage": "^7.15.0",
8
- "@terreno/api": "0.7.2",
8
+ "@terreno/api": "0.8.1",
9
9
  "express": "^5.2.1",
10
10
  "luxon": "^3.7.2",
11
11
  "multer": "^1.4.5-lts.1"
@@ -49,5 +49,5 @@
49
49
  "test:ci": "bun test"
50
50
  },
51
51
  "types": "dist/index.d.ts",
52
- "version": "0.7.2"
52
+ "version": "0.8.1"
53
53
  }
package/src/adminApp.ts CHANGED
@@ -42,6 +42,10 @@ export interface AdminModelConfig {
42
42
  defaultSort?: string;
43
43
  /** Per-field overrides for widget type and other display options */
44
44
  fieldOverrides?: Record<string, AdminFieldOverride>;
45
+ /** Ordered list of field names for the form. Fields not listed are appended at the end. */
46
+ fieldOrder?: string[];
47
+ /** Fields to hide from admin forms/responses (e.g., password hash fields). */
48
+ hiddenFields?: string[];
45
49
  }
46
50
 
47
51
  /**
@@ -76,6 +80,8 @@ interface AdminFieldMeta {
76
80
  default?: any;
77
81
  ref?: string;
78
82
  widget?: string;
83
+ /** For array fields: metadata about each item's sub-fields */
84
+ items?: Record<string, AdminFieldMeta>;
79
85
  }
80
86
 
81
87
  interface AdminModelMeta {
@@ -85,6 +91,7 @@ interface AdminModelMeta {
85
91
  listFields: string[];
86
92
  defaultSort: string;
87
93
  fields: Record<string, AdminFieldMeta>;
94
+ fieldOrder?: string[];
88
95
  }
89
96
 
90
97
  interface AdminScriptMeta {
@@ -98,6 +105,30 @@ interface AdminConfigResponse {
98
105
  scripts: AdminScriptMeta[];
99
106
  }
100
107
 
108
+ const toPlainObject = (value: any): any => {
109
+ if (value && typeof value === "object" && typeof value.toObject === "function") {
110
+ return value.toObject();
111
+ }
112
+ return value;
113
+ };
114
+
115
+ const removeHiddenFields = (value: unknown, hiddenFieldSet: Set<string>): unknown => {
116
+ if (Array.isArray(value)) {
117
+ return value.map((item) => removeHiddenFields(item, hiddenFieldSet));
118
+ }
119
+ if (!value || typeof value !== "object") {
120
+ return value;
121
+ }
122
+ const plainValue = toPlainObject(value as any);
123
+ const nextValue: Record<string, unknown> = {};
124
+ for (const [key, fieldValue] of Object.entries(plainValue)) {
125
+ if (!hiddenFieldSet.has(key)) {
126
+ nextValue[key] = fieldValue;
127
+ }
128
+ }
129
+ return nextValue;
130
+ };
131
+
101
132
  const extractFieldMeta = (
102
133
  properties: Record<string, any>,
103
134
  required: string[]
@@ -113,6 +144,12 @@ const extractFieldMeta = (
113
144
  type: prop.type ?? "string",
114
145
  };
115
146
 
147
+ // For array fields, extract item sub-field metadata
148
+ if (prop.type === "array" && prop.items?.properties) {
149
+ const itemRequired: string[] = prop.items.required ?? [];
150
+ fields[key].items = extractFieldMeta(prop.items.properties, itemRequired);
151
+ }
152
+
116
153
  // Check for ObjectId references in the raw property
117
154
  if (!fields[key].ref && prop.type === "string" && prop.format === "objectid") {
118
155
  // mongoose-to-swagger may not preserve ref directly; we'll handle this in register()
@@ -189,16 +226,21 @@ export class AdminApp {
189
226
  *
190
227
  * @param app - The Express application instance to register with
191
228
  */
192
- register(app: express.Application): void {
229
+ register(app: express.Application, openApi?: unknown): void {
193
230
  const basePath = this.options.basePath ?? "/admin";
194
231
  const modelConfigs = this.options.models;
195
232
 
196
233
  // Build config response with field metadata from Mongoose schemas
197
234
  const configModels: AdminModelMeta[] = modelConfigs.map((config) => {
198
235
  const {properties, required} = getOpenApiSpecForModel(config.model);
236
+ const hiddenFieldSet = new Set(config.hiddenFields ?? []);
237
+ const filteredProperties = Object.fromEntries(
238
+ Object.entries(properties).filter(([key]) => !hiddenFieldSet.has(key))
239
+ );
240
+ const filteredRequired = required.filter((key) => !hiddenFieldSet.has(key));
199
241
 
200
242
  // Extract ref information directly from the Mongoose schema
201
- const fields = extractFieldMeta(properties, required);
243
+ const fields = extractFieldMeta(filteredProperties, filteredRequired);
202
244
  for (const [key, field] of Object.entries(fields)) {
203
245
  const schemaPath = config.model.schema.path(key);
204
246
  if (schemaPath) {
@@ -225,8 +267,9 @@ export class AdminApp {
225
267
  return {
226
268
  defaultSort: config.defaultSort ?? "-created",
227
269
  displayName: config.displayName,
270
+ fieldOrder: config.fieldOrder,
228
271
  fields,
229
- listFields: config.listFields,
272
+ listFields: config.listFields.filter((field) => !hiddenFieldSet.has(field)),
230
273
  name: config.model.modelName,
231
274
  routePath: `${basePath}${config.routePath}`,
232
275
  };
@@ -329,7 +372,9 @@ export class AdminApp {
329
372
 
330
373
  // Mount modelRouter for each model with IsAdmin permissions
331
374
  for (const config of modelConfigs) {
375
+ const hiddenFieldSet = new Set(config.hiddenFields ?? []);
332
376
  const routerOptions: ModelRouterOptions<any> = {
377
+ ...(openApi ? {openApi: openApi as any} : {}),
333
378
  permissions: {
334
379
  create: [Permissions.IsAdmin],
335
380
  delete: [Permissions.IsAdmin],
@@ -337,6 +382,11 @@ export class AdminApp {
337
382
  read: [Permissions.IsAdmin],
338
383
  update: [Permissions.IsAdmin],
339
384
  },
385
+ responseHandler:
386
+ hiddenFieldSet.size > 0
387
+ ? async (value, _method, _request, _options): Promise<any> =>
388
+ removeHiddenFields(value, hiddenFieldSet) as any
389
+ : undefined,
340
390
  sort: config.defaultSort ?? "-created",
341
391
  };
342
392