From 7658bba184d0db029da1cac5e0df30885accc886 Mon Sep 17 00:00:00 2001 From: Abhishek Ezhava Date: Wed, 24 Apr 2024 11:07:19 +0530 Subject: [PATCH 1/8] feat: variant changes feat:varaint support commit 14 commit 13 commit 12 commit 11 commit 10 commit 9 commit 8 commit 7 commit 6 commit 5 commit 4 commit 3 commit 2 commit first commit fix: type for variant instance fix: type for variant instance & package version --- package-lock.json | 4 +- package.json | 2 +- src/stack/api/content-type/entry.ts | 50 ++++++++++++++++++++++ src/stack/index.ts | 24 +++++++++++ src/types/entry.types.ts | 4 +- src/variantGroup/index.ts | 64 +++++++++++++++++++++++++++++ 6 files changed, 144 insertions(+), 4 deletions(-) create mode 100644 src/variantGroup/index.ts 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..0e899ae 100755 --- a/src/stack/api/content-type/entry.ts +++ b/src/stack/api/content-type/entry.ts @@ -9,6 +9,7 @@ import { includeSchema, includeReference, } from "../../utils"; +import { GenericObjectType } from "../../../types/common.types"; let connection = {}; let contentTypeUid = ""; @@ -375,6 +376,55 @@ class Entry extends Base { this._query.locale = locale; return this.fetch("updateEntry", payload); } + /** + * @see {@link https://www.contentstack.com/docs/apis/content-management-api| fetch variant} + * @name Stack#ContentType#Entry#fetchVariant + * @function + * @description This call allows you to fetch variant customization of an entry. + * @param {string} variant_uid - parameter for the request + * @example appSDK.stack.ContentType('contenttype_uid').Entry('bltsomething123').fetchVariant('variant_uid').then(...).catch(...); + * @return {external:Promise} + */ + + fetchVariant(variant_uid: string) { + if (!variant_uid || typeof variant_uid !== "string") { + return Promise.reject(new Error("Kindly provide valid parameters")); + } + this._query.variant_uid = variant_uid; + return this.fetch("fetchVariant"); + } + + /** + * @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) => { diff --git a/src/stack/index.ts b/src/stack/index.ts index b482835..d2155b9 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,17 @@ 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); + } } export default Stack; diff --git a/src/types/entry.types.ts b/src/types/entry.types.ts index 90083a2..1bc53b7 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_id?: string } diff --git a/src/variantGroup/index.ts b/src/variantGroup/index.ts new file mode 100644 index 0000000..d98346e --- /dev/null +++ b/src/variantGroup/index.ts @@ -0,0 +1,64 @@ +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); + }, + }); +}; From 9d6304551636b420aeb3ad98a33eb2c782a17f80 Mon Sep 17 00:00:00 2001 From: Abhishek Ezhava Date: Mon, 29 Apr 2024 19:44:36 +0530 Subject: [PATCH 2/8] feat: get single variant group details --- src/variantGroup/index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/variantGroup/index.ts b/src/variantGroup/index.ts index d98346e..f66aad1 100644 --- a/src/variantGroup/index.ts +++ b/src/variantGroup/index.ts @@ -20,6 +20,17 @@ class VariantGroup extends Base { static get connection() { return connection; } + /** + * @function + * @name Stack#VariantGroup#getSingleVariantGroupDetails + * @description This method retrieves a single variant group details. + * @example appSDK.stack.VariantGroup("variant_group_uid").getSingleVariantGroupDetails(); + * @return {external:Promise} + */ + getSingleVariantGroupDetails() { + return this.fetch("getSingleVariantGroupDetails"); + } + /** * @function * @name Stack#VariantGroup#createVariant From 800475f938367aa473693dfea527e30b79668ce5 Mon Sep 17 00:00:00 2001 From: Abhishek Ezhava Date: Mon, 29 Apr 2024 19:53:12 +0530 Subject: [PATCH 3/8] fix:revert last commit for single variant group details --- src/variantGroup/index.ts | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/variantGroup/index.ts b/src/variantGroup/index.ts index f66aad1..e9a492e 100644 --- a/src/variantGroup/index.ts +++ b/src/variantGroup/index.ts @@ -20,17 +20,7 @@ class VariantGroup extends Base { static get connection() { return connection; } - /** - * @function - * @name Stack#VariantGroup#getSingleVariantGroupDetails - * @description This method retrieves a single variant group details. - * @example appSDK.stack.VariantGroup("variant_group_uid").getSingleVariantGroupDetails(); - * @return {external:Promise} - */ - getSingleVariantGroupDetails() { - return this.fetch("getSingleVariantGroupDetails"); - } - + /** * @function * @name Stack#VariantGroup#createVariant From 89a06059076c952f281b03b80b98f38098c861d3 Mon Sep 17 00:00:00 2001 From: Abhishek Ezhava Date: Tue, 30 Apr 2024 17:13:40 +0530 Subject: [PATCH 4/8] feat: handler for get variant by id --- src/stack/index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/stack/index.ts b/src/stack/index.ts index d2155b9..8ba38fd 100755 --- a/src/stack/index.ts +++ b/src/stack/index.ts @@ -296,6 +296,17 @@ class Stack { 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(query={}, params = {}) { + const optionParams: GenericObjectType = params; + optionParams.query = query; + const options = { params: optionParams, action: 'getVariantById' }; + return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); + } } export default Stack; From a63047236413978c364eef0389b76b96818b94ad Mon Sep 17 00:00:00 2001 From: Abhishek Ezhava Date: Tue, 30 Apr 2024 17:24:15 +0530 Subject: [PATCH 5/8] fix: param option --- src/stack/index.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/stack/index.ts b/src/stack/index.ts index 8ba38fd..c47efbc 100755 --- a/src/stack/index.ts +++ b/src/stack/index.ts @@ -301,10 +301,11 @@ class Stack { * Returns variant groups details. * @returns variant groups details. */ - getVariantById(query={}, params = {}) { - const optionParams: GenericObjectType = params; - optionParams.query = query; - const options = { params: optionParams, action: 'getVariantById' }; + getVariantById(variant_uid:string) { + if (!variant_uid) { + return Promise.reject(new Error('variant uid is required')); + } + const options = { params: variant_uid, action: 'getVariantById' }; return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); } } From 91f02d9de214e9876e0c48c019fff19e4e7ff7b6 Mon Sep 17 00:00:00 2001 From: Abhishek Ezhava Date: Tue, 30 Apr 2024 17:27:15 +0530 Subject: [PATCH 6/8] fix: param option --- src/stack/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stack/index.ts b/src/stack/index.ts index c47efbc..d8839e6 100755 --- a/src/stack/index.ts +++ b/src/stack/index.ts @@ -305,7 +305,7 @@ class Stack { if (!variant_uid) { return Promise.reject(new Error('variant uid is required')); } - const options = { params: variant_uid, action: 'getVariantById' }; + const options = { params: {uid : variant_uid}, action: 'getVariantById' }; return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); } } From e9590c5016207981cc4f38a51236f1b4d62ac60a Mon Sep 17 00:00:00 2001 From: Abhishek Ezhava Date: Tue, 30 Apr 2024 19:01:14 +0530 Subject: [PATCH 7/8] feat: variant from entry fetch --- src/stack/api/content-type/entry.ts | 17 ++++++----------- src/stack/utils.ts | 8 ++++++++ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/stack/api/content-type/entry.ts b/src/stack/api/content-type/entry.ts index 0e899ae..4da34d7 100755 --- a/src/stack/api/content-type/entry.ts +++ b/src/stack/api/content-type/entry.ts @@ -3,6 +3,7 @@ import { getReferences, addQuery, language, + variant, environment, includeOwner, includeContentType, @@ -35,6 +36,7 @@ class Entry extends Base { const entryQuery = super.Query(); Object.assign(entryQuery, { language, + variant, environment, includeOwner, includeContentType, @@ -377,23 +379,15 @@ class Entry extends Base { return this.fetch("updateEntry", payload); } /** - * @see {@link https://www.contentstack.com/docs/apis/content-management-api| fetch variant} + * @see {@link https://www.contentstack.com/docs/apis/content-management-api| variant} * @name Stack#ContentType#Entry#fetchVariant * @function - * @description This call allows you to fetch variant customization of an entry. + * @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').fetchVariant('variant_uid').then(...).catch(...); + * @example appSDK.stack.ContentType('contenttype_uid').Entry('bltsomething123').variant('variant_uid')..fetch().then(...).catch(...); * @return {external:Promise} */ - fetchVariant(variant_uid: string) { - if (!variant_uid || typeof variant_uid !== "string") { - return Promise.reject(new Error("Kindly provide valid parameters")); - } - this._query.variant_uid = variant_uid; - return this.fetch("fetchVariant"); - } - /** * @see {@link https://www.contentstack.com/docs/apis/content-management-api| update variant} * @name Stack#ContentType#Entry#updateVariant @@ -437,6 +431,7 @@ export default (uiConnection: any, contentType: string) => { getReferences, addQuery, language, + variant, environment, includeOwner, includeContentType, diff --git a/src/stack/utils.ts b/src/stack/utils.ts index d8b2758..494ddf4 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.locale = variant_uid; + return this; + } + throw Error("Argument should be a String."); +} + export function environment(environmentCode) { if (environmentCode && typeof environmentCode === "string") { this._query.environment = environmentCode; From 2a75ebac2851a2e15e6755daba6fab59ccd1acd5 Mon Sep 17 00:00:00 2001 From: Abhishek Ezhava Date: Thu, 2 May 2024 17:03:06 +0530 Subject: [PATCH 8/8] fix: entry type change --- src/stack/utils.ts | 2 +- src/types/entry.types.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stack/utils.ts b/src/stack/utils.ts index 494ddf4..ba55608 100755 --- a/src/stack/utils.ts +++ b/src/stack/utils.ts @@ -110,7 +110,7 @@ export function language(languageCode) { export function variant(variant_uid) { if (variant_uid && typeof variant_uid === "string") { - this._query.locale = variant_uid; + this._query.variant_uid = variant_uid; return this; } throw Error("Argument should be a String."); diff --git a/src/types/entry.types.ts b/src/types/entry.types.ts index 1bc53b7..f8b7882 100644 --- a/src/types/entry.types.ts +++ b/src/types/entry.types.ts @@ -48,5 +48,5 @@ export interface Entry extends AnyProperty { locale: string; url?: string; _variant?: GenericObjectType ; - variant_id?: string + variant_uid?: string }