mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 04:46:05 +00:00
public commit
This commit is contained in:
83
app/src/ui/client/utils/AppReduced.ts
Normal file
83
app/src/ui/client/utils/AppReduced.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import type { App } from "App";
|
||||
import type { Entity, EntityRelation } from "data";
|
||||
import { AppData } from "data/AppData";
|
||||
import { RelationAccessor } from "data/relations/RelationAccessor";
|
||||
import { Flow, TaskMap } from "flows";
|
||||
|
||||
export type AppType = ReturnType<App["toJSON"]>;
|
||||
|
||||
/**
|
||||
* Reduced version of the App class for frontend use
|
||||
*/
|
||||
export class AppReduced {
|
||||
// @todo: change to record
|
||||
private _entities: Entity[] = [];
|
||||
private _relations: EntityRelation[] = [];
|
||||
private _flows: Flow[] = [];
|
||||
|
||||
constructor(protected appJson: AppType) {
|
||||
console.log("received appjson", appJson);
|
||||
|
||||
this._entities = Object.entries(this.appJson.data.entities ?? {}).map(([name, entity]) => {
|
||||
return AppData.constructEntity(name, entity);
|
||||
});
|
||||
|
||||
this._relations = Object.entries(this.appJson.data.relations ?? {}).map(([, relation]) => {
|
||||
return AppData.constructRelation(relation, this.entity.bind(this));
|
||||
});
|
||||
|
||||
for (const [name, obj] of Object.entries(this.appJson.flows.flows ?? {})) {
|
||||
// @ts-ignore
|
||||
// @todo: fix constructing flow
|
||||
const flow = Flow.fromObject(name, obj, TaskMap);
|
||||
|
||||
this._flows.push(flow);
|
||||
}
|
||||
}
|
||||
|
||||
get entities(): Entity[] {
|
||||
return this._entities;
|
||||
}
|
||||
|
||||
// @todo: change to record
|
||||
entity(_entity: Entity | string): Entity {
|
||||
const name = typeof _entity === "string" ? _entity : _entity.name;
|
||||
const entity = this._entities.find((entity) => entity.name === name);
|
||||
if (!entity) {
|
||||
throw new Error(`Entity "${name}" not found`);
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
get relations(): RelationAccessor {
|
||||
return new RelationAccessor(this._relations);
|
||||
}
|
||||
|
||||
get flows(): Flow[] {
|
||||
return this._flows;
|
||||
}
|
||||
|
||||
get config() {
|
||||
return this.appJson;
|
||||
}
|
||||
|
||||
getAdminConfig() {
|
||||
return this.appJson.server.admin;
|
||||
}
|
||||
|
||||
getSettingsPath(path: string[] = []): string {
|
||||
const { basepath } = this.getAdminConfig();
|
||||
const base = `~/${basepath}/settings`.replace(/\/+/g, "/");
|
||||
return [base, ...path].join("/");
|
||||
}
|
||||
|
||||
getAbsolutePath(path?: string): string {
|
||||
const { basepath } = this.getAdminConfig();
|
||||
return (path ? `~/${basepath}/${path}` : `~/${basepath}`).replace(/\/+/g, "/");
|
||||
}
|
||||
|
||||
getAuthConfig() {
|
||||
return this.appJson.auth;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user