@kaspernj/api-maker 1.0.205 → 1.0.208

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 CHANGED
@@ -16,7 +16,7 @@
16
16
  ]
17
17
  },
18
18
  "name": "@kaspernj/api-maker",
19
- "version": "1.0.205",
19
+ "version": "1.0.208",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -44,6 +44,7 @@
44
44
  "form-data-objectizer": ">= 1.0.0",
45
45
  "form-serialize": ">= 0.7.2",
46
46
  "format-number": ">= 3.0.0",
47
+ "incorporator": ">= 1.0.2",
47
48
  "inflection": ">= 1.12.0",
48
49
  "js-money": ">= 0.6.3",
49
50
  "numberable": ">= 1.0.0",
@@ -2,7 +2,7 @@ import cloneDeep from "clone-deep"
2
2
  import CommandsPool from "./commands-pool.mjs"
3
3
  import {digg} from "diggerize"
4
4
  import inflection from "inflection"
5
- import {merge} from "./merge.mjs"
5
+ import {incorporate} from "incorporator"
6
6
  import modelClassRequire from "./model-class-require.mjs"
7
7
  import Result from "./result.mjs"
8
8
 
@@ -111,7 +111,7 @@ class ApiMakerCollection {
111
111
  params () {
112
112
  let params = {}
113
113
 
114
- if (this.queryArgs.params) params = merge(params, this.queryArgs.params)
114
+ if (this.queryArgs.params) params = incorporate(params, this.queryArgs.params)
115
115
  if (this.queryArgs.abilities) params.abilities = this.queryArgs.abilities
116
116
  if (this.queryArgs.accessibleBy) params.accessible_by = inflection.underscore(this.queryArgs.accessibleBy)
117
117
  if (this.queryArgs.count) params.count = this.queryArgs.count
@@ -230,7 +230,7 @@ class ApiMakerCollection {
230
230
  }
231
231
 
232
232
  _merge (newQueryArgs) {
233
- merge(this.queryArgs, newQueryArgs)
233
+ incorporate(this.queryArgs, newQueryArgs)
234
234
 
235
235
  return this
236
236
  }
@@ -8,7 +8,8 @@ export default (WrappedComponent, ModelClass, args = {}) => class modelLoadWrapp
8
8
 
9
9
  state = {
10
10
  model: undefined,
11
- modelId: this.props.match.params[this.paramsVariableName] || this.props.match.params.id
11
+ modelId: this.props.match.params[this.paramsVariableName] || this.props.match.params.id,
12
+ notFound: undefined
12
13
  }
13
14
 
14
15
  componentDidMount() {
@@ -37,7 +38,10 @@ export default (WrappedComponent, ModelClass, args = {}) => class modelLoadWrapp
37
38
 
38
39
  const model = await query.first()
39
40
 
40
- this.setState({model})
41
+ this.setState({
42
+ model,
43
+ notFound: !model
44
+ })
41
45
  }
42
46
 
43
47
  async loadNewModel() {
@@ -62,11 +66,12 @@ export default (WrappedComponent, ModelClass, args = {}) => class modelLoadWrapp
62
66
 
63
67
  render() {
64
68
  const {onUpdated, reloadModel} = digs(this, "onUpdated", "reloadModel")
65
- const {model, modelId} = digs(this.state, "model", "modelId")
69
+ const {model, modelId, notFound} = digs(this.state, "model", "modelId", "notFound")
66
70
  const wrappedComponentProps = {}
67
71
 
68
72
  wrappedComponentProps[this.camelizedLower] = model
69
73
  wrappedComponentProps[`${this.camelizedLower}Id`] = modelId
74
+ wrappedComponentProps[`${this.camelizedLower}NotFound`] = notFound
70
75
 
71
76
  return (
72
77
  <>
package/src/params.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import formSerialize from "form-serialize"
2
- import {merge} from "./merge.mjs"
2
+ import Incorporator from "incorporator"
3
3
  import qs from "qs"
4
4
 
5
5
  export default class Params {
@@ -8,7 +8,11 @@ export default class Params {
8
8
  }
9
9
 
10
10
  static change (given) {
11
- return merge(Params.parse(), given)
11
+ const incorporator = new Incorporator({objects: [Params.parse(), given]})
12
+
13
+ incorporator.replaceArrayIfExists(true)
14
+
15
+ return incorporator.merge()
12
16
  }
13
17
 
14
18
  static changeParams (given, opts = {}) {
@@ -1,25 +0,0 @@
1
- import {merge} from "../src/merge.mjs"
2
-
3
- describe("merge", () => {
4
- it("merges an empty object and changes nothing", () => {
5
- const object = {
6
- firstName: "Kasper",
7
- age: 35
8
- }
9
-
10
- merge(object, {})
11
-
12
- expect(object).toEqual({
13
- firstName: "Kasper",
14
- age: 35
15
- })
16
- })
17
-
18
- it("merges an empty object into a nested object", () => {
19
- const object = {ransack: {account_id_eq: 1}}
20
-
21
- merge(object, {})
22
-
23
- expect(object).toEqual({ransack: {account_id_eq: 1}})
24
- })
25
- })
package/src/merge.mjs DELETED
@@ -1,56 +0,0 @@
1
- const isPlainObject = (input) => {
2
- if (input && typeof input === "object" && !Array.isArray(input)) {
3
- return true
4
- }
5
-
6
- return false
7
- }
8
-
9
- const merge = (firstObject, ...objects) => {
10
- for (const object of objects) {
11
- mergeObjectsInto(firstObject, object)
12
- }
13
-
14
- return firstObject
15
- }
16
-
17
- const mergeArraysInto = (mergeIntoValue, ...arrays) => {
18
- for (const array of arrays) {
19
- for (const value of array) {
20
- if (!mergeIntoValue.includes(value)) {
21
- mergeIntoValue.push(value)
22
- }
23
- }
24
- }
25
- }
26
-
27
- const mergeObjectsInto = (mergeInto, object) => {
28
- for (const key in object) {
29
- const value = object[key]
30
-
31
- if (key in mergeInto) {
32
- const mergeIntoValue = mergeInto[key]
33
-
34
- if (Array.isArray(value)) {
35
- // Current value isn't an array - turn into array and then merge into that
36
- if (!Array.isArray(mergeIntoValue)) {
37
- mergeInto[key] = [mergeIntoValue]
38
- }
39
-
40
- mergeArraysInto(mergeInto[key], value)
41
- } else if (isPlainObject(mergeIntoValue) && isPlainObject(value)) {
42
- mergeObjectsInto(mergeIntoValue, value)
43
- } else {
44
- mergeInto[key] = value
45
- }
46
- } else {
47
- mergeInto[key] = value
48
- }
49
- }
50
- }
51
-
52
- export {
53
- merge,
54
- mergeArraysInto,
55
- mergeObjectsInto
56
- }