UNPKG

10.8 kBTypeScriptView Raw
1/// <reference types="node" />
2import { Stream } from 'stream';
3import { MetaSysProps, DefaultElements, EntityMetaSysProps, MetadataProps, MakeRequest } from '../common-types';
4export declare type AssetProps = {
5 sys: EntityMetaSysProps;
6 fields: {
7 /** Title for this asset */
8 title: {
9 [key: string]: string;
10 };
11 /** Description for this asset */
12 description?: {
13 [key: string]: string;
14 };
15 /** File object for this asset */
16 file: {
17 [key: string]: {
18 fileName: string;
19 contentType: string;
20 /** Url where the file is available to be downloaded from, into the Contentful asset system. After the asset is processed this field is gone. */
21 upload?: string;
22 /** Url where the file is available at the Contentful media asset system. This field won't be available until the asset is processed. */
23 url?: string;
24 /** Details for the file, depending on file type (example: image size in bytes, etc) */
25 details?: Record<string, any>;
26 uploadFrom?: Record<string, any>;
27 };
28 };
29 };
30 metadata?: MetadataProps;
31};
32export declare type CreateAssetProps = Omit<AssetProps, 'sys'>;
33export interface AssetFileProp {
34 sys: MetaSysProps;
35 fields: {
36 title: {
37 [key: string]: string;
38 };
39 description: {
40 [key: string]: string;
41 };
42 file: {
43 [key: string]: {
44 file: string | ArrayBuffer | Stream;
45 contentType: string;
46 fileName: string;
47 };
48 };
49 };
50}
51export interface AssetProcessingForLocale {
52 processingCheckWait?: number;
53 processingCheckRetries?: number;
54}
55declare type AssetApi = {
56 /**
57 * Triggers asset processing after an upload, for the files uploaded to all locales of an asset.
58 * @param options - Additional options for processing
59 * @prop options.processingCheckWait - Time in milliseconds to wait before checking again if the asset has been processed (default: 500ms)
60 * @prop options.processingCheckRetries - Maximum amount of times to check if the asset has been processed (default: 5)
61 * @return Object returned from the server with updated metadata.
62 * @throws {AssetProcessingTimeout} If the asset takes too long to process. If this happens, retrieve the asset again, and if the url property is available, then processing has succeeded. If not, your file might be damaged.
63 * @example ```javascript
64 * const contentful = require('contentful-management')
65 *
66 * const client = contentful.createClient({
67 * accessToken: '<content_management_api_key>'
68 * })
69 * client.getSpace('<space_id>')
70 * .then((space) => space.getEnvironment('<environment_id>'))
71 * .then((environment) => environment.createAssetWithId('<asset_id>', {
72 * title: {
73 * 'en-US': 'Playsam Streamliner',
74 * 'de-DE': 'Playsam Streamliner'
75 * },
76 * file: {
77 * 'en-US': {
78 * contentType: 'image/jpeg',
79 * fileName: 'example.jpeg',
80 * upload: 'https://example.com/example.jpg'
81 * },
82 * 'de-DE': {
83 * contentType: 'image/jpeg',
84 * fileName: 'example.jpeg',
85 * upload: 'https://example.com/example-de.jpg'
86 * }
87 * }
88 * }))
89 * .then((asset) => asset.processForAllLocales())
90 * .then((asset) => console.log(asset))
91 * .catch(console.error)
92 * ```
93 */
94 processForAllLocales(options?: AssetProcessingForLocale): Promise<Asset>;
95 /**
96 * Triggers asset processing after an upload, for the file uploaded to a specific locale.
97 * @param locale - Locale which processing should be triggered for
98 * @param options - Additional options for processing
99 * @prop options.processingCheckWait - Time in milliseconds to wait before checking again if the asset has been processed (default: 500ms)
100 * @prop options.processingCheckRetries - Maximum amount of times to check if the asset has been processed (default: 5)
101 * @return Object returned from the server with updated metadata.
102 * @throws {AssetProcessingTimeout} If the asset takes too long to process. If this happens, retrieve the asset again, and if the url property is available, then processing has succeeded. If not, your file might be damaged.
103 * @example ```javascript
104 * const contentful = require('contentful-management')
105 *
106 * const client = contentful.createClient({
107 * accessToken: '<content_management_api_key>'
108 * })
109 * client.getSpace('<space_id>')
110 * .then((space) => space.getEnvironment('<environment_id>'))
111 * .then((environment) => environment.createAssetWithId('<asset_id>', {
112 * title: {
113 * 'en-US': 'Playsam Streamliner',
114 * },
115 * file: {
116 * 'en-US': {
117 * contentType: 'image/jpeg',
118 * fileName: 'example.jpeg',
119 * upload: 'https://example.com/example.jpg'
120 * }
121 * }
122 * }))
123 * .then((asset) => asset.processForLocale('en-US'))
124 * .then((asset) => console.log(asset))
125 * .catch(console.error)
126 * ```
127 */
128 processForLocale(locale: string, Options?: AssetProcessingForLocale): Promise<Asset>;
129 /**
130 * Publishes the object
131 * @return Object returned from the server with updated metadata.
132 * @example ```javascript
133 * const contentful = require('contentful-management')
134 *
135 * const client = contentful.createClient({
136 * accessToken: '<content_management_api_key>'
137 * })
138 *
139 * client.getSpace('<space_id>')
140 * .then((space) => space.getEnvironment('<environment_id>'))
141 * .then((environment) => environment.getAsset('<asset_id>'))
142 * .then((asset) => asset.publish())
143 * .then((asset) => console.log(`Asset ${asset.sys.id} published.`)
144 * .catch(console.error)
145 * ```
146 */
147 publish(): Promise<Asset>;
148 /**
149 * Archives the object
150 * @return Object returned from the server with updated metadata.
151 * @example ```javascript
152 * const contentful = require('contentful-management')
153 *
154 * const client = contentful.createClient({
155 * accessToken: '<content_management_api_key>'
156 * })
157 *
158 * client.getSpace('<space_id>')
159 * .then((space) => space.getEnvironment('<environment_id>'))
160 * .then((environment) => environment.getAsset('<asset_id>'))
161 * .then((asset) => asset.archive())
162 * .then((asset) => console.log(`Asset ${asset.sys.id} archived.`)
163 * .catch(console.error)
164 * ```
165 */
166 archive(): Promise<Asset>;
167 /**
168 * Deletes this object on the server.
169 * @return Promise for the deletion. It contains no data, but the Promise error case should be handled.
170 * @example ```javascript
171 * const contentful = require('contentful-management')
172 *
173 * const client = contentful.createClient({
174 * accessToken: '<content_management_api_key>'
175 * })
176 *
177 * client.getSpace('<space_id>')
178 * .then((space) => space.getEnvironment('<environment_id>'))
179 * .then((environment) => environment.getAsset('<asset_id>'))
180 * .then((asset) => asset.delete())
181 * .then((asset) => console.log(`Asset deleted.`)
182 * .catch(console.error)
183 * ```
184 */
185 delete(): Promise<void>;
186 /**
187 * Unarchives the object
188 * @return Object returned from the server with updated metadata.
189 * @example ```javascript
190 * const contentful = require('contentful-management')
191 *
192 * const client = contentful.createClient({
193 * accessToken: '<content_management_api_key>'
194 * })
195 *
196 * client.getSpace('<space_id>')
197 * .then((space) => space.getEnvironment('<environment_id>'))
198 * .then((environment) => environment.getAsset('<asset_id>'))
199 * .then((asset) => asset.unarchive())
200 * .then((asset) => console.log(`Asset ${asset.sys.id} unarchived.`)
201 * .catch(console.error)
202 * ```
203 */
204 unarchive(): Promise<Asset>;
205 /**
206 * Unpublishes the object
207 * @return Object returned from the server with updated metadata.
208 * @example ```javascript
209 * const contentful = require('contentful-management')
210 *
211 * const client = contentful.createClient({
212 * accessToken: '<content_management_api_key>'
213 * })
214 *
215 * client.getSpace('<space_id>')
216 * .then((space) => space.getEnvironment('<environment_id>'))
217 * .then((environment) => environment.getAsset('<asset_id>'))
218 * .then((asset) => asset.unpublish())
219 * .then((asset) => console.log(`Asset ${asset.sys.id} unpublished.`)
220 * .catch(console.error)
221 * ```
222 */
223 unpublish(): Promise<Asset>;
224 /**
225 * Sends an update to the server with any changes made to the object's properties
226 * @return Object returned from the server with updated changes.
227 * @example ```javascript
228 * const contentful = require('contentful-management')
229 *
230 * const client = contentful.createClient({
231 * accessToken: '<content_management_api_key>'
232 * })
233 *
234 * client.getSpace('<space_id>')
235 * .then((space) => space.getEnvironment('<environment_id>'))
236 * .then((environment) => environment.getAsset('<asset_id>'))
237 * .then((asset) => {
238 * asset.fields.title['en-US'] = 'New asset title'
239 * return asset.update()
240 * })
241 * .then((asset) => console.log(`Asset ${asset.sys.id} updated.`)
242 * .catch(console.error)
243 * ```
244 */
245 update(): Promise<Asset>;
246 /**
247 * Checks if the asset is published. A published asset might have unpublished changes
248 */
249 isPublished(): boolean;
250 /**
251 * Checks if the asset is updated. This means the asset was previously published but has unpublished changes.
252 */
253 isUpdated(): boolean;
254 /**
255 * Checks if the asset is in draft mode. This means it is not published.
256 */
257 isDraft(): boolean;
258 /**
259 * Checks if asset is archived. This means it's not exposed to the Delivery/Preview APIs.
260 */
261 isArchived(): boolean;
262};
263export interface Asset extends AssetProps, DefaultElements<AssetProps>, AssetApi {
264}
265/**
266 * @private
267 * @param makeRequest - function to make requests via an adapter
268 * @param data - Raw asset data
269 * @return Wrapped asset data
270 */
271export declare function wrapAsset(makeRequest: MakeRequest, data: AssetProps): Asset;
272/**
273 * @private
274 */
275export declare const wrapAssetCollection: (makeRequest: MakeRequest, data: import("../common-types").CollectionProp<AssetProps>) => import("../common-types").Collection<Asset, AssetProps>;
276export {};