Compare commits
7 Commits
12e5f113b8
...
openapi-si
| Author | SHA1 | Date | |
|---|---|---|---|
| f5bc7adc37 | |||
| 9a80a52fd5 | |||
| ac7c3d6313 | |||
| 5892bca44d | |||
| 8c824d5239 | |||
| f345dafb46 | |||
| b0c5332d77 |
@@ -85,7 +85,7 @@ export function ResourceDetail({ resource, basePath }: ResourceDetailProps) {
|
|||||||
fmt = field.inlineDisplayFormat ?? resource.displayFormat;
|
fmt = field.inlineDisplayFormat ?? resource.displayFormat;
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<Grid item xs={12} sm={6} md={4} key={field.name}>
|
<Grid size={12} key={field.name}>
|
||||||
<DetailFieldRenderer field={field} value={value} displayFormat={fmt} />
|
<DetailFieldRenderer field={field} value={value} displayFormat={fmt} />
|
||||||
</Grid>
|
</Grid>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -106,7 +106,7 @@ export function ResourceForm({ resource, basePath, mode }: ResourceFormProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const opts = items.map((item: any) => ({
|
const opts = items.map((item: any) => ({
|
||||||
value: item[targetRes.primaryKey],
|
value: resolvePk(item, targetRes.primaryKey),
|
||||||
label: applyFormat(item, targetRes.displayFormat),
|
label: applyFormat(item, targetRes.displayFormat),
|
||||||
}));
|
}));
|
||||||
console.log(`[loadFkOptions] computed ${opts.length} options for field "${fieldName}"`, opts.slice(0, 3));
|
console.log(`[loadFkOptions] computed ${opts.length} options for field "${fieldName}"`, opts.slice(0, 3));
|
||||||
@@ -139,9 +139,9 @@ export function ResourceForm({ resource, basePath, mode }: ResourceFormProps) {
|
|||||||
const targetRes = allResources.find((r) => r.name === rel.config.resource);
|
const targetRes = allResources.find((r) => r.name === rel.config.resource);
|
||||||
if (targetRes) {
|
if (targetRes) {
|
||||||
if (Array.isArray(val)) {
|
if (Array.isArray(val)) {
|
||||||
resolved[rel.fieldName] = val.map((item: any) => item[targetRes.primaryKey]);
|
resolved[rel.fieldName] = val.map((item: any) => resolvePk(item, targetRes.primaryKey));
|
||||||
} else if (typeof val === "object") {
|
} else if (typeof val === "object") {
|
||||||
resolved[rel.fieldName] = val[targetRes.primaryKey];
|
resolved[rel.fieldName] = resolvePk(val, targetRes.primaryKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!rel.config.prefetch) {
|
if (!rel.config.prefetch) {
|
||||||
@@ -237,7 +237,7 @@ export function ResourceForm({ resource, basePath, mode }: ResourceFormProps) {
|
|||||||
{resource.orderedFields
|
{resource.orderedFields
|
||||||
.filter((f) => !(f.name === resource.primaryKey && mode === "edit"))
|
.filter((f) => !(f.name === resource.primaryKey && mode === "edit"))
|
||||||
.map((field) => (
|
.map((field) => (
|
||||||
<Grid item xs={12} sm={6} md={4} key={field.name}>
|
<Grid size={12} key={field.name}>
|
||||||
<FormFieldRenderer
|
<FormFieldRenderer
|
||||||
field={field}
|
field={field}
|
||||||
value={formData[field.name]}
|
value={formData[field.name]}
|
||||||
@@ -281,6 +281,11 @@ export function ResourceForm({ resource, basePath, mode }: ResourceFormProps) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function resolvePk(item: any, pk: string): any {
|
||||||
|
const v = item?.[pk];
|
||||||
|
return v != null ? v : item?.[`_${pk}`];
|
||||||
|
}
|
||||||
|
|
||||||
function applyFormat(obj: any, format: string): string {
|
function applyFormat(obj: any, format: string): string {
|
||||||
if (!obj || typeof obj !== "object") return String(obj ?? "");
|
if (!obj || typeof obj !== "object") return String(obj ?? "");
|
||||||
return format.replace(/\{(\w+)\}/g, (_, key) => String(obj[key] ?? ""));
|
return format.replace(/\{(\w+)\}/g, (_, key) => String(obj[key] ?? ""));
|
||||||
|
|||||||
@@ -387,7 +387,7 @@ export function ResourceList({ resource, basePath }: ResourceListProps) {
|
|||||||
{detailRow && (
|
{detailRow && (
|
||||||
<Grid container spacing={2} sx={{ mt: 0.5 }}>
|
<Grid container spacing={2} sx={{ mt: 0.5 }}>
|
||||||
{visibleColumns.map((col) => (
|
{visibleColumns.map((col) => (
|
||||||
<Grid key={col.name} item xs={12} sm={6}>
|
<Grid key={col.name} size={{ xs: 12, sm: 6 }}>
|
||||||
<DetailFieldRenderer
|
<DetailFieldRenderer
|
||||||
field={col}
|
field={col}
|
||||||
value={detailRow[col.name]}
|
value={detailRow[col.name]}
|
||||||
|
|||||||
@@ -62,5 +62,9 @@ export function ListCellRenderer({ field, value, displayFormat }: ListCellProps)
|
|||||||
return <Chip label={value ? "Yes" : "No"} size="small" color={value ? "success" : "default"} />;
|
return <Chip label={value ? "Yes" : "No"} size="small" color={value ? "success" : "default"} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof value === "object") {
|
||||||
|
return <Typography variant="body2">{applyDisplayFormat(value, displayFormat ?? "")}</Typography>;
|
||||||
|
}
|
||||||
|
|
||||||
return <Typography variant="body2">{String(value)}</Typography>;
|
return <Typography variant="body2">{String(value)}</Typography>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ export function AppProvider({ specConfiguration, children }: AppProviderProps) {
|
|||||||
|
|
||||||
const spec = await loadSpec(specConfiguration.specUrl);
|
const spec = await loadSpec(specConfiguration.specUrl);
|
||||||
|
|
||||||
const allMessages = validateSpec(spec);
|
const allMessages = validateSpec(spec, specConfiguration);
|
||||||
|
|
||||||
const errs = allMessages.filter((m) => m.type === "error");
|
const errs = allMessages.filter((m) => m.type === "error");
|
||||||
const warns = allMessages.filter((m) => m.type === "warning");
|
const warns = allMessages.filter((m) => m.type === "warning");
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ interface UseResourceReturn {
|
|||||||
resource: ResourceConfig;
|
resource: ResourceConfig;
|
||||||
components: Record<string, React.FC<FilterComponentProps>>;
|
components: Record<string, React.FC<FilterComponentProps>>;
|
||||||
list: (params?: Record<string, any>) => Promise<ParsedListResponse>;
|
list: (params?: Record<string, any>) => Promise<ParsedListResponse>;
|
||||||
get: (id: string | number) => Promise<any>;
|
get: (id: string | number, params?: Record<string, any>) => Promise<any>;
|
||||||
create: (data: any) => Promise<any>;
|
create: (data: any) => Promise<any>;
|
||||||
update: (id: string | number, data: any) => Promise<any>;
|
update: (id: string | number, data: any) => Promise<any>;
|
||||||
remove: (id: string | number) => Promise<void>;
|
remove: (id: string | number) => Promise<void>;
|
||||||
@@ -266,16 +266,7 @@ function buildFilterComponent(field: FieldConfig, resourceName: string): React.F
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
function buildAutocompleteFilter(getDisplayValue: (row: any) => string) {
|
||||||
!field.fk &&
|
|
||||||
!field.enumValues &&
|
|
||||||
field.type !== "boolean" &&
|
|
||||||
field.type !== "integer" &&
|
|
||||||
field.type !== "number" &&
|
|
||||||
field.format !== "date" &&
|
|
||||||
field.format !== "date-time" &&
|
|
||||||
!field.refSchema
|
|
||||||
) {
|
|
||||||
const StringAutocompleteFilter: React.FC<FilterComponentProps> = ({ value, onChange, data, labelOverride }) => {
|
const StringAutocompleteFilter: React.FC<FilterComponentProps> = ({ value, onChange, data, labelOverride }) => {
|
||||||
const { resources, config } = useAppContext();
|
const { resources, config } = useAppContext();
|
||||||
const filterMode = config.resourceConfig?.[resourceName]?.filterOptions?.mode ?? "server";
|
const filterMode = config.resourceConfig?.[resourceName]?.filterOptions?.mode ?? "server";
|
||||||
@@ -284,7 +275,12 @@ function buildFilterComponent(field: FieldConfig, resourceName: string): React.F
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (filterMode === "client" && data) {
|
if (filterMode === "client" && data) {
|
||||||
setOptions(extractDataOptions(data, field.name));
|
const vals = new Set<string>();
|
||||||
|
for (const row of data) {
|
||||||
|
const v = getDisplayValue(row);
|
||||||
|
if (v && v !== "") vals.add(v);
|
||||||
|
}
|
||||||
|
setOptions([...vals].sort());
|
||||||
fetched.current = true;
|
fetched.current = true;
|
||||||
} else if (filterMode === "server" && !fetched.current) {
|
} else if (filterMode === "server" && !fetched.current) {
|
||||||
const cacheKey = resourceName + ":" + field.name;
|
const cacheKey = resourceName + ":" + field.name;
|
||||||
@@ -302,11 +298,11 @@ function buildFilterComponent(field: FieldConfig, resourceName: string): React.F
|
|||||||
const res = await api.get(selfRes.path, { params });
|
const res = await api.get(selfRes.path, { params });
|
||||||
let items: any[];
|
let items: any[];
|
||||||
if (selfRes.pagination) {
|
if (selfRes.pagination) {
|
||||||
items = res.data.items ?? [];
|
items = Array.isArray(res.data) ? res.data : (res.data.items ?? []);
|
||||||
} else {
|
} else {
|
||||||
items = Array.isArray(res.data) ? res.data : [];
|
items = Array.isArray(res.data) ? res.data : [];
|
||||||
}
|
}
|
||||||
const values = [...new Set(items.map((r: any) => String(r[field.name] ?? "")).filter(Boolean))].sort();
|
const values = [...new Set(items.map((r: any) => getDisplayValue(r)).filter(Boolean))].sort();
|
||||||
_stringOptionsCache.set(cacheKey, values);
|
_stringOptionsCache.set(cacheKey, values);
|
||||||
setOptions(values);
|
setOptions(values);
|
||||||
fetched.current = true;
|
fetched.current = true;
|
||||||
@@ -338,6 +334,23 @@ function buildFilterComponent(field: FieldConfig, resourceName: string): React.F
|
|||||||
return StringAutocompleteFilter;
|
return StringAutocompleteFilter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isSimpleField =
|
||||||
|
!field.fk && !field.enumValues &&
|
||||||
|
field.type !== "boolean" && field.type !== "integer" && field.type !== "number" &&
|
||||||
|
field.format !== "date" && field.format !== "date-time";
|
||||||
|
|
||||||
|
if (isSimpleField && !field.refSchema) {
|
||||||
|
return buildAutocompleteFilter((row) => String(row[field.name] ?? ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (field.refSchema && field.inlineDisplayFormat) {
|
||||||
|
return buildAutocompleteFilter((row) => {
|
||||||
|
const val = row[field.name];
|
||||||
|
if (val == null || typeof val !== "object") return "";
|
||||||
|
return field.inlineDisplayFormat!.replace(/\{(\w+)\}/g, (_, key) => String(val[key] ?? ""));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return ({ value, onChange, labelOverride }) => (
|
return ({ value, onChange, labelOverride }) => (
|
||||||
<StringField
|
<StringField
|
||||||
field={{ ...field, readOnly: false, description: "", label: labelOverride ?? field.label }}
|
field={{ ...field, readOnly: false, description: "", label: labelOverride ?? field.label }}
|
||||||
@@ -353,20 +366,11 @@ export function useResource(resourceName: string): UseResourceReturn {
|
|||||||
|
|
||||||
const [state, setState] = useState<ResourceState>({ loading: false, error: null });
|
const [state, setState] = useState<ResourceState>({ loading: false, error: null });
|
||||||
|
|
||||||
if (!resource) {
|
const rPath = resource?.path;
|
||||||
const noop = async () => { throw new Error(`Resource "${resourceName}" not found yet`); };
|
const rPagination = resource?.pagination;
|
||||||
return {
|
const rUpdateMethod = resource?.updateMethod;
|
||||||
resource: null as unknown as ResourceConfig,
|
const rStreaming = resource?.streaming;
|
||||||
components: {},
|
const rFields = resource?.fields;
|
||||||
list: noop,
|
|
||||||
get: noop,
|
|
||||||
create: noop,
|
|
||||||
update: noop,
|
|
||||||
remove: noop,
|
|
||||||
loading: false,
|
|
||||||
error: null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const setLoading = useCallback((loading: boolean) => {
|
const setLoading = useCallback((loading: boolean) => {
|
||||||
setState((s) => ({ ...s, loading }));
|
setState((s) => ({ ...s, loading }));
|
||||||
@@ -378,22 +382,26 @@ export function useResource(resourceName: string): UseResourceReturn {
|
|||||||
|
|
||||||
const list = useCallback(
|
const list = useCallback(
|
||||||
async (params?: Record<string, any>): Promise<ParsedListResponse> => {
|
async (params?: Record<string, any>): Promise<ParsedListResponse> => {
|
||||||
|
if (!rPath) return { items: [] };
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const api = getApi();
|
const api = getApi();
|
||||||
const res = await api.get(resource.path, { params });
|
const res = await api.get(rPath, { params });
|
||||||
const data = res.data;
|
const data = res.data;
|
||||||
|
|
||||||
if (resource.pagination) {
|
if (rPagination) {
|
||||||
|
if (Array.isArray(data)) {
|
||||||
|
return { items: data };
|
||||||
|
}
|
||||||
if (!data || typeof data !== "object" || !Array.isArray(data.items)) {
|
if (!data || typeof data !== "object" || !Array.isArray(data.items)) {
|
||||||
throw new Error(`Expected paginated response { total, items } from ${resource.path}`);
|
throw new Error(`Expected paginated response { total, items } from ${rPath}`);
|
||||||
}
|
}
|
||||||
return { items: data.items, total: data.total ?? data.items.length };
|
return { items: data.items, total: data.total ?? data.items.length };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Array.isArray(data)) {
|
if (!Array.isArray(data)) {
|
||||||
throw new Error(`Expected array response from ${resource.path}`);
|
throw new Error(`Expected array response from ${rPath}`);
|
||||||
}
|
}
|
||||||
return { items: data };
|
return { items: data };
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
@@ -404,16 +412,17 @@ export function useResource(resourceName: string): UseResourceReturn {
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[resource.path, resource.pagination, setLoading, setError]
|
[rPath, rPagination, setLoading, setError]
|
||||||
);
|
);
|
||||||
|
|
||||||
const get = useCallback(
|
const get = useCallback(
|
||||||
async (id: string | number): Promise<any> => {
|
async (id: string | number, params?: Record<string, any>): Promise<any> => {
|
||||||
|
if (!rPath) throw new Error(`Resource "${resourceName}" not found yet`);
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const api = getApi();
|
const api = getApi();
|
||||||
const res = await api.get(`${resource.path}/${id}`);
|
const res = await api.get(`${rPath}/${id}`, { params });
|
||||||
return res.data;
|
return res.data;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
setError(parseError(e));
|
setError(parseError(e));
|
||||||
@@ -422,16 +431,17 @@ export function useResource(resourceName: string): UseResourceReturn {
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[resource.path, setLoading, setError]
|
[rPath, setLoading, setError]
|
||||||
);
|
);
|
||||||
|
|
||||||
const create = useCallback(
|
const create = useCallback(
|
||||||
async (data: any): Promise<any> => {
|
async (data: any): Promise<any> => {
|
||||||
|
if (!rPath) throw new Error(`Resource "${resourceName}" not found yet`);
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const api = getApi();
|
const api = getApi();
|
||||||
const res = await api.post(resource.path, data);
|
const res = await api.post(rPath, data);
|
||||||
return res.data;
|
return res.data;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
setError(parseError(e));
|
setError(parseError(e));
|
||||||
@@ -440,17 +450,18 @@ export function useResource(resourceName: string): UseResourceReturn {
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[resource.path, setLoading, setError]
|
[rPath, setLoading, setError]
|
||||||
);
|
);
|
||||||
|
|
||||||
const update = useCallback(
|
const update = useCallback(
|
||||||
async (id: string | number, data: any): Promise<any> => {
|
async (id: string | number, data: any): Promise<any> => {
|
||||||
|
if (!rPath) throw new Error(`Resource "${resourceName}" not found yet`);
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const api = getApi();
|
const api = getApi();
|
||||||
const method = resource.updateMethod ?? "put";
|
const method = rUpdateMethod ?? "put";
|
||||||
const res = await (method === "patch" ? api.patch : api.put)(`${resource.path}/${id}`, data);
|
const res = await (method === "patch" ? api.patch : api.put)(`${rPath}/${id}`, data);
|
||||||
return res.data;
|
return res.data;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
setError(parseError(e));
|
setError(parseError(e));
|
||||||
@@ -459,16 +470,17 @@ export function useResource(resourceName: string): UseResourceReturn {
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[resource.path, resource.updateMethod, setLoading, setError]
|
[rPath, rUpdateMethod, setLoading, setError]
|
||||||
);
|
);
|
||||||
|
|
||||||
const remove = useCallback(
|
const remove = useCallback(
|
||||||
async (id: string | number): Promise<void> => {
|
async (id: string | number): Promise<void> => {
|
||||||
|
if (!rPath) throw new Error(`Resource "${resourceName}" not found yet`);
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const api = getApi();
|
const api = getApi();
|
||||||
await api.delete(`${resource.path}/${id}`);
|
await api.delete(`${rPath}/${id}`);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
setError(parseError(e));
|
setError(parseError(e));
|
||||||
throw e;
|
throw e;
|
||||||
@@ -476,17 +488,17 @@ export function useResource(resourceName: string): UseResourceReturn {
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[resource.path, setLoading, setError]
|
[rPath, setLoading, setError]
|
||||||
);
|
);
|
||||||
|
|
||||||
const stream = useCallback(
|
const stream = useCallback(
|
||||||
(handlers: StreamHandlers): StreamSubscription => {
|
(handlers: StreamHandlers): StreamSubscription => {
|
||||||
if (!resource.streaming) {
|
if (!rPath || !rStreaming) {
|
||||||
throw new Error(`Resource "${resourceName}" does not support streaming`);
|
throw new Error(`Resource "${resourceName}" does not support streaming`);
|
||||||
}
|
}
|
||||||
const api = getApi();
|
const api = getApi();
|
||||||
const baseUrl = (api.defaults.baseURL ?? "").replace(/\/+$/, "");
|
const baseUrl = (api.defaults.baseURL ?? "").replace(/\/+$/, "");
|
||||||
const url = baseUrl + resource.path;
|
const url = baseUrl + rPath;
|
||||||
const es = new EventSource(url);
|
const es = new EventSource(url);
|
||||||
|
|
||||||
es.onopen = () => handlers.onOpen?.();
|
es.onopen = () => handlers.onOpen?.();
|
||||||
@@ -504,19 +516,35 @@ export function useResource(resourceName: string): UseResourceReturn {
|
|||||||
|
|
||||||
return { close: () => es.close() };
|
return { close: () => es.close() };
|
||||||
},
|
},
|
||||||
[resource.path, resource.streaming, resourceName]
|
[rPath, rStreaming, resourceName]
|
||||||
);
|
);
|
||||||
|
|
||||||
const components = useMemo(
|
const components = useMemo(
|
||||||
() => {
|
() => {
|
||||||
const map: Record<string, React.FC<FilterComponentProps>> = {};
|
const map: Record<string, React.FC<FilterComponentProps>> = {};
|
||||||
for (const field of resource.fields) {
|
if (!rFields) return map;
|
||||||
|
for (const field of rFields) {
|
||||||
map[field.name] = buildFilterComponent(field, resourceName);
|
map[field.name] = buildFilterComponent(field, resourceName);
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
},
|
},
|
||||||
[resource.fields, resourceName]
|
[rFields, resourceName]
|
||||||
);
|
);
|
||||||
|
|
||||||
return { resource, components, list, get, create, update, remove, stream: resource.streaming ? stream : undefined, loading: state.loading, error: state.error };
|
if (!resource) {
|
||||||
|
return {
|
||||||
|
resource: null as unknown as ResourceConfig,
|
||||||
|
components,
|
||||||
|
list,
|
||||||
|
get,
|
||||||
|
create,
|
||||||
|
update,
|
||||||
|
remove,
|
||||||
|
stream: undefined,
|
||||||
|
loading: false,
|
||||||
|
error: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return { resource, components, list, get, create, update, remove, stream: rStreaming ? stream : undefined, loading: state.loading, error: state.error };
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import axios, { AxiosInstance } from "axios";
|
import axios, { AxiosInstance } from "axios";
|
||||||
|
import { tokenStore } from "../../../react-auth/token";
|
||||||
|
|
||||||
let apiClient: AxiosInstance | null = null;
|
let apiClient: AxiosInstance | null = null;
|
||||||
|
|
||||||
@@ -26,7 +27,6 @@ export function initApi(baseUrl: string, getToken?: () => string | null): AxiosI
|
|||||||
if (error.response?.status === 401 && getToken) {
|
if (error.response?.status === 401 && getToken) {
|
||||||
const currentToken = getToken();
|
const currentToken = getToken();
|
||||||
if (currentToken) {
|
if (currentToken) {
|
||||||
const tokenStore = { clear: () => localStorage.removeItem("token") };
|
|
||||||
tokenStore.clear();
|
tokenStore.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { OpenApiSpec, ValidationMessage } from "./types";
|
import type { OpenApiSpec, ValidationMessage, SpecConfiguration } from "./types";
|
||||||
|
|
||||||
export function validateSpec(spec: OpenApiSpec): ValidationMessage[] {
|
export function validateSpec(spec: OpenApiSpec, specConfig?: SpecConfiguration): ValidationMessage[] {
|
||||||
const messages: ValidationMessage[] = [];
|
const messages: ValidationMessage[] = [];
|
||||||
const schemas = (spec.components?.schemas ?? {}) as Record<string, any>;
|
const schemas = (spec.components?.schemas ?? {}) as Record<string, any>;
|
||||||
const paths = spec.paths ?? {};
|
const paths = spec.paths ?? {};
|
||||||
@@ -13,7 +13,7 @@ export function validateSpec(spec: OpenApiSpec): ValidationMessage[] {
|
|||||||
messages.push({ type: "error", message: "Missing 'info.title'" });
|
messages.push({ type: "error", message: "Missing 'info.title'" });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!spec.servers?.[0]?.url) {
|
if (!spec.servers?.[0]?.url && !specConfig?.baseApiUrl) {
|
||||||
messages.push({ type: "warning", message: "No 'servers[0].url' defined — provide 'baseApiUrl' in specConfiguration" });
|
messages.push({ type: "warning", message: "No 'servers[0].url' defined — provide 'baseApiUrl' in specConfiguration" });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,12 +13,15 @@ export function extractFields(schemaName: string, schema: any, schemas: Record<s
|
|||||||
.map(([name, prop]: [string, any]) => {
|
.map(([name, prop]: [string, any]) => {
|
||||||
const isDirectRef = !!prop.$ref;
|
const isDirectRef = !!prop.$ref;
|
||||||
const isItemsRef = prop.type === "array" && !!prop.items?.$ref;
|
const isItemsRef = prop.type === "array" && !!prop.items?.$ref;
|
||||||
const isRef = isDirectRef || isItemsRef;
|
const isOneOf = !!prop.oneOf;
|
||||||
|
const isRef = isDirectRef || isItemsRef || isOneOf;
|
||||||
|
|
||||||
const refSchemaName = isDirectRef
|
const refSchemaName = isDirectRef
|
||||||
? resolveRef(prop.$ref)
|
? resolveRef(prop.$ref)
|
||||||
: isItemsRef
|
: isItemsRef
|
||||||
? resolveRef(prop.items.$ref)
|
? resolveRef(prop.items.$ref)
|
||||||
|
: isOneOf && prop.oneOf[0]?.$ref
|
||||||
|
? resolveRef(prop.oneOf[0].$ref)
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
const refSchema = refSchemaName ? schemas[refSchemaName] : undefined;
|
const refSchema = refSchemaName ? schemas[refSchemaName] : undefined;
|
||||||
@@ -31,7 +34,7 @@ export function extractFields(schemaName: string, schema: any, schemas: Record<s
|
|||||||
name,
|
name,
|
||||||
label: prop["x-label"],
|
label: prop["x-label"],
|
||||||
description: prop["x-description"] ?? prop["x-label"] ?? name,
|
description: prop["x-description"] ?? prop["x-label"] ?? name,
|
||||||
type: isRef && refSchema ? "object" : (prop.type ?? "string"),
|
type: isRef && refSchema ? "object" : isOneOf ? "object" : (prop.type ?? "string"),
|
||||||
format: prop.format,
|
format: prop.format,
|
||||||
order: prop["x-order"],
|
order: prop["x-order"],
|
||||||
hidden: prop["x-hidden"] ?? {},
|
hidden: prop["x-hidden"] ?? {},
|
||||||
|
|||||||
26
src/RequireAuth.tsx
Normal file
26
src/RequireAuth.tsx
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import * as React from "react";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import { useAuth, AuthPage } from "../react-auth";
|
||||||
|
|
||||||
|
export function RequireAuth({ children }: { children: React.ReactNode }) {
|
||||||
|
const { currentUser, loading, error, login, register } = useAuth();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const [mode, setMode] = React.useState<"login" | "register">("login");
|
||||||
|
|
||||||
|
if (currentUser) {
|
||||||
|
return <>{children}</>;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<AuthPage
|
||||||
|
mode={mode}
|
||||||
|
onBack={() => navigate("/")}
|
||||||
|
onSwitchMode={() => setMode(mode === "login" ? "register" : "login")}
|
||||||
|
login={login}
|
||||||
|
register={register}
|
||||||
|
loading={loading}
|
||||||
|
error={error}
|
||||||
|
currentUser={currentUser}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -12,9 +12,11 @@ export interface ReportParams {
|
|||||||
export function useReport(params: ReportParams) {
|
export function useReport(params: ReportParams) {
|
||||||
const { get } = useResource("reports");
|
const { get } = useResource("reports");
|
||||||
|
|
||||||
|
const { snapshot_id, ...queryParams } = params;
|
||||||
|
|
||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: ["reports", "read", params],
|
queryKey: ["reports", "read", params],
|
||||||
queryFn: () =>
|
queryFn: () =>
|
||||||
get(params.snapshot_id ? params.snapshot_id : "latest"),
|
get(snapshot_id ?? "latest", queryParams),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/main.jsx
14
src/main.jsx
@@ -16,6 +16,7 @@ import Dashboard from './Dashboard';
|
|||||||
import FetchRequests from './FetchRequests';
|
import FetchRequests from './FetchRequests';
|
||||||
import FetchRequestDetail from './FetchRequestDetail';
|
import FetchRequestDetail from './FetchRequestDetail';
|
||||||
import ReportSnapshots from './ReportSnapshots';
|
import ReportSnapshots from './ReportSnapshots';
|
||||||
|
import { RequireAuth } from './RequireAuth';
|
||||||
import { AppProvider, Admin } from '../react-openapi';
|
import { AppProvider, Admin } from '../react-openapi';
|
||||||
import { Buffer } from 'buffer';
|
import { Buffer } from 'buffer';
|
||||||
import process from 'process';
|
import process from 'process';
|
||||||
@@ -23,6 +24,7 @@ import { AuthProvider } from "../react-auth";
|
|||||||
import Header from './Header';
|
import Header from './Header';
|
||||||
import Footer from './Footer';
|
import Footer from './Footer';
|
||||||
import AppTheme from './shared-theme/AppTheme';
|
import AppTheme from './shared-theme/AppTheme';
|
||||||
|
import { specConfiguration } from './openapi-config';
|
||||||
|
|
||||||
const queryClient = new QueryClient();
|
const queryClient = new QueryClient();
|
||||||
|
|
||||||
@@ -33,14 +35,6 @@ const rootElement = document.getElementById('root');
|
|||||||
const root = createRoot(rootElement);
|
const root = createRoot(rootElement);
|
||||||
|
|
||||||
const AUTH_BASE = import.meta.env.VITE_AUTH_BASE_URL;
|
const AUTH_BASE = import.meta.env.VITE_AUTH_BASE_URL;
|
||||||
const API_BASE = import.meta.env.VITE_API_BASE_URL;
|
|
||||||
|
|
||||||
const specConfig = {
|
|
||||||
specUrl: `${API_BASE}/openapi.yaml`,
|
|
||||||
baseApiUrl: API_BASE,
|
|
||||||
title: 'Khata Admin',
|
|
||||||
getToken: () => localStorage.getItem('token'),
|
|
||||||
};
|
|
||||||
|
|
||||||
const routerMapping = [
|
const routerMapping = [
|
||||||
{ path: "/", component: Home, headerTitle: "Home" },
|
{ path: "/", component: Home, headerTitle: "Home" },
|
||||||
@@ -54,7 +48,7 @@ const routerMapping = [
|
|||||||
|
|
||||||
root.render(
|
root.render(
|
||||||
<QueryClientProvider client={queryClient}>
|
<QueryClientProvider client={queryClient}>
|
||||||
<AppProvider specConfiguration={specConfig}>
|
<AppProvider specConfiguration={specConfiguration}>
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
<AuthProvider authBaseUrl={AUTH_BASE}>
|
<AuthProvider authBaseUrl={AUTH_BASE}>
|
||||||
<AppTheme>
|
<AppTheme>
|
||||||
@@ -71,7 +65,7 @@ root.render(
|
|||||||
path={path}
|
path={path}
|
||||||
element={
|
element={
|
||||||
path.startsWith("/admin") ? (
|
path.startsWith("/admin") ? (
|
||||||
<Component basePath="/admin" />
|
<RequireAuth><Component basePath="/admin" /></RequireAuth>
|
||||||
) : (
|
) : (
|
||||||
<Component />
|
<Component />
|
||||||
)
|
)
|
||||||
|
|||||||
16
src/openapi-config.ts
Normal file
16
src/openapi-config.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import type { SpecConfiguration } from "../react-openapi";
|
||||||
|
// import { tokenStore } from "../react-auth";
|
||||||
|
|
||||||
|
const apiBase = import.meta.env.VITE_API_BASE_URL;
|
||||||
|
|
||||||
|
export const specConfiguration: SpecConfiguration = {
|
||||||
|
specUrl: `${apiBase}/openapi.json`,
|
||||||
|
baseApiUrl: apiBase,
|
||||||
|
title: "Khata",
|
||||||
|
resourceConfig: {
|
||||||
|
expenses: {
|
||||||
|
filterOptions: { mode: "client" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// getToken: () => tokenStore.get(),
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user