Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Variants (Entry, VariantGroup & Type support) & package version bump #108

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
45 changes: 45 additions & 0 deletions src/stack/api/content-type/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -34,6 +36,7 @@ class Entry extends Base {
const entryQuery = super.Query();
Object.assign(entryQuery, {
language,
variant,
environment,
includeOwner,
includeContentType,
Expand Down Expand Up @@ -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) => {
Expand All @@ -387,6 +431,7 @@ export default (uiConnection: any, contentType: string) => {
getReferences,
addQuery,
language,
variant,
environment,
includeOwner,
includeContentType,
Expand Down
36 changes: 36 additions & 0 deletions src/stack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";


/**
Expand All @@ -18,6 +20,7 @@ class Stack {
_data: StackDetail
ContentType: any
Asset: any
VariantGroup: any
private _currentBranch: BranchDetail | null = null;


Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getVariantInfo


export default Stack;
8 changes: 8 additions & 0 deletions src/stack/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/types/entry.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Field from "../field";
import { AnyProperty } from "./common.types";
import { AnyProperty, GenericObjectType } from "./common.types";

export declare interface IGetFieldOptions {
/**
Expand Down Expand Up @@ -47,4 +47,6 @@ export interface Entry extends AnyProperty {
publish_details: Array<any>;
locale: string;
url?: string;
_variant?: GenericObjectType ;
variant_uid?: string
}
65 changes: 65 additions & 0 deletions src/variantGroup/index.ts
Original file line number Diff line number Diff line change
@@ -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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets not have it now.

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() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getVariants

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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets not expose it now

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);
},
});
};
Loading