@terreno/admin-backend 0.3.2

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.
@@ -0,0 +1,18 @@
1
+ import type express from "express";
2
+ import type { Model } from "mongoose";
3
+ export interface AdminModelConfig {
4
+ model: Model<any>;
5
+ routePath: string;
6
+ displayName: string;
7
+ listFields: string[];
8
+ defaultSort?: string;
9
+ }
10
+ export interface AdminOptions {
11
+ models: AdminModelConfig[];
12
+ basePath?: string;
13
+ }
14
+ export declare class AdminApp {
15
+ private options;
16
+ constructor(options: AdminOptions);
17
+ register(app: express.Application): void;
18
+ }
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+ var __values = (this && this.__values) || function(o) {
3
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4
+ if (m) return m.call(o);
5
+ if (o && typeof o.length === "number") return {
6
+ next: function () {
7
+ if (o && i >= o.length) o = void 0;
8
+ return { value: o && o[i++], done: !o };
9
+ }
10
+ };
11
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
12
+ };
13
+ var __read = (this && this.__read) || function (o, n) {
14
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
15
+ if (!m) return o;
16
+ var i = m.call(o), r, ar = [], e;
17
+ try {
18
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
+ }
20
+ catch (error) { e = { error: error }; }
21
+ finally {
22
+ try {
23
+ if (r && !r.done && (m = i["return"])) m.call(i);
24
+ }
25
+ finally { if (e) throw e.error; }
26
+ }
27
+ return ar;
28
+ };
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.AdminApp = void 0;
31
+ var api_1 = require("@terreno/api");
32
+ var extractFieldMeta = function (properties, required) {
33
+ var e_1, _a;
34
+ var _b;
35
+ var fields = {};
36
+ try {
37
+ for (var _c = __values(Object.entries(properties)), _d = _c.next(); !_d.done; _d = _c.next()) {
38
+ var _e = __read(_d.value, 2), key = _e[0], prop = _e[1];
39
+ fields[key] = {
40
+ default: prop.default,
41
+ description: prop.description,
42
+ enum: prop.enum,
43
+ ref: prop.$ref ? prop.$ref.replace("#/components/schemas/", "") : undefined,
44
+ required: required.includes(key),
45
+ type: (_b = prop.type) !== null && _b !== void 0 ? _b : "string",
46
+ };
47
+ // Check for ObjectId references in the raw property
48
+ if (!fields[key].ref && prop.type === "string" && prop.format === "objectid") {
49
+ // mongoose-to-swagger may not preserve ref directly; we'll handle this in register()
50
+ }
51
+ }
52
+ }
53
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
54
+ finally {
55
+ try {
56
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
57
+ }
58
+ finally { if (e_1) throw e_1.error; }
59
+ }
60
+ return fields;
61
+ };
62
+ var AdminApp = /** @class */ (function () {
63
+ function AdminApp(options) {
64
+ this.options = options;
65
+ }
66
+ AdminApp.prototype.register = function (app) {
67
+ var e_2, _a;
68
+ var _b, _c;
69
+ var basePath = (_b = this.options.basePath) !== null && _b !== void 0 ? _b : "/admin";
70
+ var modelConfigs = this.options.models;
71
+ // Build config response with field metadata from Mongoose schemas
72
+ var configModels = modelConfigs.map(function (config) {
73
+ var e_3, _a;
74
+ var _b, _c;
75
+ var _d = (0, api_1.getOpenApiSpecForModel)(config.model), properties = _d.properties, required = _d.required;
76
+ // Extract ref information directly from the Mongoose schema
77
+ var fields = extractFieldMeta(properties, required);
78
+ try {
79
+ for (var _e = __values(Object.entries(fields)), _f = _e.next(); !_f.done; _f = _e.next()) {
80
+ var _g = __read(_f.value, 2), key = _g[0], field = _g[1];
81
+ var schemaPath = config.model.schema.path(key);
82
+ if (schemaPath) {
83
+ var pathOptions = schemaPath.options;
84
+ if (pathOptions === null || pathOptions === void 0 ? void 0 : pathOptions.ref) {
85
+ field.ref = pathOptions.ref;
86
+ }
87
+ // Handle array of refs
88
+ if (Array.isArray(pathOptions === null || pathOptions === void 0 ? void 0 : pathOptions.type) && ((_b = pathOptions.type[0]) === null || _b === void 0 ? void 0 : _b.ref)) {
89
+ field.ref = pathOptions.type[0].ref;
90
+ }
91
+ }
92
+ }
93
+ }
94
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
95
+ finally {
96
+ try {
97
+ if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
98
+ }
99
+ finally { if (e_3) throw e_3.error; }
100
+ }
101
+ return {
102
+ defaultSort: (_c = config.defaultSort) !== null && _c !== void 0 ? _c : "-created",
103
+ displayName: config.displayName,
104
+ fields: fields,
105
+ listFields: config.listFields,
106
+ name: config.model.modelName,
107
+ routePath: "".concat(basePath).concat(config.routePath),
108
+ };
109
+ });
110
+ var configResponse = { models: configModels };
111
+ // GET /admin/config
112
+ app.get("".concat(basePath, "/config"), function (_req, res) {
113
+ return res.json(configResponse);
114
+ });
115
+ try {
116
+ // Mount modelRouter for each model with IsAdmin permissions
117
+ for (var modelConfigs_1 = __values(modelConfigs), modelConfigs_1_1 = modelConfigs_1.next(); !modelConfigs_1_1.done; modelConfigs_1_1 = modelConfigs_1.next()) {
118
+ var config = modelConfigs_1_1.value;
119
+ var routerOptions = {
120
+ permissions: {
121
+ create: [api_1.Permissions.IsAdmin],
122
+ delete: [api_1.Permissions.IsAdmin],
123
+ list: [api_1.Permissions.IsAdmin],
124
+ read: [api_1.Permissions.IsAdmin],
125
+ update: [api_1.Permissions.IsAdmin],
126
+ },
127
+ sort: (_c = config.defaultSort) !== null && _c !== void 0 ? _c : "-created",
128
+ };
129
+ app.use("".concat(basePath).concat(config.routePath), (0, api_1.modelRouter)(config.model, routerOptions));
130
+ }
131
+ }
132
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
133
+ finally {
134
+ try {
135
+ if (modelConfigs_1_1 && !modelConfigs_1_1.done && (_a = modelConfigs_1.return)) _a.call(modelConfigs_1);
136
+ }
137
+ finally { if (e_2) throw e_2.error; }
138
+ }
139
+ };
140
+ return AdminApp;
141
+ }());
142
+ exports.AdminApp = AdminApp;
@@ -0,0 +1,2 @@
1
+ export type { AdminModelConfig, AdminOptions } from "./adminApp";
2
+ export { AdminApp } from "./adminApp";
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AdminApp = void 0;
4
+ var adminApp_1 = require("./adminApp");
5
+ Object.defineProperty(exports, "AdminApp", { enumerable: true, get: function () { return adminApp_1.AdminApp; } });
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "author": "Josh Gachnang",
3
+ "bugs": {
4
+ "url": "https://github.com/FlourishHealth/terreno/issues"
5
+ },
6
+ "dependencies": {
7
+ "@terreno/api": "0.3.1",
8
+ "express": "^4.21.2",
9
+ "mongoose": "^8.15.1"
10
+ },
11
+ "devDependencies": {
12
+ "@types/express": "^4.17.21",
13
+ "typescript": "~5.9.2"
14
+ },
15
+ "description": "Admin panel backend plugin for @terreno/api",
16
+ "homepage": "https://github.com/FlourishHealth/terreno#readme",
17
+ "keywords": ["admin", "api", "express", "terreno"],
18
+ "license": "Apache-2.0",
19
+ "main": "dist/index.js",
20
+ "name": "@terreno/admin-backend",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+ssh://git@github.com/FlourishHealth/terreno.git"
24
+ },
25
+ "scripts": {
26
+ "compile": "tsc"
27
+ },
28
+ "types": "dist/index.d.ts",
29
+ "version": "0.3.2"
30
+ }
@@ -0,0 +1,132 @@
1
+ import {
2
+ getOpenApiSpecForModel,
3
+ type ModelRouterOptions,
4
+ modelRouter,
5
+ Permissions,
6
+ } from "@terreno/api";
7
+ import type express from "express";
8
+ import type {Model} from "mongoose";
9
+
10
+ export interface AdminModelConfig {
11
+ model: Model<any>;
12
+ routePath: string;
13
+ displayName: string;
14
+ listFields: string[];
15
+ defaultSort?: string;
16
+ }
17
+
18
+ export interface AdminOptions {
19
+ models: AdminModelConfig[];
20
+ basePath?: string;
21
+ }
22
+
23
+ interface AdminFieldMeta {
24
+ type: string;
25
+ required: boolean;
26
+ description?: string;
27
+ enum?: string[];
28
+ default?: any;
29
+ ref?: string;
30
+ }
31
+
32
+ interface AdminModelMeta {
33
+ name: string;
34
+ routePath: string;
35
+ displayName: string;
36
+ listFields: string[];
37
+ defaultSort: string;
38
+ fields: Record<string, AdminFieldMeta>;
39
+ }
40
+
41
+ interface AdminConfigResponse {
42
+ models: AdminModelMeta[];
43
+ }
44
+
45
+ const extractFieldMeta = (
46
+ properties: Record<string, any>,
47
+ required: string[]
48
+ ): Record<string, AdminFieldMeta> => {
49
+ const fields: Record<string, AdminFieldMeta> = {};
50
+ for (const [key, prop] of Object.entries(properties)) {
51
+ fields[key] = {
52
+ default: prop.default,
53
+ description: prop.description,
54
+ enum: prop.enum,
55
+ ref: prop.$ref ? prop.$ref.replace("#/components/schemas/", "") : undefined,
56
+ required: required.includes(key),
57
+ type: prop.type ?? "string",
58
+ };
59
+
60
+ // Check for ObjectId references in the raw property
61
+ if (!fields[key].ref && prop.type === "string" && prop.format === "objectid") {
62
+ // mongoose-to-swagger may not preserve ref directly; we'll handle this in register()
63
+ }
64
+ }
65
+ return fields;
66
+ };
67
+
68
+ export class AdminApp {
69
+ private options: AdminOptions;
70
+
71
+ constructor(options: AdminOptions) {
72
+ this.options = options;
73
+ }
74
+
75
+ register(app: express.Application): void {
76
+ const basePath = this.options.basePath ?? "/admin";
77
+ const modelConfigs = this.options.models;
78
+
79
+ // Build config response with field metadata from Mongoose schemas
80
+ const configModels: AdminModelMeta[] = modelConfigs.map((config) => {
81
+ const {properties, required} = getOpenApiSpecForModel(config.model);
82
+
83
+ // Extract ref information directly from the Mongoose schema
84
+ const fields = extractFieldMeta(properties, required);
85
+ for (const [key, field] of Object.entries(fields)) {
86
+ const schemaPath = config.model.schema.path(key);
87
+ if (schemaPath) {
88
+ const pathOptions = schemaPath.options;
89
+ if (pathOptions?.ref) {
90
+ field.ref = pathOptions.ref;
91
+ }
92
+ // Handle array of refs
93
+ if (Array.isArray(pathOptions?.type) && pathOptions.type[0]?.ref) {
94
+ field.ref = pathOptions.type[0].ref;
95
+ }
96
+ }
97
+ }
98
+
99
+ return {
100
+ defaultSort: config.defaultSort ?? "-created",
101
+ displayName: config.displayName,
102
+ fields,
103
+ listFields: config.listFields,
104
+ name: config.model.modelName,
105
+ routePath: `${basePath}${config.routePath}`,
106
+ };
107
+ });
108
+
109
+ const configResponse: AdminConfigResponse = {models: configModels};
110
+
111
+ // GET /admin/config
112
+ app.get(`${basePath}/config`, (_req, res) => {
113
+ return res.json(configResponse);
114
+ });
115
+
116
+ // Mount modelRouter for each model with IsAdmin permissions
117
+ for (const config of modelConfigs) {
118
+ const routerOptions: ModelRouterOptions<any> = {
119
+ permissions: {
120
+ create: [Permissions.IsAdmin],
121
+ delete: [Permissions.IsAdmin],
122
+ list: [Permissions.IsAdmin],
123
+ read: [Permissions.IsAdmin],
124
+ update: [Permissions.IsAdmin],
125
+ },
126
+ sort: config.defaultSort ?? "-created",
127
+ };
128
+
129
+ app.use(`${basePath}${config.routePath}`, modelRouter(config.model, routerOptions));
130
+ }
131
+ }
132
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export type {AdminModelConfig, AdminOptions} from "./adminApp";
2
+ export {AdminApp} from "./adminApp";
package/tsconfig.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "compilerOptions": {
3
+ "allowJs": true,
4
+ "allowSyntheticDefaultImports": true,
5
+ "baseUrl": "./",
6
+ "declaration": true,
7
+ "downlevelIteration": true,
8
+ "esModuleInterop": true,
9
+ "forceConsistentCasingInFileNames": true,
10
+ "jsx": "react",
11
+ "lib": ["es2017", "dom", "es2017.object"],
12
+ "module": "commonjs",
13
+ "moduleResolution": "node",
14
+ "noEmit": false,
15
+ "noImplicitAny": false,
16
+ "noImplicitReturns": true,
17
+ "noImplicitThis": true,
18
+ "noUnusedLocals": false,
19
+ "noUnusedParameters": false,
20
+ "outDir": "./dist",
21
+ "preserveConstEnums": true,
22
+ "removeComments": false,
23
+ "skipLibCheck": true,
24
+ "strict": true,
25
+ "strictFunctionTypes": true,
26
+ "strictNullChecks": true,
27
+ "target": "es5"
28
+ },
29
+ "exclude": ["node_modules"],
30
+ "include": ["src/**/*.ts*", "src/*.ts"]
31
+ }