api_maker 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. checksums.yaml +4 -4
  2. data/app/api_maker/api_helpers/api_maker_helpers.rb +5 -0
  3. data/app/api_maker/services/can_can/load_abilities.rb +30 -0
  4. data/app/api_maker/services/devise/sign_in.rb +64 -0
  5. data/app/api_maker/services/devise/sign_out.rb +9 -0
  6. data/app/api_maker/services/models/find_or_create_by.rb +18 -0
  7. data/app/channels/api_maker/subscriptions_channel.rb +33 -2
  8. data/app/controllers/api_maker/base_controller.rb +7 -3
  9. data/app/controllers/api_maker/commands_controller.rb +26 -4
  10. data/app/controllers/api_maker/session_statuses_controller.rb +1 -1
  11. data/app/services/api_maker/abilities_loader.rb +104 -0
  12. data/app/services/api_maker/application_service.rb +2 -1
  13. data/app/services/api_maker/base_command.rb +248 -0
  14. data/app/services/api_maker/collection_command_service.rb +29 -15
  15. data/app/services/api_maker/collection_loader.rb +124 -0
  16. data/app/services/api_maker/command_failed_error.rb +3 -0
  17. data/app/services/api_maker/command_response.rb +17 -6
  18. data/app/services/api_maker/command_service.rb +3 -3
  19. data/app/services/api_maker/create_command.rb +11 -26
  20. data/app/services/api_maker/create_command_service.rb +3 -3
  21. data/app/services/api_maker/database_type.rb +9 -0
  22. data/app/services/api_maker/deep_merge_params.rb +26 -0
  23. data/app/services/api_maker/deserializer.rb +35 -0
  24. data/app/services/api_maker/destroy_command.rb +15 -21
  25. data/app/services/api_maker/destroy_command_service.rb +3 -3
  26. data/app/services/api_maker/generate_react_native_api_service.rb +3 -19
  27. data/app/services/api_maker/include_helpers.rb +17 -0
  28. data/app/services/api_maker/index_command.rb +8 -88
  29. data/app/services/api_maker/index_command_service.rb +5 -5
  30. data/app/services/api_maker/js_method_namer_service.rb +1 -1
  31. data/app/services/api_maker/locals_from_controller.rb +14 -0
  32. data/app/services/api_maker/member_command_service.rb +15 -13
  33. data/app/services/api_maker/model_classes_java_script_generator_service.rb +37 -0
  34. data/app/services/api_maker/model_content_generator_service.rb +17 -21
  35. data/app/services/api_maker/models/save.rb +29 -0
  36. data/app/services/api_maker/models_finder_service.rb +6 -2
  37. data/app/services/api_maker/models_generator_service.rb +6 -43
  38. data/app/services/api_maker/move_components_to_routes.rb +50 -0
  39. data/app/services/api_maker/primary_id_for_model.rb +6 -0
  40. data/app/services/api_maker/reset_indexed_db_service.rb +36 -0
  41. data/app/services/api_maker/routes_file_reloader.rb +20 -0
  42. data/app/services/api_maker/select_columns_on_collection.rb +78 -0
  43. data/app/services/api_maker/select_parser.rb +32 -0
  44. data/app/services/api_maker/service_command.rb +27 -0
  45. data/app/services/api_maker/service_command_service.rb +14 -0
  46. data/app/services/api_maker/simple_model_errors.rb +52 -0
  47. data/app/services/api_maker/update_command.rb +8 -24
  48. data/app/services/api_maker/update_command_service.rb +3 -3
  49. data/app/services/api_maker/valid_command.rb +4 -13
  50. data/app/services/api_maker/valid_command_service.rb +3 -3
  51. data/app/services/api_maker/validation_errors_generator_service.rb +146 -0
  52. data/app/views/api_maker/_data.html.erb +17 -11
  53. data/config/routes.rb +0 -2
  54. data/lib/api_maker/ability.rb +22 -7
  55. data/lib/api_maker/ability_loader.rb +9 -6
  56. data/lib/api_maker/base_collection_instance.rb +15 -0
  57. data/lib/api_maker/base_resource.rb +135 -9
  58. data/lib/api_maker/base_service.rb +14 -0
  59. data/lib/api_maker/collection_serializer.rb +95 -34
  60. data/lib/api_maker/command_spec_helper.rb +41 -11
  61. data/lib/api_maker/configuration.rb +31 -4
  62. data/lib/api_maker/expect_to_able_to_helper.rb +31 -0
  63. data/lib/api_maker/individual_command.rb +24 -9
  64. data/lib/api_maker/javascript/model-template.js.erb +39 -25
  65. data/lib/api_maker/javascript/models.js.erb +6 -0
  66. data/lib/api_maker/loader.rb +1 -1
  67. data/lib/api_maker/memory_storage.rb +1 -1
  68. data/lib/api_maker/model_extensions.rb +34 -18
  69. data/lib/api_maker/permitted_params_argument.rb +5 -1
  70. data/lib/api_maker/preloader.rb +71 -32
  71. data/lib/api_maker/preloader_base.rb +108 -0
  72. data/lib/api_maker/preloader_belongs_to.rb +34 -33
  73. data/lib/api_maker/preloader_has_many.rb +45 -39
  74. data/lib/api_maker/preloader_has_one.rb +30 -47
  75. data/lib/api_maker/railtie.rb +3 -11
  76. data/lib/api_maker/relationship_preloader.rb +42 -0
  77. data/lib/api_maker/resource_routing.rb +18 -4
  78. data/lib/api_maker/result_parser.rb +34 -20
  79. data/lib/api_maker/serializer.rb +53 -22
  80. data/lib/api_maker/spec_helper/browser_logs.rb +14 -0
  81. data/lib/api_maker/spec_helper/execute_collection_command.rb +46 -0
  82. data/lib/api_maker/spec_helper/execute_member_command.rb +52 -0
  83. data/lib/api_maker/spec_helper/expect_no_browser_errors.rb +18 -0
  84. data/lib/api_maker/spec_helper/wait_for_expect.rb +20 -0
  85. data/lib/api_maker/spec_helper/wait_for_flash_message.rb +21 -0
  86. data/lib/api_maker/spec_helper.rb +112 -48
  87. data/lib/api_maker/version.rb +1 -1
  88. data/lib/api_maker.rb +7 -3
  89. metadata +108 -89
  90. data/README.md +0 -476
  91. data/app/controllers/api_maker/devise_controller.rb +0 -60
  92. data/lib/api_maker/base_command.rb +0 -81
  93. data/lib/api_maker/javascript/api.js +0 -92
  94. data/lib/api_maker/javascript/base-model.js +0 -543
  95. data/lib/api_maker/javascript/bootstrap/attribute-row.jsx +0 -16
  96. data/lib/api_maker/javascript/bootstrap/attribute-rows.jsx +0 -47
  97. data/lib/api_maker/javascript/bootstrap/card.jsx +0 -79
  98. data/lib/api_maker/javascript/bootstrap/checkbox.jsx +0 -127
  99. data/lib/api_maker/javascript/bootstrap/checkboxes.jsx +0 -105
  100. data/lib/api_maker/javascript/bootstrap/live-table.jsx +0 -168
  101. data/lib/api_maker/javascript/bootstrap/money-input.jsx +0 -136
  102. data/lib/api_maker/javascript/bootstrap/radio-buttons.jsx +0 -80
  103. data/lib/api_maker/javascript/bootstrap/select.jsx +0 -168
  104. data/lib/api_maker/javascript/bootstrap/string-input.jsx +0 -203
  105. data/lib/api_maker/javascript/cable-connection-pool.js +0 -169
  106. data/lib/api_maker/javascript/cable-subscription-pool.js +0 -111
  107. data/lib/api_maker/javascript/cable-subscription.js +0 -33
  108. data/lib/api_maker/javascript/collection.js +0 -186
  109. data/lib/api_maker/javascript/commands-pool.js +0 -123
  110. data/lib/api_maker/javascript/custom-error.js +0 -14
  111. data/lib/api_maker/javascript/deserializer.js +0 -35
  112. data/lib/api_maker/javascript/devise.js.erb +0 -113
  113. data/lib/api_maker/javascript/error-logger.js +0 -119
  114. data/lib/api_maker/javascript/event-connection.jsx +0 -24
  115. data/lib/api_maker/javascript/event-created.jsx +0 -26
  116. data/lib/api_maker/javascript/event-destroyed.jsx +0 -26
  117. data/lib/api_maker/javascript/event-emitter-listener.jsx +0 -32
  118. data/lib/api_maker/javascript/event-listener.jsx +0 -41
  119. data/lib/api_maker/javascript/event-updated.jsx +0 -26
  120. data/lib/api_maker/javascript/form-data-to-object.js +0 -70
  121. data/lib/api_maker/javascript/included.js +0 -39
  122. data/lib/api_maker/javascript/key-value-store.js +0 -47
  123. data/lib/api_maker/javascript/logger.js +0 -23
  124. data/lib/api_maker/javascript/model-name.js +0 -21
  125. data/lib/api_maker/javascript/models-response-reader.js +0 -43
  126. data/lib/api_maker/javascript/paginate.jsx +0 -128
  127. data/lib/api_maker/javascript/params.js +0 -68
  128. data/lib/api_maker/javascript/resource-route.jsx +0 -75
  129. data/lib/api_maker/javascript/resource-routes.jsx +0 -36
  130. data/lib/api_maker/javascript/result.js +0 -25
  131. data/lib/api_maker/javascript/session-status-updater.js +0 -113
  132. data/lib/api_maker/javascript/sort-link.jsx +0 -88
  133. data/lib/api_maker/javascript/updated-attribute.jsx +0 -60
  134. data/lib/api_maker/preloader_through.rb +0 -101
  135. data/lib/api_maker/relationship_includer.rb +0 -42
