@kaspernj/api-maker 1.0.131 → 1.0.134
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/base-model.cjs +5 -2
- package/src/collection.cjs +18 -6
- package/src/model-load-wrapper.jsx +57 -0
- package/src/model-name.cjs +8 -0
- package/src/serializer.cjs +2 -2
package/package.json
CHANGED
package/src/base-model.cjs
CHANGED
|
@@ -6,7 +6,6 @@ const {digg} = require("diggerize")
|
|
|
6
6
|
const FormDataObjectizer = require("form-data-objectizer")
|
|
7
7
|
const inflection = require("inflection")
|
|
8
8
|
const ModelName = require("./model-name.cjs")
|
|
9
|
-
const ModelsResponseReader = require("./models-response-reader.cjs")
|
|
10
9
|
const NotLoadedError = require("./not-loaded-error.cjs")
|
|
11
10
|
const objectToFormData = require("object-to-formdata").serialize
|
|
12
11
|
const Services = require("./services.cjs")
|
|
@@ -15,7 +14,7 @@ const {ValidationErrors} = require("./validation-errors.cjs")
|
|
|
15
14
|
|
|
16
15
|
const shared = {}
|
|
17
16
|
|
|
18
|
-
|
|
17
|
+
class BaseModel {
|
|
19
18
|
static modelClassData () {
|
|
20
19
|
throw new Error("modelClassData should be overriden by child")
|
|
21
20
|
}
|
|
@@ -826,3 +825,7 @@ module.exports = class BaseModel {
|
|
|
826
825
|
}
|
|
827
826
|
}
|
|
828
827
|
}
|
|
828
|
+
|
|
829
|
+
BaseModel.apiMakerType = "BaseModel"
|
|
830
|
+
|
|
831
|
+
module.exports = BaseModel
|
package/src/collection.cjs
CHANGED
|
@@ -5,7 +5,7 @@ const inflection = require("inflection")
|
|
|
5
5
|
const {merge} = require("./merge.cjs")
|
|
6
6
|
const Result = require("./result.cjs")
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
class ApiMakerCollection {
|
|
9
9
|
constructor (args, queryArgs = {}) {
|
|
10
10
|
this.queryArgs = queryArgs
|
|
11
11
|
this.args = args
|
|
@@ -37,7 +37,7 @@ module.exports = class ApiMakerCollection {
|
|
|
37
37
|
async count () {
|
|
38
38
|
const response = await this.clone()._merge({count: true})._response()
|
|
39
39
|
|
|
40
|
-
return response
|
|
40
|
+
return digg(response, "count")
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
distinct () {
|
|
@@ -146,7 +146,11 @@ module.exports = class ApiMakerCollection {
|
|
|
146
146
|
async result () {
|
|
147
147
|
const response = await this._response()
|
|
148
148
|
const models = digg(response, "collection")
|
|
149
|
+
|
|
150
|
+
this._addCollectionToModels(models)
|
|
151
|
+
|
|
149
152
|
const result = new Result({collection: this, models, response})
|
|
153
|
+
|
|
150
154
|
return result
|
|
151
155
|
}
|
|
152
156
|
|
|
@@ -200,10 +204,7 @@ module.exports = class ApiMakerCollection {
|
|
|
200
204
|
const response = await this._response()
|
|
201
205
|
const models = digg(response, "collection")
|
|
202
206
|
|
|
203
|
-
|
|
204
|
-
for(const model of models) {
|
|
205
|
-
model.collection = this
|
|
206
|
-
}
|
|
207
|
+
this._addCollectionToModels(models)
|
|
207
208
|
|
|
208
209
|
return models
|
|
209
210
|
}
|
|
@@ -220,6 +221,13 @@ module.exports = class ApiMakerCollection {
|
|
|
220
221
|
return new ApiMakerCollection(this.args, clonedQueryArgs)
|
|
221
222
|
}
|
|
222
223
|
|
|
224
|
+
// This is needed when reloading a version of the model with the same selected attributes and preloads
|
|
225
|
+
_addCollectionToModels(models) {
|
|
226
|
+
for(const model of models) {
|
|
227
|
+
model.collection = this
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
223
231
|
_merge (newQueryArgs) {
|
|
224
232
|
merge(this.queryArgs, newQueryArgs)
|
|
225
233
|
|
|
@@ -240,3 +248,7 @@ module.exports = class ApiMakerCollection {
|
|
|
240
248
|
)
|
|
241
249
|
}
|
|
242
250
|
}
|
|
251
|
+
|
|
252
|
+
ApiMakerCollection.apiMakerType = "Collection"
|
|
253
|
+
|
|
254
|
+
module.exports = ApiMakerCollection
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import Params from "./params.cjs"
|
|
2
|
+
import React from "react"
|
|
3
|
+
|
|
4
|
+
export default (WrappedComponent, ModelClass, args = {}) => class modelLoadWrapper extends React.PureComponent {
|
|
5
|
+
camelizedLower = ModelClass.modelName().camelizedLower()
|
|
6
|
+
paramsVariableName = `${ModelClass.modelName().paramKey()}_id`
|
|
7
|
+
|
|
8
|
+
state = {
|
|
9
|
+
model: undefined,
|
|
10
|
+
modelId: this.props.match.params[this.paramsVariableName] || this.props.match.params.id
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
componentDidMount() {
|
|
14
|
+
if (args.newIfNoId && !this.getModelId()) {
|
|
15
|
+
this.loadNewModel()
|
|
16
|
+
} else if (!args.optional || this.getModelId()) {
|
|
17
|
+
this.loadExistingModel()
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
getModelId() {
|
|
22
|
+
return this.props.match.params[this.paramsVariableName] || this.props.match.params.id
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async loadExistingModel() {
|
|
26
|
+
const {modelId} = digs(this.shape, "modelId")
|
|
27
|
+
const query = await ModelClass.ransack({id_eq: modelId})
|
|
28
|
+
|
|
29
|
+
if (args.preload) query.preload(args.preload)
|
|
30
|
+
if (args.select) query.select(args.select)
|
|
31
|
+
|
|
32
|
+
const model = await query.first()
|
|
33
|
+
|
|
34
|
+
this.setState({model})
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
loadNewModel() {
|
|
38
|
+
const params = Params.parse()
|
|
39
|
+
const paramKey = ModelClass.modelName().paramKey()
|
|
40
|
+
const modelData = params[paramKey] || {}
|
|
41
|
+
const model = new ModelClass(modelData)
|
|
42
|
+
|
|
43
|
+
this.setState({model})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
render() {
|
|
47
|
+
const {model, modelId} = digs(this.state, "model", "modelId")
|
|
48
|
+
const wrappedComponentProps = {}
|
|
49
|
+
|
|
50
|
+
wrappedComponentProps[this.camelizedLower] = model
|
|
51
|
+
wrappedComponentProps[`${this.camelizedLower}Id`] = modelId
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<WrappedComponent {...wrappedComponentProps} {...this.props} />
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/model-name.cjs
CHANGED
|
@@ -3,6 +3,10 @@ module.exports = class ModelName {
|
|
|
3
3
|
this.data = data
|
|
4
4
|
}
|
|
5
5
|
|
|
6
|
+
camelizedLower() {
|
|
7
|
+
return this.data.modelClassData.camelizedLower
|
|
8
|
+
}
|
|
9
|
+
|
|
6
10
|
human (args) {
|
|
7
11
|
let argsToUse = args
|
|
8
12
|
|
|
@@ -20,4 +24,8 @@ module.exports = class ModelName {
|
|
|
20
24
|
|
|
21
25
|
return this.data.i18n.t(key)
|
|
22
26
|
}
|
|
27
|
+
|
|
28
|
+
paramKey() {
|
|
29
|
+
return this.data.modelClassData.paramKey
|
|
30
|
+
}
|
|
23
31
|
}
|
package/src/serializer.cjs
CHANGED
|
@@ -16,7 +16,7 @@ module.exports = class Serializer {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
serializeArgument (arg) {
|
|
19
|
-
if (typeof arg == "function" && arg.
|
|
19
|
+
if (typeof arg == "function" && arg.apiMakerType == "BaseModel") {
|
|
20
20
|
return {
|
|
21
21
|
api_maker_type: "resource",
|
|
22
22
|
name: digg(arg.modelClassData(), "name")
|
|
@@ -38,7 +38,7 @@ module.exports = class Serializer {
|
|
|
38
38
|
}
|
|
39
39
|
} else if (Array.isArray(arg)) {
|
|
40
40
|
return this.serializeArray(arg)
|
|
41
|
-
} else if (typeof arg == "object" && arg.constructor.
|
|
41
|
+
} else if (typeof arg == "object" && arg.constructor && arg.constructor.apiMakerType == "Collection") {
|
|
42
42
|
return {
|
|
43
43
|
api_maker_type: "collection",
|
|
44
44
|
value: this.serializeObject(arg)
|