forked from contentstack/contentstack-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassets.js
More file actions
92 lines (87 loc) · 2.87 KB
/
assets.js
File metadata and controls
92 lines (87 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import * as Utils from '../lib/utils';
import Stack from '../stack';
import Query from './query';
/**
* @summary Creates an instance of `Assets`.
* @description An initializer is responsible for creating Asset object.
* @param {String} uid - uid of the asset
* @example
* let Assets = Contentstack.Stack().Assets('bltsomething123');
* @returns {Assets}
* @ignore
*/
export default class Assets {
constructor() {
this._query = {};
/**
* @method only
* @description This method is use to show the selected fields of the assets in resultset.
* @param {String} [key=BASE] - single field in asset
* @param {Array} values - array of fields to be show in resultset
* @example
* <caption> .only with field uid </caption>
* Assets().only('title')
* @example
* <caption> .only with field uid </caption>
* Assets().only('BASE','title')
* @example
* <caption> .only with field uids(array) </caption>
* Assets().only(['title','description'])
* @returns {Asset}
*/
this.only = Utils.transform('only');
return this;
}
/**
* @method toJSON
* @description This method is used to convert the result in to plain javascript object.
* @example
* assetQuery
* .toJSON()
* .then(function (result) {
* let value = result.get(field_uid)
* },function (error) {
* // error function
* })
* @returns {Object}
*/
toJSON() {
this.tojson = true;
return this;
}
/**
* @method AddParam
* @description This method includes query parameter in query.
* @example Stack.Assets('bltsomething123').addParam('include_dimension', 'true').fetch()
*/
addParam(key, value) {
if (key && typeof key === 'string' && value && typeof value === 'string') {
this._query[key] = value;
return this;
} else {
console.error("Kindly provide a valid parameters.");
}
}
/**
* @method fetch
* @description fetch asset obhect of requested Asset uid of defined query if present.
* @example
* Stack.Assets('bltsomething123').fetch()
*/
fetch() {
if (this.asset_uid) {
this.requestParams = {
method: 'POST',
headers: this.headers,
url: this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version + this.config.urls.assets + this.asset_uid,
body: {
_method: 'GET',
query: this._query
}
}
return Utils.sendRequest(this);
} else {
console.error("Kindly provide an asset uid. e.g. .Assets('bltsomething123')");
}
}
}