@@ -1,41 +0,0 @@
1
- import PropTypes from "prop-types"
2
- import PropTypesExact from "prop-types-exact"
3
- import React from "react"
4
-
5
- export default class ApiMakerEventListener extends React.Component {
6
- static callEvent(target, eventName, args = []) {
7
- let event = document.createEvent("Event")
8
- event.initEvent(eventName, false, true)
9
- target.dispatchEvent(event, args)
10
- }
11
-
12
- static propTypes = PropTypesExact({
13
- event: PropTypes.string.isRequired,
14
- onCalled: PropTypes.func.isRequired,
15
- target: PropTypes.object
16
- })
17
-
18
- constructor(props) {
19
- super(props)
20
- this.onCalled = this.onCalled.bind(this)
21
-
22
- if (!this.props.target)
23
- throw new Error("'target' was expected but not given")
24
- }
25
-
26
- componentDidMount() {
27
- this.props.target.addEventListener(this.props.event, this.onCalled)
28
- }
29
-
30
- componentWillUnmount() {
31
- this.props.target.removeEventListener(this.props.event, this.onCalled)
32
- }
33
-
34
- onCalled(...args) {
35
- this.props.onCalled.apply(null, args)
36
- }
37
-
38
- render() {
39
- return ""
40
- }
41
- }
@@ -1,26 +0,0 @@
1
- import PropTypes from "prop-types"
2
- import PropTypesExact from "prop-types-exact"
3
- import React from "react"
4
-
5
- export default class ApiMakerEventUpdated extends React.Component {
6
- static propTypes = PropTypesExact({
7
- model: PropTypes.object.isRequired,
8
- onUpdated: PropTypes.func.isRequired
9
- })
10
-
11
- componentDidMount() {
12
- this.connect()
13
- }
14
-
15
- componentWillUnmount() {
16
- this.connectUpdated.unsubscribe()
17
- }
18
-
19
- connect() {
20
- this.connectUpdated = this.props.model.connectUpdated(this.props.onUpdated)
21
- }
22
-
23
- render() {
24
- return ""
25
- }
26
- }
@@ -1,70 +0,0 @@
1
- export default class FormDataToObject {
2
- static toObject(formData) {
3
- var formDataToObject = new FormDataToObject(formData)
4
- return formDataToObject.toObject()
5
- }
6
-
7
- constructor(formData) {
8
- this.formData = formData
9
- }
10
-
11
- toObject() {
12
- var result = {}
13
-
14
- for(var entry of this.formData.entries()) {
15
- var key = entry[0]
16
- var value = entry[1]
17
-
18
- this.treatInitial(key, value, result)
19
- }
20
-
21
- return result
22
- }
23
-
24
- treatInitial(key, value, result) {
25
- var firstMatch = key.match(/^(.+?)(\[([\s\S]+$))/)
26
-
27
- if (firstMatch) {
28
- var inputName = firstMatch[1]
29
- var rest = firstMatch[2]
30
-
31
- if (inputName in result) {
32
- var newResult = result[inputName]
33
- } else if (rest == "[]") {
34
- var newResult = []
35
- result[inputName] = newResult
36
- } else {
37
- var newResult = {}
38
- result[inputName] = newResult
39
- }
40
-
41
- this.treatSecond(value, rest, newResult)
42
- } else {
43
- result[key] = value
44
- }
45
- }
46
-
47
- treatSecond(value, rest, result) {
48
- var secondMatch = rest.match(/^\[(.*?)\]([\s\S]*)$/)
49
- var key = secondMatch[1]
50
- var newRest = secondMatch[2]
51
-
52
- if (rest == "[]") {
53
- result.push(value)
54
- } else if (newRest == "") {
55
- result[key] = value
56
- } else {
57
- if (key in result) {
58
- var newResult = result[key]
59
- } else if (newRest == "[]") {
60
- var newResult = []
61
- result[key] = newResult
62
- } else {
63
- var newResult = {}
64
- result[key] = newResult
65
- }
66
-
67
- this.treatSecond(value, newRest, newResult)
68
- }
69
- }
70
- }
@@ -1,39 +0,0 @@
1
- const inflection = require("inflection")
2
-
3
- export default class ApiMakerIncluded {
4
- constructor(response) {
5
- this.response = response
6
- this.loadIncludedModels()
7
- }
8
-
9
- loadIncludedModels() {
10
- this.included = {}
11
-
12
- for(var includedType in this.response.included) {
13
- for(var includedId in this.response.included[includedType]) {
14
- var includedData = this.response.included[includedType][includedId]
15
- var modelClassName = inflection.dasherize(inflection.singularize(includedType))
16
- var modelClass = require(`api-maker/models/${modelClassName}`).default
17
- var model = new modelClass({data: includedData, isNewRecord: false})
18
-
19
- if (!this.included[includedType])
20
- this.included[includedType] = {}
21
-
22
- this.included[includedType][includedId] = model
23
- }
24
- }
25
-
26
- for(var modelType in this.included) {
27
- for(var modelId in this.included[modelType]) {
28
- this.included[modelType][modelId]._readIncludedRelationships(this)
29
- }
30
- }
31
- }
32
-
33
- getModel(type, id) {
34
- if (!this.included[type] || !this.included[type][id])
35
- throw new Error(`Could not find a ${type} by that ID: ${id}`)
36
-
37
- return this.included[type][id]
38
- }
39
- }
@@ -1,47 +0,0 @@
1
- import Dexie from "dexie"
2
-
3
- export default class KeyValueStore {
4
- static current() {
5
- if (!window.currentKeyValueStore)
6
- window.currentKeyValueStore = new KeyValueStore()
7
-
8
- return window.currentKeyValueStore
9
- }
10
-
11
- static async get(key) {
12
- return await KeyValueStore.current().get(key)
13
- }
14
-
15
- static async set(key, value) {
16
- return await KeyValueStore.current().set(key, value)
17
- }
18
-
19
- constructor() {
20
- this.database = new Dexie("KeyValueStoreDatabase")
21
- this.database.version(1).stores({
22
- keyValues: "++id, key, value"
23
- })
24
- }
25
-
26
- async get(key) {
27
- var row = await this.database.keyValues.where("key").equals(key).first()
28
-
29
- if (row)
30
- return row.value
31
- }
32
-
33
- async set(key, value) {
34
- var row = await this.database.keyValues.where("key").equals(key).first()
35
-
36
- if (row) {
37
- await this.database.keyValues.update(row.id, {value: value})
38
- } else {
39
- await this.database.keyValues.add({
40
- key: key,
41
- value: value
42
- })
43
- }
44
-
45
- return true
46
- }
47
- }
@@ -1,23 +0,0 @@
1
- export default class ApiMakerLogger {
2
- static current() {
3
- if (!window.apiMakerLogger) {
4
- window.apiMakerLogger = new ApiMakerLogger()
5
- // window.apiMakerLogger.setDebug(true)
6
- }
7
-
8
- return window.apiMakerLogger
9
- }
10
-
11
- static log(message) {
12
- ApiMakerLogger.current().log(message)
13
- }
14
-
15
- log(message) {
16
- if (this.debug)
17
- console.log("ApiMaker", message)
18
- }
19
-
20
- setDebug(value) {
21
- this.debug = value
22
- }
23
- }
@@ -1,21 +0,0 @@
1
- export default class ModelName {
2
- constructor(data) {
3
- this.data = data
4
- }
5
-
6
- human(args) {
7
- if (!args)
8
- args = {count: 1}
9
-
10
- var countKey
11
-
12
- if (args.count > 1 || args.count < 0) {
13
- countKey = "other"
14
- } else {
15
- countKey = "one"
16
- }
17
-
18
- var key = `activerecord.models.${this.data.modelClassData.i18nKey}.${countKey}`
19
- return I18n.t(key)
20
- }
21
- }
@@ -1,43 +0,0 @@
1
- import Included from "./included"
2
-
3
- const inflection = require("inflection")
4
-
5
- export default class ModelsResponseReader {
6
- static first(response) {
7
- return ModelsResponseReader.collection(response)[0]
8
- }
9
-
10
- static collection(response) {
11
- var reader = new ModelsResponseReader({response: response})
12
- return reader.models()
13
- }
14
-
15
- constructor(args) {
16
- this.response = args.response
17
- }
18
-
19
- models() {
20
- var included = new Included(this.response)
21
- var models = []
22
-
23
- for(var modelType in this.response.data) {
24
- var modelClassName = inflection.singularize(modelType)
25
- var modelClass = require(`api-maker/models/${modelClassName}`).default
26
- var collectionName = inflection.dasherize(modelClass.modelClassData().collectionName)
27
-
28
- for(var modelId of this.response.data[modelType]) {
29
- var modelData = this.response.included[collectionName][modelId]
30
-
31
- if (!modelData)
32
- throw new Error(`Couldn't find model data for ${collectionName}(${modelId})`)
33
-
34
- var model = new modelClass({data: modelData, isNewRecord: false, response: this.response})
35
-
36
- model._readIncludedRelationships(included)
37
- models.push(model)
38
- }
39
- }
40
-
41
- return models
42
- }
43
- }
@@ -1,128 +0,0 @@
1
- import { Link } from "react-router-dom"
2
- import qs from "qs"
3
- import React from "react"
4
-
5
- export default class extends React.Component {
6
- isPageActiveClass(pageNumber) {
7
- if (this.props.result.currentPage() == pageNumber)
8
- return "active"
9
- }
10
-
11
- pages() {
12
- var currentPage = this.props.result.currentPage()
13
- var pages = []
14
- var totalPages = this.props.result.totalPages()
15
- var pagesFrom = currentPage - 5
16
- var pagesTo = currentPage + 5
17
-
18
- if (pagesFrom < 1)
19
- pagesFrom = 1
20
-
21
- if (pagesTo > totalPages)
22
- pagesTo = totalPages
23
-
24
- for(var i = pagesFrom; i <= pagesTo; i++) {
25
- pages.push(i)
26
- }
27
-
28
- return pages
29
- }
30
-
31
- pagePath(pageNumber) {
32
- var pageKey = this.props.result.data.collection.queryArgs.pageKey
33
- if (!pageKey)
34
- pageKey = "page"
35
-
36
- var currentParams = qs.parse(window.location.search.substr(1))
37
- currentParams[pageKey] = pageNumber
38
- var newParams = qs.stringify(currentParams)
39
- var newPath = `${location.pathname}?${newParams}`
40
-
41
- return newPath
42
- }
43
-
44
- previousPagePath() {
45
- var previousPage
46
-
47
- if (this.props.result.currentPage() > 1) {
48
- previousPage = this.props.result.currentPage() - 1
49
- } else {
50
- previousPage = this.props.result.currentPage()
51
- }
52
-
53
- return this.pagePath(previousPage)
54
- }
55
-
56
- nextPagePath() {
57
- var nextPage
58
-
59
- if (this.props.result.currentPage() < this.props.result.totalPages()) {
60
- nextPage = this.props.result.currentPage() + 1
61
- } else {
62
- nextPage = this.props.result.currentPage()
63
- }
64
-
65
- return this.pagePath(nextPage)
66
- }
67
-
68
- showBackwardsDots() {
69
- var currentPage = this.props.result.currentPage()
70
-
71
- return (currentPage - 5 > 1)
72
- }
73
-
74
- showForwardsDots() {
75
- var currentPage = this.props.result.currentPage()
76
- var totalPages = this.props.result.totalPages()
77
-
78
- return (currentPage + 5 < totalPages)
79
- }
80
-
81
- render() {
82
- return (
83
- <ul className="pagination">
84
- <li className={`page-item ${this.props.result.currentPage() <= 1 ? "disabled" : ""}`} key="page-first">
85
- <Link className="page-link" to={this.pagePath(1)}>
86
-
87
- </Link>
88
- </li>
89
- <li className={`page-item ${this.props.result.currentPage() <= 1 ? "disabled" : ""}`} key="page-previous">
90
- <Link className="page-link" to={this.previousPagePath()}>
91
-
92
- </Link>
93
- </li>
94
- {this.showBackwardsDots() &&
95
- <li className="page-item">
96
- <a className="page-link disabled" href="#">
97
- &hellip;
98
- </a>
99
- </li>
100
- }
101
- {this.pages().map(page =>
102
- <li className={`page-item ${this.isPageActiveClass(page)}`} key={`page-${page}`}>
103
- <Link className="page-link" to={this.pagePath(page)}>
104
- {page}
105
- </Link>
106
- </li>
107
- )}
108
- {this.showForwardsDots() &&
109
- <li className="page-item">
110
- <a className="page-link disabled" href="#">
111
- &hellip;
112
- </a>
113
- </li>
114
- }
115
- <li className={`page-item ${this.props.result.currentPage() >= this.props.result.totalPages() ? "disabled" : ""}`} key="page-next">
116
- <Link className="page-link" to={this.nextPagePath()}>
117
-
118
- </Link>
119
- </li>
120
- <li className={`page-item ${this.props.result.currentPage() >= this.props.result.totalPages() ? "disabled" : ""}`} key="page-last">
121
- <Link className="page-link" to={this.pagePath(this.props.result.totalPages())}>
122
-
123
- </Link>
124
- </li>
125
- </ul>
126
- )
127
- }
128
- }
@@ -1,68 +0,0 @@
1
- import formSerialize from "form-serialize"
2
- import KeyValueStore from "api-maker/key-value-store"
3
- import merge from "merge"
4
- import qs from "qs"
5
-
6
- export default class Params {
7
- static parse() {
8
- return qs.parse(window.location.search.substr(1))
9
- }
10
-
11
- static change(given) {
12
- return merge.recursive(true, Params.parse(), given)
13
- }
14
-
15
- static changeParams(given) {
16
- var params = Params.change(given)
17
- var newParams = qs.stringify(params)
18
- var newPath = `${location.pathname}?${newParams}`
19
-
20
- AppHistory.push(newPath)
21
- }
22
-
23
- static async getCachedParams(paramName, args = {}) {
24
- var oldQuery = await KeyValueStore.get(paramName)
25
- var params = Params.parse()
26
-
27
- if (params && paramName in params) {
28
- return params[paramName]
29
- } else if (oldQuery) {
30
- return oldQuery
31
- } else {
32
- return args.default || {}
33
- }
34
- }
35
-
36
- static async setCachedParams(paramName, qParams) {
37
- return await KeyValueStore.set(paramName, qParams)
38
- }
39
-
40
- static serializeForm(form) {
41
- var hash = formSerialize(form, {empty: true, hash: true})
42
- return Params.setUndefined(hash)
43
- }
44
-
45
- // This is used to set all empty values to 'undefined' which makes qs removed those elements from the query string
46
- static setUndefined(given) {
47
- if (Array.isArray(given)) {
48
- if (given.length == 0)
49
- return undefined
50
-
51
- return given.map(givenI => Params.setUndefined(givenI))
52
- } else if (typeof given === "object") {
53
- if (Object.keys(given).length == 0)
54
- return undefined
55
-
56
- var newGiven = {}
57
- for(var key in given) {
58
- newGiven[key] = Params.setUndefined(given[key])
59
- }
60
-
61
- return newGiven
62
- } else if (given === "") {
63
- return undefined
64
- } else {
65
- return given
66
- }
67
- }
68
- }
@@ -1,75 +0,0 @@
1
- const inflection = require("inflection")
2
-
3
- export default class ResourceRoute {
4
- constructor(args) {
5
- this.args = args.args
6
- this.parsedContext = args.parsedContext
7
- this.route = args.route
8
- }
9
-
10
- routes() {
11
- if (this.args.locales) {
12
- return this.withLocale()
13
- } else {
14
- return this.withoutLocale()
15
- }
16
- }
17
-
18
- findRouteParams(route) {
19
- var result = []
20
- var parts = route.path.split("/")
21
-
22
- for(var part of parts) {
23
- if (part.match(/^:([a-z_]+)$/))
24
- result.push(part)
25
- }
26
-
27
- return result
28
- }
29
-
30
- requireComponent() {
31
- var requireResult = this.parsedContext[`components/${this.route.component}.jsx`]
32
-
33
- if (!requireResult)
34
- var requireResult = this.parsedContext[`components/${this.route.component}/index.jsx`]
35
-
36
- if (!requireResult)
37
- throw new Error(`No such require: ${this.route.component}`)
38
-
39
- return requireResult.default
40
- }
41
-
42
- withLocale() {
43
- var component = this.requireComponent()
44
- var Locales = require("shared/locales").default
45
- var Path = require("shared/path").default
46
- var routes = []
47
-
48
- for(var locale of Locales.availableLocales()) {
49
- var path = Path.localized(inflection.camelize(this.route.name, true), this.findRouteParams(this.route), {locale: locale})
50
-
51
- routes.push({
52
- path: path,
53
- component: component
54
- })
55
- }
56
-
57
- return routes
58
- }
59
-
60
- withoutLocale() {
61
- var routePathName = inflection.camelize(this.route.name, true)
62
- var routePathMethod = Routes[`${routePathName}Path`]
63
-
64
- if (!routePathMethod)
65
- throw new Error(`No such route could be found: ${routePathName}`)
66
-
67
- var path = routePathMethod.apply(null, this.findRouteParams(this.route))
68
- var component = this.requireComponent()
69
-
70
- return [{
71
- path: path,
72
- component: component
73
- }]
74
- }
75
- }
@@ -1,36 +0,0 @@
1
- import React from "react" // This fixes an issue with the Baristo project where it needed it to be loaded
2
- import ResourceRoute from "./resource-route"
3
- import { Route } from "react-router-dom"
4
-
5
- export default class ApiMakerResourceRoutes {
6
- static readRoutes(args = {}) {
7
- if (!args.routes)
8
- throw new Error("Please pass 'routes' to this method")
9
-
10
- var parsedContext = ApiMakerResourceRoutes.parseContext(args)
11
- var routesJson = args.routes
12
- var routes = []
13
-
14
- for(var route of routesJson.routes) {
15
- var resourceRoute = new ResourceRoute({args, parsedContext, route})
16
-
17
- for(var newRoute of resourceRoute.routes()) {
18
- routes.push(
19
- <Route exact key={`route-${newRoute.path}`} path={newRoute.path} component={newRoute.component} />
20
- )
21
- }
22
- }
23
-
24
- return routes
25
- }
26
-
27
- static parseContext(args) {
28
- var result = {}
29
- args.context.keys().forEach(key => {
30
- var newKey = `${args.path}/${key.substring(2, key.length)}`
31
- result[newKey] = args.context(key)
32
- })
33
-
34
- return result
35
- }
36
- }
@@ -1,25 +0,0 @@
1
- export default class Result {
2
- constructor(data) {
3
- this.data = data
4
- }
5
-
6
- currentPage() {
7
- return this.data.response.meta.currentPage
8
- }
9
-
10
- models() {
11
- return this.data.models
12
- }
13
-
14
- modelClass() {
15
- return this.data.collection.modelClass()
16
- }
17
-
18
- totalCount() {
19
- return this.data.response.meta.totalCount
20
- }
21
-
22
- totalPages() {
23
- return this.data.response.meta.totalPages
24
- }
25
- }