@weapnl/js-junction 0.0.4 → 0.0.5

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/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## v0.0.5
6
+ - Added license file.
7
+ - Private field bugfix, proxy access.
8
+
5
9
  ## v0.0.4
6
10
  - Added support for whereNotIn.
7
11
  - Reactive bugfix, private property.
package/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) WEAP informatie@weap.nl
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weapnl/js-junction",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "This project allows you to easily consume API's built with Junction.",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
@@ -9,7 +9,7 @@
9
9
  "prod": "webpack"
10
10
  },
11
11
  "author": "Robin",
12
- "license": "ISC",
12
+ "license": "MIT",
13
13
  "devDependencies": {
14
14
  "@babel/core": "^7.12.13",
15
15
  "@babel/preset-env": "^7.12.13",
@@ -26,10 +26,10 @@ export default class Model extends Request {
26
26
  static fromJson (json) {
27
27
  const instance = new (this)();
28
28
 
29
- instance._accessors.fromJson(json);
30
- instance._attributes.fromJson(json);
31
- instance._counts.fromJson(json);
32
- instance._relations.fromJson(json);
29
+ instance._accessors.fromJson(instance, json);
30
+ instance._attributes.fromJson(instance, json);
31
+ instance._counts.fromJson(instance, json);
32
+ instance._relations.fromJson(instance, json);
33
33
 
34
34
  return instance;
35
35
  }
@@ -41,10 +41,10 @@ export default class Model extends Request {
41
41
  */
42
42
  toJson () {
43
43
  return {
44
- ...this._accessors.toJson(),
45
- ...this._attributes.toJson(),
46
- ...this._counts.toJson(),
47
- ...this._relations.toJson(),
44
+ ...this._accessors.toJson(this),
45
+ ...this._attributes.toJson(this),
46
+ ...this._counts.toJson(this),
47
+ ...this._relations.toJson(this),
48
48
  };
49
49
  }
50
50
 
@@ -53,8 +53,8 @@ export default class Model extends Request {
53
53
  * @returns {this}
54
54
  */
55
55
  fill (values = {}) {
56
- this._attributes.set(values);
57
- this._relations.set(values);
56
+ this._attributes.set(this, values);
57
+ this._relations.set(this, values);
58
58
 
59
59
  return this;
60
60
  }
@@ -171,7 +171,7 @@ export default class Model extends Request {
171
171
  async store (extraData = {}) {
172
172
  this._response = await this._connection.post(
173
173
  this._queryString(),
174
- { ...this._attributes.toJson(), ...extraData },
174
+ { ...this._attributes.toJson(this), ...extraData },
175
175
  );
176
176
 
177
177
  let item;
@@ -195,7 +195,7 @@ export default class Model extends Request {
195
195
  async update (extraData = {}) {
196
196
  this._response = await this._connection.put(
197
197
  this._queryString(this._identifier),
198
- { ...this._attributes.toJson(), ...extraData },
198
+ { ...this._attributes.toJson(this), ...extraData },
199
199
  );
200
200
 
201
201
  let item;
@@ -4,24 +4,21 @@ import Caster from '../caster';
4
4
  * @implements {Property}
5
5
  */
6
6
  export default class Accessors {
7
- #model;
8
-
9
7
  /**
10
8
  * @param {Model} model Instance of the model.
11
9
  */
12
10
  constructor (model) {
13
- this.#model = model;
14
-
15
11
  _.each(model.constructor.accessors(), (options, key) => {
16
- this.set(key, _.has(options, 'default') ? options.default : null);
12
+ this.set(model, key, _.has(options, 'default') ? options.default : null);
17
13
  });
18
14
  }
19
15
 
20
16
  /**
17
+ * @param {Model} model
21
18
  * @param {Object} json.
22
19
  */
23
- fromJson (json) {
24
- _.each(this.#model.constructor.accessors(), (options, key) => {
20
+ fromJson (model, json) {
21
+ _.each(model.constructor.accessors(), (options, key) => {
25
22
  let value = _.get(json, options.jsonKey ?? _.snakeCase(key), _.get(json, _.camelCase(key)));
26
23
 
27
24
  if (_.isNil(value)) {
@@ -30,18 +27,20 @@ export default class Accessors {
30
27
  value = Accessors._getCastedFromJsonValue(value, options);
31
28
  }
32
29
 
33
- this.set(key, value);
30
+ this.set(model, key, value);
34
31
  });
35
32
  }
36
33
 
37
34
  /**
35
+ * @param {Model} model
36
+ *
38
37
  * @return {Object} The attributes casted to a json object.
39
38
  */
40
- toJson () {
39
+ toJson (model) {
41
40
  const json = {};
42
41
 
43
- _.each(this.#model.constructor.accessors(), (options, key) => {
44
- let jsonValue = this.get(key);
42
+ _.each(model.constructor.accessors(), (options, key) => {
43
+ let jsonValue = this.get(model, key);
45
44
 
46
45
  jsonValue = Accessors._getCastedToJsonValue(jsonValue, options);
47
46
 
@@ -52,22 +51,24 @@ export default class Accessors {
52
51
  }
53
52
 
54
53
  /**
54
+ * @param {Model} model
55
55
  * @param {string} attribute
56
56
  *
57
57
  * @returns {*} The value of the attribute.
58
58
  */
59
- get (attribute) {
60
- return _.get(this.#model, attribute);
59
+ get (model, attribute) {
60
+ return _.get(model, attribute);
61
61
  }
62
62
 
63
63
  /**
64
- * @param {string} attribute
65
- * @param {*} value
64
+ * @param {Model} model
65
+ * @param {string} attribute
66
+ * @param {*} value
66
67
  *
67
68
  * @returns {*} The value that was set.
68
69
  */
69
- set (attribute, value) {
70
- this.#model[attribute] = value;
70
+ set (model, attribute, value) {
71
+ model[attribute] = value;
71
72
 
72
73
  return value;
73
74
  }
@@ -4,24 +4,21 @@ import Caster from '../caster';
4
4
  * @implements {Property}
5
5
  */
6
6
  export default class Attributes {
7
- #model;
8
-
9
7
  /**
10
8
  * @param {Model} model Instance of the model.
11
9
  */
12
10
  constructor (model) {
13
- this.#model = model;
14
-
15
11
  _.each(model.constructor.attributes(), (options, key) => {
16
- this.set(key, _.has(options, 'default') ? options.default : null);
12
+ this.set(model, key, _.has(options, 'default') ? options.default : null);
17
13
  });
18
14
  }
19
15
 
20
16
  /**
17
+ * @param {Model} model
21
18
  * @param {Object} json.
22
19
  */
23
- fromJson (json) {
24
- _.each(this.#model.constructor.attributes(), (options, key) => {
20
+ fromJson (model, json) {
21
+ _.each(model.constructor.attributes(), (options, key) => {
25
22
  let value = _.get(json, options.jsonKey ?? _.snakeCase(key), _.get(json, _.camelCase(key)));
26
23
 
27
24
  if (_.isNil(value)) {
@@ -30,18 +27,20 @@ export default class Attributes {
30
27
  value = Attributes._getCastedFromJsonValue(value, options);
31
28
  }
32
29
 
33
- this.set(key, value);
30
+ this.set(model, key, value);
34
31
  });
35
32
  }
36
33
 
37
34
  /**
35
+ * @param {Model} model
36
+ *
38
37
  * @return {Object} The attributes casted to a json object.
39
38
  */
40
- toJson () {
39
+ toJson (model) {
41
40
  const json = {};
42
41
 
43
- _.each(this.#model.constructor.attributes(), (options, key) => {
44
- let jsonValue = this.get(key);
42
+ _.each(model.constructor.attributes(), (options, key) => {
43
+ let jsonValue = this.get(model, key);
45
44
 
46
45
  jsonValue = Attributes._getCastedToJsonValue(jsonValue, options);
47
46
 
@@ -52,32 +51,34 @@ export default class Attributes {
52
51
  }
53
52
 
54
53
  /**
54
+ * @param {Model} model
55
55
  * @param {string} attribute
56
56
  *
57
57
  * @returns {*} The value of the attribute.
58
58
  */
59
- get (attribute) {
60
- return _.get(this.#model, attribute);
59
+ get (model, attribute) {
60
+ return _.get(model, attribute);
61
61
  }
62
62
 
63
63
  /**
64
- * @param {string|Object} attribute
65
- * @param {*} value
64
+ * @param {Model} model
65
+ * @param {string|Object} attribute
66
+ * @param {*} value
66
67
  *
67
68
  * @returns {Attributes}
68
69
  */
69
- set (attribute, value = null) {
70
+ set (model, attribute, value = null) {
70
71
  if (_.isObject(attribute)) {
71
- _.each(this.#model.constructor.attributes(), (options, key) => {
72
+ _.each(model.constructor.attributes(), (options, key) => {
72
73
  if (! _.has(attribute, key)) return;
73
74
 
74
- this.set(key, attribute[key]);
75
+ this.set(model, key, attribute[key]);
75
76
  });
76
77
 
77
78
  return this;
78
79
  }
79
80
 
80
- this.#model[attribute] = value;
81
+ model[attribute] = value;
81
82
 
82
83
  return this;
83
84
  }
@@ -2,64 +2,65 @@
2
2
  * @implements {Property}
3
3
  */
4
4
  export default class Counts {
5
- #model;
6
-
7
5
  /**
8
6
  * @param {Model} model Instance of the model.
9
7
  */
10
8
  constructor (model) {
11
- this.#model = model;
12
-
13
9
  _.each(model.constructor.counts(), (options, key) => {
14
- this.set(this.key(key, true), _.has(options, 'default') ? options.default : null);
10
+ this.set(model, this.key(key, true), _.has(options, 'default') ? options.default : null);
15
11
  });
16
12
  }
17
13
 
18
14
  /**
15
+ * @param {Model} model
19
16
  * @param {Object} json.
20
17
  */
21
- fromJson (json) {
22
- _.each(this.#model.constructor.counts(), (options, key) => {
18
+ fromJson (model, json) {
19
+ _.each(model.constructor.counts(), (options, key) => {
23
20
  let value = _.get(json, this.key(key));
24
21
 
25
22
  value = value !== undefined
26
23
  ? _.toInteger(value)
27
24
  : null;
28
25
 
29
- this.set(this.key(key, true), value);
26
+ this.set(model, this.key(key, true), value);
30
27
  });
31
28
  }
32
29
 
33
30
  /**
31
+ * @param {Model} model
32
+ *
34
33
  * @return {Object} The attributes casted to a json object.
35
34
  */
36
- toJson () {
35
+ toJson (model) {
37
36
  const json = {};
38
37
 
39
- _.each(this.#model.constructor.counts(), (options, key) => {
40
- _.set(json, key, this.get(key));
38
+ _.each(model.constructor.counts(), (options, key) => {
39
+ _.set(json, key, this.get(model, key));
41
40
  });
42
41
 
43
42
  return json;
44
43
  }
45
44
 
46
45
  /**
46
+ * @param {Model} model
47
47
  * @param {string} attribute
48
48
  *
49
49
  * @returns {*} The value of the attribute.
50
50
  */
51
- get (attribute) {
52
- return _.get(this.#model, this.key(attribute, true));
51
+ get (model, attribute) {
52
+ return _.get(model, this.key(attribute, true));
53
53
  }
54
54
 
55
55
  /**
56
- * @param {string} attribute
57
- * @param {*} value
56
+ * @param {Model} model
57
+ * @param {string} attribute
58
+ * @param {*} value
58
59
  *
59
60
  * @returns {*} The value that was set.
60
61
  */
61
- set (attribute, value) {
62
- this.#model[this.key(attribute, true)] = value;
62
+ set (model, attribute, value) {
63
+ model[this.key(attribute, true)] = value;
63
64
 
64
65
  return value;
65
66
  }
@@ -3,35 +3,40 @@
3
3
  */
4
4
  class Property {
5
5
  /**
6
+ * @param {Model} model
6
7
  * @param {string} key
7
8
  *
8
9
  * @returns {*} The value of the attribute.
9
10
  */
10
- get (key) {
11
+ get (model, key) {
11
12
  throw new Error('not implemented');
12
13
  }
13
14
 
14
15
  /**
15
- * @param {string} key
16
- * @param {*} value
16
+ * @param {Model} model
17
+ * @param {string} key
18
+ * @param {*} value
17
19
  *
18
20
  * @returns {*} The value that was set.
19
21
  */
20
- set (key, value) {
22
+ set (model, key, value) {
21
23
  throw new Error('not implemented');
22
24
  }
23
25
 
24
26
  /**
27
+ * @param {Model} model
25
28
  * @param {Object} json.
26
29
  */
27
- fromJson (json) {
30
+ fromJson (model, json) {
28
31
  throw new Error('not implemented');
29
32
  }
30
33
 
31
34
  /**
35
+ * @param {Model} model
36
+ *
32
37
  * @return {Object} The json object.
33
38
  */
34
- toJson () {
39
+ toJson (model) {
35
40
  throw new Error('not implemented');
36
41
  }
37
42
  }
@@ -4,42 +4,41 @@ import Caster from '../caster';
4
4
  * @implements {Property}
5
5
  */
6
6
  export default class Relations {
7
- #model;
8
-
9
7
  /**
10
8
  * @param {Model} model Instance of the model.
11
9
  */
12
10
  constructor (model) {
13
- this.#model = model;
14
-
15
11
  _.each(model.constructor.relations(), (options, key) => {
16
- this.set(key, _.has(options, 'default') ? options.default : null);
12
+ this.set(model, key, _.has(options, 'default') ? options.default : null);
17
13
  });
18
14
  }
19
15
 
20
16
  /**
17
+ * @param {Model} model
21
18
  * @param {Object} json.
22
19
  */
23
- fromJson (json) {
24
- _.each(this.#model.constructor.relations(), (options, key) => {
20
+ fromJson (model, json) {
21
+ _.each(model.constructor.relations(), (options, key) => {
25
22
  let value = _.get(json, options.jsonKey ?? _.snakeCase(key), _.get(json, _.camelCase(key)));
26
23
 
27
24
  value = value
28
25
  ? Relations._getCastedFromJsonValue(value, options)
29
26
  : value;
30
27
 
31
- this.set(key, value);
28
+ this.set(model, key, value);
32
29
  });
33
30
  }
34
31
 
35
32
  /**
33
+ * @param {Model} model
34
+ *
36
35
  * @return {Object} The attributes casted to a json object.
37
36
  */
38
- toJson () {
37
+ toJson (model) {
39
38
  const json = {};
40
39
 
41
- _.each(this.#model.constructor.relations(), (options, key) => {
42
- let jsonValue = this.get(key);
40
+ _.each(model.constructor.relations(), (options, key) => {
41
+ let jsonValue = this.get(model, key);
43
42
 
44
43
  jsonValue = Relations._getCastedToJsonValue(jsonValue, options);
45
44
 
@@ -50,32 +49,34 @@ export default class Relations {
50
49
  }
51
50
 
52
51
  /**
52
+ * @param {Model} model
53
53
  * @param {string} relation
54
54
  *
55
55
  * @returns {*} The value of the relation.
56
56
  */
57
- get (relation) {
58
- return _.get(this.#model, relation);
57
+ get (model, relation) {
58
+ return _.get(model, relation);
59
59
  }
60
60
 
61
61
  /**
62
- * @param {string|Object} relation
63
- * @param {*} value
62
+ * @param {Model} model
63
+ * @param {string|Object} relation
64
+ * @param {*} value
64
65
  *
65
66
  * @returns {Relations}
66
67
  */
67
- set (relation, value = null) {
68
+ set (model, relation, value = null) {
68
69
  if (_.isObject(relation)) {
69
- _.each(this.#model.constructor.relations(), (options, key) => {
70
+ _.each(model.constructor.relations(), (options, key) => {
70
71
  if (! _.has(relation, key)) return;
71
72
 
72
- this.set(key, relation[key]);
73
+ this.set(model, key, relation[key]);
73
74
  });
74
75
 
75
76
  return this;
76
77
  }
77
78
 
78
- this.#model[relation] = value;
79
+ model[relation] = value;
79
80
 
80
81
  return this;
81
82
  }
package/src/request.js CHANGED
@@ -68,7 +68,7 @@ export default class Request {
68
68
  const url = this.url ?? this.constructor.endpoint;
69
69
 
70
70
  this._response = await this._connection.get(
71
- `${url}`,
71
+ url,
72
72
  this.bodyParameters,
73
73
  );
74
74
 
@@ -86,7 +86,7 @@ export default class Request {
86
86
  const url = this.url ?? this.constructor.endpoint;
87
87
 
88
88
  this._response = await this._connection.post(
89
- `${url}`,
89
+ url,
90
90
  data,
91
91
  );
92
92
 
@@ -104,7 +104,7 @@ export default class Request {
104
104
  const url = this.url ?? this.constructor.endpoint;
105
105
 
106
106
  this._response = await this._connection.put(
107
- `${url}`,
107
+ url,
108
108
  { ...data, ...this.bodyParameters },
109
109
  );
110
110
 
@@ -120,7 +120,7 @@ export default class Request {
120
120
  const url = this.url ?? this.constructor.endpoint;
121
121
 
122
122
  this._response = await this._connection.delete(
123
- `${url}`,
123
+ url,
124
124
  );
125
125
 
126
126
  await this.triggerResponseEvents(this._response);