UNPKG

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