|
| 1 | +import cloneDeep from 'lodash/cloneDeep' |
| 2 | +import { create, update, deleteEntity, fetch, query } from '../../entity' |
| 3 | +import { Variants } from './variants/index' |
| 4 | + |
| 5 | +/** |
| 6 | + * Contentstack has a sophisticated multilingual capability. It allows you to create and publish entries in any language. This feature allows you to set up multilingual websites and cater to a wide variety of audience by serving content in their local language(s). Read more about <a href='https://www.contentstack.com/docs/developers/multi-language-content'>VariantGroups</a>. |
| 7 | + * @namespace VariantGroup |
| 8 | + */ |
| 9 | + |
| 10 | +export function VariantGroup (http, data = {}) { |
| 11 | + this.stackHeaders = data.stackHeaders |
| 12 | + this.urlPath = `/variant_groups` |
| 13 | + |
| 14 | + if (data.variant_group) { |
| 15 | + Object.assign(this, cloneDeep(data.variant_group)) |
| 16 | + this.urlPath += `/${this.uid}` |
| 17 | + /** |
| 18 | + * @description The Update VariantGroup call lets you update the name and description of an existing VariantGroup. |
| 19 | + * @memberof VariantGroup |
| 20 | + * @func update |
| 21 | + * @returns {Promise<VariantGroup.VariantGroup>} Promise for VariantGroup instance |
| 22 | + * @example |
| 23 | + * import * as contentstack from '@contentstack/management' |
| 24 | + * const client = contentstack.client() |
| 25 | + * |
| 26 | + * client.stack({ api_key: 'api_key'}).VariantGroup('variant_group_uid').fetch() |
| 27 | + * .then((variant_group) => { |
| 28 | + * variant_group.name = 'test' |
| 29 | + * variant_group.content_types = ['iphone_product_page'] |
| 30 | + * return variant_group.update() |
| 31 | + * }) |
| 32 | + * .then((variant_group) => console.log(variant_group)) |
| 33 | + * |
| 34 | + */ |
| 35 | + this.update = update(/* The `http` parameter is being used as a dependency injection to make HTTP |
| 36 | + requests within the `VariantGroup` and `VariantGroupCollection` functions. |
| 37 | + It allows the functions to interact with an external API or server to |
| 38 | + perform operations such as creating, updating, deleting, fetching, or |
| 39 | + querying variant groups. */ |
| 40 | + /* The `http` parameter in the `VariantGroup` function is being used to make |
| 41 | + HTTP requests to interact with the Contentstack API. It is likely being |
| 42 | + used to send requests to create, update, delete, fetch, or query variant |
| 43 | + groups and their related entities. */ |
| 44 | + http, 'variant_group') |
| 45 | + |
| 46 | + /** |
| 47 | + * @description The Delete VariantGroup call is used to delete an existing VariantGroup permanently from your Stack. |
| 48 | + * @memberof VariantGroup |
| 49 | + * @func delete |
| 50 | + * @returns {Object} Response Object. |
| 51 | + * @example |
| 52 | + * import * as contentstack from '@contentstack/management' |
| 53 | + * const client = contentstack.client() |
| 54 | + * |
| 55 | + * client.stack({ api_key: 'api_key'}).VariantGroup('variant_group_uid').delete() |
| 56 | + * .then((response) => console.log(response.notice)) |
| 57 | + */ |
| 58 | + this.delete = deleteEntity(http) |
| 59 | + |
| 60 | + /** |
| 61 | + * @description The fetch VariantGroup call fetches VariantGroup details. |
| 62 | + * @memberof VariantGroup |
| 63 | + * @func fetch |
| 64 | + * @returns {Promise<VariantGroup.VariantGroup>} Promise for VariantGroup instance |
| 65 | + * @param {Int} version Enter the unique ID of the content type of which you want to retrieve the details. The UID is generated based on the title of the content type. The unique ID of a content type is unique across a stack. |
| 66 | + * @example |
| 67 | + * import * as contentstack from '@contentstack/management' |
| 68 | + * const client = contentstack.client() |
| 69 | + * |
| 70 | + * client.stack({ api_key: 'api_key'}).VariantGroup('variant_group_uid').fetch() |
| 71 | + * .then((variant_group) => console.log(variant_group)) |
| 72 | + * |
| 73 | + */ |
| 74 | + this.fetch = fetch(http, 'variant_group') |
| 75 | + |
| 76 | + /** |
| 77 | + * @description Content type defines the structure or schema of a page or a section of your web or mobile property. |
| 78 | + * @param {String} uid The UID of the ContentType you want to get details. |
| 79 | + * @returns {ContenType} Instace of ContentType. |
| 80 | + * @example |
| 81 | + * import * as contentstack from '@contentstack/management' |
| 82 | + * const client = contentstack.client() |
| 83 | + * |
| 84 | + * client.stack({ api_key: 'api_key'}).VariantGroup('variant_group_uid').variants('variant_uid').fetch() |
| 85 | + * .then((Variants) => console.log(Variants)) |
| 86 | + */ |
| 87 | + this.variants = (uid = null) => { |
| 88 | + const data = { stackHeaders: this.stackHeaders } |
| 89 | + data.variant_group_uid = this.uid |
| 90 | + if (uid) { |
| 91 | + data.variants = { uid: uid } |
| 92 | + } |
| 93 | + return new Variants(http, data) |
| 94 | + } |
| 95 | + |
| 96 | + } else { |
| 97 | + /** |
| 98 | + * @description The Create a variant group call creates a new variant group in a particular stack of your Contentstack account. |
| 99 | + * @memberof VariantGroup |
| 100 | + * @func create |
| 101 | + * @returns {Promise<VariantGroup.VariantGroup>} Promise for VariantGroup instance |
| 102 | + * |
| 103 | + * @example |
| 104 | + * import * as contentstack from '@contentstack/management' |
| 105 | + * const client = contentstack.client() |
| 106 | + * const variant_group = { |
| 107 | + * "name": "Colors", |
| 108 | + * "content_types": [ |
| 109 | + * "iphone_product_page" |
| 110 | + * ], |
| 111 | + * "uid": "iphone_color_white", // optional |
| 112 | + * } |
| 113 | + * client.stack().VariantGroup().create({ variant_group } ) |
| 114 | + * .then((variant_group) => console.log(variant_group)) |
| 115 | + */ |
| 116 | + this.create = create({ http: http }) |
| 117 | + |
| 118 | + /** |
| 119 | + * @description The Query on Variant Groups will allow to fetch details of all or specific Variant Groups |
| 120 | + * @memberof VariantGroup |
| 121 | + * @func query |
| 122 | + * @param {Boolean} include_count Set this to 'true' to include in response the total count of content types available in your stack. |
| 123 | + * @returns {Array<VariantGroup>} Array of ContentTyoe. |
| 124 | + * |
| 125 | + * @example |
| 126 | + * import * as contentstack from '@contentstack/management' |
| 127 | + * const client = contentstack.client() |
| 128 | + * |
| 129 | + * client.stack(api_key).VariantGroup().query({ query: { code: 'variant_group-code' } }).find() |
| 130 | + * .then((variant_groups) => console.log(variant_groups)) |
| 131 | + */ |
| 132 | + this.query = query({ http: http, wrapperCollection: VariantGroupCollection }) |
| 133 | + } |
| 134 | + return this |
| 135 | +} |
| 136 | + |
| 137 | +export function VariantGroupCollection (http, data) { |
| 138 | + const obj = cloneDeep(data.variant_groups) || [] |
| 139 | + const variant_groupCollection = obj.map((userdata) => { |
| 140 | + return new VariantGroup(http, { variant_group: userdata, stackHeaders: data.stackHeaders }) |
| 141 | + }) |
| 142 | + return variant_groupCollection |
| 143 | +} |
0 commit comments