-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathasset.ts
More file actions
150 lines (131 loc) · 4.62 KB
/
asset.ts
File metadata and controls
150 lines (131 loc) · 4.62 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { AxiosInstance, getData } from '@contentstack/core';
export class Asset {
private _client: AxiosInstance;
private _urlPath: string;
_queryParams: { [key: string]: string | number } = {};
constructor(client: AxiosInstance, assetUid: string) {
this._client = client;
this._urlPath = `/assets/${assetUid}`;
}
/**
* @method includeFallback
* @memberof Asset
* @description When an entry is not published in a specific language, content can be fetched from its fallback language
* @returns {Asset}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.asset('asset_uid').includeFallback().fetch();
*/
includeFallback(): Asset {
this._queryParams.include_fallback = 'true';
return this;
}
/**
* @method includeMetadata
* @memberof Asset
* @description Include the metadata for getting metadata content for the asset.
* @returns {Asset}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.asset('asset_uid').includeMetadata().fetch();
*/
includeMetadata(): Asset {
this._queryParams.include_metadata = 'true';
return this;
}
/**
* @method includeDimension
* @memberof Asset
* @description Includes the dimensions (height and width) of the image in result
* @returns {Asset}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.asset('asset_uid').includeDimension().fetch();
*/
includeDimension(): Asset {
this._queryParams.include_dimension = 'true';
return this;
}
/**
* @method includeBranch
* @memberof Asset
* @description Includes the branch in result
* @returns {Asset}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.asset('asset_uid').includeBranch().fetch();
*/
includeBranch(): Asset {
this._queryParams.include_branch = 'true';
return this;
}
/**
* @method relativeUrls
* @memberof Asset
* @description Includes the relative URLs of the asset in result
* @returns {Asset}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.asset('asset_uid').relativeUrls().fetch();
*/
relativeUrls(): Asset {
this._queryParams.relative_urls = 'true';
return this;
}
/**
* @method version
* @memberof Asset
* @description Retrieve a specific version of an asset in result
* @returns {Asset}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.asset('asset_uid').version(1).fetch();
*/
version(version: number): Asset {
this._queryParams.version = String(version);
return this;
}
/**
* @method locale
* @memberof Asset
* @description The asset published in the locale will be fetched
* @returns {Asset}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.asset('asset_uid').locale('en-us').fetch();
*/
locale(locale: string): Asset {
this._queryParams.locale = locale;
return this;
}
/**
* @method fetch
* @memberof Asset
* @description Fetches the asset data on the basis of the asset uid
* @returns {Promise<T>} Promise that resolves to the asset data
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.asset('asset_uid').fetch();
*/
async fetch<T>(): Promise<T> {
const response = await getData(this._client, this._urlPath, { params: this._queryParams});
if (response.asset) return response.asset as T;
return response;
}
}