Merge remote-tracking branch 'origin/release/0.10' into feat/remove-admin-config

# Conflicts:
#	app/src/modules/server/AdminController.tsx
#	app/src/ui/Admin.tsx
This commit is contained in:
dswbx
2025-03-11 13:56:27 +01:00
498 changed files with 14118 additions and 5427 deletions

View File

@@ -1,7 +1,21 @@
import { Hono } from "hono";
import type { ServerEnv } from "modules/Module";
import type { App } from "App";
import { type Context, Hono } from "hono";
import * as middlewares from "modules/middlewares";
export type ServerEnv = {
Variables: {
app: App;
// to prevent resolving auth multiple times
auth?: {
resolved: boolean;
registered: boolean;
skip: boolean;
user?: { id: any; role?: string; [key: string]: any };
};
html?: string;
};
};
export class Controller {
protected middlewares = middlewares;
@@ -16,4 +30,19 @@ export class Controller {
getController(): Hono<ServerEnv> {
return this.create();
}
protected isJsonRequest(c: Context<ServerEnv>) {
return (
c.req.header("Content-Type") === "application/json" ||
c.req.header("Accept") === "application/json"
);
}
protected notFound(c: Context<ServerEnv>) {
if (this.isJsonRequest(c)) {
return c.json({ error: "Not found" }, 404);
}
return c.notFound();
}
}

View File

@@ -1,4 +1,3 @@
import type { App } from "App";
import type { Guard } from "auth";
import { type DebugLogger, SchemaObject } from "core";
import type { EventManager } from "core/events";
@@ -10,25 +9,12 @@ import {
type Field,
FieldPrototype,
make,
type em as prototypeEm
type em as prototypeEm,
} from "data";
import { Entity } from "data";
import type { Hono } from "hono";
import { isEqual } from "lodash-es";
export type ServerEnv = {
Variables: {
app: App;
// to prevent resolving auth multiple times
auth?: {
resolved: boolean;
registered: boolean;
skip: boolean;
user?: { id: any; role?: string; [key: string]: any };
};
html?: string;
};
};
import type { ServerEnv } from "modules/Controller";
export type ModuleBuildContext = {
connection: Connection;
@@ -47,7 +33,7 @@ export abstract class Module<Schema extends TSchema = TSchema, ConfigSchema = St
constructor(
initial?: Partial<Static<Schema>>,
protected _ctx?: ModuleBuildContext
protected _ctx?: ModuleBuildContext,
) {
this._schema = new SchemaObject(this.getSchema(), initial, {
forceParse: this.useForceParse(),
@@ -56,13 +42,13 @@ export abstract class Module<Schema extends TSchema = TSchema, ConfigSchema = St
},
restrictPaths: this.getRestrictedPaths(),
overwritePaths: this.getOverwritePaths(),
onBeforeUpdate: this.onBeforeUpdate.bind(this)
onBeforeUpdate: this.onBeforeUpdate.bind(this),
});
}
static ctx_flags = {
sync_required: false,
ctx_reload_required: false
ctx_reload_required: false,
} as {
// signal that a sync is required at the end of build
sync_required: boolean;
@@ -138,7 +124,7 @@ export abstract class Module<Schema extends TSchema = TSchema, ConfigSchema = St
forceParse: this.useForceParse(),
restrictPaths: this.getRestrictedPaths(),
overwritePaths: this.getOverwritePaths(),
onBeforeUpdate: this.onBeforeUpdate.bind(this)
onBeforeUpdate: this.onBeforeUpdate.bind(this),
});
}
@@ -183,7 +169,7 @@ export abstract class Module<Schema extends TSchema = TSchema, ConfigSchema = St
// replace entity (mainly to keep the ensured type)
this.ctx.em.__replaceEntity(
new Entity(instance.name, instance.fields, instance.config, entity.type)
new Entity(instance.name, instance.fields, instance.config, entity.type),
);
}
@@ -204,7 +190,7 @@ export abstract class Module<Schema extends TSchema = TSchema, ConfigSchema = St
protected setEntityFieldConfigs(
parent: Field,
child: Field,
props: string[] = ["hidden", "fillable", "required"]
props: string[] = ["hidden", "fillable", "required"],
) {
let changes = 0;
for (const prop of props) {
@@ -219,7 +205,7 @@ export abstract class Module<Schema extends TSchema = TSchema, ConfigSchema = St
protected replaceEntityField(
_entity: string | Entity,
field: Field | string,
_newField: Field | FieldPrototype
_newField: Field | FieldPrototype,
) {
const entity = this.ctx.em.entity(_entity);
const name = typeof field === "string" ? field : field.name;

View File

@@ -28,7 +28,7 @@ export abstract class ModuleApi<Options extends BaseModuleApiOptions = BaseModul
constructor(
protected readonly _options: Partial<Options> = {},
fetcher?: typeof fetch
fetcher?: typeof fetch,
) {
this.fetcher = fetcher ?? fetch;
}
@@ -42,7 +42,7 @@ export abstract class ModuleApi<Options extends BaseModuleApiOptions = BaseModul
host: "http://localhost",
token: undefined,
...this.getDefaultOptions(),
...this._options
...this._options,
} as Options;
}
@@ -61,7 +61,7 @@ export abstract class ModuleApi<Options extends BaseModuleApiOptions = BaseModul
protected request<Data = any>(
_input: TInput,
_query?: Record<string, any> | URLSearchParams,
_init?: RequestInit
_init?: RequestInit,
): FetchPromise<ResponseObject<Data>> {
const method = _init?.method ?? "GET";
const input = Array.isArray(_input) ? _input.join("/") : _input;
@@ -104,23 +104,23 @@ export abstract class ModuleApi<Options extends BaseModuleApiOptions = BaseModul
..._init,
method,
body,
headers
headers,
});
return new FetchPromise(request, {
fetcher: this.fetcher,
verbose: this.options.verbose
verbose: this.options.verbose,
});
}
get<Data = any>(
_input: TInput,
_query?: Record<string, any> | URLSearchParams,
_init?: RequestInit
_init?: RequestInit,
) {
return this.request<Data>(_input, _query, {
..._init,
method: "GET"
method: "GET",
});
}
@@ -128,7 +128,7 @@ export abstract class ModuleApi<Options extends BaseModuleApiOptions = BaseModul
return this.request<Data>(_input, undefined, {
..._init,
body,
method: "POST"
method: "POST",
});
}
@@ -136,7 +136,7 @@ export abstract class ModuleApi<Options extends BaseModuleApiOptions = BaseModul
return this.request<Data>(_input, undefined, {
..._init,
body,
method: "PATCH"
method: "PATCH",
});
}
@@ -144,14 +144,15 @@ export abstract class ModuleApi<Options extends BaseModuleApiOptions = BaseModul
return this.request<Data>(_input, undefined, {
..._init,
body,
method: "PUT"
method: "PUT",
});
}
delete<Data = any>(_input: TInput, _init?: RequestInit) {
delete<Data = any>(_input: TInput, body?: any, _init?: RequestInit) {
return this.request<Data>(_input, undefined, {
..._init,
method: "DELETE"
body,
method: "DELETE",
});
}
}
@@ -169,9 +170,9 @@ export type ResponseObject<Body = any, Data = Body extends { data: infer R } ? R
export function createResponseProxy<Body = any, Data = any>(
raw: Response,
body: Body,
data?: Data
data?: Data,
): ResponseObject<Body, Data> {
let actualData: any = data ?? body;
let actualData: any = typeof data !== "undefined" ? data : body;
const _props = ["raw", "body", "ok", "status", "res", "data", "toJSON"];
// that's okay, since you have to check res.ok anyway
@@ -189,6 +190,7 @@ export function createResponseProxy<Body = any, Data = any>(
if (prop === "toJSON") {
return () => target;
}
return Reflect.get(target, prop, receiver);
},
has(target, prop) {
@@ -205,11 +207,11 @@ export function createResponseProxy<Body = any, Data = any>(
return {
configurable: true,
enumerable: true,
value: Reflect.get({ raw, body, ok: raw.ok, status: raw.status }, prop)
value: Reflect.get({ raw, body, ok: raw.ok, status: raw.status }, prop),
};
}
return Reflect.getOwnPropertyDescriptor(target, prop);
}
},
}) as ResponseObject<Body, Data>;
}
@@ -222,13 +224,21 @@ export class FetchPromise<T = ApiResponse<any>> implements Promise<T> {
protected options?: {
fetcher?: typeof fetch;
verbose?: boolean;
}
},
// keep "any" here, it gets inferred correctly with the "refine" fn
protected refineData?: (data: any) => any,
) {}
get verbose() {
return this.options?.verbose ?? false;
}
refine<N>(fn: (data: T) => N) {
return new FetchPromise(this.request, this.options, fn) as unknown as FetchPromise<
ApiResponse<N>
>;
}
async execute(): Promise<ResponseObject<T>> {
// delay in dev environment
isDebug() && (await new Promise((resolve) => setTimeout(resolve, 200)));
@@ -237,7 +247,7 @@ export class FetchPromise<T = ApiResponse<any>> implements Promise<T> {
if (this.verbose) {
console.log("[FetchPromise] Request", {
method: this.request.method,
url: this.request.url
url: this.request.url,
});
}
@@ -246,7 +256,7 @@ export class FetchPromise<T = ApiResponse<any>> implements Promise<T> {
console.log("[FetchPromise] Response", {
res: res,
ok: res.ok,
status: res.status
status: res.status,
});
}
@@ -264,7 +274,15 @@ export class FetchPromise<T = ApiResponse<any>> implements Promise<T> {
} else {
resBody = res.body;
}
console.groupEnd();
if (this.refineData) {
try {
resData = this.refineData(resData);
} catch (e) {
console.warn("[FetchPromise] Error in refineData", e);
resData = undefined;
}
}
return createResponseProxy<T>(res, resBody, resData);
}
@@ -272,13 +290,13 @@ export class FetchPromise<T = ApiResponse<any>> implements Promise<T> {
// biome-ignore lint/suspicious/noThenProperty: it's a promise :)
then<TResult1 = T, TResult2 = never>(
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined,
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined,
): Promise<TResult1 | TResult2> {
return this.execute().then(onfulfilled as any, onrejected);
}
catch<TResult = never>(
onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined
onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined,
): Promise<T | TResult> {
return this.then(undefined, onrejected);
}
@@ -292,7 +310,7 @@ export class FetchPromise<T = ApiResponse<any>> implements Promise<T> {
(reason) => {
onfinally?.();
throw reason;
}
},
);
}
@@ -310,7 +328,7 @@ export class FetchPromise<T = ApiResponse<any>> implements Promise<T> {
const url = new URL(this.request.url);
const path = this.path().split("/");
return (options?.search !== false ? [...path, url.searchParams.toString()] : path).filter(
Boolean
Boolean,
);
}
}

