@kaspernj/api-maker 1.0.128 → 1.0.131
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 -3
- package/src/collection.cjs +9 -11
- 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
|
@@ -416,6 +416,8 @@ module.exports = class BaseModel {
|
|
|
416
416
|
}
|
|
417
417
|
|
|
418
418
|
setNewModelData (model) {
|
|
419
|
+
if (!("modelData" in model)) throw new Error(`No modelData in model: ${JSON.stringify(model)}`)
|
|
420
|
+
|
|
419
421
|
this.previousModelData = digg(this, "modelData")
|
|
420
422
|
this.modelData = digg(model, "modelData")
|
|
421
423
|
}
|
|
@@ -531,12 +533,18 @@ module.exports = class BaseModel {
|
|
|
531
533
|
}
|
|
532
534
|
|
|
533
535
|
_refreshModelFromResponse (response) {
|
|
534
|
-
|
|
536
|
+
let newModel = digg(response, "model")
|
|
537
|
+
|
|
538
|
+
if (Array.isArray(newModel)) newModel = newModel[0]
|
|
539
|
+
|
|
535
540
|
this.setNewModel(newModel)
|
|
536
541
|
}
|
|
537
542
|
|
|
538
543
|
_refreshModelDataFromResponse (response) {
|
|
539
|
-
|
|
544
|
+
let newModel = digg(response, "model")
|
|
545
|
+
|
|
546
|
+
if (Array.isArray(newModel)) newModel = newModel[0]
|
|
547
|
+
|
|
540
548
|
this.setNewModelData(newModel)
|
|
541
549
|
}
|
|
542
550
|
|
|
@@ -669,7 +677,7 @@ module.exports = class BaseModel {
|
|
|
669
677
|
if (attributeName in attributes) return null
|
|
670
678
|
}
|
|
671
679
|
|
|
672
|
-
throw new AttributeNotLoadedError(`No such attribute: ${digg(this.modelClassData(), "name")}#${attributeName}`)
|
|
680
|
+
throw new AttributeNotLoadedError(`No such attribute: ${digg(this.modelClassData(), "name")}#${attributeName}: ${JSON.stringify(this.modelData)}`)
|
|
673
681
|
}
|
|
674
682
|
|
|
675
683
|
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 {
|
|
@@ -146,7 +145,7 @@ 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")
|
|
150
149
|
const result = new Result({collection: this, models, response})
|
|
151
150
|
return result
|
|
152
151
|
}
|
|
@@ -199,7 +198,14 @@ module.exports = class ApiMakerCollection {
|
|
|
199
198
|
|
|
200
199
|
async toArray () {
|
|
201
200
|
const response = await this._response()
|
|
202
|
-
|
|
201
|
+
const models = digg(response, "collection")
|
|
202
|
+
|
|
203
|
+
// This is needed when reloading a version of the model with the same selected attributes and preloads
|
|
204
|
+
for(const model of models) {
|
|
205
|
+
model.collection = this
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return models
|
|
203
209
|
}
|
|
204
210
|
|
|
205
211
|
modelClass () {
|
|
@@ -233,12 +239,4 @@ module.exports = class ApiMakerCollection {
|
|
|
233
239
|
{}
|
|
234
240
|
)
|
|
235
241
|
}
|
|
236
|
-
|
|
237
|
-
_responseToModels (response) {
|
|
238
|
-
const modelsResponseReader = new ModelsResponseReader({
|
|
239
|
-
collection: this,
|
|
240
|
-
response
|
|
241
|
-
})
|
|
242
|
-
return modelsResponseReader.models()
|
|
243
|
-
}
|
|
244
242
|
}
|
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 {
|