Skip to content

Commit 9b2c4da

Browse files
feat: added , , and for taxonomy query
1 parent 084eed9 commit 9b2c4da

File tree

5 files changed

+125
-89
lines changed

5 files changed

+125
-89
lines changed

.talismanrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ fileignoreconfig:
33
checksum: b55c22a1b5dca347c79fea4cac3a0a3ea7487180cb21e4e3d5b2bbdf1981cebe
44
- filename: test/typescript/taxonomy.test.ts
55
checksum: e4bdf633e147fd60d929d379f20c814eed5f68b11421d7b53ec8826e9142de37
6+
- filename: src/core/modules/taxonomy.js
7+
checksum: d9406f41360c036cb14b8ba2ec2edb265bd5d47de0060c4f4593e4750ee2e322
8+
- filename: src/core/lib/utils.js
9+
checksum: 6018f9f13fa32b724d09b9cdf5f78cf030a6332ca549651e1e35fe91e8c7e0e7
610
version: ""

src/core/lib/utils.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,3 +556,79 @@ 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: 9 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,25 @@
11
import * as Utils from '../lib/utils.js';
22
import Entry from './entry';
33

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;
795

806
/**
817
* @function getRequestUrl
828
* @description Returns host url based on this.type
839
* @param {Object} this `this` variable from Query class
8410
* @return {string} returns the url that will be used to make API calls
8511
*/
86-
function getRequestUrl(this, baseURL) {
87-
switch(this.type) {
12+
function getRequestUrl(type, config, baseURL) {
13+
switch(type) {
8814
case 'asset':
89-
url = baseURL + this.config.urls.assets;
15+
url = baseURL + config.urls.assets;
9016
break;
9117
case 'taxonomy':
92-
url = baseURL + this.config.urls.taxonomies + this.config.urls.entries;
18+
url = baseURL + config.urls.taxonomies + config.urls.entries;
9319
break;
9420
case 'contentType':
9521
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;
9723
break;
9824
}
9925
return url;
@@ -464,7 +390,7 @@ export default class Query extends Entry {
464390
*/
465391
count() {
466392
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);
468394
this._query['count'] = true;
469395
this.requestParams = {
470396
method: 'POST',
@@ -774,7 +700,7 @@ export default class Query extends Entry {
774700
host = this.live_preview.host;
775701
}
776702
const baseURL = this.config.protocol + "://" + host + '/' + this.config.version
777-
const url = getRequestUrl(this, baseURL)
703+
const url = getRequestUrl(this.type, this.config, baseURL)
778704

779705

780706
this.requestParams = {
@@ -810,7 +736,7 @@ export default class Query extends Entry {
810736
if(this.type && this.type !== 'asset' && this.live_preview && this.live_preview.enable === true && this.live_preview.content_type_uid === this.content_type_uid ) {
811737
host = this.config.protocol + "://" + this.live_preview.host + '/' + this.config.version
812738
}
813-
const url = getRequestUrl(this, host)
739+
const url = getRequestUrl(this.type, this.config, host)
814740

815741
this.singleEntry = true;
816742
this._query.limit = 1;

src/core/modules/taxonomy.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Query from "./query";
2+
import * as Utils from "../lib/utils"
3+
4+
const _extend = Utils._extend
5+
6+
// 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+
};
20+
}
21+
22+
export default class TaxonomyQuery extends Query {
23+
constructor() {
24+
super();
25+
this.above = _extend.compare('$above')
26+
this.equalAndAbove = _extend.compare('$eq_above')
27+
this.below = _extend.compare('$below')
28+
this.equalAndBelow = _extend.compare('$eq_below')
29+
}
30+
}

src/core/stack.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Utils from './lib/utils';
33
import Entry from './modules/entry';
44
import Assets from './modules/assets';
55
import Query from './modules/query';
6+
import TaxonomyQuery from './modules/taxonomy';
67
import Request from './lib/request';
78
import CacheProvider from './cache-provider/index';
89
let errorRetry = [408, 429]
@@ -355,11 +356,8 @@ export default class Stack {
355356
* [Taxonomies description]
356357
* @param {[type]} uid [description]
357358
*/
358-
Taxonomies(uid) {
359-
if (uid && typeof uid === 'string') {
360-
this.taxonomy_uid = uid;
361-
this.type = "taxonomy"
362-
}
359+
Taxonomies() {
360+
this.type = "taxonomy"
363361
return this;
364362
}
365363

@@ -456,7 +454,9 @@ export default class Stack {
456454
* @instance
457455
*/
458456
Query() {
459-
let query = new Query();
457+
let query = (this.type === "taxonomy" || this.type === "contentType") ?
458+
new TaxonomyQuery() :
459+
new Query();
460460
return Utils.merge(query, this);
461461
}
462462

0 commit comments

Comments
 (0)