|
1 | 1 | import * as Utils from '../lib/utils.js'; |
2 | 2 | import Entry from './entry'; |
3 | 3 |
|
4 | | -const _extend = { |
5 | | - compare: function(type) { |
6 | | - return function(key, value) { |
7 | | - if (key && value && typeof key === 'string' && typeof value !== 'undefined') { |
8 | | - this._query['query'][key] = this._query['query']['file_size'] || {}; |
9 | | - this._query['query'][key][type] = value; |
10 | | - return this; |
11 | | - } else { |
12 | | - if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); |
13 | | - } |
14 | | - }; |
15 | | - }, |
16 | | - contained: function(bool) { |
17 | | - let type = (bool) ? '$in' : '$nin'; |
18 | | - return function(key, value) { |
19 | | - if (key && value && typeof key === 'string' && Array.isArray(value)) { |
20 | | - this._query['query'][key] = this._query['query'][key] || {}; |
21 | | - this._query['query'][key][type] = this._query['query'][key][type] || []; |
22 | | - this._query['query'][key][type] = this._query['query'][key][type].concat(value); |
23 | | - return this; |
24 | | - } else { |
25 | | - if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); |
26 | | - } |
27 | | - }; |
28 | | - }, |
29 | | - exists: function(bool) { |
30 | | - return function(key) { |
31 | | - if (key && typeof key === 'string') { |
32 | | - this._query['query'][key] = this._query['query'][key] || {}; |
33 | | - this._query['query'][key]['$exists'] = bool; |
34 | | - return this; |
35 | | - } else { |
36 | | - if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); |
37 | | - } |
38 | | - }; |
39 | | - }, |
40 | | - logical: function(type) { |
41 | | - return function() { |
42 | | - let _query = []; |
43 | | - for (let i = 0, _i = arguments.length; i < _i; i++) { |
44 | | - if (arguments[i] instanceof Query && arguments[i]._query.query) { |
45 | | - _query.push(arguments[i]._query.query); |
46 | | - } else if (typeof arguments[i] === "object") { |
47 | | - _query.push(arguments[i]); |
48 | | - } |
49 | | - } |
50 | | - if (this._query['query'][type]) { |
51 | | - this._query['query'][type] = this._query['query'][type].concat(_query); |
52 | | - } else { |
53 | | - this._query['query'][type] = _query; |
54 | | - } |
55 | | - return this; |
56 | | - }; |
57 | | - }, |
58 | | - sort: function(type) { |
59 | | - return function(key) { |
60 | | - if (key && typeof key === 'string') { |
61 | | - this._query[type] = key; |
62 | | - return this; |
63 | | - } else { |
64 | | - if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Argument should be a string."); |
65 | | - } |
66 | | - }; |
67 | | - }, |
68 | | - pagination: function(type) { |
69 | | - return function(value) { |
70 | | - if (typeof value === 'number') { |
71 | | - this._query[type] = value; |
72 | | - return this; |
73 | | - } else { |
74 | | - if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Argument should be a number."); |
75 | | - } |
76 | | - } |
77 | | - } |
78 | | -}; |
| 4 | +const _extend = Utils._extend; |
79 | 5 |
|
80 | 6 | /** |
81 | 7 | * @function getRequestUrl |
82 | 8 | * @description Returns host url based on this.type |
83 | 9 | * @param {Object} this `this` variable from Query class |
84 | 10 | * @return {string} returns the url that will be used to make API calls |
85 | 11 | */ |
86 | | -function getRequestUrl(this, baseURL) { |
87 | | - switch(this.type) { |
| 12 | +function getRequestUrl(type, config, baseURL) { |
| 13 | + switch(type) { |
88 | 14 | case 'asset': |
89 | | - url = baseURL + this.config.urls.assets; |
| 15 | + url = baseURL + config.urls.assets; |
90 | 16 | break; |
91 | 17 | case 'taxonomy': |
92 | | - url = baseURL + this.config.urls.taxonomies + this.config.urls.entries; |
| 18 | + url = baseURL + config.urls.taxonomies + config.urls.entries; |
93 | 19 | break; |
94 | 20 | case 'contentType': |
95 | 21 | default: |
96 | | - url = baseURL + this.config.urls.content_types + this.content_type_uid + this.config.urls.entries; |
| 22 | + url = baseURL + config.urls.content_types + content_type_uid + config.urls.entries; |
97 | 23 | break; |
98 | 24 | } |
99 | 25 | return url; |
@@ -464,7 +390,7 @@ export default class Query extends Entry { |
464 | 390 | */ |
465 | 391 | count() { |
466 | 392 | const host = this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version, |
467 | | - url = getRequestUrl(this, host); |
| 393 | + url = getRequestUrl(this.type, this.config, host); |
468 | 394 | this._query['count'] = true; |
469 | 395 | this.requestParams = { |
470 | 396 | method: 'POST', |
@@ -774,7 +700,7 @@ export default class Query extends Entry { |
774 | 700 | host = this.live_preview.host; |
775 | 701 | } |
776 | 702 | const baseURL = this.config.protocol + "://" + host + '/' + this.config.version |
777 | | - const url = getRequestUrl(this, baseURL) |
| 703 | + const url = getRequestUrl(this.type, this.config, baseURL) |
778 | 704 |
|
779 | 705 |
|
780 | 706 | this.requestParams = { |
@@ -810,7 +736,7 @@ export default class Query extends Entry { |
810 | 736 | if(this.type && this.type !== 'asset' && this.live_preview && this.live_preview.enable === true && this.live_preview.content_type_uid === this.content_type_uid ) { |
811 | 737 | host = this.config.protocol + "://" + this.live_preview.host + '/' + this.config.version |
812 | 738 | } |
813 | | - const url = getRequestUrl(this, host) |
| 739 | + const url = getRequestUrl(this.type, this.config, host) |
814 | 740 |
|
815 | 741 | this.singleEntry = true; |
816 | 742 | this._query.limit = 1; |
|
0 commit comments