defaults.js 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const { pascal, title, camel, param } = require('change-case')
  2. const plural = 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} apiPrefix API url path prefix. Default: /api/{param-plural}
  13. * @property {string} appPrefix APP url path prefix. Default: /{param-plural}
  14. * @property {CrudColumnOptions[]} columns
  15. */
  16. /** @define CrudColumnOptions
  17. * @property {string} titleName Field name in Title Case. This is used for field labels.
  18. * @property {string} camelName Field name in camelCase. This is used to reference data in the model.
  19. * @property {string} header HTML template for the list table header.
  20. * @property {string} cell HTML template for the list table cell
  21. * @property {string} type Field type. Can be applied to <input type="{type}" /> or used to determine a renderer.
  22. * @property {boolean} inList Default: true. Includes column in list page.
  23. */
  24. const defaults = (opts) => {
  25. opts = Object.assign({}, opts)
  26. if (!opts.pascalName) opts.pascalName = pascal(opts.titleName || opts.camelName || opts.paramName || (opts.Type && opts.Type.name) || '')
  27. if (!opts.pascalName) throw new Error('pascalName is required')
  28. if (opts.pascalName !== pascal(opts.pascalName)) throw new Error('pascalName should be PascalCased')
  29. if (!opts.pascalPlural) opts.pascalPlural = plural(opts.pascalName)
  30. if (opts.pascalPlural !== pascal(opts.pascalPlural)) throw new Error('pascalPlural should be PascalCased')
  31. if (!opts.titleName) opts.titleName = title(opts.pascalName)
  32. if (!opts.titlePlural) opts.titlePlural = plural(opts.titleName)
  33. if (!opts.camelName) opts.camelName = camel(opts.pascalName)
  34. if (opts.camelName !== camel(opts.camelName)) throw new Error('camelName should be camelCased')
  35. if (!opts.camelPlural) opts.camelPlural = plural(opts.camelName)
  36. if (opts.camelPlural !== camel(opts.camelPlural)) throw new Error('camelPlural should be camelCased')
  37. if (!opts.paramName) opts.paramName = param(opts.pascalName)
  38. if (opts.paramName !== param(opts.paramName)) throw new Error('paramName should be param-cased')
  39. if (!opts.paramPlural) opts.paramPlural = plural(opts.paramName)
  40. if (opts.paramPlural !== param(opts.paramPlural)) throw new Error('paramPlural should be param-cased')
  41. if (!opts.apiPrefix) opts.apiPrefix = `/api/${opts.paramPlural}`
  42. if (!opts.appPrefix) opts.appPrefix = `/${opts.paramPlural}`
  43. if (!opts.routeParam) opts.routeParam = `${opts.camelName}Id`
  44. if (!opts.listComponentName) opts.listComponentName = `app${opts.pascalPlural}List`
  45. if (opts.listComponentName !== camel(opts.listComponentName)) throw new Error('listComponentName should be camelCased')
  46. if (!opts.listComponentTag) opts.listComponentTag = `app-${opts.paramName}-list`
  47. if (opts.listComponentTag !== param(opts.listComponentTag)) throw new Error('listComponentTag should be param-cased')
  48. if (!opts.listPageComponentName) opts.listPageComponentName = `app${opts.pascalPlural}Page`
  49. if (opts.listPageComponentName !== camel(opts.listPageComponentName)) throw new Error('listPageComponentName should be camelCased')
  50. if (!opts.listPageComponentTag) opts.listPageComponentTag = `app-${opts.paramPlural}-page`
  51. if (opts.listPageComponentTag !== param(opts.listPageComponentTag)) throw new Error('listPageComponentTag should be param-cased')
  52. if (opts.columns) {
  53. opts.columns = opts.columns.map(col => {
  54. col = Object.assign({}, col)
  55. if (!col.camelName) col.camelName = camel(opts.titleName)
  56. if (!col.camelName) throw new Error('camelName is required')
  57. if (col.camelName !== camel(col.camelName)) throw new Error('column.camelName should be camelCased')
  58. if (!col.titleName) col.titleName = title(col.camelName)
  59. if (col.type === undefined) col.type = 'text'
  60. if (col.inList === undefined) col.inList = true
  61. return col
  62. })
  63. }
  64. return opts;
  65. }
  66. module.exports = defaults