View File

@@ -1,5 +1,5 @@
import { Guard } from "auth";
import { BkndError, DebugLogger, withDisabledConsole } from "core";
import { $console, BkndError, DebugLogger, env } from "core";
import { EventManager } from "core/events";
import { clone, diff } from "core/object/diff";
import {
@@ -10,7 +10,7 @@ import {
mark,
objectEach,
stripMark,
transformObject
transformObject,
} from "core/utils";
import {
type Connection,
@@ -20,7 +20,7 @@ import {
entity,
enumm,
jsonSchema,
number
number,
} from "data";
import { TransformPersistFailedException } from "data/errors";
import { Hono } from "hono";
@@ -32,7 +32,8 @@ import { AppAuth } from "../auth/AppAuth";
import { AppData } from "../data/AppData";
import { AppFlows } from "../flows/AppFlows";
import { AppMedia } from "../media/AppMedia";
import { Module, type ModuleBuildContext, type ServerEnv } from "./Module";
import type { ServerEnv } from "./Controller";
import { Module, type ModuleBuildContext } from "./Module";
export type { ModuleBuildContext };
@@ -41,7 +42,7 @@ export const MODULES = {
data: AppData,
auth: AppAuth,
media: AppMedia,
flows: AppFlows
flows: AppFlows,
} as const;
// get names of MODULES as an array
@@ -70,7 +71,7 @@ export type InitialModuleConfigs =
enum Verbosity {
silent = 0,
error = 1,
log = 2
log = 2,
}
export type ModuleManagerOptions = {
@@ -78,7 +79,7 @@ export type ModuleManagerOptions = {
eventManager?: EventManager<any>;
onUpdated?: <Module extends keyof Modules>(
module: Module,
config: ModuleConfigs[Module]
config: ModuleConfigs[Module],
) => Promise<void>;
// triggered when no config table existed
onFirstBoot?: () => Promise<void>;
@@ -90,7 +91,7 @@ export type ModuleManagerOptions = {
trustFetched?: boolean;
// runs when initial config provided on a fresh database
seed?: (ctx: ModuleBuildContext) => Promise<void>;
// wether
/** @deprecated */
verbosity?: Verbosity;
};
@@ -110,22 +111,24 @@ const configJsonSchema = Type.Union([
t: StringEnum(["a", "r", "e"]),
p: Type.Array(Type.Union([Type.String(), Type.Number()])),
o: Type.Optional(Type.Any()),
n: Type.Optional(Type.Any())
})
)
n: Type.Optional(Type.Any()),
}),
),
]);
const __bknd = entity(TABLE_NAME, {
version: number().required(),
type: enumm({ enum: ["config", "diff", "backup"] }).required(),
json: jsonSchema({ schema: configJsonSchema }).required(),
created_at: datetime(),
updated_at: datetime()
updated_at: datetime(),
});
type ConfigTable2 = Schema<typeof __bknd>;
interface T_INTERNAL_EM {
__bknd: ConfigTable2;
}
const debug_modules = env("modules_debug");
// @todo: cleanup old diffs on upgrade
// @todo: cleanup multiple backups on upgrade
export class ModuleManager {
@@ -146,13 +149,12 @@ export class ModuleManager {
constructor(
private readonly connection: Connection,
private options?: Partial<ModuleManagerOptions>
private options?: Partial<ModuleManagerOptions>,
) {
this.__em = new EntityManager([__bknd], this.connection);
this.modules = {} as Modules;
this.emgr = new EventManager();
this.logger = new DebugLogger(this.verbosity === Verbosity.log);
const context = this.ctx(true);
this.logger = new DebugLogger(debug_modules);
let initial = {} as Partial<ModuleConfigs>;
if (options?.initial) {
@@ -168,15 +170,29 @@ export class ModuleManager {
}
}
for (const key in MODULES) {
const moduleConfig = key in initial ? initial[key] : {};
const module = new MODULES[key](moduleConfig, context) as Module;
module.setListener(async (c) => {
await this.onModuleConfigUpdated(key, c);
});
this.createModules(initial);
}
this.modules[key] = module;
private createModules(initial: Partial<ModuleConfigs>) {
this.logger.context("createModules").log("creating modules");
try {
const context = this.ctx(true);
for (const key in MODULES) {
const moduleConfig = key in initial ? initial[key] : {};
const module = new MODULES[key](moduleConfig, context) as Module;
module.setListener(async (c) => {
await this.onModuleConfigUpdated(key, c);
});
this.modules[key] = module;
}
this.logger.log("modules created");
} catch (e) {
this.logger.log("failed to create modules", e);
throw e;
}
this.logger.clear();
}
private get verbosity() {
@@ -196,12 +212,14 @@ export class ModuleManager {
if (this.options?.onUpdated) {
await this.options.onUpdated(key as any, config);
} else {
this.buildModules();
await this.buildModules();
}
}
private repo() {
return this.__em.repo(__bknd);
return this.__em.repo(__bknd, {
silent: !debug_modules,
});
}
private mutator() {
@@ -212,6 +230,7 @@ export class ModuleManager {
return this.connection.kysely as Kysely<{ table: ConfigTable }>;
}
// @todo: add indices for: version, type
async syncConfigTable() {
this.logger.context("sync").log("start");
const result = await this.__em.schema().sync({ force: true });
@@ -248,7 +267,7 @@ export class ModuleManager {
emgr: this.emgr,
guard: this.guard,
flags: Module.ctx_flags,
logger: this.logger
logger: this.logger,
};
}
@@ -257,31 +276,26 @@ export class ModuleManager {
const startTime = performance.now();
// disabling console log, because the table might not exist yet
const result = await withDisabledConsole(
async () => {
const { data: result } = await this.repo().findOne(
{ type: "config" },
{
sort: { by: "version", dir: "desc" }
}
);
if (!result) {
throw BkndError.with("no config");
}
return result as unknown as ConfigTable;
const { data: result } = await this.repo().findOne(
{ type: "config" },
{
sort: { by: "version", dir: "desc" },
},
this.verbosity > Verbosity.silent ? [] : ["log", "error", "warn"]
);
if (!result) {
this.logger.log("error fetching").clear();
throw BkndError.with("no config");
}
this.logger
.log("took", performance.now() - startTime, "ms", {
version: result.version,
id: result.id
id: result.id,
})
.clear();
return result;
return result as unknown as ConfigTable;
}
async save() {
@@ -299,12 +313,12 @@ export class ModuleManager {
await this.mutator().insertOne({
version: state.version,
type: "backup",
json: configs
json: configs,
});
await this.mutator().insertOne({
version: version,
type: "config",
json: configs
json: configs,
});
} else {
this.logger.log("version matches");
@@ -318,7 +332,7 @@ export class ModuleManager {
await this.mutator().insertOne({
version,
type: "diff",
json: clone(diffs)
json: clone(diffs),
});
// store new version
@@ -326,12 +340,12 @@ export class ModuleManager {
{
version,
json: configs,
updated_at: new Date()
updated_at: new Date(),
} as any,
{
type: "config",
version
}
version,
},
);
} else {
this.logger.log("no diff, not saving");
@@ -346,7 +360,7 @@ export class ModuleManager {
version,
json: configs,
created_at: new Date(),
updated_at: new Date()
updated_at: new Date(),
});
} else if (e instanceof TransformPersistFailedException) {
console.error("Cannot save invalid config");
@@ -367,26 +381,37 @@ export class ModuleManager {
}
private async migrate() {
const state = {
success: false,
migrated: false,
version: {
before: this.version(),
after: this.version(),
},
};
this.logger.context("migrate").log("migrating?", this.version(), CURRENT_VERSION);
if (this.version() < CURRENT_VERSION) {
state.version.before = this.version();
this.logger.log("there are migrations, verify version");
// sync __bknd table
await this.syncConfigTable();
// modules must be built before migration
this.logger.log("building modules");
await this.buildModules({ graceful: true });
this.logger.log("modules built");
try {
const state = await this.fetch();
if (state.version !== this.version()) {
// @todo: potentially drop provided config and use database version
throw new Error(
`Given version (${this.version()}) and fetched version (${state.version}) do not match.`
`Given version (${this.version()}) and fetched version (${state.version}) do not match.`,
);
}
} catch (e: any) {
this.logger.clear(); // fetch couldn't clear
throw new Error(`Version is ${this.version()}, fetch failed: ${e.message}`);
}
@@ -399,22 +424,32 @@ export class ModuleManager {
}
const [_version, _configs] = await migrate(version, configs, {
db: this.db
db: this.db,
});
version = _version;
configs = _configs;
this.setConfigs(configs);
this._version = version;
state.version.after = version;
state.migrated = true;
this.ctx().flags.sync_required = true;
this.logger.log("setting configs");
this.createModules(configs);
await this.buildModules();
this.logger.log("migrated to", version);
$console.log("Migrated config from", state.version.before, "to", state.version.after);
await this.save();
} else {
this.logger.log("no migrations needed");
}
state.success = true;
this.logger.clear();
return state;
}
private setConfigs(configs: ModuleConfigs): void {
@@ -426,19 +461,24 @@ export class ModuleManager {
} catch (e) {
console.error(e);
throw new Error(
`Failed to set config for module ${key}: ${JSON.stringify(config, null, 2)}`
`Failed to set config for module ${key}: ${JSON.stringify(config, null, 2)}`,
);
}
});
}
async build() {
async build(opts?: { fetch?: boolean }) {
this.logger.context("build").log("version", this.version());
this.logger.log("booted with", this._booted_with);
// if no config provided, try fetch from db
if (this.version() === 0) {
this.logger.context("no version").log("version is 0");
if (this.version() === 0 || opts?.fetch === true) {
if (this.version() === 0) {
this.logger.context("no version").log("version is 0");
} else {
this.logger.context("force fetch").log("force fetch");
}
try {
const result = await this.fetch();
@@ -479,10 +519,16 @@ export class ModuleManager {
}
// migrate to latest if needed
await this.migrate();
this.logger.log("check migrate");
const migration = await this.migrate();
if (migration.success && migration.migrated) {
this.logger.log("skipping build after migration");
} else {
this.logger.log("trigger build modules");
await this.buildModules();
}
this.logger.log("building");
await this.buildModules();
this.logger.log("done");
return this;
}
@@ -492,10 +538,10 @@ export class ModuleManager {
modules: [] as ModuleKey[],
synced: false,
saved: false,
reloaded: false
reloaded: false,
};
this.logger.log("buildModules() triggered", options, this._built);
this.logger.context("buildModules").log("triggered", options, this._built);
if (options?.graceful && this._built) {
this.logger.log("skipping build (graceful)");
return state;
@@ -535,8 +581,10 @@ export class ModuleManager {
}
// reset all falgs
this.logger.log("resetting flags");
ctx.flags = Module.ctx_flags;
this.logger.clear();
return state;
}
@@ -544,7 +592,7 @@ export class ModuleManager {
const ctx = {
...this.ctx(),
// disable events for initial setup
em: this.ctx().em.fork()
em: this.ctx().em.fork(),
};
// perform a sync
@@ -556,7 +604,7 @@ export class ModuleManager {
}
mutateConfigSafe<Module extends keyof Modules>(
name: Module
name: Module,
): Pick<ReturnType<Modules[Module]["schema"]>, "set" | "patch" | "overwrite" | "remove"> {
const module = this.modules[name];
const copy = structuredClone(this.configs());
@@ -601,7 +649,7 @@ export class ModuleManager {
throw e;
}
};
}
},
});
}
@@ -629,7 +677,7 @@ export class ModuleManager {
return {
version: this.version(),
...schemas
...schemas,
};
}
@@ -645,7 +693,7 @@ export class ModuleManager {
return {
version: this.version(),
...modules
...modules,
} as any;
}
}
@@ -653,7 +701,7 @@ export class ModuleManager {
export function getDefaultSchema() {
const schema = {
type: "object",
...transformObject(MODULES, (module) => module.prototype.getSchema())
...transformObject(MODULES, (module) => module.prototype.getSchema()),
};
return schema as any;

View File

@@ -12,7 +12,7 @@ export type ApiSchemaResponse = {
export class SystemApi extends ModuleApi<any> {
protected override getDefaultOptions(): Partial<any> {
return {
basepath: "/api/system"
basepath: "/api/system",
};
}
@@ -20,21 +20,22 @@ export class SystemApi extends ModuleApi<any> {
return this.get<{ version: number } & ModuleConfigs>("config");
}
readSchema(options?: { config?: boolean; secrets?: boolean }) {
readSchema(options?: { config?: boolean; secrets?: boolean; fresh?: boolean }) {
return this.get<ApiSchemaResponse>("schema", {
config: options?.config ? 1 : 0,
secrets: options?.secrets ? 1 : 0
secrets: options?.secrets ? 1 : 0,
fresh: options?.fresh ? 1 : 0,
});
}
setConfig<Module extends ModuleKey>(
module: Module,
value: ModuleConfigs[Module],
force?: boolean
force?: boolean,
) {
return this.post<ConfigUpdateResponse>(
["config", "set", module].join("/") + `?force=${force ? 1 : 0}`,
value
value,
);
}

View File

@@ -9,7 +9,7 @@ export {
type ModuleConfigs,
type ModuleSchemas,
MODULE_NAMES,
type ModuleKey
type ModuleKey,
} from "./ModuleManager";
export type { ModuleBuildContext } from "./Module";
@@ -17,5 +17,5 @@ export {
type PrimaryFieldType,
type BaseModuleApiOptions,
type ApiResponse,
ModuleApi
ModuleApi,
} from "./ModuleApi";

View File

@@ -0,0 +1 @@
export { auth, permission } from "auth/middlewares";

View File

@@ -1,4 +1,4 @@
import { _jsonp } from "core/utils";
import { _jsonp, transformObject } from "core/utils";
import { type Kysely, sql } from "kysely";
import { set } from "lodash-es";
@@ -17,18 +17,18 @@ export const migrations: Migration[] = [
{
version: 1,
//schema: true,
up: async (config) => config
up: async (config) => config,
},
{
version: 2,
up: async (config, { db }) => {
return config;
}
},
},
{
version: 3,
//schema: true,
up: async (config) => config
up: async (config) => config,
},
{
version: 4,
@@ -37,10 +37,10 @@ export const migrations: Migration[] = [
...config,
auth: {
...config.auth,
basepath: "/api/auth2"
}
basepath: "/api/auth2",
},
};
}
},
},
{
version: 5,
@@ -49,13 +49,13 @@ export const migrations: Migration[] = [
const cors = config.server.cors?.allow_methods ?? [];
set(config.server, "cors.allow_methods", [...new Set([...cors, "PATCH"])]);
return config;
}
},
},
{
version: 6,
up: async (config, { db }) => {
return config;
}
},
},
{
version: 7,
@@ -67,18 +67,30 @@ export const migrations: Migration[] = [
...config,
auth: {
...config.auth,
jwt
}
jwt,
},
};
}
}
/*{
},
},
{
version: 8,
up: async (config, { db }) => {
await db.deleteFrom(TABLE_NAME).where("type", "=", "diff").execute();
return config;
}
}*/
up: async (config) => {
const strategies = transformObject(config.auth.strategies, (strategy) => {
return {
...strategy,
enabled: true,
};
});
return {
...config,
auth: {
...config.auth,
strategies: strategies,
},
};
},
},
];
export const CURRENT_VERSION = migrations[migrations.length - 1]?.version ?? 0;
@@ -88,7 +100,7 @@ export async function migrateTo(
current: number,
to: number,
config: GenericConfigObject,
ctx: MigrationContext
ctx: MigrationContext,
): Promise<[number, GenericConfigObject]> {
//console.log("migrating from", current, "to", CURRENT_VERSION, config);
const todo = migrations.filter((m) => m.version > current && m.version <= to);
@@ -115,7 +127,7 @@ export async function migrateTo(
export async function migrate(
current: number,
config: GenericConfigObject,
ctx: MigrationContext
ctx: MigrationContext,
): Promise<[number, GenericConfigObject]> {
return migrateTo(current, CURRENT_VERSION, config, ctx);
}

View File

@@ -1,7 +1,7 @@
import { MediaAdapterRegistry } from "media";
const registries = {
media: MediaAdapterRegistry
media: MediaAdapterRegistry,
} as const;
export { registries };

View File

@@ -1,4 +1,4 @@
import { Exception } from "core";
import { Exception, isDebug } from "core";
import { type Static, StringEnum, Type } from "core/utils";
import { cors } from "hono/cors";
import { Module } from "modules/Module";
@@ -16,29 +16,29 @@ export const serverConfigSchema = Type.Object(
logo_return_path: Type.Optional(
Type.String({
default: "/",
description: "Path to return to after *clicking* the logo"
})
)
description: "Path to return to after *clicking* the logo",
}),
),
},
{ default: {}, additionalProperties: false }
{ default: {}, additionalProperties: false },
),
cors: Type.Object(
{
origin: Type.String({ default: "*" }),
allow_methods: Type.Array(StringEnum(serverMethods), {
default: serverMethods,
uniqueItems: true
uniqueItems: true,
}),
allow_headers: Type.Array(Type.String(), {
default: ["Content-Type", "Content-Length", "Authorization", "Accept"]
})
default: ["Content-Type", "Content-Length", "Authorization", "Accept"],
}),
},
{ default: {}, additionalProperties: false }
)
{ default: {}, additionalProperties: false },
),
},
{
additionalProperties: false
}
additionalProperties: false,
},
);
export type AppServerConfig = Static<typeof serverConfigSchema>;
@@ -70,8 +70,8 @@ export class AppServer extends Module<typeof serverConfigSchema> {
cors({
origin: this.config.cors.origin,
allowMethods: this.config.cors.allow_methods,
allowHeaders: this.config.cors.allow_headers
})
allowHeaders: this.config.cors.allow_headers,
}),
);
// add an initial fallback route
@@ -83,7 +83,7 @@ export class AppServer extends Module<typeof serverConfigSchema> {
if (new URL(c.req.url).pathname === "/") {
c.res = undefined;
c.res = Response.json({
bknd: "hello world!"
bknd: "hello world!",
});
}
}
@@ -102,6 +102,12 @@ export class AppServer extends Module<typeof serverConfigSchema> {
return c.json(err.toJSON(), err.code as any);
}
if (err instanceof Error) {
if (isDebug()) {
return c.json({ error: err.message, stack: err.stack }, 500);
}
}
return c.json({ error: err.message }, 500);
});
this.setBuilt();

