57 lines
1.9 KiB
TypeScript
57 lines
1.9 KiB
TypeScript
import type { FieldConfig } from "../types";
|
|
|
|
function resolveRef(ref: string): string | undefined {
|
|
return ref.split("/").pop();
|
|
}
|
|
|
|
export function extractFields(schemaName: string, schema: any, schemas: Record<string, any>): FieldConfig[] {
|
|
const props = schema.properties ?? {};
|
|
const requiredFields: string[] = schema.required ?? [];
|
|
|
|
return Object.entries(props)
|
|
.filter(([, prop]: [string, any]) => prop && typeof prop === "object")
|
|
.map(([name, prop]: [string, any]) => {
|
|
const isDirectRef = !!prop.$ref;
|
|
const isItemsRef = prop.type === "array" && !!prop.items?.$ref;
|
|
const isOneOf = !!prop.oneOf;
|
|
const isRef = isDirectRef || isItemsRef || isOneOf;
|
|
|
|
const refSchemaName = isDirectRef
|
|
? resolveRef(prop.$ref)
|
|
: isItemsRef
|
|
? resolveRef(prop.items.$ref)
|
|
: isOneOf && prop.oneOf[0]?.$ref
|
|
? resolveRef(prop.oneOf[0].$ref)
|
|
: undefined;
|
|
|
|
const refSchema = refSchemaName ? schemas[refSchemaName] : undefined;
|
|
|
|
const inlineDisplayFormat = isRef && refSchema && !prop["x-fk"]
|
|
? refSchema["x-display-format"]
|
|
: undefined;
|
|
|
|
const field: FieldConfig = {
|
|
name,
|
|
label: prop["x-label"],
|
|
description: prop["x-description"] ?? prop["x-label"] ?? name,
|
|
type: isRef && refSchema ? "object" : isOneOf ? "object" : (prop.type ?? "string"),
|
|
format: prop.format,
|
|
order: prop["x-order"],
|
|
hidden: prop["x-hidden"] ?? {},
|
|
filterable: prop["x-filterable"] ?? false,
|
|
sortable: prop["x-sortable"] ?? false,
|
|
readOnly: prop.readOnly ?? false,
|
|
required: requiredFields.includes(name),
|
|
enumValues: prop.enum,
|
|
fk: prop["x-fk"],
|
|
uiType: prop["x-ui-type"],
|
|
uploadUrl: prop["x-upload-url"],
|
|
refSchema: refSchemaName,
|
|
inlineDisplayFormat,
|
|
isArray: prop.type === "array",
|
|
};
|
|
|
|
return field;
|
|
});
|
|
}
|