|
1 | 1 | import * as Utils from '../lib/utils.js'; |
2 | 2 | import Entry from './entry'; |
3 | 3 |
|
4 | | -const _extend = Utils._extend; |
| 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 | +}; |
5 | 79 |
|
6 | 80 | /** |
7 | 81 | * @function getRequestUrl |
8 | 82 | * @description Returns host url based on this.type |
9 | 83 | * @param {Object} this `this` variable from Query class |
10 | 84 | * @return {string} returns the url that will be used to make API calls |
11 | 85 | */ |
12 | | -function getRequestUrl(type, config, baseURL) { |
| 86 | +function getRequestUrl(type, config, content_type_uid, baseURL) { |
| 87 | + let url; |
13 | 88 | switch(type) { |
14 | 89 | case 'asset': |
15 | 90 | url = baseURL + config.urls.assets; |
@@ -390,7 +465,7 @@ export default class Query extends Entry { |
390 | 465 | */ |
391 | 466 | count() { |
392 | 467 | const host = this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version, |
393 | | - url = getRequestUrl(this.type, this.config, host); |
| 468 | + url = getRequestUrl(this.type, this.config, this.content_type_uid, host); |
394 | 469 | this._query['count'] = true; |
395 | 470 | this.requestParams = { |
396 | 471 | method: 'POST', |
@@ -700,7 +775,7 @@ export default class Query extends Entry { |
700 | 775 | host = this.live_preview.host; |
701 | 776 | } |
702 | 777 | const baseURL = this.config.protocol + "://" + host + '/' + this.config.version |
703 | | - const url = getRequestUrl(this.type, this.config, baseURL) |
| 778 | + const url = getRequestUrl(this.type, this.config, this.content_type_uid, baseURL) |
704 | 779 |
|
705 | 780 |
|
706 | 781 | this.requestParams = { |
@@ -736,7 +811,7 @@ export default class Query extends Entry { |
736 | 811 | if(this.type && this.type !== 'asset' && this.live_preview && this.live_preview.enable === true && this.live_preview.content_type_uid === this.content_type_uid ) { |
737 | 812 | host = this.config.protocol + "://" + this.live_preview.host + '/' + this.config.version |
738 | 813 | } |
739 | | - const url = getRequestUrl(this.type, this.config, host) |
| 814 | + const url = getRequestUrl(this.type, this.config, this.content_type_uid, host) |
740 | 815 |
|
741 | 816 | this.singleEntry = true; |
742 | 817 | this._query.limit = 1; |
|
0 commit comments