|
| 1 | +import cloneDeep from 'lodash/cloneDeep' |
| 2 | +import { |
| 3 | + deleteEntity, |
| 4 | + fetch, |
| 5 | + query, |
| 6 | + create |
| 7 | +} from '../../entity' |
| 8 | +/** |
| 9 | + * Variantss allow you to group a collection of content within a stack. Using variants you can group content types that need to work together. Read more about <a href='https://www.contentstack.com/docs/developers/create-content-types/manage-variants'>Variantss</a>. |
| 10 | + * @namespace Variants |
| 11 | + */ |
| 12 | +export function Variants (http, data) { |
| 13 | + this.stackHeaders = data.stackHeaders |
| 14 | + this.urlPath = `/variants` |
| 15 | + |
| 16 | + if (data.variants) { |
| 17 | + Object.assign(this, cloneDeep(data.variants)) |
| 18 | + this.urlPath += `/${this.uid}` |
| 19 | + |
| 20 | + /** |
| 21 | + * @description The Delete variants call is used to delete a specific variants. |
| 22 | + * @memberof Variants |
| 23 | + * @func delete |
| 24 | + * @returns {Object} Response Object. |
| 25 | + * @example |
| 26 | + * import * as contentstack from '@contentstack/management' |
| 27 | + * const client = contentstack.client() |
| 28 | + * |
| 29 | + * client.stack({ api_key: 'api_key'}).variants('variants_uid').delete() |
| 30 | + * .then((response) => console.log(response.notice)) |
| 31 | + */ |
| 32 | + this.delete = deleteEntity(http) |
| 33 | + |
| 34 | + /** |
| 35 | + * @description The fetch Variants returns information about a particular variants of a stack. |
| 36 | + * @memberof Variants |
| 37 | + * @func fetch |
| 38 | + * @returns {Promise<Variants.Variants>} Promise for Variants instance |
| 39 | + * @example |
| 40 | + * import * as contentstack from '@contentstack/management' |
| 41 | + * const client = contentstack.client() |
| 42 | + * |
| 43 | + * client.stack({ api_key: 'api_key'}).variants('variants_uid').fetch() |
| 44 | + * .then((variants) => console.log(variants)) |
| 45 | + * |
| 46 | + */ |
| 47 | + this.fetch = fetch(http, 'variants') |
| 48 | + } else { |
| 49 | + /** |
| 50 | + * @description The Create an variants call creates a new variants. |
| 51 | + * @memberof Variants |
| 52 | + * @func create |
| 53 | + * @returns {Promise<Variants.Variants>} Promise for Variants instance |
| 54 | + * |
| 55 | + * @example |
| 56 | + * import * as contentstack from '@contentstack/management' |
| 57 | + * const client = contentstack.client() |
| 58 | + * const variants = { |
| 59 | + * "uid": "iphone_color_white", // optional |
| 60 | + * "name": "White", |
| 61 | + * "personalize_metadata": { |
| 62 | + * "experience_uid": "exp1", |
| 63 | + * "experience_short_uid": "expShortUid1", |
| 64 | + * "project_uid": "project_uid1", |
| 65 | + * "variant_short_uid": "variantShort_uid1" |
| 66 | + * }, |
| 67 | + * } |
| 68 | + * client.stack().variants().create({ variants }) |
| 69 | + * .then((variants) => console.log(variants)) |
| 70 | + */ |
| 71 | + this.create = create({ http }) |
| 72 | + |
| 73 | + /** |
| 74 | + * @description The Query on Variants will allow to fetch details of all or specific Variants. |
| 75 | + * @memberof Variants |
| 76 | + * @param {Object} params - URI parameters |
| 77 | + * @prop {Object} params.query - Queries that you can use to fetch filtered results. |
| 78 | + * @func query |
| 79 | + * @returns {Array<Variants>} Array of Variants. |
| 80 | + * |
| 81 | + * @example |
| 82 | + * import * as contentstack from '@contentstack/management' |
| 83 | + * const client = contentstack.client() |
| 84 | + * |
| 85 | + * client.stack().variants().query({ query: { name: 'Variants Name' } }).find() |
| 86 | + * .then((variants) => console.log(variants)) |
| 87 | + */ |
| 88 | + this.query = query({ http: http, wrapperCollection: VariantsCollection }) |
| 89 | + |
| 90 | + /** |
| 91 | + * @description The fetchByUIDs on Variants will allow to fetch details of specific Variants UID. |
| 92 | + * @memberof Variants |
| 93 | + * @param {Object} params - URI parameters |
| 94 | + * @prop {Object} params.query - fetchByUIDs that you can use to fetch filtered results. |
| 95 | + * @func query |
| 96 | + * @returns {Array<Variants>} Array of Variants. |
| 97 | + * |
| 98 | + * @example |
| 99 | + * import * as contentstack from '@contentstack/management' |
| 100 | + * const client = contentstack.client() |
| 101 | + * |
| 102 | + * client.stack().variants().fetchByUIDs(['uid1','uid2']).find() |
| 103 | + * .then((variants) => console.log(variants)) |
| 104 | + */ |
| 105 | + this.fetchByUIDs = async (variantUids) => { |
| 106 | + |
| 107 | + try { |
| 108 | + const response = await http.get(this.urlPath, { |
| 109 | + params: { |
| 110 | + uids: variantUids |
| 111 | + }, |
| 112 | + headers: { |
| 113 | + ...cloneDeep(this.stackHeaders) |
| 114 | + } |
| 115 | + }) |
| 116 | + if (response.data) { |
| 117 | + return response.data |
| 118 | + } else { |
| 119 | + throw error(response) |
| 120 | + } |
| 121 | + } catch (err) { |
| 122 | + throw error(err) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +export function VariantsCollection (http, data) { |
| 129 | + const obj = cloneDeep(data.variants) || [] |
| 130 | + const VariantsCollection = obj.map((userdata) => { |
| 131 | + return new Variants(http, { variants: userdata, stackHeaders: data.stackHeaders }) |
| 132 | + }) |
| 133 | + return VariantsCollection |
| 134 | +} |
0 commit comments