Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/cs 42865 taxonomy support #103

Closed
wants to merge 10 commits into from
11 changes: 8 additions & 3 deletions .talismanrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
threshold: medium
fileignoreconfig:
- filename: package-lock.json
checksum: 0dbf56c1fd0eda1145d69b718de5b7d419650556492851c343fab05867e233a2
version: ""
checksum: b55c22a1b5dca347c79fea4cac3a0a3ea7487180cb21e4e3d5b2bbdf1981cebe
- filename: test/typescript/taxonomy.test.ts
checksum: e4bdf633e147fd60d929d379f20c814eed5f68b11421d7b53ec8826e9142de37
- filename: src/core/modules/taxonomy.js
checksum: d9406f41360c036cb14b8ba2ec2edb265bd5d47de0060c4f4593e4750ee2e322
- filename: src/core/lib/utils.js
checksum: 6018f9f13fa32b724d09b9cdf5f78cf030a6332ca549651e1e35fe91e8c7e0e7
version: ""
3 changes: 2 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const config = {
content_types: "/content_types/",
entries: "/entries/",
assets: "/assets/",
environments: "/environments/"
environments: "/environments/",
taxonomies: "/taxonomies/"
},
live_preview: {
enable: false,
Expand Down
22,341 changes: 10,398 additions & 11,943 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 9 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "contentstack",
"version": "3.17.2",
"version": "3.17.3",
"description": "Contentstack Javascript SDK",
"homepage": "https://www.contentstack.com/",
"author": {
Expand Down Expand Up @@ -65,40 +65,36 @@
"tmp": "tmp/contentstack-3.15.0.tgz_1477830884275_0.9869455888401717"
},
"devDependencies": {
"@babel/core": "^7.23.3",
"@babel/preset-env": "^7.23.3",
"@babel/runtime": "^7.23.4",
"@types/jest": "^26.0.24",
"babel-core": "6.26.0",
"babel-loader": "7.1.2",
"babel-plugin-transform-runtime": "6.23.0",
"babel-preset-env": "1.6.1",
"babel-preset-es2015": "6.24.1",
"babel-preset-es2016": "6.24.1",
"babel-preset-stage-1": "6.24.1",
"babel-runtime": "6.26.0",
"babel-loader": "^9.1.3",
"clean-webpack-plugin": "^4.0.0",
"compression-webpack-plugin": "^10.0.0",
"dotenv": "^16.0.3",
"es3ify-loader": "0.2.0",
"fetch-mock-jest": "^1.3.0",
"http-proxy-agent": "^3.0.0",
"jest": "^27.5.1",
"jest": "^29.7.0",
"jest-html-reporters": "^2.1.7",
"jsdoc": "^4.0.2",
"jshint": "^2.13.6",
"minami": "^1.2.3",
"node-request-interceptor": "^0.6.3",
"nodemailer": "^6.9.1",
"request": "^2.88.2",
"string-replace-loader": "1.3.0",
"string-replace-loader": "^3.1.0",
"taffydb": "^2.7.3",
"tap": "^16.3.4",
"tap-html": "^1.1.0",
"tap-json": "1.0.0",
"tape": "4.8.0",
"terser-webpack-plugin": "^5.3.7",
"ts-jest": "^27.1.5",
"ts-jest": "^29.1.1",
"typescript": "^4.9.5",
"uglify-js": "2.8.29",
"webpack": "^5.76.3",
"webpack": "^5.89.0",
"webpack-cli": "^4.10.0",
"webpack-md5-hash": "0.0.5",
"webpack-merge": "4.1.0",
Expand Down
76 changes: 76 additions & 0 deletions src/core/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,79 @@ function getIncludeParamForReference(referenceMap) {
buildParamStringRecursively(referenceMap, "");
return newRefences.filter((currentReference) => currentReference !== "");
}

export const _extend = {
compare: function(type) {
return function(key, value) {
if (key && value && typeof key === 'string' && typeof value !== 'undefined') {
this._query['query'][key] = this._query['query']['file_size'] || {};
this._query['query'][key][type] = value;
return this;
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters.");
}
};
},
contained: function(bool) {
let type = (bool) ? '$in' : '$nin';
return function(key, value) {
if (key && value && typeof key === 'string' && Array.isArray(value)) {
this._query['query'][key] = this._query['query'][key] || {};
this._query['query'][key][type] = this._query['query'][key][type] || [];
this._query['query'][key][type] = this._query['query'][key][type].concat(value);
return this;
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters.");
}
};
},
exists: function(bool) {
return function(key) {
if (key && typeof key === 'string') {
this._query['query'][key] = this._query['query'][key] || {};
this._query['query'][key]['$exists'] = bool;
return this;
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters.");
}
};
},
logical: function(type) {
return function() {
let _query = [];
for (let i = 0, _i = arguments.length; i < _i; i++) {
if (arguments[i] instanceof Query && arguments[i]._query.query) {
_query.push(arguments[i]._query.query);
} else if (typeof arguments[i] === "object") {
_query.push(arguments[i]);
}
}
if (this._query['query'][type]) {
this._query['query'][type] = this._query['query'][type].concat(_query);
} else {
this._query['query'][type] = _query;
}
return this;
};
},
sort: function(type) {
return function(key) {
if (key && typeof key === 'string') {
this._query[type] = key;
return this;
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Argument should be a string.");
}
};
},
pagination: function(type) {
return function(value) {
if (typeof value === 'number') {
this._query[type] = value;
return this;
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Argument should be a number.");
}
}
}
};
107 changes: 29 additions & 78 deletions src/core/modules/query.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,29 @@
import * as Utils from '../lib/utils.js';
import Entry from './entry';

const _extend = {
compare: function(type) {
return function(key, value) {
if (key && value && typeof key === 'string' && typeof value !== 'undefined') {
this._query['query'][key] = this._query['query']['file_size'] || {};
this._query['query'][key][type] = value;
return this;
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters.");
}
};
},
contained: function(bool) {
let type = (bool) ? '$in' : '$nin';
return function(key, value) {
if (key && value && typeof key === 'string' && Array.isArray(value)) {
this._query['query'][key] = this._query['query'][key] || {};
this._query['query'][key][type] = this._query['query'][key][type] || [];
this._query['query'][key][type] = this._query['query'][key][type].concat(value);
return this;
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters.");
}
};
},
exists: function(bool) {
return function(key) {
if (key && typeof key === 'string') {
this._query['query'][key] = this._query['query'][key] || {};
this._query['query'][key]['$exists'] = bool;
return this;
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters.");
}
};
},
logical: function(type) {
return function() {
let _query = [];
for (let i = 0, _i = arguments.length; i < _i; i++) {
if (arguments[i] instanceof Query && arguments[i]._query.query) {
_query.push(arguments[i]._query.query);
} else if (typeof arguments[i] === "object") {
_query.push(arguments[i]);
}
}
if (this._query['query'][type]) {
this._query['query'][type] = this._query['query'][type].concat(_query);
} else {
this._query['query'][type] = _query;
}
return this;
};
},
sort: function(type) {
return function(key) {
if (key && typeof key === 'string') {
this._query[type] = key;
return this;
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Argument should be a string.");
}
};
},
pagination: function(type) {
return function(value) {
if (typeof value === 'number') {
this._query[type] = value;
return this;
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Argument should be a number.");
}
}
const _extend = Utils._extend;

/**
* @function getRequestUrl
* @description Returns host url based on this.type
* @param {Object} this `this` variable from Query class
* @return {string} returns the url that will be used to make API calls
*/
function getRequestUrl(type, config, baseURL) {
switch(type) {
case 'asset':
url = baseURL + config.urls.assets;
break;
case 'taxonomy':
url = baseURL + config.urls.taxonomies + config.urls.entries;
break;
case 'contentType':
default:
url = baseURL + config.urls.content_types + content_type_uid + config.urls.entries;
break;
}
};
return url;
}

/**
* @class
Expand Down Expand Up @@ -400,6 +348,7 @@ export default class Query extends Entry {
}

/**
* @method where
* @memberOf Query
* @description Retrieve entries in which a specific field satisfies the value provided
* @param {String} key - uid of the field
Expand Down Expand Up @@ -441,7 +390,7 @@ export default class Query extends Entry {
*/
count() {
const host = this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version,
url = (this.type && this.type === 'asset') ? host + this.config.urls.assets : host + this.config.urls.content_types + this.content_type_uid + this.config.urls.entries;
url = getRequestUrl(this.type, this.config, host);
this._query['count'] = true;
this.requestParams = {
method: 'POST',
Expand Down Expand Up @@ -750,8 +699,10 @@ export default class Query extends Entry {
if (this.type && this.type !== 'asset' && this.live_preview && this.live_preview.enable === true && this.live_preview.content_type_uid === this.content_type_uid ) {
host = this.live_preview.host;
}
const baseURL = this.config.protocol + "://" + host + '/' + this.config.version,
url = (this.type && this.type === 'asset') ? baseURL + this.config.urls.assets : baseURL + this.config.urls.content_types + this.content_type_uid + this.config.urls.entries;
const baseURL = this.config.protocol + "://" + host + '/' + this.config.version
const url = getRequestUrl(this.type, this.config, baseURL)


this.requestParams = {
method: 'POST',
headers: Utils.mergeDeep({}, this.headers),
Expand Down Expand Up @@ -785,7 +736,7 @@ export default class Query extends Entry {
if(this.type && this.type !== 'asset' && this.live_preview && this.live_preview.enable === true && this.live_preview.content_type_uid === this.content_type_uid ) {
host = this.config.protocol + "://" + this.live_preview.host + '/' + this.config.version
}
const url = (this.type && this.type === 'asset') ? host + this.config.urls.assets : host + this.config.urls.content_types + this.content_type_uid + this.config.urls.entries;
const url = getRequestUrl(this.type, this.config, host)

this.singleEntry = true;
this._query.limit = 1;
Expand Down
8 changes: 2 additions & 6 deletions src/core/modules/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import * as Utils from '../lib/utils'
* @returns {Result}
* @instance
*/
class Result {
export default class Result {
constructor(object){
if(object) {
this.object = function() {
Expand Down Expand Up @@ -106,8 +106,4 @@ class Result {
return (url) ? url + '?disposition=' + _disposition : null;
}
}
}

module.exports = function(object) {
return new Result(object);
};
}
14 changes: 14 additions & 0 deletions src/core/modules/taxonomies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as Utils from '../lib/utils';


export default class Taxonomies {
constructor() {
this._query = {};
return this;
}

toJSON() {
this.toJSON = true;
return this;
}
}
30 changes: 30 additions & 0 deletions src/core/modules/taxonomy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Query from "./query";
import * as Utils from "../lib/utils"

const _extend = Utils._extend

// Overrideing compare function to include level
_extend.compare = function(type) {
return function(key, value, level=0) {
if (key && value && typeof key === 'string' && typeof value !== 'undefined') {
this._query['query'][key] = this._query['query']['file_size'] || {};
this._query['query'][key][type] = value;
if (level > 0 && level <= 10) {
this._query['query'][key]['level'] = level
}
return this;
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters.");
}
};
}

export default class TaxonomyQuery extends Query {
constructor() {
super();
this.above = _extend.compare('$above')
this.equalAndAbove = _extend.compare('$eq_above')
this.below = _extend.compare('$below')
this.equalAndBelow = _extend.compare('$eq_below')
}
}
Loading
Loading