@kaspernj/api-maker 1.0.126 → 1.0.127
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/.eslintrc.js +33 -30
- package/__tests__/cable-connection-pool.test.js +4 -4
- package/__tests__/custom-error.test.js +13 -0
- package/__tests__/support/task.js +5 -5
- package/__tests__/support/user.js +5 -5
- package/package.json +5 -4
- package/src/api.cjs +15 -15
- package/src/base-model.cjs +82 -81
- package/src/cable-connection-pool.cjs +11 -11
- package/src/cable-subscription-pool.cjs +34 -34
- package/src/cable-subscription.cjs +2 -2
- package/src/can-can-loader.jsx +7 -7
- package/src/can-can.cjs +17 -16
- package/src/collection.cjs +36 -36
- package/src/command-submit-data.cjs +10 -11
- package/src/commands-pool.cjs +13 -13
- package/src/custom-error.cjs +20 -14
- package/src/deserializer.cjs +7 -7
- package/src/devise.cjs +15 -15
- package/src/error-logger.cjs +9 -9
- package/src/event-connection.jsx +6 -6
- package/src/event-created.jsx +8 -8
- package/src/event-destroyed.jsx +6 -6
- package/src/event-emitter-listener.jsx +6 -6
- package/src/event-listener.jsx +8 -8
- package/src/event-model-class.jsx +6 -6
- package/src/event-updated.jsx +10 -10
- package/src/instance-of-class-name.cjs +5 -7
- package/src/key-value-store.cjs +21 -18
- package/src/logger.cjs +4 -4
- package/src/merge.cjs +4 -4
- package/src/model-events.cjs +6 -5
- package/src/model-name.cjs +6 -5
- package/src/model-prop-type.cjs +11 -13
- package/src/model-recipes-loader.cjs +4 -4
- package/src/model-recipes-model-loader.cjs +39 -14
- package/src/models-response-reader.cjs +10 -10
- package/src/money-formatter.cjs +11 -11
- package/src/params.cjs +7 -7
- package/src/preloaded.cjs +6 -6
- package/src/resource-route.cjs +14 -12
- package/src/resource-routes.jsx +9 -4
- package/src/result.cjs +7 -7
- package/src/routes-native.cjs +6 -6
- package/src/routes.cjs +1 -1
- package/src/serializer.cjs +9 -9
- package/src/services.cjs +3 -3
- package/src/session-status-updater.cjs +14 -14
- package/src/source-maps-loader.cjs +11 -11
- package/src/translated-collections.cjs +2 -1
- package/src/updated-attribute.jsx +17 -14
- package/src/validation-error.cjs +5 -8
- package/src/validation-errors.cjs +16 -16
|
@@ -6,12 +6,12 @@ const inflection = require("inflection")
|
|
|
6
6
|
const Logger = require("./logger.cjs")
|
|
7
7
|
|
|
8
8
|
module.exports = class ApiMakerCableSubscriptionPool {
|
|
9
|
-
constructor() {
|
|
9
|
+
constructor () {
|
|
10
10
|
this.activeSubscriptions = 0
|
|
11
11
|
this.connected = false
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
connect(subscriptionData) {
|
|
14
|
+
connect (subscriptionData) {
|
|
15
15
|
const globalData = CommandsPool.current().globalRequestData
|
|
16
16
|
|
|
17
17
|
this.subscription = ChannelsConsumer.subscriptions.create(
|
|
@@ -28,7 +28,7 @@ module.exports = class ApiMakerCableSubscriptionPool {
|
|
|
28
28
|
this.connected = true
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
forEachSubscription(callback) {
|
|
31
|
+
forEachSubscription (callback) {
|
|
32
32
|
const modelIdModes = ["destroys", "updates"]
|
|
33
33
|
const subscriptions = digg(this, "subscriptions")
|
|
34
34
|
|
|
@@ -43,24 +43,24 @@ module.exports = class ApiMakerCableSubscriptionPool {
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
if (subscriptions[modelName]
|
|
47
|
-
for (const subscription of subscriptions[modelName]
|
|
46
|
+
if (subscriptions[modelName].creates) {
|
|
47
|
+
for (const subscription of subscriptions[modelName].creates) {
|
|
48
48
|
callback({mode: "creates", modelName, subscription})
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
if (subscriptions[modelName]
|
|
53
|
-
for (const eventName in subscriptions[modelName]
|
|
54
|
-
for (const subscription of subscriptions[modelName]
|
|
52
|
+
if (subscriptions[modelName].model_class_events) {
|
|
53
|
+
for (const eventName in subscriptions[modelName].model_class_events) {
|
|
54
|
+
for (const subscription of subscriptions[modelName].model_class_events[eventName]) {
|
|
55
55
|
callback({eventName, mode: "model_class_events", modelName, subscription})
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
if (subscriptions[modelName]
|
|
61
|
-
for (const modelId in subscriptions[modelName]
|
|
62
|
-
for (const eventName in subscriptions[modelName]
|
|
63
|
-
for (const subscription of subscriptions[modelName]
|
|
60
|
+
if (subscriptions[modelName].events) {
|
|
61
|
+
for (const modelId in subscriptions[modelName].events) {
|
|
62
|
+
for (const eventName in subscriptions[modelName].events[modelId]) {
|
|
63
|
+
for (const subscription of subscriptions[modelName].events[modelId][eventName]) {
|
|
64
64
|
callback({eventName, mode: "updates", modelId, modelName, subscription})
|
|
65
65
|
}
|
|
66
66
|
}
|
|
@@ -69,17 +69,17 @@ module.exports = class ApiMakerCableSubscriptionPool {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
isConnected() {
|
|
72
|
+
isConnected () {
|
|
73
73
|
return digg(this, "connected")
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
onConnected() {
|
|
76
|
+
onConnected () {
|
|
77
77
|
this.forEachSubscription(({subscription}) => {
|
|
78
78
|
subscription.events.emit("connected")
|
|
79
79
|
})
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
onReceived(rawData) {
|
|
82
|
+
onReceived (rawData) {
|
|
83
83
|
const data = Deserializer.parse(rawData)
|
|
84
84
|
const {a: args, e: eventName, m: model, mi: modelId, mt: modelType, t: type} = data
|
|
85
85
|
const subscriptions = digg(this, "subscriptions")
|
|
@@ -94,29 +94,29 @@ module.exports = class ApiMakerCableSubscriptionPool {
|
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
if (type == "u") {
|
|
97
|
-
for(const subscription of subscriptions[modelName]
|
|
97
|
+
for (const subscription of subscriptions[modelName].updates[modelId]) {
|
|
98
98
|
subscription.events.emit("received", {model})
|
|
99
99
|
}
|
|
100
100
|
} else if (type == "c") {
|
|
101
|
-
for(const subscription of subscriptions[modelName]
|
|
101
|
+
for (const subscription of subscriptions[modelName].creates) {
|
|
102
102
|
subscription.events.emit("received", {model})
|
|
103
103
|
}
|
|
104
104
|
} else if (type == "d") {
|
|
105
105
|
const destroySubscriptions = digg(subscriptions, modelName, "destroys", modelId)
|
|
106
106
|
|
|
107
|
-
for(const subscription of destroySubscriptions) {
|
|
107
|
+
for (const subscription of destroySubscriptions) {
|
|
108
108
|
subscription.events.emit("received", {model})
|
|
109
109
|
}
|
|
110
110
|
} else if (type == "e") {
|
|
111
111
|
const eventSubscriptions = digg(subscriptions, modelName, "events", eventName, modelId)
|
|
112
112
|
|
|
113
|
-
for(const subscription of eventSubscriptions) {
|
|
113
|
+
for (const subscription of eventSubscriptions) {
|
|
114
114
|
subscription.events.emit("received", {args, eventName, model})
|
|
115
115
|
}
|
|
116
116
|
} else if (type == "mce") {
|
|
117
117
|
const modelClassEventSubscriptions = digg(subscriptions, modelName, "model_class_events", eventName)
|
|
118
118
|
|
|
119
|
-
for(const subscription of modelClassEventSubscriptions) {
|
|
119
|
+
for (const subscription of modelClassEventSubscriptions) {
|
|
120
120
|
subscription.events.emit("received", {args, eventName})
|
|
121
121
|
}
|
|
122
122
|
} else {
|
|
@@ -124,7 +124,7 @@ module.exports = class ApiMakerCableSubscriptionPool {
|
|
|
124
124
|
}
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
-
onUnsubscribe() {
|
|
127
|
+
onUnsubscribe () {
|
|
128
128
|
Logger.log(`activeSubscriptions before unsub: ${this.activeSubscriptions}`)
|
|
129
129
|
this.activeSubscriptions -= 1
|
|
130
130
|
Logger.log(`activeSubscriptions after unsub: ${this.activeSubscriptions}`)
|
|
@@ -136,32 +136,32 @@ module.exports = class ApiMakerCableSubscriptionPool {
|
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
-
registerSubscriptions(subscriptions) {
|
|
139
|
+
registerSubscriptions (subscriptions) {
|
|
140
140
|
this.subscriptions = subscriptions
|
|
141
141
|
|
|
142
142
|
Logger.log(`registerSubscriptions: ${subscriptions.length}`)
|
|
143
143
|
Logger.log(subscriptions)
|
|
144
144
|
|
|
145
|
-
for(const modelName in subscriptions) {
|
|
146
|
-
if (subscriptions[modelName]
|
|
147
|
-
for(const subscription of subscriptions[modelName]
|
|
145
|
+
for (const modelName in subscriptions) {
|
|
146
|
+
if (subscriptions[modelName].creates) {
|
|
147
|
+
for (const subscription of subscriptions[modelName].creates) {
|
|
148
148
|
this.connectUnsubscriptionForSubscription(subscription)
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
-
if (subscriptions[modelName]
|
|
153
|
-
for(const eventName in subscriptions[modelName]
|
|
154
|
-
for(const modelId in subscriptions[modelName]
|
|
155
|
-
for(const subscription of subscriptions[modelName]
|
|
152
|
+
if (subscriptions[modelName].events) {
|
|
153
|
+
for (const eventName in subscriptions[modelName].events) {
|
|
154
|
+
for (const modelId in subscriptions[modelName].events[eventName]) {
|
|
155
|
+
for (const subscription of subscriptions[modelName].events[eventName][modelId]) {
|
|
156
156
|
this.connectUnsubscriptionForSubscription(subscription)
|
|
157
157
|
}
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
-
if (subscriptions[modelName]
|
|
163
|
-
for(const modelId in subscriptions[modelName]
|
|
164
|
-
for(const subscription of subscriptions[modelName]
|
|
162
|
+
if (subscriptions[modelName].updates) {
|
|
163
|
+
for (const modelId in subscriptions[modelName].updates) {
|
|
164
|
+
for (const subscription of subscriptions[modelName].updates[modelId]) {
|
|
165
165
|
this.connectUnsubscriptionForSubscription(subscription)
|
|
166
166
|
}
|
|
167
167
|
}
|
|
@@ -169,9 +169,9 @@ module.exports = class ApiMakerCableSubscriptionPool {
|
|
|
169
169
|
}
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
connectUnsubscriptionForSubscription(subscription) {
|
|
172
|
+
connectUnsubscriptionForSubscription (subscription) {
|
|
173
173
|
Logger.log("Connecting to unsubscribe on subscription")
|
|
174
|
-
Logger.log({
|
|
174
|
+
Logger.log({subscription})
|
|
175
175
|
|
|
176
176
|
this.activeSubscriptions += 1
|
|
177
177
|
|
|
@@ -2,12 +2,12 @@ const EventEmitter = require("events")
|
|
|
2
2
|
const Logger = require("./logger.cjs")
|
|
3
3
|
|
|
4
4
|
module.exports = class ApiMakerCableSubscription {
|
|
5
|
-
constructor() {
|
|
5
|
+
constructor () {
|
|
6
6
|
this.events = new EventEmitter()
|
|
7
7
|
this.subscribed = true
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
unsubscribe() {
|
|
10
|
+
unsubscribe () {
|
|
11
11
|
if (!this.subscribed) {
|
|
12
12
|
Logger.log("Unsubscribed already called")
|
|
13
13
|
return
|
package/src/can-can-loader.jsx
CHANGED
|
@@ -2,20 +2,20 @@ import ApiMakerEventEmitterListener from "./event-emitter-listener"
|
|
|
2
2
|
import {digg, digs} from "diggerize"
|
|
3
3
|
const CanCan = require("./can-can.cjs")
|
|
4
4
|
const PropTypes = require("prop-types")
|
|
5
|
-
const
|
|
5
|
+
const propTypesExact = require("prop-types-exact")
|
|
6
6
|
const React = require("react")
|
|
7
7
|
|
|
8
8
|
export default class ApiMakerCanCanLoader extends React.PureComponent {
|
|
9
|
-
static propTypes =
|
|
9
|
+
static propTypes = propTypesExact({
|
|
10
10
|
abilities: PropTypes.array.isRequired,
|
|
11
11
|
component: PropTypes.object.isRequired
|
|
12
12
|
})
|
|
13
13
|
|
|
14
|
-
componentDidMount() {
|
|
14
|
+
componentDidMount () {
|
|
15
15
|
this.loadAbilities()
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
async loadAbilities() {
|
|
18
|
+
async loadAbilities () {
|
|
19
19
|
const canCan = CanCan.current()
|
|
20
20
|
const {abilities} = digs(this.props, "abilities")
|
|
21
21
|
|
|
@@ -24,14 +24,14 @@ export default class ApiMakerCanCanLoader extends React.PureComponent {
|
|
|
24
24
|
this.updateComponent({canCan})
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
render() {
|
|
27
|
+
render () {
|
|
28
28
|
const canCan = CanCan.current()
|
|
29
29
|
const events = digg(canCan, "events")
|
|
30
30
|
|
|
31
31
|
return (
|
|
32
32
|
<ApiMakerEventEmitterListener
|
|
33
|
-
events={events}
|
|
34
33
|
event="onResetAbilities"
|
|
34
|
+
events={events}
|
|
35
35
|
onCalled={this.onResetAbilities}
|
|
36
36
|
/>
|
|
37
37
|
)
|
|
@@ -42,7 +42,7 @@ export default class ApiMakerCanCanLoader extends React.PureComponent {
|
|
|
42
42
|
this.loadAbilities()
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
updateComponent(updatedState) {
|
|
45
|
+
updateComponent (updatedState) {
|
|
46
46
|
const {component} = digs(this.props, "component")
|
|
47
47
|
|
|
48
48
|
if (component.shape) {
|
package/src/can-can.cjs
CHANGED
|
@@ -5,7 +5,7 @@ const {ReadersWriterLock} = require("epic-locks")
|
|
|
5
5
|
const Services = require("./services.cjs")
|
|
6
6
|
|
|
7
7
|
module.exports = class ApiMakerCanCan {
|
|
8
|
-
static current() {
|
|
8
|
+
static current () {
|
|
9
9
|
if (!global.currentApiMakerCanCan) {
|
|
10
10
|
global.currentApiMakerCanCan = new ApiMakerCanCan()
|
|
11
11
|
}
|
|
@@ -13,7 +13,7 @@ module.exports = class ApiMakerCanCan {
|
|
|
13
13
|
return global.currentApiMakerCanCan
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
constructor() {
|
|
16
|
+
constructor () {
|
|
17
17
|
this.abilities = []
|
|
18
18
|
this.abilitiesToLoad = []
|
|
19
19
|
this.abilitiesToLoadData = []
|
|
@@ -21,19 +21,19 @@ module.exports = class ApiMakerCanCan {
|
|
|
21
21
|
this.lock = new ReadersWriterLock()
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
can(ability, subject) {
|
|
25
|
-
|
|
26
|
-
const foundAbility = this.findAbility(
|
|
24
|
+
can (ability, subject) {
|
|
25
|
+
let abilityToUse = inflection.underscore(ability)
|
|
26
|
+
const foundAbility = this.findAbility(abilityToUse, subject)
|
|
27
27
|
|
|
28
28
|
if (foundAbility === undefined) {
|
|
29
29
|
let subjectLabel = subject
|
|
30
30
|
|
|
31
31
|
// Translate resource-models into class name strings
|
|
32
|
-
if (typeof subject == "function" && subject
|
|
32
|
+
if (typeof subject == "function" && subject.modelClassData) {
|
|
33
33
|
subjectLabel = digg(subject.modelClassData(), "name")
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
console.error(`Ability not loaded ${subjectLabel}#${
|
|
36
|
+
console.error(`Ability not loaded ${subjectLabel}#${abilityToUse}`)
|
|
37
37
|
|
|
38
38
|
return false
|
|
39
39
|
} else {
|
|
@@ -41,11 +41,11 @@ module.exports = class ApiMakerCanCan {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
findAbility(ability, subject) {
|
|
44
|
+
findAbility (ability, subject) {
|
|
45
45
|
return this.abilities.find((abilityData) => digg(abilityData, "subject") == subject && digg(abilityData, "ability") == ability)
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
isAbilityLoaded(ability, subject) {
|
|
48
|
+
isAbilityLoaded (ability, subject) {
|
|
49
49
|
const foundAbility = this.findAbility(ability, subject)
|
|
50
50
|
|
|
51
51
|
if (foundAbility !== undefined) {
|
|
@@ -55,8 +55,8 @@ module.exports = class ApiMakerCanCan {
|
|
|
55
55
|
return false
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
async loadAbilities(abilities) {
|
|
59
|
-
await this.lock.read(async() => {
|
|
58
|
+
async loadAbilities (abilities) {
|
|
59
|
+
await this.lock.read(async () => {
|
|
60
60
|
const promises = []
|
|
61
61
|
|
|
62
62
|
for (const abilityData of abilities) {
|
|
@@ -73,12 +73,13 @@ module.exports = class ApiMakerCanCan {
|
|
|
73
73
|
})
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
loadAbility(ability, subject) {
|
|
76
|
+
loadAbility (ability, subject) {
|
|
77
77
|
return new Promise((resolve) => {
|
|
78
78
|
ability = inflection.underscore(ability)
|
|
79
79
|
|
|
80
80
|
if (this.isAbilityLoaded(ability, subject)) {
|
|
81
|
-
|
|
81
|
+
resolve()
|
|
82
|
+
return
|
|
82
83
|
}
|
|
83
84
|
|
|
84
85
|
const foundAbility = this.abilitiesToLoad.find((abilityToLoad) => digg(abilityToLoad, "ability") == ability && digg(abilityToLoad, "subject") == subject)
|
|
@@ -94,7 +95,7 @@ module.exports = class ApiMakerCanCan {
|
|
|
94
95
|
})
|
|
95
96
|
}
|
|
96
97
|
|
|
97
|
-
queueAbilitiesRequest() {
|
|
98
|
+
queueAbilitiesRequest () {
|
|
98
99
|
if (this.queueAbilitiesRequestTimeout) {
|
|
99
100
|
clearTimeout(this.queueAbilitiesRequestTimeout)
|
|
100
101
|
}
|
|
@@ -102,14 +103,14 @@ module.exports = class ApiMakerCanCan {
|
|
|
102
103
|
this.queueAbilitiesRequestTimeout = setTimeout(() => this.sendAbilitiesRequest(), 0)
|
|
103
104
|
}
|
|
104
105
|
|
|
105
|
-
async resetAbilities() {
|
|
106
|
+
async resetAbilities () {
|
|
106
107
|
await this.lock.write(() => {
|
|
107
108
|
this.abilities = []
|
|
108
109
|
})
|
|
109
110
|
this.events.emit("onResetAbilities")
|
|
110
111
|
}
|
|
111
112
|
|
|
112
|
-
async sendAbilitiesRequest() {
|
|
113
|
+
async sendAbilitiesRequest () {
|
|
113
114
|
const abilitiesToLoad = this.abilitiesToLoad
|
|
114
115
|
const abilitiesToLoadData = this.abilitiesToLoadData
|
|
115
116
|
|
package/src/collection.cjs
CHANGED
|
@@ -7,20 +7,20 @@ const ModelsResponseReader = require("./models-response-reader.cjs")
|
|
|
7
7
|
const Result = require("./result.cjs")
|
|
8
8
|
|
|
9
9
|
module.exports = class ApiMakerCollection {
|
|
10
|
-
constructor(args, queryArgs = {}) {
|
|
10
|
+
constructor (args, queryArgs = {}) {
|
|
11
11
|
this.queryArgs = queryArgs
|
|
12
12
|
this.args = args
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
abilities(originalAbilities) {
|
|
15
|
+
abilities (originalAbilities) {
|
|
16
16
|
const newAbilities = {}
|
|
17
17
|
|
|
18
|
-
for(const originalAbilityName in originalAbilities) {
|
|
18
|
+
for (const originalAbilityName in originalAbilities) {
|
|
19
19
|
const newModelName = inflection.underscore(originalAbilityName)
|
|
20
20
|
const newValues = []
|
|
21
21
|
const originalValues = originalAbilities[originalAbilityName]
|
|
22
22
|
|
|
23
|
-
for(const originalAbilityName of originalValues) {
|
|
23
|
+
for (const originalAbilityName of originalValues) {
|
|
24
24
|
const newAbilityName = inflection.underscore(originalAbilityName)
|
|
25
25
|
newValues.push(newAbilityName)
|
|
26
26
|
}
|
|
@@ -31,34 +31,34 @@ module.exports = class ApiMakerCollection {
|
|
|
31
31
|
return this._merge({abilities: newAbilities})
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
accessibleBy(abilityName) {
|
|
34
|
+
accessibleBy (abilityName) {
|
|
35
35
|
return this._merge({accessibleBy: inflection.underscore(abilityName)})
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
async count() {
|
|
38
|
+
async count () {
|
|
39
39
|
const response = await this.clone()._merge({count: true})._response()
|
|
40
40
|
|
|
41
41
|
return response.count
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
distinct() {
|
|
44
|
+
distinct () {
|
|
45
45
|
return this._merge({distinct: true})
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
async each(callback) {
|
|
48
|
+
async each (callback) {
|
|
49
49
|
const array = await this.toArray()
|
|
50
50
|
|
|
51
|
-
for(const model in array) {
|
|
51
|
+
for (const model in array) {
|
|
52
52
|
callback.call(model)
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
async first() {
|
|
56
|
+
async first () {
|
|
57
57
|
const models = await this.toArray()
|
|
58
58
|
return models[0]
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
groupBy(...arrayOfTablesAndColumns) {
|
|
61
|
+
groupBy (...arrayOfTablesAndColumns) {
|
|
62
62
|
const arrayOfTablesAndColumnsWithLowercaseColumns = arrayOfTablesAndColumns.map((tableAndColumn) => {
|
|
63
63
|
if (Array.isArray(tableAndColumn)) {
|
|
64
64
|
return [tableAndColumn[0], tableAndColumn[1].toLowerCase()]
|
|
@@ -74,18 +74,18 @@ module.exports = class ApiMakerCollection {
|
|
|
74
74
|
})
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
isLoaded() {
|
|
77
|
+
isLoaded () {
|
|
78
78
|
if (this.args.reflectionName in this.args.model.relationshipsCache)
|
|
79
79
|
return true
|
|
80
80
|
|
|
81
81
|
return false
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
limit(amount) {
|
|
84
|
+
limit (amount) {
|
|
85
85
|
return this._merge({limit: amount})
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
loaded() {
|
|
88
|
+
loaded () {
|
|
89
89
|
if (!(this.args.reflectionName in this.args.model.relationshipsCache)) {
|
|
90
90
|
throw new Error(`${this.args.reflectionName} hasnt been loaded yet`)
|
|
91
91
|
}
|
|
@@ -93,22 +93,22 @@ module.exports = class ApiMakerCollection {
|
|
|
93
93
|
return this.args.model.relationshipsCache[this.args.reflectionName]
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
-
preload(preloadValue) {
|
|
96
|
+
preload (preloadValue) {
|
|
97
97
|
return this._merge({preload: preloadValue})
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
page(page) {
|
|
100
|
+
page (page) {
|
|
101
101
|
if (!page)
|
|
102
102
|
page = 1
|
|
103
103
|
|
|
104
104
|
return this._merge({page})
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
pageKey(pageKey) {
|
|
107
|
+
pageKey (pageKey) {
|
|
108
108
|
return this._merge({pageKey})
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
-
params() {
|
|
111
|
+
params () {
|
|
112
112
|
let params = {}
|
|
113
113
|
|
|
114
114
|
if (this.queryArgs.params) params = merge(params, this.queryArgs.params)
|
|
@@ -128,15 +128,15 @@ module.exports = class ApiMakerCollection {
|
|
|
128
128
|
return params
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
-
per(per) {
|
|
131
|
+
per (per) {
|
|
132
132
|
return this._merge({per})
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
-
perKey(perKey) {
|
|
135
|
+
perKey (perKey) {
|
|
136
136
|
return this._merge({perKey})
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
-
ransack(params) {
|
|
139
|
+
ransack (params) {
|
|
140
140
|
if (params) {
|
|
141
141
|
this._merge({ransack: params})
|
|
142
142
|
}
|
|
@@ -144,26 +144,26 @@ module.exports = class ApiMakerCollection {
|
|
|
144
144
|
return this
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
-
async result() {
|
|
147
|
+
async result () {
|
|
148
148
|
const response = await this._response()
|
|
149
149
|
const models = this._responseToModels(response)
|
|
150
150
|
const result = new Result({collection: this, models, response})
|
|
151
151
|
return result
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
-
searchKey(searchKey) {
|
|
155
|
-
return this._merge({searchKey
|
|
154
|
+
searchKey (searchKey) {
|
|
155
|
+
return this._merge({searchKey})
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
-
select(originalSelect) {
|
|
158
|
+
select (originalSelect) {
|
|
159
159
|
const newSelect = {}
|
|
160
160
|
|
|
161
|
-
for(const originalModelName in originalSelect) {
|
|
161
|
+
for (const originalModelName in originalSelect) {
|
|
162
162
|
const newModelName = inflection.underscore(originalModelName)
|
|
163
163
|
const newValues = []
|
|
164
164
|
const originalValues = originalSelect[originalModelName]
|
|
165
165
|
|
|
166
|
-
for(const originalAttributeName of originalValues) {
|
|
166
|
+
for (const originalAttributeName of originalValues) {
|
|
167
167
|
const newAttributeName = inflection.underscore(originalAttributeName)
|
|
168
168
|
newValues.push(newAttributeName)
|
|
169
169
|
}
|
|
@@ -174,10 +174,10 @@ module.exports = class ApiMakerCollection {
|
|
|
174
174
|
return this._merge({select: newSelect})
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
-
selectColumns(originalSelect) {
|
|
177
|
+
selectColumns (originalSelect) {
|
|
178
178
|
const newSelect = {}
|
|
179
179
|
|
|
180
|
-
for(const originalModelName in originalSelect) {
|
|
180
|
+
for (const originalModelName in originalSelect) {
|
|
181
181
|
const newModelName = inflection.underscore(inflection.underscore(originalModelName))
|
|
182
182
|
const newValues = []
|
|
183
183
|
const originalValues = originalSelect[originalModelName]
|
|
@@ -193,34 +193,34 @@ module.exports = class ApiMakerCollection {
|
|
|
193
193
|
return this._merge({selectColumns: newSelect})
|
|
194
194
|
}
|
|
195
195
|
|
|
196
|
-
sort(sortBy) {
|
|
196
|
+
sort (sortBy) {
|
|
197
197
|
return this._merge({ransack: {s: sortBy}})
|
|
198
198
|
}
|
|
199
199
|
|
|
200
|
-
async toArray() {
|
|
200
|
+
async toArray () {
|
|
201
201
|
const response = await this._response()
|
|
202
202
|
return this._responseToModels(response)
|
|
203
203
|
}
|
|
204
204
|
|
|
205
|
-
modelClass() {
|
|
205
|
+
modelClass () {
|
|
206
206
|
const modelName = digg(this.args.modelClass.modelClassData(), "name")
|
|
207
207
|
|
|
208
208
|
return digg(require("@kaspernj/api-maker/src/models"), modelName)
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
-
clone() {
|
|
211
|
+
clone () {
|
|
212
212
|
const clonedQueryArgs = cloneDeep(this.queryArgs)
|
|
213
213
|
|
|
214
214
|
return new ApiMakerCollection(this.args, clonedQueryArgs)
|
|
215
215
|
}
|
|
216
216
|
|
|
217
|
-
_merge(newQueryArgs) {
|
|
217
|
+
_merge (newQueryArgs) {
|
|
218
218
|
merge(this.queryArgs, newQueryArgs)
|
|
219
219
|
|
|
220
220
|
return this
|
|
221
221
|
}
|
|
222
222
|
|
|
223
|
-
_response() {
|
|
223
|
+
_response () {
|
|
224
224
|
const modelClassData = this.args.modelClass.modelClassData()
|
|
225
225
|
|
|
226
226
|
return CommandsPool.addCommand(
|
|
@@ -234,7 +234,7 @@ module.exports = class ApiMakerCollection {
|
|
|
234
234
|
)
|
|
235
235
|
}
|
|
236
236
|
|
|
237
|
-
_responseToModels(response) {
|
|
237
|
+
_responseToModels (response) {
|
|
238
238
|
const modelsResponseReader = new ModelsResponseReader({
|
|
239
239
|
collection: this,
|
|
240
240
|
response
|
|
@@ -1,22 +1,21 @@
|
|
|
1
1
|
const objectToFormData = require("object-to-formdata").serialize
|
|
2
2
|
|
|
3
3
|
module.exports = class ApiMakerCommandSubmitData {
|
|
4
|
-
constructor(data) {
|
|
4
|
+
constructor (data) {
|
|
5
5
|
this.data = data
|
|
6
|
-
this.formData
|
|
7
6
|
this.filesCount = 0
|
|
8
7
|
this.jsonData = this.traverseObject(this.data, "json")
|
|
9
8
|
}
|
|
10
9
|
|
|
11
|
-
getFilesCount() {
|
|
10
|
+
getFilesCount () {
|
|
12
11
|
return this.filesCount
|
|
13
12
|
}
|
|
14
13
|
|
|
15
|
-
getJsonData() {
|
|
14
|
+
getJsonData () {
|
|
16
15
|
return this.jsonData
|
|
17
16
|
}
|
|
18
17
|
|
|
19
|
-
getRawData() {
|
|
18
|
+
getRawData () {
|
|
20
19
|
if (!this.rawData) {
|
|
21
20
|
this.rawData = this.traverseObject(this.data, "raw")
|
|
22
21
|
}
|
|
@@ -24,7 +23,7 @@ module.exports = class ApiMakerCommandSubmitData {
|
|
|
24
23
|
return this.rawData
|
|
25
24
|
}
|
|
26
25
|
|
|
27
|
-
getFormData() {
|
|
26
|
+
getFormData () {
|
|
28
27
|
const objectForFormData = this.getRawData() || {}
|
|
29
28
|
|
|
30
29
|
objectForFormData.json = JSON.stringify(this.getJsonData())
|
|
@@ -34,7 +33,7 @@ module.exports = class ApiMakerCommandSubmitData {
|
|
|
34
33
|
return formData
|
|
35
34
|
}
|
|
36
35
|
|
|
37
|
-
convertDynamic(value, type) {
|
|
36
|
+
convertDynamic (value, type) {
|
|
38
37
|
if (Array.isArray(value)) {
|
|
39
38
|
return this.traverseArray(value, type)
|
|
40
39
|
} else if (typeof value == "object" && value !== null && value.constructor.name == "Object") {
|
|
@@ -44,7 +43,7 @@ module.exports = class ApiMakerCommandSubmitData {
|
|
|
44
43
|
}
|
|
45
44
|
}
|
|
46
45
|
|
|
47
|
-
shouldSkip(object, type) {
|
|
46
|
+
shouldSkip (object, type) {
|
|
48
47
|
if (type == "json" && object instanceof File) {
|
|
49
48
|
this.filesCount += 1
|
|
50
49
|
return true
|
|
@@ -57,7 +56,7 @@ module.exports = class ApiMakerCommandSubmitData {
|
|
|
57
56
|
return false
|
|
58
57
|
}
|
|
59
58
|
|
|
60
|
-
isObject(value) {
|
|
59
|
+
isObject (value) {
|
|
61
60
|
if (typeof value == "object" && value !== null && value.constructor.name == "Object") {
|
|
62
61
|
return true
|
|
63
62
|
}
|
|
@@ -65,7 +64,7 @@ module.exports = class ApiMakerCommandSubmitData {
|
|
|
65
64
|
return false
|
|
66
65
|
}
|
|
67
66
|
|
|
68
|
-
traverseArray(array, type) {
|
|
67
|
+
traverseArray (array, type) {
|
|
69
68
|
const newArray = []
|
|
70
69
|
|
|
71
70
|
for (const value of array) {
|
|
@@ -85,7 +84,7 @@ module.exports = class ApiMakerCommandSubmitData {
|
|
|
85
84
|
return newArray
|
|
86
85
|
}
|
|
87
86
|
|
|
88
|
-
traverseObject(object, type) {
|
|
87
|
+
traverseObject (object, type) {
|
|
89
88
|
const newObject = {}
|
|
90
89
|
|
|
91
90
|
for (const key in object) {
|