defaults.js 5.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const { pascal, title, camel, param, constant } = require('change-case')
  2. const pluralLib = require('plural')
  3. /** @define CrudOptions
  4. * @property {string} titleName Type name in Title Case. This is used for labels like "New {Title Name}"
  5. * @property {string} titlePlural Plural type name in Title Case. This is used for labels like "View All {Title Plurals}"
  6. * @property {string} pascalName Type name in PascalCase. This is used for Class definitions like "app{PascalName}Page"
  7. * @property {string} pascalPlural Plural type name in PascalCase. This is used for Class definitions like "app{PascalPlural}List"
  8. * @property {string} camelName Type name in camelCase. This is used for referencing models, like "model.{camelName}"
  9. * @property {string} camelPlural Plural type name in camelCase.
  10. * @property {string} paramName Type name in param-case.
  11. * @property {string} paramPlural Plural type name in param-case. This is used for urls, like "/api/{param-plural}"
  12. * @property {string} constantName Type name in CONSTANT_CASE. This is used for permissions like CONSTANT_NAME_READ.
  13. * @property {string} constantPlural Plural type name in CONSTANT_CASE.
  14. * @property {string} apiPrefix API url path prefix. Default: /api/{param-plural}
  15. * @property {string} appPrefix APP url path prefix. Default: /{param-plural}
  16. * @property {CrudColumnOptions[]} columns
  17. */
  18. /** @define CrudColumnOptions
  19. * @property {string} titleName Field name in Title Case. This is used for field labels.
  20. * @property {string} camelName Field name in camelCase. This is used to reference data in the model.
  21. * @property {string} header HTML template for the list table header.
  22. * @property {string} cell HTML template for the list table cell
  23. * @property {string} type Field type. Can be applied to <input type="{type}" /> or used to determine a renderer.
  24. * @property {boolean} inList Default: true. Includes column in list page.
  25. */
  26. const plural = text => {
  27. const words = title(text).split(' ')
  28. words[words.length - 1] = pluralLib(words[words.length - 1].toLowerCase())
  29. return words.join(' ')
  30. }
  31. const defaults = (opts) => {
  32. opts = Object.assign({}, opts)
  33. if (!opts.pascalName) opts.pascalName = pascal(opts.titleName || opts.camelName || opts.paramName || (opts.Type && opts.Type.name) || '')
  34. if (!opts.pascalName) throw new Error('pascalName is required')
  35. if (opts.pascalName !== pascal(opts.pascalName)) throw new Error('pascalName should be PascalCased')
  36. if (!opts.pascalPlural) opts.pascalPlural = pascal(opts.titlePlural || opts.camelPlural || opts.paramPlural || plural(opts.pascalName))
  37. if (opts.pascalPlural !== pascal(opts.pascalPlural)) throw new Error('pascalPlural should be PascalCased')
  38. if (!opts.titleName) opts.titleName = title(opts.pascalName)
  39. if (!opts.titlePlural) opts.titlePlural = title(opts.pascalPlural || plural(opts.titleName))
  40. if (!opts.camelName) opts.camelName = camel(opts.pascalName)
  41. if (opts.camelName !== camel(opts.camelName)) throw new Error('camelName should be camelCased')
  42. if (!opts.camelPlural) opts.camelPlural = camel(opts.pascalPlural || plural(opts.camelName))
  43. if (opts.camelPlural !== camel(opts.camelPlural)) throw new Error('camelPlural should be camelCased')
  44. if (!opts.paramName) opts.paramName = param(opts.pascalName)
  45. if (opts.paramName !== param(opts.paramName)) throw new Error('paramName should be param-cased')
  46. if (!opts.paramPlural) opts.paramPlural = param(opts.pascalPlural || plural(opts.paramName))
  47. if (opts.paramPlural !== param(opts.paramPlural)) throw new Error('paramPlural should be param-cased')
  48. if (!opts.constantName) opts.constantName = constant(opts.pascalName)
  49. if (opts.constantName !== constant(opts.constantName)) throw new Error('constantName should be constant-cased')
  50. if (!opts.constantPlural) opts.constantPlural = constant(opts.pascalPlural || plural(opts.constantName))
  51. if (opts.constantPlural !== constant(opts.constantPlural)) throw new Error('constantPlural should be constant-cased')
  52. if (!opts.apiPrefix) opts.apiPrefix = `/api/${opts.paramPlural}`
  53. if (!opts.appPrefix) opts.appPrefix = `/${opts.paramPlural}`
  54. if (!opts.routeParam) opts.routeParam = opts.camelName
  55. if (!opts.listComponentName) opts.listComponentName = `app${opts.pascalPlural}List`
  56. if (opts.listComponentName !== camel(opts.listComponentName)) throw new Error('listComponentName should be camelCased')
  57. if (!opts.listComponentTag) opts.listComponentTag = `app-${opts.paramName}-list`
  58. if (opts.listComponentTag !== param(opts.listComponentTag)) throw new Error('listComponentTag should be param-cased')
  59. if (!opts.listPageComponentName) opts.listPageComponentName = `app${opts.pascalPlural}Page`
  60. if (opts.listPageComponentName !== camel(opts.listPageComponentName)) throw new Error('listPageComponentName should be camelCased')
  61. if (!opts.listPageComponentTag) opts.listPageComponentTag = `app-${opts.paramPlural}-page`
  62. if (opts.listPageComponentTag !== param(opts.listPageComponentTag)) throw new Error('listPageComponentTag should be param-cased')
  63. if (!opts.camelAll) opts.camelAll = camel(opts.paramAll || opts.titleAll || 'all')
  64. if (!opts.paramAll) opts.paramAll = param(opts.camelAll)
  65. if (!opts.titleAll) opts.titleAll = title(opts.camelAll)
  66. if (!opts.camelNew) opts.camelNew = camel(opts.paramNew || opts.titleNew || 'new')
  67. if (!opts.paramNew) opts.paramNew = param(opts.camelNew)
  68. if (!opts.titleNew) opts.titleNew = title(opts.camelNew)
  69. if (opts.columns) {
  70. opts.columns = opts.columns.map(col => {
  71. col = Object.assign({}, col)
  72. if (!col.camelName) col.camelName = camel(opts.titleName)
  73. if (!col.camelName) throw new Error('camelName is required')
  74. if (col.camelName !== camel(col.camelName)) throw new Error('column.camelName should be camelCased')
  75. if (!col.titleName) col.titleName = title(col.camelName)
  76. if (col.type === undefined) col.type = 'text'
  77. if (col.inList === undefined) col.inList = true
  78. return col
  79. })
  80. }
  81. return opts;
  82. }
  83. module.exports = defaults