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,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,
},
};
}