index.js 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const app = require('../../app')
  2. const { pascal, camel, param, title } = require('change-case')
  3. const plural = require('plural')
  4. const list = require('./list')
  5. const details = require('./details')
  6. const trash = require('./trash')
  7. /** @define CrudPagesOptions
  8. * @property {string} titleName Type name in Title Case. This is used for labels like "New {Title Name}"
  9. * @property {string} titlePlural Plural type name in Title Case. This is used for labels like "View All {Title Plurals}"
  10. * @property {string} pascalName Type name in PascalCase. This is used for Class definitions like "app{PascalName}Page"
  11. * @property {string} pascalPlural Plural type name in PascalCase. This is used for Class definitions like "app{PascalPlural}List"
  12. * @property {string} camelName Type name in camelCase. This is used for referencing models, like "model.{camelName}"
  13. * @property {string} camelPlural Plural type name in camelCase.
  14. * @property {string} paramName Type name in param-case.
  15. * @property {string} paramPlural Plural type name in param-case. This is used for urls, like "/api/{param-plural}"
  16. * @property {string} apiPrefix API url path prefix. Default: /api/{param-plural}
  17. * @property {string} appPrefix APP url path prefix. Default: /{param-plural}
  18. * @property {CrudColumnOptions[]} columns
  19. */
  20. /** @define CrudColumnOptions
  21. * @property {string} titleName Field name in Title Case. This is used for field labels.
  22. * @property {string} camelName Field name in camelCase. This is used to reference data in the model.
  23. * @property {string} header HTML template for the list table header.
  24. * @property {string} cell HTML template for the list table cell
  25. * @property {string} type Field type. Can be applied to <input type="{type}" /> or used to determine a renderer.
  26. * @property {boolean} inList Default: true. Includes column in list page.
  27. */
  28. /**
  29. * @param {CrudPagesOptions} opts
  30. */
  31. const crudPages = (opts) => {
  32. if (!opts.pascalName) opts.pascalName = pascal(opts.titleName || opts.camelName || opts.paramName || '')
  33. if (!opts.pascalName) throw new Error('pascalName is required')
  34. if (opts.pascalName !== pascal(opts.pascalName)) throw new Error('pascalName should be PascalCased')
  35. if (!opts.pascalPlural) opts.pascalPlural = plural(opts.pascalName)
  36. if (opts.pascalPlural !== pascal(opts.pascalPlural)) throw new Error('pascalPlural should be PascalCased')
  37. if (!opts.titleName) opts.titleName = title(opts.pascalName)
  38. if (!opts.titlePlural) opts.titlePlural = plural(opts.titleName)
  39. if (!opts.camelName) opts.camelName = camel(opts.pascalName)
  40. if (opts.camelName !== camel(opts.camelName)) throw new Error('camelName should be camelCased')
  41. if (!opts.camelPlural) opts.camelPlural = plural(opts.camelName)
  42. if (opts.camelPlural !== camel(opts.camelPlural)) throw new Error('camelPlural should be camelCased')
  43. if (!opts.paramName) opts.paramName = param(opts.pascalName)
  44. if (opts.paramName !== param(opts.paramName)) throw new Error('paramName should be param-cased')
  45. if (!opts.paramPlural) opts.paramPlural = plural(opts.paramName)
  46. if (opts.paramPlural !== param(opts.paramPlural)) throw new Error('paramPlural should be param-cased')
  47. if (!opts.apiPrefix) opts.apiPrefix = `/api/${opts.paramPlural}`
  48. if (!opts.appPrefix) opts.appPrefix = `/${opts.paramPlural}`
  49. if (!opts.listComponentName) opts.listComponentName = `app${opts.pascalPlural}List`
  50. if (opts.listComponentName !== camel(opts.listComponentName)) throw new Error('listComponentName should be camelCased')
  51. if (!opts.listComponentTag) opts.listComponentTag = `app-${opts.paramName}-list`
  52. if (opts.listComponentTag !== param(opts.listComponentTag)) throw new Error('listComponentTag should be param-cased')
  53. if (!opts.listPageComponentName) opts.listPageComponentName = `app${opts.pascalPlural}Page`
  54. if (opts.listPageComponentName !== camel(opts.listPageComponentName)) throw new Error('listPageComponentName should be camelCased')
  55. if (!opts.listPageComponentTag) opts.listPageComponentTag = `app-${opts.paramPlural}-page`
  56. if (opts.listPageComponentTag !== param(opts.listPageComponentTag)) throw new Error('listPageComponentTag should be param-cased')
  57. if (!opts.columns) throw new Error('Columns are required')
  58. opts.columns.forEach(col => {
  59. if (!col.camelName) col.camelName = camel(opts.titleName)
  60. if (!col.camelName) throw new Error('camelName is required')
  61. if (col.camelName !== camel(col.camelName)) throw new Error('column.camelName should be camelCased')
  62. if (!col.titleName) col.titleName = title(col.camelName)
  63. if (col.type === undefined) col.type = 'text'
  64. if (col.inList === undefined) col.inList = true
  65. })
  66. list(opts)
  67. details(opts)
  68. trash(opts)
  69. }
  70. // TODO: Create Read Update Delete pages...
  71. module.exports = crudPages