{"version":3,"file":"custom-fields.mjs","sources":["../../src/registries/custom-fields.ts"],"sourcesContent":["import { has, isPlainObject } from 'lodash/fp';\n\nimport type { Core, Modules } from '@strapi/types';\n\nconst ALLOWED_TYPES = [\n  'biginteger',\n  'boolean',\n  'date',\n  'datetime',\n  'decimal',\n  'email',\n  'enumeration',\n  'float',\n  'integer',\n  'json',\n  'password',\n  'richtext',\n  'string',\n  'text',\n  'time',\n  'uid',\n] as const;\n\nconst customFieldsRegistry = (strapi: Core.Strapi) => {\n  const customFields: Record<string, unknown> = {};\n\n  return {\n    getAll() {\n      return customFields;\n    },\n    get(customField: string) {\n      const registeredCustomField = customFields[customField];\n      if (!registeredCustomField) {\n        throw new Error(`Could not find Custom Field: ${customField}`);\n      }\n\n      return registeredCustomField;\n    },\n    add(\n      customField:\n        | Modules.CustomFields.CustomFieldServerOptions\n        | Modules.CustomFields.CustomFieldServerOptions[]\n    ) {\n      const customFieldList = Array.isArray(customField) ? customField : [customField];\n\n      for (const cf of customFieldList) {\n        if (!has('name', cf) || !has('type', cf)) {\n          throw new Error(`Custom fields require a 'name' and 'type' key`);\n        }\n\n        const { name, plugin, type, inputSize } = cf;\n        if (!ALLOWED_TYPES.includes(type)) {\n          throw new Error(\n            `Custom field type: '${type}' is not a valid Strapi type or it can't be used with a Custom Field`\n          );\n        }\n\n        const isValidObjectKey = /^(?![0-9])[a-zA-Z0-9$_-]+$/g;\n        if (!isValidObjectKey.test(name)) {\n          throw new Error(`Custom field name: '${name}' is not a valid object key`);\n        }\n\n        // Validate inputSize when provided\n        if (inputSize) {\n          if (\n            !isPlainObject(inputSize) ||\n            !has('default', inputSize) ||\n            !has('isResizable', inputSize)\n          ) {\n            throw new Error(`inputSize should be an object with 'default' and 'isResizable' keys`);\n          }\n          if (![4, 6, 8, 12].includes(inputSize.default)) {\n            throw new Error('Custom fields require a valid default input size');\n          }\n          if (typeof inputSize.isResizable !== 'boolean') {\n            throw new Error('Custom fields should specify if their input is resizable');\n          }\n        }\n\n        // When no plugin is specified, or it isn't found in Strapi, default to global\n        const uid =\n          plugin && strapi.plugin(plugin) ? `plugin::${plugin}.${name}` : `global::${name}`;\n\n        if (has(uid, customFields)) {\n          throw new Error(`Custom field: '${uid}' has already been registered`);\n        }\n\n        customFields[uid] = cf;\n      }\n    },\n  };\n};\n\nexport default customFieldsRegistry;\n"],"names":["ALLOWED_TYPES","customFieldsRegistry","strapi","customFields","getAll","get","customField","registeredCustomField","Error","add","customFieldList","Array","isArray","cf","has","name","plugin","type","inputSize","includes","isValidObjectKey","test","isPlainObject","default","isResizable","uid"],"mappings":";;AAIA,MAAMA,aAAAA,GAAgB;AACpB,IAAA,YAAA;AACA,IAAA,SAAA;AACA,IAAA,MAAA;AACA,IAAA,UAAA;AACA,IAAA,SAAA;AACA,IAAA,OAAA;AACA,IAAA,aAAA;AACA,IAAA,OAAA;AACA,IAAA,SAAA;AACA,IAAA,MAAA;AACA,IAAA,UAAA;AACA,IAAA,UAAA;AACA,IAAA,QAAA;AACA,IAAA,MAAA;AACA,IAAA,MAAA;AACA,IAAA;AACD,CAAA;AAED,MAAMC,uBAAuB,CAACC,MAAAA,GAAAA;AAC5B,IAAA,MAAMC,eAAwC,EAAC;IAE/C,OAAO;AACLC,QAAAA,MAAAA,CAAAA,GAAAA;YACE,OAAOD,YAAAA;AACT,QAAA,CAAA;AACAE,QAAAA,GAAAA,CAAAA,CAAIC,WAAmB,EAAA;YACrB,MAAMC,qBAAAA,GAAwBJ,YAAY,CAACG,WAAAA,CAAY;AACvD,YAAA,IAAI,CAACC,qBAAAA,EAAuB;AAC1B,gBAAA,MAAM,IAAIC,KAAAA,CAAM,CAAC,6BAA6B,EAAEF,WAAAA,CAAAA,CAAa,CAAA;AAC/D,YAAA;YAEA,OAAOC,qBAAAA;AACT,QAAA,CAAA;AACAE,QAAAA,GAAAA,CAAAA,CACEH,WAEmD,EAAA;AAEnD,YAAA,MAAMI,eAAAA,GAAkBC,KAAAA,CAAMC,OAAO,CAACN,eAAeA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