Skip to content

Commit 7fad55f

Browse files
committed
changing the js sdk assets class.
1 parent 446eb78 commit 7fad55f

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

src/core/modules/assets.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import * as Utils from '../lib/utils';
2+
import Stack from '../stack';
3+
import Query from './query';
4+
5+
const _extend = function(type) {
6+
return function() {
7+
this._query[type] = this._query[type] || {};
8+
switch (arguments.length) {
9+
case 1:
10+
if (Array.isArray(arguments[0]) || typeof arguments[0] === "string") {
11+
let query = this._query[type]['BASE'] || [];
12+
query = query.concat(arguments[0]);
13+
this._query[type]['BASE'] = query;
14+
return this;
15+
} else {
16+
console.error("Kindly provide valid parameters");
17+
}
18+
break;
19+
case 2:
20+
if (typeof arguments[0] === "string" && (Array.isArray(arguments[1]) || typeof arguments[1] === "string")) {
21+
let query = this._query[type][arguments[0]] || [];
22+
query = query.concat(arguments[1]);
23+
this._query[type][arguments[0]] = query;
24+
return this;
25+
} else {
26+
console.error("Kindly provide valid parameters");
27+
}
28+
break;
29+
default:
30+
console.error("Kindly provide valid parameters");
31+
}
32+
};
33+
};
34+
/**
35+
* @summary Creates an instance of `Assets`.
36+
* @description An initializer is responsible for creating Asset object.
37+
* @param {String} uid - uid of the asset
38+
* @example
39+
* let Assets = Contentstack.Stack().Assets('bltsomething123');
40+
* @returns {Assets}
41+
* @ignore
42+
*/
43+
export default class Assets {
44+
constructor() {
45+
/**
46+
* @method only
47+
* @description This method is use to show the selected fields of the assets in resultset.
48+
* @param {String} [key=BASE] - single field in asset
49+
* @param {Array} values - array of fields to be show in resultset
50+
* @example
51+
* <caption> .only with field uid </caption>
52+
* Assets().only('title')
53+
* @example
54+
* <caption> .only with field uid </caption>
55+
* Assets().only('BASE','title')
56+
* @example
57+
* <caption> .only with field uids(array) </caption>
58+
* Assets().only(['title','description'])
59+
* @returns {Asset}
60+
*/
61+
this.only = _extend('only');
62+
return this;
63+
}
64+
65+
/**
66+
* @method Query
67+
* @description Query instance to provide support for all search queries.
68+
* @example Assets().Query()
69+
* @returns {Query}
70+
*/
71+
Query() {
72+
let query = new Query();
73+
return Utils.merge(query, this);
74+
}
75+
76+
/**
77+
* @method addQuery
78+
* @description This method is used to add query to Entry object.
79+
* @param {String} key - key of the query
80+
* @param {String} value - value of the query
81+
* @example Assets().addQuery('include_schema',true)
82+
* @returns {Entry}
83+
*/
84+
addQuery(key, value) {
85+
if (key && value && typeof key === 'string') {
86+
this._query[key] = value;
87+
return this;
88+
} else {
89+
console.error("First argument should be a String.");
90+
}
91+
}
92+
93+
/**
94+
* @method toJSON
95+
* @description This method is used to convert the result in to plain javascript object.
96+
* @example
97+
* assetQuery
98+
* .toJSON()
99+
* .then(function (result) {
100+
* let value = result.get(field_uid)
101+
* },function (error) {
102+
* // error function
103+
* })
104+
* @returns {Object}
105+
*/
106+
toJSON() {
107+
this.tojson = true;
108+
return this;
109+
}
110+
111+
112+
/**
113+
* @method fetch
114+
* @description fetch asset obhect of requested Asset uid of defined query if present.
115+
* @example
116+
* Stack.Assets('bltsomething123').fetch()
117+
*/
118+
fetch() {
119+
if (this.asset_uid) {
120+
this.requestParams = {
121+
method: 'POST',
122+
headers: this.headers,
123+
url: this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version + this.config.urls.assets + this.asset_uid,
124+
body: {
125+
_method: 'GET',
126+
query: this._query
127+
}
128+
}
129+
return Utils.sendRequest(this);
130+
} else {
131+
console.error("Kindly provide an asset uid. e.g. .Assets('bltsomething123')");
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)