diff --git a/package-lock.json b/package-lock.json index 81ecc7d..971cc66 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@contentstack/app-sdk", - "version": "2.0.1", + "version": "2.0.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@contentstack/app-sdk", - "version": "2.0.1", + "version": "2.0.2", "license": "MIT", "dependencies": { "loader-utils": "^3.2.1", diff --git a/package.json b/package.json index 8c3da56..e341a27 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/app-sdk", - "version": "2.0.1", + "version": "2.0.2", "types": "dist/src/index.d.ts", "description": "The Contentstack App SDK allows you to customize your Contentstack applications.", "main": "dist/index.js", diff --git a/src/stack/api/content-type/entry.ts b/src/stack/api/content-type/entry.ts index 5cb1e75..4da34d7 100755 --- a/src/stack/api/content-type/entry.ts +++ b/src/stack/api/content-type/entry.ts @@ -3,12 +3,14 @@ import { getReferences, addQuery, language, + variant, environment, includeOwner, includeContentType, includeSchema, includeReference, } from "../../utils"; +import { GenericObjectType } from "../../../types/common.types"; let connection = {}; let contentTypeUid = ""; @@ -34,6 +36,7 @@ class Entry extends Base { const entryQuery = super.Query(); Object.assign(entryQuery, { language, + variant, environment, includeOwner, includeContentType, @@ -375,6 +378,47 @@ class Entry extends Base { this._query.locale = locale; return this.fetch("updateEntry", payload); } + /** + * @see {@link https://www.contentstack.com/docs/apis/content-management-api| variant} + * @name Stack#ContentType#Entry#fetchVariant + * @function + * @description This call allows you to get a variant customization of an entry. + * @param {string} variant_uid - parameter for the request + * @example appSDK.stack.ContentType('contenttype_uid').Entry('bltsomething123').variant('variant_uid')..fetch().then(...).catch(...); + * @return {external:Promise} + */ + + /** + * @see {@link https://www.contentstack.com/docs/apis/content-management-api| update variant} + * @name Stack#ContentType#Entry#updateVariant + * @function + * @description This call allows you to update variant customization of an entry. + * @param {string} variant_uid - parameter for the request + * @param {object} payload - parameter for the request + * @example appSDK.stack.ContentType('contenttype_uid').Entry('bltsomething123').updateVariant('variant_uid', + * { "entry": { + "title": "test variant entry", + "url": "/variant-url", + "_variant": { + "_change_set": ["title", "url"] + } + } + }).then(...).catch(...); + * @return {external:Promise} + */ + + updateVariant(variant_uid: string, payload: GenericObjectType) { + if ( + !variant_uid || + typeof variant_uid !== "string" || + typeof payload !== "object" || + payload instanceof Array + ) { + return Promise.reject(new Error("Kindly provide valid parameters")); + } + this._query.variant_uid = variant_uid; + return this.fetch("updateVariant", payload); + } } export default (uiConnection: any, contentType: string) => { @@ -387,6 +431,7 @@ export default (uiConnection: any, contentType: string) => { getReferences, addQuery, language, + variant, environment, includeOwner, includeContentType, diff --git a/src/stack/index.ts b/src/stack/index.ts index b482835..d8839e6 100755 --- a/src/stack/index.ts +++ b/src/stack/index.ts @@ -3,6 +3,8 @@ import ContentType from './api/content-type/index'; import { onData, onError } from "../utils/utils"; import { BranchDetail, GetAllStacksOptions, StackAdditionalData, StackDetail, StackSearchQuery } from '../types/stack.types'; import { IManagementTokenDetails } from '../types'; +import VariantGroup from "../variantGroup"; +import { GenericObjectType } from "../types/common.types"; /** @@ -18,6 +20,7 @@ class Stack { _data: StackDetail ContentType: any Asset: any + VariantGroup: any private _currentBranch: BranchDetail | null = null; @@ -43,6 +46,16 @@ class Stack { * */ this.Asset = Asset(connection); + /** + * @constructor + * @hideconstructor + * @desc An initializer is responsible for creating an VariantGroup object. + * @see {@link https://www.contentstack.com/docs/apis/content-management-api/| VariantGroup} + * @param {string} uid - variant group uid. + * @example appSDK.stack.VariantGroup('variant_group_uid') + * */ + this.VariantGroup = VariantGroup(connection); + const currentBranch = additionalData.currentBranch || "" if (currentBranch) { @@ -272,6 +285,29 @@ class Stack { getCurrentBranch(): BranchDetail | null { return this._currentBranch; } + + /** + * Returns variant groups of the current stack. + * @returns variant groups of the current stack + */ + getVariantGroups(query = {}, params = {}) { + const optionParams: GenericObjectType = params; + optionParams.query = query; + const options = { params: optionParams, action: 'getVariantGroups' }; + return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); + } + + /** + * Returns variant groups details. + * @returns variant groups details. + */ + getVariantById(variant_uid:string) { + if (!variant_uid) { + return Promise.reject(new Error('variant uid is required')); + } + const options = { params: {uid : variant_uid}, action: 'getVariantById' }; + return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); + } } export default Stack; diff --git a/src/stack/utils.ts b/src/stack/utils.ts index d8b2758..ba55608 100755 --- a/src/stack/utils.ts +++ b/src/stack/utils.ts @@ -108,6 +108,14 @@ export function language(languageCode) { throw Error("Argument should be a String."); } +export function variant(variant_uid) { + if (variant_uid && typeof variant_uid === "string") { + this._query.variant_uid = variant_uid; + return this; + } + throw Error("Argument should be a String."); +} + export function environment(environmentCode) { if (environmentCode && typeof environmentCode === "string") { this._query.environment = environmentCode; diff --git a/src/types/entry.types.ts b/src/types/entry.types.ts index 90083a2..f8b7882 100644 --- a/src/types/entry.types.ts +++ b/src/types/entry.types.ts @@ -1,5 +1,5 @@ import Field from "../field"; -import { AnyProperty } from "./common.types"; +import { AnyProperty, GenericObjectType } from "./common.types"; export declare interface IGetFieldOptions { /** @@ -47,4 +47,6 @@ export interface Entry extends AnyProperty { publish_details: Array; locale: string; url?: string; + _variant?: GenericObjectType ; + variant_uid?: string } diff --git a/src/variantGroup/index.ts b/src/variantGroup/index.ts new file mode 100644 index 0000000..e9a492e --- /dev/null +++ b/src/variantGroup/index.ts @@ -0,0 +1,65 @@ +import Base from "../stack/api/base"; +import { GenericObjectType } from "../types/common.types"; + +/** + * Class representing the Variant Group. + */ + +let connection = {}; + +class VariantGroup extends Base { + constructor(uid: string) { + if (!uid) { + throw new Error("uid is required"); + } + super(uid); + this._query = {}; + return this; + } + + static get connection() { + return connection; + } + + /** + * @function + * @name Stack#VariantGroup#createVariant + * @description This method creates a new variant in a group. + * @example appSDK.stack.VariantGroup("variant_group_uid").createVariant(variant_payload); + * @return {external:Promise} + */ + createVariant(payload: GenericObjectType) { + return this.fetch("createVariant", payload); + } + /** + * @function + * @name Stack#VariantGroup#getVariantsByGroup + * @description This method returns all the variants within a group. + * @example appSDK.stack.VariantGroup("variant_group_uid").getVariantsByGroup(); + * @return {external:Promise} + */ + getVariantsByGroup() { + return this.fetch("getVariantsByGroup"); + } + + /** + * @function + * @name Stack#VariantGroup#getVariantsByGroup#deleteVariant + * @description This method deletes a specified variant from a group. + * @example appSDK.stack.VariantGroup("variant_group_uid").deleteVariant("variant_uid"); + * @return {external:Promise} + */ + deleteVariant(variant_uid: string) { + this._query.variant_uid = variant_uid; + return this.fetch("deleteVariant"); + } +} + +export default (uiConnection: GenericObjectType) => { + connection = uiConnection; + return new Proxy(VariantGroup, { + apply(Target: any, thisArg, argumentsList: any[]) { + return new Target(...argumentsList); + }, + }); +};