Skip to content

Commit 0efe6da

Browse files
refactor: moved _extend
1 parent 23268ba commit 0efe6da

File tree

3 files changed

+95
-97
lines changed

3 files changed

+95
-97
lines changed

src/core/lib/utils.js

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -556,79 +556,3 @@ function getIncludeParamForReference(referenceMap) {
556556
buildParamStringRecursively(referenceMap, "");
557557
return newRefences.filter((currentReference) => currentReference !== "");
558558
}
559-
560-
export const _extend = {
561-
compare: function(type) {
562-
return function(key, value) {
563-
if (key && value && typeof key === 'string' && typeof value !== 'undefined') {
564-
this._query['query'][key] = this._query['query']['file_size'] || {};
565-
this._query['query'][key][type] = value;
566-
return this;
567-
} else {
568-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters.");
569-
}
570-
};
571-
},
572-
contained: function(bool) {
573-
let type = (bool) ? '$in' : '$nin';
574-
return function(key, value) {
575-
if (key && value && typeof key === 'string' && Array.isArray(value)) {
576-
this._query['query'][key] = this._query['query'][key] || {};
577-
this._query['query'][key][type] = this._query['query'][key][type] || [];
578-
this._query['query'][key][type] = this._query['query'][key][type].concat(value);
579-
return this;
580-
} else {
581-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters.");
582-
}
583-
};
584-
},
585-
exists: function(bool) {
586-
return function(key) {
587-
if (key && typeof key === 'string') {
588-
this._query['query'][key] = this._query['query'][key] || {};
589-
this._query['query'][key]['$exists'] = bool;
590-
return this;
591-
} else {
592-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters.");
593-
}
594-
};
595-
},
596-
logical: function(type) {
597-
return function() {
598-
let _query = [];
599-
for (let i = 0, _i = arguments.length; i < _i; i++) {
600-
if (arguments[i] instanceof Query && arguments[i]._query.query) {
601-
_query.push(arguments[i]._query.query);
602-
} else if (typeof arguments[i] === "object") {
603-
_query.push(arguments[i]);
604-
}
605-
}
606-
if (this._query['query'][type]) {
607-
this._query['query'][type] = this._query['query'][type].concat(_query);
608-
} else {
609-
this._query['query'][type] = _query;
610-
}
611-
return this;
612-
};
613-
},
614-
sort: function(type) {
615-
return function(key) {
616-
if (key && typeof key === 'string') {
617-
this._query[type] = key;
618-
return this;
619-
} else {
620-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Argument should be a string.");
621-
}
622-
};
623-
},
624-
pagination: function(type) {
625-
return function(value) {
626-
if (typeof value === 'number') {
627-
this._query[type] = value;
628-
return this;
629-
} else {
630-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Argument should be a number.");
631-
}
632-
}
633-
}
634-
};

src/core/modules/query.js

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,90 @@
11
import * as Utils from '../lib/utils.js';
22
import Entry from './entry';
33

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+
};
579

680
/**
781
* @function getRequestUrl
882
* @description Returns host url based on this.type
983
* @param {Object} this `this` variable from Query class
1084
* @return {string} returns the url that will be used to make API calls
1185
*/
12-
function getRequestUrl(type, config, baseURL) {
86+
function getRequestUrl(type, config, content_type_uid, baseURL) {
87+
let url;
1388
switch(type) {
1489
case 'asset':
1590
url = baseURL + config.urls.assets;
@@ -390,7 +465,7 @@ export default class Query extends Entry {
390465
*/
391466
count() {
392467
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);
394469
this._query['count'] = true;
395470
this.requestParams = {
396471
method: 'POST',
@@ -700,7 +775,7 @@ export default class Query extends Entry {
700775
host = this.live_preview.host;
701776
}
702777
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)
704779

705780

706781
this.requestParams = {
@@ -736,7 +811,7 @@ export default class Query extends Entry {
736811
if(this.type && this.type !== 'asset' && this.live_preview && this.live_preview.enable === true && this.live_preview.content_type_uid === this.content_type_uid ) {
737812
host = this.config.protocol + "://" + this.live_preview.host + '/' + this.config.version
738813
}
739-
const url = getRequestUrl(this.type, this.config, host)
814+
const url = getRequestUrl(this.type, this.config, this.content_type_uid, host)
740815

741816
this.singleEntry = true;
742817
this._query.limit = 1;

src/core/modules/taxonomy.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import Query from "./query";
2-
import * as Utils from "../lib/utils"
3-
4-
const _extend = Utils._extend
52

63
// Overrideing compare function to include level
7-
_extend.compare = function(type) {
8-
return function(key, value, level=0) {
9-
if (key && value && typeof key === 'string' && typeof value !== 'undefined') {
10-
this._query['query'][key] = this._query['query']['file_size'] || {};
11-
this._query['query'][key][type] = value;
12-
if (level > 0 && level <= 10) {
13-
this._query['query'][key]['level'] = level
14-
}
15-
return this;
16-
} else {
17-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters.");
18-
}
19-
};
4+
const _extend = {
5+
compare: function(type) {
6+
return function(key, value, level=0) {
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+
if (level > 0 && level <= 10) {
11+
this._query['query'][key]['level'] = level
12+
}
13+
return this;
14+
} else {
15+
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters.");
16+
}
17+
};
18+
}
2019
}
2120

2221
export default class TaxonomyQuery extends Query {

0 commit comments

Comments
 (0)