UNPKG

@premieroctet/next-admin

Version:

Next-Admin provides a customizable and turnkey admin dashboard for applications built with Next.js and powered by the Prisma ORM. It aims to simplify the development process by providing a turnkey admin system that can be easily integrated into your proje

58 lines (57 loc) 2.23 kB
function filterProperties(properties) { const filteredProperties = {}; Object.entries(properties).map(([property, attributes])=>{ if (attributes && !Object.keys(attributes).includes("$ref") && !Object.keys(attributes.items || {}).includes("$ref") && !Object.keys(attributes.anyOf?.[0] ?? {}).includes("$ref")) filteredProperties[property] = attributes; }); return filteredProperties; } function getSchemaForResource(schema, resource) { let resourceSchema = schema.definitions[resource]; resourceSchema = { ...resourceSchema, properties: filterProperties(resourceSchema.properties) }; return resourceSchema; } function getSchemas(data, schema, editFieldsOptions) { const uiSchema = {}; let edit = false; let id; const { disabledFields, requiredFields } = Object.entries(editFieldsOptions ?? {}).reduce((acc, [name, opts])=>{ if (opts?.disabled) acc.disabledFields.push(name); if (opts?.required) acc.requiredFields.push(name); return acc; }, { requiredFields: [], disabledFields: [] }); const properties = schema.properties; const idProperty = Object.keys(properties).find((property)=>{ const propertyData = properties[property]; if ("boolean" == typeof propertyData) return false; return propertyData?.__nextadmin?.primaryKey; }); edit = !!data?.[idProperty ?? "id"]; id = data?.[idProperty ?? "id"]; Object.keys(properties).forEach((property)=>{ if (requiredFields?.includes(property) && !schema.required?.includes(property)) schema.required = [ ...schema.required ?? [], property ]; if (properties[property]?.__nextadmin?.disabled || disabledFields?.includes(property)) edit ? uiSchema[property] = { ...uiSchema[property], "ui:disabled": true } : delete properties[property]; }); return { uiSchema, schema, edit, id }; } const getDefinitionFromRef = (schema, ref)=>{ const [definition] = ref.split("/").reverse(); return schema.definitions[definition]; }; export { getDefinitionFromRef, getSchemaForResource, getSchemas };