prefixed data entity endpoints with /entity

This commit is contained in:
dswbx
2025-02-18 09:12:12 +01:00
parent bd362607ae
commit 3e6d381239
14 changed files with 147 additions and 73 deletions

View File

@@ -9,6 +9,7 @@ export type BaseModuleApiOptions = {
token?: string;
headers?: Headers;
token_transport?: "header" | "cookie" | "none";
verbose?: boolean;
};
/** @deprecated */
@@ -107,7 +108,8 @@ export abstract class ModuleApi<Options extends BaseModuleApiOptions = BaseModul
});
return new FetchPromise(request, {
fetcher: this.fetcher
fetcher: this.fetcher,
verbose: this.options.verbose
});
}
@@ -219,15 +221,35 @@ export class FetchPromise<T = ApiResponse<any>> implements Promise<T> {
public request: Request,
protected options?: {
fetcher?: typeof fetch;
verbose?: boolean;
}
) {}
get verbose() {
return this.options?.verbose ?? false;
}
async execute(): Promise<ResponseObject<T>> {
// delay in dev environment
isDebug() && (await new Promise((resolve) => setTimeout(resolve, 200)));
const fetcher = this.options?.fetcher ?? fetch;
if (this.verbose) {
console.log("[FetchPromise] Request", {
method: this.request.method,
url: this.request.url
});
}
const res = await fetcher(this.request);
if (this.verbose) {
console.log("[FetchPromise] Response", {
res: res,
ok: res.ok,
status: res.status
});
}
let resBody: any;
let resData: any;
@@ -242,6 +264,7 @@ export class FetchPromise<T = ApiResponse<any>> implements Promise<T> {
} else {
resBody = res.body;
}
console.groupEnd();
return createResponseProxy<T>(res, resBody, resData);
}

View File

@@ -556,7 +556,9 @@ export class ModuleManager {
await this.options?.onFirstBoot?.();
}
mutateConfigSafe<Module extends keyof Modules>(name: Module) {
mutateConfigSafe<Module extends keyof Modules>(
name: Module
): Pick<ReturnType<Modules[Module]["schema"]>, "set" | "patch" | "overwrite" | "remove"> {
const module = this.modules[name];
const copy = structuredClone(this.configs());

View File

@@ -142,14 +142,13 @@ export class SystemController extends Controller {
const value = await c.req.json();
const path = c.req.param("path") as string;
const moduleConfig = this.app.mutateConfig(module);
if (moduleConfig.has(path)) {
if (this.app.modules.get(module).schema().has(path)) {
return c.json({ success: false, path, error: "Path already exists" }, { status: 400 });
}
console.log("-- add", module, path, value);
return await handleConfigUpdateResponse(c, async () => {
await moduleConfig.patch(path, value);
await this.app.mutateConfig(module).patch(path, value);
return {
success: true,
module,
@@ -283,6 +282,6 @@ export class SystemController extends Controller {
return c.json(generateOpenAPI(config));
});
return hono;
return hono.all("*", (c) => c.notFound());
}
}

View File

@@ -127,7 +127,7 @@ function dataRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
const tags = ["data"];
const paths: OAS.PathsObject = {
"/{entity}": {
"/entity/{entity}": {
get: {
summary: "List entities",
parameters: [params.entity],
@@ -148,7 +148,7 @@ function dataRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
tags
}
},
"/{entity}/{id}": {
"/entity/{entity}/{id}": {
get: {
summary: "Get entity",
parameters: [params.entity, params.entityId],