|
1 | 1 | import cloneDeep from 'lodash/cloneDeep' |
2 | | -import { create } from '../../../entity' |
| 2 | +import { |
| 3 | + update, |
| 4 | + deleteEntity, |
| 5 | + fetch, |
| 6 | + create |
| 7 | +} from '../../../entity' |
3 | 8 |
|
4 | 9 | /** |
5 | | - * Assets refer to all the media files (images, videos, PDFs, audio files, and so on) uploaded in your Contentstack repository for future use. |
6 | | - * These files can be attached and used in multiple entries. Read more about <a href='https://www.contentstack.com/docs/guide/content-management'>Assets</a>. |
7 | | - * @namespace Asset |
| 10 | + * Folders refer to Asset Folders. |
| 11 | + * @namespace Folder |
8 | 12 | */ |
9 | 13 |
|
10 | 14 | export function Folder (http, data = {}) { |
11 | | - this.stackHeaders = data.stackHeaders |
| 15 | + if (data.stackHeaders) { |
| 16 | + this.stackHeaders = data.stackHeaders |
| 17 | + } |
12 | 18 | this.urlPath = `/assets/folders` |
13 | 19 |
|
14 | 20 | if (data.asset) { |
15 | 21 | Object.assign(this, cloneDeep(data.asset)) |
| 22 | + this.urlPath = `/assets/folders/${this.uid}` |
| 23 | + |
| 24 | + /** |
| 25 | + * @description The Update Folder call lets you update the name and description of an existing Folder. |
| 26 | + * @memberof Folder |
| 27 | + * @func update |
| 28 | + * @returns {Promise<Folder.Folder>} Promise for Folder instance |
| 29 | + * @example |
| 30 | + * import * as contentstack from '@contentstack/management' |
| 31 | + * const client = contentstack.client() |
| 32 | + * |
| 33 | + * client.stack({ api_key: 'api_key'}).asset().folder('uid').fetch() |
| 34 | + * .then((folder) => { |
| 35 | + * folder.name = 'My New folder' |
| 36 | + * return folder.update() |
| 37 | + * }) |
| 38 | + * .then((folder) => console.log(folder)) |
| 39 | + * |
| 40 | + */ |
| 41 | + this.update = update(http, 'asset') |
| 42 | + |
| 43 | + /** |
| 44 | + * @description The Delete folder call will delete an existing folder from the stack. |
| 45 | + * @memberof Folder |
| 46 | + * @func delete |
| 47 | + * @returns {Object} Response Object. |
| 48 | + * @example |
| 49 | + * import * as contentstack from '@contentstack/management' |
| 50 | + * const client = contentstack.client() |
| 51 | + * |
| 52 | + * client.stack({ api_key: 'api_key'}).asset().folder('uid').delete() |
| 53 | + * .then((notice) => console.log(notice)) |
| 54 | + */ |
| 55 | + this.delete = deleteEntity(http) |
| 56 | + |
| 57 | + /** |
| 58 | + * @description The fetch an asset call returns comprehensive information about a specific version of an asset of a stack. |
| 59 | + * @memberof Folder |
| 60 | + * @func fetch |
| 61 | + * @returns {Promise<Folder.Folder>} Promise for Folder instance |
| 62 | + * @example |
| 63 | + * import * as contentstack from '@contentstack/management' |
| 64 | + * const client = contentstack.client() |
| 65 | + * |
| 66 | + * client.stack({ api_key: 'api_key'}).asset().folder('uid').fetch() |
| 67 | + * .then((folder) => console.log(folder)) |
| 68 | + * |
| 69 | + */ |
| 70 | + this.fetch = fetch(http, 'asset') |
16 | 71 | } else { |
17 | 72 | /** |
18 | | - * @description The Create a folder into the assets. |
19 | | - * @memberof Folder |
20 | | - * @func create |
21 | | - * @returns {Promise<Folder.Folder>} Promise for Folder instance |
22 | | - * |
23 | | - * @example |
24 | | - * import * as contentstack from '@contentstack/management' |
25 | | - * const client = contentstack.client() |
26 | | - * const asset = {name: 'My New contentType'} |
27 | | - * client.stack().asset().folders().create({ asset }) |
28 | | - * .then((folder) => console.log(folder)) |
29 | | - */ |
| 73 | + * @description The Create a folder into the assets. |
| 74 | + * @memberof Folder |
| 75 | + * @func create |
| 76 | + * @returns {Promise<Folder.Folder>} Promise for Folder instance |
| 77 | + * |
| 78 | + * @example |
| 79 | + * import * as contentstack from '@contentstack/management' |
| 80 | + * const client = contentstack.client() |
| 81 | + * const asset = {name: 'My New contentType'} |
| 82 | + * client.stack().asset().folders().create({ asset }) |
| 83 | + * .then((folder) => console.log(folder)) |
| 84 | + */ |
30 | 85 | this.create = create({ http: http }) |
31 | 86 | } |
32 | 87 | } |
| 88 | + |
| 89 | +export function FolderCollection (http, data) { |
| 90 | + const obj = cloneDeep(data.assets) || [] |
| 91 | + const assetCollection = obj.map((userdata) => { |
| 92 | + return new Folder(http, { asset: userdata, stackHeaders: data.stackHeaders }) |
| 93 | + }) |
| 94 | + return assetCollection |
| 95 | +} |
0 commit comments