View File

@@ -1,8 +1,16 @@
/// <reference types="@cloudflare/workers-types" />
import type { App } from "App";
import { tbValidator as tb } from "core";
import { StringEnum, Type, TypeInvalidError } from "core/utils";
import { $console, tbValidator as tb } from "core";
import {
StringEnum,
Type,
TypeInvalidError,
datetimeStringLocal,
datetimeStringUTC,
getTimezone,
getTimezoneOffset,
} from "core/utils";
import { getRuntimeKey } from "core/utils";
import type { Context, Hono } from "hono";
import { Controller } from "modules/Controller";
@@ -11,7 +19,7 @@ import {
MODULE_NAMES,
type ModuleConfigs,
type ModuleKey,
getDefaultConfig
getDefaultConfig,
} from "modules/ModuleManager";
import * as SystemPermissions from "modules/permissions";
import { generateOpenAPI } from "modules/server/openapi";
@@ -56,8 +64,8 @@ export class SystemController extends Controller {
tb(
"query",
Type.Object({
secrets: Type.Optional(booleanLike)
})
secrets: Type.Optional(booleanLike),
}),
),
async (c) => {
// @todo: allow secrets if authenticated user is admin
@@ -73,11 +81,11 @@ export class SystemController extends Controller {
? {
version: this.app.version(),
module,
config: config[module]
config: config[module],
}
: config
: config,
);
}
},
);
async function handleConfigUpdateResponse(c: Context<any>, cb: () => Promise<ConfigUpdate>) {
@@ -89,7 +97,7 @@ export class SystemController extends Controller {
if (e instanceof TypeInvalidError) {
return c.json(
{ success: false, type: "type-invalid", errors: e.errors },
{ status: 400 }
{ status: 400 },
);
}
if (e instanceof Error) {
@@ -106,8 +114,8 @@ export class SystemController extends Controller {
tb(
"query",
Type.Object({
force: Type.Optional(booleanLike)
})
force: Type.Optional(booleanLike),
}),
),
async (c) => {
const module = c.req.param("module") as any;
@@ -121,7 +129,7 @@ export class SystemController extends Controller {
// force overwrite defined keys
const newConfig = {
...this.app.module[module].config,
...value
...value,
};
await this.app.mutateConfig(module).set(newConfig);
} else {
@@ -130,10 +138,10 @@ export class SystemController extends Controller {
return {
success: true,
module,
config: this.app.module[module].config
config: this.app.module[module].config,
};
});
}
},
);
hono.post("/add/:module/:path", permission(SystemPermissions.configWrite), async (c) => {
@@ -152,7 +160,7 @@ export class SystemController extends Controller {
return {
success: true,
module,
config: this.app.module[module].config
config: this.app.module[module].config,
};
});
});
@@ -168,7 +176,7 @@ export class SystemController extends Controller {
return {
success: true,
module,
config: this.app.module[module].config
config: this.app.module[module].config,
};
});
});
@@ -184,7 +192,7 @@ export class SystemController extends Controller {
return {
success: true,
module,
config: this.app.module[module].config
config: this.app.module[module].config,
};
});
});
@@ -199,7 +207,7 @@ export class SystemController extends Controller {
return {
success: true,
module,
config: this.app.module[module].config
config: this.app.module[module].config,
};
});
});
@@ -220,24 +228,30 @@ export class SystemController extends Controller {
"query",
Type.Object({
config: Type.Optional(booleanLike),
secrets: Type.Optional(booleanLike)
})
secrets: Type.Optional(booleanLike),
fresh: Type.Optional(booleanLike),
}),
),
async (c) => {
const module = c.req.param("module") as ModuleKey | undefined;
const { config, secrets } = c.req.valid("query");
const { config, secrets, fresh } = c.req.valid("query");
config && this.ctx.guard.throwUnlessGranted(SystemPermissions.configRead, c);
secrets && this.ctx.guard.throwUnlessGranted(SystemPermissions.configReadSecrets, c);
const { version, ...schema } = this.app.getSchema();
if (fresh) {
// in cases of concurrency, refetching schema/config must be always fresh
await this.app.build({ fetch: true });
}
if (module) {
return c.json({
module,
version,
schema: schema[module],
config: config ? this.app.module[module].toJSON(secrets) : undefined
config: config ? this.app.module[module].toJSON(secrets) : undefined,
});
}
@@ -246,9 +260,9 @@ export class SystemController extends Controller {
version,
schema,
config: config ? this.app.toJSON(secrets) : undefined,
permissions: this.app.modules.ctx().guard.getPermissionNames()
permissions: this.app.modules.ctx().guard.getPermissionNames(),
});
}
},
);
hono.post(
@@ -256,16 +270,20 @@ export class SystemController extends Controller {
tb(
"query",
Type.Object({
sync: Type.Optional(booleanLike)
})
sync: Type.Optional(booleanLike),
fetch: Type.Optional(booleanLike),
}),
),
async (c) => {
const { sync } = c.req.valid("query") as Record<string, boolean>;
const options = c.req.valid("query") as Record<string, boolean>;
this.ctx.guard.throwUnlessGranted(SystemPermissions.build, c);
await this.app.build({ sync });
return c.json({ success: true, options: { sync } });
}
await this.app.build(options);
return c.json({
success: true,
options,
});
},
);
hono.get("/ping", (c) => c.json({ pong: true }));
@@ -273,8 +291,14 @@ export class SystemController extends Controller {
hono.get("/info", (c) =>
c.json({
version: c.get("app")?.version(),
runtime: getRuntimeKey()
})
runtime: getRuntimeKey(),
timezone: {
name: getTimezone(),
offset: getTimezoneOffset(),
local: datetimeStringLocal(),
utc: datetimeStringUTC(),
},
}),
);
hono.get("/openapi.json", async (c) => {

View File

@@ -22,14 +22,14 @@ function systemRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
content: {
"application/json": {
schema: Type.Object({
pong: Type.Boolean({ default: true })
})
}
}
}
pong: Type.Boolean({ default: true }),
}),
},
},
},
},
tags
}
tags,
},
},
"/config": {
get: {
@@ -45,14 +45,14 @@ function systemRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
data: Type.Object({}),
auth: Type.Object({}),
flows: Type.Object({}),
media: Type.Object({})
})
}
}
}
media: Type.Object({}),
}),
},
},
},
},
tags
}
tags,
},
},
"/schema": {
get: {
@@ -69,16 +69,16 @@ function systemRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
data: Type.Object({}),
auth: Type.Object({}),
flows: Type.Object({}),
media: Type.Object({})
})
})
}
}
}
media: Type.Object({}),
}),
}),
},
},
},
},
tags
}
}
tags,
},
},
};
return { paths: prefixPaths(paths, "/api/system") };
@@ -87,42 +87,42 @@ function systemRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
function dataRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
const schemas = {
entityData: Type.Object({
id: Type.Number() as any
})
id: Type.Number() as any,
}),
};
const repoManyResponses: OAS.ResponsesObject = {
"200": {
description: "List of entities",
content: {
"application/json": {
schema: Type.Array(schemas.entityData)
}
}
}
schema: Type.Array(schemas.entityData),
},
},
},
};
const repoSingleResponses: OAS.ResponsesObject = {
"200": {
description: "Entity",
content: {
"application/json": {
schema: schemas.entityData
}
}
}
schema: schemas.entityData,
},
},
},
};
const params = {
entity: {
name: "entity",
in: "path",
required: true,
schema: Type.String()
schema: Type.String(),
},
entityId: {
name: "id",
in: "path",
required: true,
schema: Type.Number() as any
}
schema: Type.Number() as any,
},
};
const tags = ["data"];
@@ -132,7 +132,7 @@ function dataRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
summary: "List entities",
parameters: [params.entity],
responses: repoManyResponses,
tags
tags,
},
post: {
summary: "Create entity",
@@ -140,20 +140,20 @@ function dataRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
requestBody: {
content: {
"application/json": {
schema: Type.Object({})
}
}
schema: Type.Object({}),
},
},
},
responses: repoSingleResponses,
tags
}
tags,
},
},
"/entity/{entity}/{id}": {
get: {
summary: "Get entity",
parameters: [params.entity, params.entityId],
responses: repoSingleResponses,
tags
tags,
},
patch: {
summary: "Update entity",
@@ -161,24 +161,24 @@ function dataRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
requestBody: {
content: {
"application/json": {
schema: Type.Object({})
}
}
schema: Type.Object({}),
},
},
},
responses: repoSingleResponses,
tags
tags,
},
delete: {
summary: "Delete entity",
parameters: [params.entity, params.entityId],
responses: {
"200": {
description: "Entity deleted"
}
description: "Entity deleted",
},
},
tags
}
}
tags,
},
},
};
return { paths: prefixPaths(paths, config.data.basepath!) };
@@ -189,8 +189,8 @@ function authRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
user: Type.Object({
id: Type.String(),
email: Type.String(),
name: Type.String()
})
name: Type.String(),
}),
};
const tags = ["auth"];
@@ -203,10 +203,10 @@ function authRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
"application/json": {
schema: Type.Object({
email: Type.String(),
password: Type.String()
})
}
}
password: Type.String(),
}),
},
},
},
responses: {
"200": {
@@ -214,14 +214,14 @@ function authRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
content: {
"application/json": {
schema: Type.Object({
user: schemas.user
})
}
}
}
user: schemas.user,
}),
},
},
},
},
tags
}
tags,
},
},
"/password/register": {
post: {
@@ -231,10 +231,10 @@ function authRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
"application/json": {
schema: Type.Object({
email: Type.String(),
password: Type.String()
})
}
}
password: Type.String(),
}),
},
},
},
responses: {
"200": {
@@ -242,14 +242,14 @@ function authRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
content: {
"application/json": {
schema: Type.Object({
user: schemas.user
})
}
}
}
user: schemas.user,
}),
},
},
},
},
tags
}
tags,
},
},
"/me": {
get: {
@@ -260,14 +260,14 @@ function authRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
content: {
"application/json": {
schema: Type.Object({
user: schemas.user
})
}
}
}
user: schemas.user,
}),
},
},
},
},
tags
}
tags,
},
},
"/strategies": {
get: {
@@ -278,15 +278,15 @@ function authRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
content: {
"application/json": {
schema: Type.Object({
strategies: Type.Object({})
})
}
}
}
strategies: Type.Object({}),
}),
},
},
},
},
tags
}
}
tags,
},
},
};
return { paths: prefixPaths(paths, config.auth.basepath!) };
@@ -301,12 +301,12 @@ export function generateOpenAPI(config: ModuleConfigs): OAS.Document {
openapi: "3.1.0",
info: {
title: "bknd API",
version: "0.0.0"
version: "0.0.0",
},
paths: {
...system.paths,
...data.paths,
...auth.paths
}
...auth.paths,
},
};
}