forked from contentstack/contentstack-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult.js
More file actions
106 lines (99 loc) · 2.75 KB
/
result.js
File metadata and controls
106 lines (99 loc) · 2.75 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import * as Utils from '../lib/utils'
/**
* @summary Creates an instance of `Result`.
* @description An initializer is responsible for creating Result object.
* @param {Object} object - API result object
* @example
* blogEntry.then(function (result) {
* // sucess function
* },function (error) {
* // error function
* })
* @example
* assetQuery.then(function (result) {
* // sucess function
* },function (error) {
* // error function
* })
* @returns {Result}
*/
class Result {
constructor(object){
if(object) {
this.object = function() {
return object;
}
}
return this;
}
/**
* @method toJSON
* @description Converts `Result` to plain javascript object.
* @example
* blogEntry.then(function (result) {
* result = result.toJSON()
* },function (error) {
* // error function
* })
* @example
* assetQuery.then(function (result) {
* result = result.toJSON()
* },function (error) {
* // error function
* })
* @returns {object}
*/
toJSON() {
return (this.object()) ? Utils.mergeDeep(JSON.parse(JSON.stringify({})), this.object()) : null;
}
/**
* @method get
* @description `get` to access the key value.
* @param field_uid
* @example
* blogEntry.then(function (result) {
* let value = result.get(field_uid)
* },function (error) {
* // error function
* })
* @example
* assetQuery.then(function (result) {
* let value = result.get(field_uid)
* },function (error) {
* // error function
* })
* @returns {Object}
*/
get(key){
if(this.object() && key) {
let fields = key.split('.');
let value = fields.reduce(function(prev, field) {
return prev[field];
}, this.object());
return value;
}
return ;
}
/**
* @method getDownloadUrl
* @description `getDownloadUrl` to get the download url.
* @param {String} string - Disposition value
* @example
* assetQuery.then(function (result) {
* let value = result.getDownloadUrl(disposition_value)
* },function (error) {
* // error function
* })
* @returns {Object}
*/
getDownloadUrl(disposition) {
if (this.object()) {
let url = (this.object().url) ? this.object().url : null,
_disposition = (disposition && typeof disposition === 'string') ? disposition: 'attachment';
return (url) ? url + '?disposition=' + _disposition : null;
}
}
}
module.exports = function(object) {
return new Result(object);
};