khata compliant with new react-openapi

This commit is contained in:
2026-06-18 23:20:57 +05:30
parent cbac57dc36
commit 12e5f113b8
12 changed files with 103 additions and 113 deletions

View File

@@ -1,4 +1,11 @@
function getNested(obj: any, path: string): any {
return path.split(".").reduce((o, k) => o?.[k], obj);
}
export function applyDisplayFormat(item: any, format: string): string {
if (!item || typeof item !== "object") return String(item ?? "");
return format.replace(/\{(\w+)\}/g, (_, key) => String(item[key] ?? ""));
return format.replace(/\{([\w.]+)\}/g, (_, key) => {
const val = getNested(item, key);
return val != null ? String(val) : "";
});
}

View File

@@ -350,12 +350,24 @@ function buildFilterComponent(field: FieldConfig, resourceName: string): React.F
export function useResource(resourceName: string): UseResourceReturn {
const { resources } = useAppContext();
const resource = resources.find((r) => r.name === resourceName);
if (!resource) {
throw new Error(`Resource "${resourceName}" not found`);
}
const [state, setState] = useState<ResourceState>({ loading: false, error: null });
if (!resource) {
const noop = async () => { throw new Error(`Resource "${resourceName}" not found yet`); };
return {
resource: null as unknown as ResourceConfig,
components: {},
list: noop,
get: noop,
create: noop,
update: noop,
remove: noop,
loading: false,
error: null,
};
}
const setLoading = useCallback((loading: boolean) => {
setState((s) => ({ ...s, loading }));
}, []);
@@ -437,7 +449,8 @@ export function useResource(resourceName: string): UseResourceReturn {
setError(null);
try {
const api = getApi();
const res = await api.put(`${resource.path}/${id}`, data);
const method = resource.updateMethod ?? "put";
const res = await (method === "patch" ? api.patch : api.put)(`${resource.path}/${id}`, data);
return res.data;
} catch (e: any) {
setError(parseError(e));
@@ -446,7 +459,7 @@ export function useResource(resourceName: string): UseResourceReturn {
setLoading(false);
}
},
[resource.path, setLoading, setError]
[resource.path, resource.updateMethod, setLoading, setError]
);
const remove = useCallback(

View File

@@ -120,10 +120,10 @@ export function validateSpec(spec: OpenApiSpec): ValidationMessage[] {
messages.push({ type: "error", message: `"${resourcePath}/{id}" has no GET endpoint — detail view not possible` });
}
if (!itemPath?.put) {
messages.push({ type: "error", message: `"${resourcePath}/{id}" has no PUT endpoint — update not possible` });
messages.push({ type: "info", message: `"${resourcePath}/{id}" has no PUT endpoint — update not available` });
}
if (!itemPath?.delete) {
messages.push({ type: "error", message: `"${resourcePath}/{id}" has no DELETE endpoint — deletion not possible` });
messages.push({ type: "info", message: `"${resourcePath}/{id}" has no DELETE endpoint — deletion not available` });
}
}
}

View File

@@ -81,9 +81,10 @@ export function buildResourceConfigs(spec: OpenApiSpec): ResourceConfig[] {
list: hasOperation(collectionPathObj, "get"),
get: hasOperation(itemPathObj, "get"),
create: hasOperation(collectionPathObj, "post"),
update: hasOperation(itemPathObj, "put"),
update: hasOperation(itemPathObj, "put") || hasOperation(itemPathObj, "patch"),
delete: hasOperation(itemPathObj, "delete"),
},
updateMethod: hasOperation(itemPathObj, "patch") && !hasOperation(itemPathObj, "put") ? "patch" : "put",
pagination: detectPagination(collectionPathObj),
relationships,
streaming: hasSSE || undefined,
@@ -91,6 +92,7 @@ export function buildResourceConfigs(spec: OpenApiSpec): ResourceConfig[] {
if (hasSSE) {
resource.operations = { list: true, get: false, create: false, update: false, delete: false };
resource.updateMethod = "put";
resource.pagination = null;
resource.relationships = [];
resource.fields = [SSE_RECEIVED_FIELD, ...fields.map((f) => ({ ...f, readOnly: true }))];

View File

@@ -42,6 +42,7 @@ export interface ResourceConfig {
update: boolean;
delete: boolean;
};
updateMethod: "put" | "patch";
pagination: {
limitParam: string;
offsetParam: string;