@weapnl/js-junction 0.0.2 → 0.0.4

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/index.d.ts CHANGED
@@ -53,10 +53,12 @@ declare class Mixins {
53
53
  scope(name: string, ...params: any[]): this;
54
54
  scopes(...params: any[]): this;
55
55
  search(value: any, columns?: any[]): this;
56
- where(column: string, operator: string, value: any): this;
56
+ where(column: string, operator: string, value?: any): this;
57
57
  wheres(...params: any[]): this;
58
58
  whereIn(column: string, values: any[]): this;
59
59
  whereIns(...params: any[]): this;
60
+ whereNotIn(column: string, values: any[]): this;
61
+ whereNotIns(...params: any[]): this;
60
62
  pluck(fields: any[]): this;
61
63
 
62
64
  appends(appends: string | string[]): this;
@@ -84,7 +86,8 @@ declare class Request extends Mixins {
84
86
  onUnauthorized(callback?: (response: Response) => void): this;
85
87
  onForbidden(callback?: (response: Response) => void): this;
86
88
  triggerResponseEvents(response: Response, successResponse?: any): Promise<void>;
87
- customParameters(parameters?: any[]): this;
89
+ customParameters(parameters?: object): this;
90
+ setApi(api: Api): this;
88
91
  }
89
92
 
90
93
  export class Model extends Request {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weapnl/js-junction",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "This project allows you to easily consume API's built with Junction.",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
@@ -4,11 +4,13 @@ import Caster from '../caster';
4
4
  * @implements {Property}
5
5
  */
6
6
  export default class Accessors {
7
+ #model;
8
+
7
9
  /**
8
10
  * @param {Model} model Instance of the model.
9
11
  */
10
12
  constructor (model) {
11
- this.model = model;
13
+ this.#model = model;
12
14
 
13
15
  _.each(model.constructor.accessors(), (options, key) => {
14
16
  this.set(key, _.has(options, 'default') ? options.default : null);
@@ -19,7 +21,7 @@ export default class Accessors {
19
21
  * @param {Object} json.
20
22
  */
21
23
  fromJson (json) {
22
- _.each(this.model.constructor.accessors(), (options, key) => {
24
+ _.each(this.#model.constructor.accessors(), (options, key) => {
23
25
  let value = _.get(json, options.jsonKey ?? _.snakeCase(key), _.get(json, _.camelCase(key)));
24
26
 
25
27
  if (_.isNil(value)) {
@@ -38,7 +40,7 @@ export default class Accessors {
38
40
  toJson () {
39
41
  const json = {};
40
42
 
41
- _.each(this.model.constructor.accessors(), (options, key) => {
43
+ _.each(this.#model.constructor.accessors(), (options, key) => {
42
44
  let jsonValue = this.get(key);
43
45
 
44
46
  jsonValue = Accessors._getCastedToJsonValue(jsonValue, options);
@@ -55,7 +57,7 @@ export default class Accessors {
55
57
  * @returns {*} The value of the attribute.
56
58
  */
57
59
  get (attribute) {
58
- return _.get(this.model, attribute);
60
+ return _.get(this.#model, attribute);
59
61
  }
60
62
 
61
63
  /**
@@ -65,7 +67,7 @@ export default class Accessors {
65
67
  * @returns {*} The value that was set.
66
68
  */
67
69
  set (attribute, value) {
68
- this.model[attribute] = value;
70
+ this.#model[attribute] = value;
69
71
 
70
72
  return value;
71
73
  }
@@ -4,11 +4,13 @@ import Caster from '../caster';
4
4
  * @implements {Property}
5
5
  */
6
6
  export default class Attributes {
7
+ #model;
8
+
7
9
  /**
8
10
  * @param {Model} model Instance of the model.
9
11
  */
10
12
  constructor (model) {
11
- this.model = model;
13
+ this.#model = model;
12
14
 
13
15
  _.each(model.constructor.attributes(), (options, key) => {
14
16
  this.set(key, _.has(options, 'default') ? options.default : null);
@@ -19,7 +21,7 @@ export default class Attributes {
19
21
  * @param {Object} json.
20
22
  */
21
23
  fromJson (json) {
22
- _.each(this.model.constructor.attributes(), (options, key) => {
24
+ _.each(this.#model.constructor.attributes(), (options, key) => {
23
25
  let value = _.get(json, options.jsonKey ?? _.snakeCase(key), _.get(json, _.camelCase(key)));
24
26
 
25
27
  if (_.isNil(value)) {
@@ -38,7 +40,7 @@ export default class Attributes {
38
40
  toJson () {
39
41
  const json = {};
40
42
 
41
- _.each(this.model.constructor.attributes(), (options, key) => {
43
+ _.each(this.#model.constructor.attributes(), (options, key) => {
42
44
  let jsonValue = this.get(key);
43
45
 
44
46
  jsonValue = Attributes._getCastedToJsonValue(jsonValue, options);
@@ -55,7 +57,7 @@ export default class Attributes {
55
57
  * @returns {*} The value of the attribute.
56
58
  */
57
59
  get (attribute) {
58
- return _.get(this.model, attribute);
60
+ return _.get(this.#model, attribute);
59
61
  }
60
62
 
61
63
  /**
@@ -66,7 +68,7 @@ export default class Attributes {
66
68
  */
67
69
  set (attribute, value = null) {
68
70
  if (_.isObject(attribute)) {
69
- _.each(this.model.constructor.attributes(), (options, key) => {
71
+ _.each(this.#model.constructor.attributes(), (options, key) => {
70
72
  if (! _.has(attribute, key)) return;
71
73
 
72
74
  this.set(key, attribute[key]);
@@ -75,7 +77,7 @@ export default class Attributes {
75
77
  return this;
76
78
  }
77
79
 
78
- this.model[attribute] = value;
80
+ this.#model[attribute] = value;
79
81
 
80
82
  return this;
81
83
  }
@@ -2,11 +2,13 @@
2
2
  * @implements {Property}
3
3
  */
4
4
  export default class Counts {
5
+ #model;
6
+
5
7
  /**
6
8
  * @param {Model} model Instance of the model.
7
9
  */
8
10
  constructor (model) {
9
- this.model = model;
11
+ this.#model = model;
10
12
 
11
13
  _.each(model.constructor.counts(), (options, key) => {
12
14
  this.set(this.key(key, true), _.has(options, 'default') ? options.default : null);
@@ -17,7 +19,7 @@ export default class Counts {
17
19
  * @param {Object} json.
18
20
  */
19
21
  fromJson (json) {
20
- _.each(this.model.constructor.counts(), (options, key) => {
22
+ _.each(this.#model.constructor.counts(), (options, key) => {
21
23
  let value = _.get(json, this.key(key));
22
24
 
23
25
  value = value !== undefined
@@ -34,7 +36,7 @@ export default class Counts {
34
36
  toJson () {
35
37
  const json = {};
36
38
 
37
- _.each(this.model.constructor.counts(), (options, key) => {
39
+ _.each(this.#model.constructor.counts(), (options, key) => {
38
40
  _.set(json, key, this.get(key));
39
41
  });
40
42
 
@@ -47,7 +49,7 @@ export default class Counts {
47
49
  * @returns {*} The value of the attribute.
48
50
  */
49
51
  get (attribute) {
50
- return _.get(this.model, this.key(attribute, true));
52
+ return _.get(this.#model, this.key(attribute, true));
51
53
  }
52
54
 
53
55
  /**
@@ -57,7 +59,7 @@ export default class Counts {
57
59
  * @returns {*} The value that was set.
58
60
  */
59
61
  set (attribute, value) {
60
- this.model[this.key(attribute, true)] = value;
62
+ this.#model[this.key(attribute, true)] = value;
61
63
 
62
64
  return value;
63
65
  }
@@ -4,11 +4,13 @@ import Caster from '../caster';
4
4
  * @implements {Property}
5
5
  */
6
6
  export default class Relations {
7
+ #model;
8
+
7
9
  /**
8
10
  * @param {Model} model Instance of the model.
9
11
  */
10
12
  constructor (model) {
11
- this.model = model;
13
+ this.#model = model;
12
14
 
13
15
  _.each(model.constructor.relations(), (options, key) => {
14
16
  this.set(key, _.has(options, 'default') ? options.default : null);
@@ -19,7 +21,7 @@ export default class Relations {
19
21
  * @param {Object} json.
20
22
  */
21
23
  fromJson (json) {
22
- _.each(this.model.constructor.relations(), (options, key) => {
24
+ _.each(this.#model.constructor.relations(), (options, key) => {
23
25
  let value = _.get(json, options.jsonKey ?? _.snakeCase(key), _.get(json, _.camelCase(key)));
24
26
 
25
27
  value = value
@@ -36,7 +38,7 @@ export default class Relations {
36
38
  toJson () {
37
39
  const json = {};
38
40
 
39
- _.each(this.model.constructor.relations(), (options, key) => {
41
+ _.each(this.#model.constructor.relations(), (options, key) => {
40
42
  let jsonValue = this.get(key);
41
43
 
42
44
  jsonValue = Relations._getCastedToJsonValue(jsonValue, options);
@@ -53,7 +55,7 @@ export default class Relations {
53
55
  * @returns {*} The value of the relation.
54
56
  */
55
57
  get (relation) {
56
- return _.get(this.model, relation);
58
+ return _.get(this.#model, relation);
57
59
  }
58
60
 
59
61
  /**
@@ -64,7 +66,7 @@ export default class Relations {
64
66
  */
65
67
  set (relation, value = null) {
66
68
  if (_.isObject(relation)) {
67
- _.each(this.model.constructor.relations(), (options, key) => {
69
+ _.each(this.#model.constructor.relations(), (options, key) => {
68
70
  if (! _.has(relation, key)) return;
69
71
 
70
72
  this.set(key, relation[key]);
@@ -73,7 +75,7 @@ export default class Relations {
73
75
  return this;
74
76
  }
75
77
 
76
- this.model[relation] = value;
78
+ this.#model[relation] = value;
77
79
 
78
80
  return this;
79
81
  }
package/src/connection.js CHANGED
@@ -6,6 +6,7 @@ export default class Connection {
6
6
  this._abortController = null;
7
7
 
8
8
  this._config = {};
9
+ this._api = null;
9
10
 
10
11
  this.running = false;
11
12
  this.canceled = false;
@@ -24,6 +25,10 @@ export default class Connection {
24
25
  this._config = config;
25
26
  }
26
27
 
28
+ setApi (api) {
29
+ this._api = api;
30
+ }
31
+
27
32
  async get (query, params) {
28
33
  return this._execute(query, 'get', params);
29
34
  }
@@ -48,7 +53,7 @@ export default class Connection {
48
53
  }
49
54
 
50
55
  const config = {
51
- url: api.baseUrl + url,
56
+ url: (this._api ? this._api.baseUrl : api.baseUrl) + url,
52
57
  method,
53
58
  ...({
54
59
  [method === 'get' ? 'params' : 'data']: data,
@@ -5,6 +5,7 @@ import Scopes from './scopes';
5
5
  import Search from './search';
6
6
  import Wheres from './wheres';
7
7
  import WhereIn from './whereIn';
8
+ import WhereNotIn from './whereNotIn';
8
9
  import Count from './count';
9
10
  import Pluck from './pluck';
10
11
 
@@ -18,13 +19,14 @@ export default class Filters {
18
19
  this.search = new Search();
19
20
  this.wheres = new Wheres();
20
21
  this.whereIn = new WhereIn();
22
+ this.whereNotIn = new WhereNotIn();
21
23
  this.pluck = new Pluck();
22
24
  }
23
25
 
24
26
  toObject () {
25
27
  const items = [];
26
28
 
27
- for (let i = 0, filters = ['count', 'limit', 'order', 'relations', 'scopes', 'search', 'wheres', 'whereIn', 'pluck']; i < filters.length; i++) {
29
+ for (let i = 0, filters = ['count', 'limit', 'order', 'relations', 'scopes', 'search', 'wheres', 'whereIn', 'whereNotIn', 'pluck']; i < filters.length; i++) {
28
30
  if (this[filters[i]].filled()) items.push(this[filters[i]].toObject());
29
31
  }
30
32
 
@@ -1,7 +1,7 @@
1
1
  import Filter from './filter';
2
2
  import Format from '../utilities/format';
3
3
 
4
- export default class Relations extends Filter {
4
+ export default class WhereIn extends Filter {
5
5
  constructor () {
6
6
  super();
7
7
 
@@ -0,0 +1,31 @@
1
+ import Filter from './filter';
2
+ import Format from '../utilities/format';
3
+
4
+ export default class WhereNotIn extends Filter {
5
+ constructor () {
6
+ super();
7
+
8
+ this._whereNotIns = [];
9
+ }
10
+
11
+ filled () {
12
+ return this._whereNotIns.length > 0;
13
+ }
14
+
15
+ add (column, values) {
16
+ this._whereNotIns.push({
17
+ column: Format.snakeCase(column),
18
+ values,
19
+ });
20
+ }
21
+
22
+ toObject () {
23
+ const data = {};
24
+
25
+ if (this.filled()) {
26
+ data.where_not_in = this._whereNotIns;
27
+ }
28
+
29
+ return data;
30
+ }
31
+ }
package/src/index.js CHANGED
@@ -7,6 +7,6 @@ window._ = _;
7
7
  const api = new Api();
8
8
  window.api = api;
9
9
 
10
- export { Model };
10
+ export { Model, Api };
11
11
 
12
12
  export default api;
@@ -56,6 +56,18 @@ const filterMixin = {
56
56
  return this;
57
57
  },
58
58
 
59
+ scopes (input) {
60
+ input.forEach(scope => {
61
+ if (Array.isArray(scope)) {
62
+ this.scope(...scope);
63
+ } else {
64
+ this.scope(scope);
65
+ }
66
+ });
67
+
68
+ return this;
69
+ },
70
+
59
71
  search (value, columns = []) {
60
72
  if (! Array.isArray(columns)) columns = [columns];
61
73
 
@@ -76,8 +88,12 @@ const filterMixin = {
76
88
  return this;
77
89
  },
78
90
 
79
- wheres (...params) {
80
- return this.where(...params);
91
+ wheres (input) {
92
+ input.forEach(where => {
93
+ this.where(...where);
94
+ });
95
+
96
+ return this;
81
97
  },
82
98
 
83
99
  whereIn (column, values) {
@@ -88,8 +104,28 @@ const filterMixin = {
88
104
  return this;
89
105
  },
90
106
 
91
- whereIns (...params) {
92
- return this.whereIn(...params);
107
+ whereIns (input) {
108
+ input.forEach(whereIn => {
109
+ this.whereIn(...whereIn);
110
+ });
111
+
112
+ return this;
113
+ },
114
+
115
+ whereNotIn (column, values) {
116
+ if (! Array.isArray(values)) values = [values];
117
+
118
+ this._filters.whereNotIn.add(column, values);
119
+
120
+ return this;
121
+ },
122
+
123
+ whereNotIns (input) {
124
+ input.forEach(whereIn => {
125
+ this.whereNotIn(...whereIn);
126
+ });
127
+
128
+ return this;
93
129
  },
94
130
 
95
131
  pluck (fields) {
package/src/request.js CHANGED
@@ -8,6 +8,7 @@ import actionMixin from './mixins/actionMixin';
8
8
  import filterMixin from './mixins/filterMixin';
9
9
  import modifierMixin from './mixins/modifierMixin';
10
10
  import paginationMixin from './mixins/paginationMixin';
11
+ import Api from './api';
11
12
 
12
13
  /**
13
14
  * @mixes actionMixin
@@ -163,7 +164,7 @@ export default class Request {
163
164
  this._modifiers.toObject(),
164
165
  this._pagination.toObject(),
165
166
  this._action.toObject(),
166
- ...this._customParameters,
167
+ _.merge(...this._customParameters),
167
168
  ]));
168
169
  }
169
170
 
@@ -261,12 +262,12 @@ export default class Request {
261
262
  /**
262
263
  * Add custom parameters to the request.
263
264
  *
264
- * @param {Array} parameters
265
+ * @param {Object} parameters
265
266
  *
266
267
  * @returns {this} The current instance.
267
268
  */
268
- customParameters (parameters = []) {
269
- this._customParameters.push(...parameters);
269
+ customParameters (parameters = {}) {
270
+ this._customParameters.push(parameters);
270
271
 
271
272
  return this;
272
273
  }
@@ -309,6 +310,17 @@ export default class Request {
309
310
 
310
311
  return formData;
311
312
  }
313
+
314
+ /**
315
+ * @param {Api} api
316
+ *
317
+ * @returns {this} The current instance.
318
+ */
319
+ setApi (api) {
320
+ this._connection.setApi(api);
321
+
322
+ return this;
323
+ }
312
324
  }
313
325
 
314
326
  Object.assign(Request.prototype, actionMixin);