@kaspernj/api-maker 1.0.129 → 1.0.132
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 +11 -4
- package/src/collection.cjs +18 -12
- package/src/deserializer.cjs +6 -0
- package/src/devise.cjs +12 -8
- package/src/serializer.cjs +5 -0
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")
|
|
@@ -416,6 +415,8 @@ module.exports = class BaseModel {
|
|
|
416
415
|
}
|
|
417
416
|
|
|
418
417
|
setNewModelData (model) {
|
|
418
|
+
if (!("modelData" in model)) throw new Error(`No modelData in model: ${JSON.stringify(model)}`)
|
|
419
|
+
|
|
419
420
|
this.previousModelData = digg(this, "modelData")
|
|
420
421
|
this.modelData = digg(model, "modelData")
|
|
421
422
|
}
|
|
@@ -531,12 +532,18 @@ module.exports = class BaseModel {
|
|
|
531
532
|
}
|
|
532
533
|
|
|
533
534
|
_refreshModelFromResponse (response) {
|
|
534
|
-
|
|
535
|
+
let newModel = digg(response, "model")
|
|
536
|
+
|
|
537
|
+
if (Array.isArray(newModel)) newModel = newModel[0]
|
|
538
|
+
|
|
535
539
|
this.setNewModel(newModel)
|
|
536
540
|
}
|
|
537
541
|
|
|
538
542
|
_refreshModelDataFromResponse (response) {
|
|
539
|
-
|
|
543
|
+
let newModel = digg(response, "model")
|
|
544
|
+
|
|
545
|
+
if (Array.isArray(newModel)) newModel = newModel[0]
|
|
546
|
+
|
|
540
547
|
this.setNewModelData(newModel)
|
|
541
548
|
}
|
|
542
549
|
|
|
@@ -669,7 +676,7 @@ module.exports = class BaseModel {
|
|
|
669
676
|
if (attributeName in attributes) return null
|
|
670
677
|
}
|
|
671
678
|
|
|
672
|
-
throw new AttributeNotLoadedError(`No such attribute: ${digg(this.modelClassData(), "name")}#${attributeName}`)
|
|
679
|
+
throw new AttributeNotLoadedError(`No such attribute: ${digg(this.modelClassData(), "name")}#${attributeName}: ${JSON.stringify(this.modelData)}`)
|
|
673
680
|
}
|
|
674
681
|
|
|
675
682
|
isAttributeLoaded (attributeName) {
|
package/src/collection.cjs
CHANGED
|
@@ -3,7 +3,6 @@ const CommandsPool = require("./commands-pool.cjs")
|
|
|
3
3
|
const {digg} = require("diggerize")
|
|
4
4
|
const inflection = require("inflection")
|
|
5
5
|
const {merge} = require("./merge.cjs")
|
|
6
|
-
const ModelsResponseReader = require("./models-response-reader.cjs")
|
|
7
6
|
const Result = require("./result.cjs")
|
|
8
7
|
|
|
9
8
|
module.exports = class ApiMakerCollection {
|
|
@@ -38,7 +37,7 @@ module.exports = class ApiMakerCollection {
|
|
|
38
37
|
async count () {
|
|
39
38
|
const response = await this.clone()._merge({count: true})._response()
|
|
40
39
|
|
|
41
|
-
return response
|
|
40
|
+
return digg(response, "count")
|
|
42
41
|
}
|
|
43
42
|
|
|
44
43
|
distinct () {
|
|
@@ -146,8 +145,12 @@ module.exports = class ApiMakerCollection {
|
|
|
146
145
|
|
|
147
146
|
async result () {
|
|
148
147
|
const response = await this._response()
|
|
149
|
-
const models =
|
|
148
|
+
const models = digg(response, "collection")
|
|
149
|
+
|
|
150
|
+
this._addCollectionToModels(models)
|
|
151
|
+
|
|
150
152
|
const result = new Result({collection: this, models, response})
|
|
153
|
+
|
|
151
154
|
return result
|
|
152
155
|
}
|
|
153
156
|
|
|
@@ -199,7 +202,11 @@ module.exports = class ApiMakerCollection {
|
|
|
199
202
|
|
|
200
203
|
async toArray () {
|
|
201
204
|
const response = await this._response()
|
|
202
|
-
|
|
205
|
+
const models = digg(response, "collection")
|
|
206
|
+
|
|
207
|
+
this._addCollectionToModels(models)
|
|
208
|
+
|
|
209
|
+
return models
|
|
203
210
|
}
|
|
204
211
|
|
|
205
212
|
modelClass () {
|
|
@@ -214,6 +221,13 @@ module.exports = class ApiMakerCollection {
|
|
|
214
221
|
return new ApiMakerCollection(this.args, clonedQueryArgs)
|
|
215
222
|
}
|
|
216
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
|
+
|
|
217
231
|
_merge (newQueryArgs) {
|
|
218
232
|
merge(this.queryArgs, newQueryArgs)
|
|
219
233
|
|
|
@@ -233,12 +247,4 @@ module.exports = class ApiMakerCollection {
|
|
|
233
247
|
{}
|
|
234
248
|
)
|
|
235
249
|
}
|
|
236
|
-
|
|
237
|
-
_responseToModels (response) {
|
|
238
|
-
const modelsResponseReader = new ModelsResponseReader({
|
|
239
|
-
collection: this,
|
|
240
|
-
response
|
|
241
|
-
})
|
|
242
|
-
return modelsResponseReader.models()
|
|
243
|
-
}
|
|
244
250
|
}
|
package/src/deserializer.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const {digg} = require("diggerize")
|
|
2
2
|
const inflection = require("inflection")
|
|
3
|
+
const ModelsResponseReader = require("./models-response-reader.cjs")
|
|
3
4
|
const Money = require("js-money")
|
|
4
5
|
|
|
5
6
|
module.exports = class ApiMakerDeserializer {
|
|
@@ -11,6 +12,11 @@ module.exports = class ApiMakerDeserializer {
|
|
|
11
12
|
const date = new Date(digg(object, "value"))
|
|
12
13
|
|
|
13
14
|
return date
|
|
15
|
+
} else if (object.api_maker_type == "collection") {
|
|
16
|
+
// Need to remove type to avoid circular error
|
|
17
|
+
const {api_maker_type, ...restObject} = object
|
|
18
|
+
|
|
19
|
+
return ModelsResponseReader.collection(ApiMakerDeserializer.parse(restObject))
|
|
14
20
|
} else if (object.api_maker_type == "money") {
|
|
15
21
|
const cents = digg(object, "amount")
|
|
16
22
|
const currency = digg(object, "currency")
|
package/src/devise.cjs
CHANGED
|
@@ -45,20 +45,21 @@ module.exports = class ApiMakerDevise {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
static async signIn (username, password, args = {}) {
|
|
48
|
-
if (!args.scope)
|
|
49
|
-
args.scope = "user"
|
|
48
|
+
if (!args.scope) args.scope = "user"
|
|
50
49
|
|
|
51
50
|
const postData = {username, password, args}
|
|
52
51
|
const response = await Services.current().sendRequest("Devise::SignIn", postData)
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
|
|
53
|
+
let model = response.model
|
|
54
|
+
|
|
55
|
+
if (Array.isArray(model)) model = model[0]
|
|
55
56
|
|
|
56
57
|
await CanCan.current().resetAbilities()
|
|
57
58
|
|
|
58
|
-
ApiMakerDevise.updateSession(
|
|
59
|
+
ApiMakerDevise.updateSession(model)
|
|
59
60
|
ApiMakerDevise.events().emit("onDeviseSignIn", Object.assign({username}, args))
|
|
60
61
|
|
|
61
|
-
return {model
|
|
62
|
+
return {model, response}
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
static updateSession (model) {
|
|
@@ -105,10 +106,13 @@ module.exports = class ApiMakerDevise {
|
|
|
105
106
|
loadCurrentScope (scope) {
|
|
106
107
|
const scopeData = global.apiMakerDeviseCurrent[scope]
|
|
107
108
|
|
|
108
|
-
if (!scopeData)
|
|
109
|
-
return null
|
|
109
|
+
if (!scopeData) return null
|
|
110
110
|
|
|
111
111
|
const parsedScopeData = Deserializer.parse(scopeData)
|
|
112
|
+
|
|
113
|
+
// Might be a collection with preloaded relationships
|
|
114
|
+
if (Array.isArray(parsedScopeData)) return parsedScopeData[0]
|
|
115
|
+
|
|
112
116
|
const ModelClass = digg(require("@kaspernj/api-maker/src/models"), inflection.camelize(scope))
|
|
113
117
|
const modelInstance = new ModelClass({data: parsedScopeData})
|
|
114
118
|
|
package/src/serializer.cjs
CHANGED
|
@@ -38,6 +38,11 @@ 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.name == "ApiMakerCollection") {
|
|
42
|
+
return {
|
|
43
|
+
api_maker_type: "collection",
|
|
44
|
+
value: this.serializeObject(arg)
|
|
45
|
+
}
|
|
41
46
|
} else if (typeof arg == "object" && arg !== null && arg.constructor.name == "Object") {
|
|
42
47
|
return this.serializeObject(arg)
|
|
43
48
|
} else {
|