From adabefb023e5ed6b81c591c8f692e2f75cdd2680 Mon Sep 17 00:00:00 2001 From: Praveen Mohan Date: Fri, 24 May 2024 17:14:40 +0530 Subject: [PATCH 1/3] feat: DEVREL-22 Added global fields handler --- src/stack/index.ts | 515 ++++++++++++++++++++++++++------------------- 1 file changed, 297 insertions(+), 218 deletions(-) diff --git a/src/stack/index.ts b/src/stack/index.ts index b482835..efe6d8b 100755 --- a/src/stack/index.ts +++ b/src/stack/index.ts @@ -1,249 +1,289 @@ -import Asset from './api/asset/index'; -import ContentType from './api/content-type/index'; +import Asset from "./api/asset/index"; +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 { + BranchDetail, + GetAllStacksOptions, + StackAdditionalData, + StackDetail, + StackSearchQuery, +} from "../types/stack.types"; +import { IManagementTokenDetails } from "../types"; /** * Class representing the current stack in Contentstack UI. */ class Stack { - /** - * @hideconstructor - */ - - _connection: any - _data: StackDetail - ContentType: any - Asset: any - private _currentBranch: BranchDetail | null = null; - - - constructor(data: StackDetail = {} as StackDetail, connection: any, additionalData: StackAdditionalData) { - this._connection = connection; - this._data = data; - /** - * @constructor - * @hideconstructor - * @desc Content type defines the structure or schema of a page or a section of your web or mobile property - * @see {@link https://www.contentstack.com/docs/apis/content-management-api/#content-types| ContentType} - * @param {string} uid - Uid of contenttype. - * @example appSDK.stack.ContentType('content_type_uid') - * */ - this.ContentType = ContentType(connection); /** - * @constructor * @hideconstructor - * @desc An initializer is responsible for creating an Asset object. - * @see {@link https://www.contentstack.com/docs/apis/content-management-api/#assets| Asset} - * @param {string} uid - UID of the asset. - * @example appSDK.stack.Asset('asset_uid') - * */ - this.Asset = Asset(connection); - - const currentBranch = additionalData.currentBranch || "" - - if (currentBranch) { - this._currentBranch = - (data.branches || []).find( - (branch) => branch.uid === additionalData.currentBranch - ) || null; - } + */ - } + _connection: any; + _data: StackDetail; + ContentType: any; + Asset: any; + private _currentBranch: BranchDetail | null = null; + constructor( + data: StackDetail = {} as StackDetail, + connection: any, + additionalData: StackAdditionalData + ) { + this._connection = connection; + this._data = data; + /** + * @constructor + * @hideconstructor + * @desc Content type defines the structure or schema of a page or a section of your web or mobile property + * @see {@link https://www.contentstack.com/docs/apis/content-management-api/#content-types| ContentType} + * @param {string} uid - Uid of contenttype. + * @example appSDK.stack.ContentType('content_type_uid') + * */ + this.ContentType = ContentType(connection); + /** + * @constructor + * @hideconstructor + * @desc An initializer is responsible for creating an Asset object. + * @see {@link https://www.contentstack.com/docs/apis/content-management-api/#assets| Asset} + * @param {string} uid - UID of the asset. + * @example appSDK.stack.Asset('asset_uid') + * */ + this.Asset = Asset(connection); - /** - * This method returns the data of the current stack. - * @return Returns stack data. - */ + const currentBranch = additionalData.currentBranch || ""; - getData(): StackDetail { - return this._data; - } + if (currentBranch) { + this._currentBranch = + (data.branches || []).find( + (branch) => branch.uid === additionalData.currentBranch + ) || null; + } + } + /** + * This method returns the data of the current stack. + * @return Returns stack data. + */ - /** - * This method returns all the stacks in the current organization. - * @param query asks for organization UID and query params to get all stacks - * @returns Stacks within current organization - */ - async getAllStacks({orgUid = "", params = {}}: GetAllStacksOptions = {}): Promise { - - // validation - if (typeof orgUid !== 'string') { - throw new TypeError('orgUid must be a string'); + getData(): StackDetail { + return this._data; } - const options = { - action: "getStacks", - headers: { organization_uid: orgUid || this._data.org_uid }, - skip_api_key: true, - params - }; - return this._connection - .sendToParent("stackQuery", options) - .then(onData) - .then((data) => data.stacks || []) - .catch(onError); - - } + /** + * This method returns all the stacks in the current organization. + * @param query asks for organization UID and query params to get all stacks + * @returns Stacks within current organization + */ + async getAllStacks({ + orgUid = "", + params = {}, + }: GetAllStacksOptions = {}): Promise { + // validation + if (typeof orgUid !== "string") { + throw new TypeError("orgUid must be a string"); + } - /** - * Get the details of all the management tokens of the stack. - * Note: This API does not return the token value. - * @see {@link https://www.contentstack.com/docs/developers/apis/content-management-api/#get-all-management-tokens | Get all management tokens} - * @returns Details of all the management token of the stack - */ - async getManagementTokens(): Promise { - const options = { action: "getManagementTokens" }; - return this._connection - .sendToParent("stackQuery", options) - .then(async (response) => { - const data = await onData<{tokens: IManagementTokenDetails[]}>(response); - return data.tokens || []; - }) - .catch(onError); - } + const options = { + action: "getStacks", + headers: { organization_uid: orgUid || this._data.org_uid }, + skip_api_key: true, + params, + }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .then((data) => data.stacks || []) + .catch(onError); + } - /** - * Gets the results of the search based on user query - * @param queries Array of key value pair of query parameters - * @param apiKey API key of the stack - * @returns Result of the query - */ - search(queries: StackSearchQuery, apiKey: string | null = this._data.api_key) { - const options = { params: queries, api_key: apiKey, action: "search" }; - return this._connection - .sendToParent("stackQuery", options) - .then(onData) - .catch(onError); - } + /** + * Get the details of all the management tokens of the stack. + * Note: This API does not return the token value. + * @see {@link https://www.contentstack.com/docs/developers/apis/content-management-api/#get-all-management-tokens | Get all management tokens} + * @returns Details of all the management token of the stack + */ + async getManagementTokens(): Promise { + const options = { action: "getManagementTokens" }; + return this._connection + .sendToParent("stackQuery", options) + .then(async (response) => { + const data = await onData<{ + tokens: IManagementTokenDetails[]; + }>(response); + return data.tokens || []; + }) + .catch(onError); + } + /** + * Gets the results of the search based on user query + * @param queries Array of key value pair of query parameters + * @param apiKey API key of the stack + * @returns Result of the query + */ + search( + queries: StackSearchQuery, + apiKey: string | null = this._data.api_key + ) { + const options = { params: queries, api_key: apiKey, action: "search" }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .catch(onError); + } - /** - * This API allows you to retrieve data of a content type of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-single-content-type| Content Type API} requests. This method returns a Promise object. - * @param {string} uid Uid of the desired content type - * @param {Object} params Optional parameters for the GET call - * @return {Object} A promise object which will be resolved with content type details. - */ - getContentType(uid: string, params = {}): Promise<{ [key: string]: any }> { - if (!uid) { - return Promise.reject(new Error('uid is required')); + /** + * This API allows you to retrieve data of a content type of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-single-content-type| Content Type API} requests. This method returns a Promise object. + * @param {string} uid Uid of the desired content type + * @param {Object} params Optional parameters for the GET call + * @return {Object} A promise object which will be resolved with content type details. + */ + getContentType(uid: string, params = {}): Promise<{ [key: string]: any }> { + if (!uid) { + return Promise.reject(new Error("uid is required")); + } + const options = { uid, params, action: "getContentType" }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .catch(onError); } - const options = { uid, params, action: 'getContentType' }; - return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); - } - /** - * This API allows you to retrieve data of a content types of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-content-types| Content Types API} requests. This method returns a Promise object. - * @param {Object} query Query for the GET call - * @param {Object} params Optional parameters for the GET call - * @return {Object} A promise object which will be resolved with details of the content type. - */ - getContentTypes(query = {}, params: { [key: string]: any } = {}): Promise<{ [key: string]: any }> { - const optionParams = params; - optionParams.query = query; - const options = { params: optionParams, action: 'getContentTypes' }; - return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); - } + /** + * This API allows you to retrieve data of a content types of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-content-types| Content Types API} requests. This method returns a Promise object. + * @param {Object} query Query for the GET call + * @param {Object} params Optional parameters for the GET call + * @return {Object} A promise object which will be resolved with details of the content type. + */ + getContentTypes( + query = {}, + params: { [key: string]: any } = {} + ): Promise<{ [key: string]: any }> { + const optionParams = params; + optionParams.query = query; + const options = { params: optionParams, action: "getContentTypes" }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .catch(onError); + } - /** - * This API allows you to retrieve environment details of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-single-environment| Environment API} requests. This method returns a Promise object. - * @param {string} name Name of the desired environment - * @param {Object} params Optional parameters for the GET call - * @return {Object} A promise object which will be resolved with environment details. - */ - getEnvironment(name: string, params = {}) { - if (!name) { - return Promise.reject(new Error('name is required')); + /** + * This API allows you to retrieve environment details of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-single-environment| Environment API} requests. This method returns a Promise object. + * @param {string} name Name of the desired environment + * @param {Object} params Optional parameters for the GET call + * @return {Object} A promise object which will be resolved with environment details. + */ + getEnvironment(name: string, params = {}) { + if (!name) { + return Promise.reject(new Error("name is required")); + } + const options = { name, params, action: "getEnvironment" }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .catch(onError); } - const options = { name, params, action: 'getEnvironment' }; - return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); - } - /** - * This API allows you to retrieve details of environments of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-environments| Environments API} requests. This method returns a Promise object. - * @param {Object} query Query for the GET call - * @param {Object} params Optional parameters for the GET call - * @return {Object} A Promise object which will be resolved with details of the environments. - */ - getEnvironments(query = {}, params = {}) { - const optionParams: { [key: string]: any } = params; - optionParams.query = query; - const options = { params: optionParams, action: 'getEnvironments' }; - return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); - } + /** + * This API allows you to retrieve details of environments of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-environments| Environments API} requests. This method returns a Promise object. + * @param {Object} query Query for the GET call + * @param {Object} params Optional parameters for the GET call + * @return {Object} A Promise object which will be resolved with details of the environments. + */ + getEnvironments(query = {}, params = {}) { + const optionParams: { [key: string]: any } = params; + optionParams.query = query; + const options = { params: optionParams, action: "getEnvironments" }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .catch(onError); + } - /** - * This API allows you to retrieve details of releases of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api/#get-all-releases| Releases API} requests. This method returns a Promise object. - * @param {Object} query Query for the GET call - * @param {Object} params Optional parameters for the GET call - * @return {Object} A Promise object which will be resolved with details of the releases. - */ - getReleases(query = {}, params = {}) { - const optionParams: { [key: string]: any } = params; - optionParams.query = query; - const options = { params: optionParams, action: 'getReleases' }; - return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); - } + /** + * This API allows you to retrieve details of releases of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api/#get-all-releases| Releases API} requests. This method returns a Promise object. + * @param {Object} query Query for the GET call + * @param {Object} params Optional parameters for the GET call + * @return {Object} A Promise object which will be resolved with details of the releases. + */ + getReleases(query = {}, params = {}) { + const optionParams: { [key: string]: any } = params; + optionParams.query = query; + const options = { params: optionParams, action: "getReleases" }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .catch(onError); + } - /** - * This API allows you to retrieve details of publish queue of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api/#get-publish-queue| Publish Queue API} requests. This method returns a Promise object. - * @param {Object} query Query for the GET call - * @param {Object} params Optional parameters for the GET call - * @return {Object} A Promise object which will be resolved with details of the publish queue. - */ - getPublishes(query = {}, params = {}) { - const optionParams: { [key: string]: any } = params; - optionParams.query = query; - const options = { params: optionParams, action: 'getPublishes' }; - return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); - } + /** + * This API allows you to retrieve details of publish queue of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api/#get-publish-queue| Publish Queue API} requests. This method returns a Promise object. + * @param {Object} query Query for the GET call + * @param {Object} params Optional parameters for the GET call + * @return {Object} A Promise object which will be resolved with details of the publish queue. + */ + getPublishes(query = {}, params = {}) { + const optionParams: { [key: string]: any } = params; + optionParams.query = query; + const options = { params: optionParams, action: "getPublishes" }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .catch(onError); + } - /** - * This API allows you to retrive a locale of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-language| Language API} requests. Method returns a Promise object. - * @param {string} code Code of the desired locale - * @param {Object} params Optional parameters for the GET call - * @return {Object} A promise object which will be resolved with locale details. - */ - getLocale(code: string, params = {}) { - if (!code) { - return Promise.reject(new Error('code is required')); + /** + * This API allows you to retrive a locale of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-language| Language API} requests. Method returns a Promise object. + * @param {string} code Code of the desired locale + * @param {Object} params Optional parameters for the GET call + * @return {Object} A promise object which will be resolved with locale details. + */ + getLocale(code: string, params = {}) { + if (!code) { + return Promise.reject(new Error("code is required")); + } + const options = { code, params, action: "getLocale" }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .catch(onError); } - const options = { code, params, action: 'getLocale' }; - return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); - } - /** - * This API allows you to retrive the locales of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-content-types| Languages API} requests. Method returns a Promise object. - * @param {Object} query Query for the GET call - * @param {Object} params Optional parameters for the GET call - * @return {Object} A Promise object which will be resolved with details of the locales. - */ - getLocales(query = {}, params = {}) { - const optionParams: { [key: string]: any } = params; - optionParams.query = query; - const options = { params: optionParams, action: 'getLocales' }; - return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); - } + /** + * This API allows you to retrive the locales of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-content-types| Languages API} requests. Method returns a Promise object. + * @param {Object} query Query for the GET call + * @param {Object} params Optional parameters for the GET call + * @return {Object} A Promise object which will be resolved with details of the locales. + */ + getLocales(query = {}, params = {}) { + const optionParams: { [key: string]: any } = params; + optionParams.query = query; + const options = { params: optionParams, action: "getLocales" }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .catch(onError); + } - /** - * This API allows you to retrive a workflow of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-workflow| Language API} requests. Method returns a Promise object. - * @param {string} code Code of the desired locale - * @param {Object} params Optional parameters for the GET call - * @return {Object} A promise object which will be resolved with locale details. - */ + /** + * This API allows you to retrive a workflow of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-workflow| Language API} requests. Method returns a Promise object. + * @param {string} code Code of the desired locale + * @param {Object} params Optional parameters for the GET call + * @return {Object} A promise object which will be resolved with locale details. + */ getWorkflow(uid: string, params = {}) { - if (!uid) { - return Promise.reject(new Error('workflow uid is required')); - } - const options = { uid, params, action: 'getWorkflow' }; - return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); + if (!uid) { + return Promise.reject(new Error("workflow uid is required")); + } + const options = { uid, params, action: "getWorkflow" }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .catch(onError); } - + /** * This API allows you to retrive the locales of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-content-types| Languages API} requests. Method returns a Promise object. * @param {Object} query Query for the GET call @@ -251,10 +291,13 @@ class Stack { * @return {Object} A Promise object which will be resolved with details of the locales. */ getWorkflows(query = {}, params = {}) { - const optionParams: { [key: string]: any } = params; - optionParams.query = query; - const options = { params: optionParams, action: 'getWorkflows' }; - return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); + const optionParams: { [key: string]: any } = params; + optionParams.query = query; + const options = { params: optionParams, action: "getWorkflows" }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .catch(onError); } /** @@ -262,7 +305,7 @@ class Stack { * @returns All branches of the current stack */ getAllBranches(): BranchDetail[] { - return this._data.branches || []; + return this._data.branches || []; } /** @@ -270,7 +313,43 @@ class Stack { * @returns current branch of the current stack if available */ getCurrentBranch(): BranchDetail | null { - return this._currentBranch; + return this._currentBranch; + } + + /** + * This API allows you to retrieve data of a single global field of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api#get-single-global-field| Global Field API} requests. This method returns a Promise object. + * @param {string} uid UID of the desired global field + * @param {Object} params Optional parameters for the GET call + * @return {Object} A promise object which will be resolved with global field details. + */ + getGlobalField(uid: string, params = {}): Promise<{ [key: string]: any }> { + if (!uid) { + return Promise.reject(new Error("uid is required")); + } + const options = { uid, params, action: "getGlobalField" }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .catch(onError); + } + + /** + * This API allows you to retrieve data of all global fields of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api#get-all-global-fields| Global Fields API} requests. This method returns a Promise object. + * @param {Object} query Query for the GET call + * @param {Object} params Optional parameters for the GET call + * @return {Object} A promise object which will be resolved with global field details. + */ + getGlobalFields( + query = {}, + params: { [key: string]: any } = {} + ): Promise<{ [key: string]: any }> { + const optionParams = params; + optionParams.query = query; + const options = { params: optionParams, action: "getGlobalFields" }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .catch(onError); } } From 9f28c7e421d8d1a7d7cfa02e2baedfcd4c0d54de Mon Sep 17 00:00:00 2001 From: Praveen Mohan Date: Fri, 24 May 2024 17:20:38 +0530 Subject: [PATCH 2/3] feat: DEVREL-22 Added global fields handler --- src/stack/index.ts | 517 +++++++++++++++++++-------------------------- 1 file changed, 219 insertions(+), 298 deletions(-) diff --git a/src/stack/index.ts b/src/stack/index.ts index efe6d8b..192d0a4 100755 --- a/src/stack/index.ts +++ b/src/stack/index.ts @@ -1,289 +1,249 @@ -import Asset from "./api/asset/index"; -import ContentType from "./api/content-type/index"; +import Asset from './api/asset/index'; +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 { BranchDetail, GetAllStacksOptions, StackAdditionalData, StackDetail, StackSearchQuery } from '../types/stack.types'; +import { IManagementTokenDetails } from '../types'; + /** * Class representing the current stack in Contentstack UI. */ class Stack { + /** + * @hideconstructor + */ + + _connection: any + _data: StackDetail + ContentType: any + Asset: any + private _currentBranch: BranchDetail | null = null; + + + constructor(data: StackDetail = {} as StackDetail, connection: any, additionalData: StackAdditionalData) { + this._connection = connection; + this._data = data; /** + * @constructor * @hideconstructor - */ + * @desc Content type defines the structure or schema of a page or a section of your web or mobile property + * @see {@link https://www.contentstack.com/docs/apis/content-management-api/#content-types| ContentType} + * @param {string} uid - Uid of contenttype. + * @example appSDK.stack.ContentType('content_type_uid') + * */ + this.ContentType = ContentType(connection); + /** + * @constructor + * @hideconstructor + * @desc An initializer is responsible for creating an Asset object. + * @see {@link https://www.contentstack.com/docs/apis/content-management-api/#assets| Asset} + * @param {string} uid - UID of the asset. + * @example appSDK.stack.Asset('asset_uid') + * */ + this.Asset = Asset(connection); - _connection: any; - _data: StackDetail; - ContentType: any; - Asset: any; - private _currentBranch: BranchDetail | null = null; + const currentBranch = additionalData.currentBranch || "" - constructor( - data: StackDetail = {} as StackDetail, - connection: any, - additionalData: StackAdditionalData - ) { - this._connection = connection; - this._data = data; - /** - * @constructor - * @hideconstructor - * @desc Content type defines the structure or schema of a page or a section of your web or mobile property - * @see {@link https://www.contentstack.com/docs/apis/content-management-api/#content-types| ContentType} - * @param {string} uid - Uid of contenttype. - * @example appSDK.stack.ContentType('content_type_uid') - * */ - this.ContentType = ContentType(connection); - /** - * @constructor - * @hideconstructor - * @desc An initializer is responsible for creating an Asset object. - * @see {@link https://www.contentstack.com/docs/apis/content-management-api/#assets| Asset} - * @param {string} uid - UID of the asset. - * @example appSDK.stack.Asset('asset_uid') - * */ - this.Asset = Asset(connection); + if (currentBranch) { + this._currentBranch = + (data.branches || []).find( + (branch) => branch.uid === additionalData.currentBranch + ) || null; + } - const currentBranch = additionalData.currentBranch || ""; + } - if (currentBranch) { - this._currentBranch = - (data.branches || []).find( - (branch) => branch.uid === additionalData.currentBranch - ) || null; - } - } - /** - * This method returns the data of the current stack. - * @return Returns stack data. - */ + /** + * This method returns the data of the current stack. + * @return Returns stack data. + */ - getData(): StackDetail { - return this._data; - } + getData(): StackDetail { + return this._data; + } - /** - * This method returns all the stacks in the current organization. - * @param query asks for organization UID and query params to get all stacks - * @returns Stacks within current organization - */ - async getAllStacks({ - orgUid = "", - params = {}, - }: GetAllStacksOptions = {}): Promise { - // validation - if (typeof orgUid !== "string") { - throw new TypeError("orgUid must be a string"); - } - const options = { - action: "getStacks", - headers: { organization_uid: orgUid || this._data.org_uid }, - skip_api_key: true, - params, - }; - return this._connection - .sendToParent("stackQuery", options) - .then(onData) - .then((data) => data.stacks || []) - .catch(onError); + /** + * This method returns all the stacks in the current organization. + * @param query asks for organization UID and query params to get all stacks + * @returns Stacks within current organization + */ + async getAllStacks({orgUid = "", params = {}}: GetAllStacksOptions = {}): Promise { + + // validation + if (typeof orgUid !== 'string') { + throw new TypeError('orgUid must be a string'); } - /** - * Get the details of all the management tokens of the stack. - * Note: This API does not return the token value. - * @see {@link https://www.contentstack.com/docs/developers/apis/content-management-api/#get-all-management-tokens | Get all management tokens} - * @returns Details of all the management token of the stack - */ - async getManagementTokens(): Promise { - const options = { action: "getManagementTokens" }; - return this._connection - .sendToParent("stackQuery", options) - .then(async (response) => { - const data = await onData<{ - tokens: IManagementTokenDetails[]; - }>(response); - return data.tokens || []; - }) - .catch(onError); - } + const options = { + action: "getStacks", + headers: { organization_uid: orgUid || this._data.org_uid }, + skip_api_key: true, + params + }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .then((data) => data.stacks || []) + .catch(onError); - /** - * Gets the results of the search based on user query - * @param queries Array of key value pair of query parameters - * @param apiKey API key of the stack - * @returns Result of the query - */ - search( - queries: StackSearchQuery, - apiKey: string | null = this._data.api_key - ) { - const options = { params: queries, api_key: apiKey, action: "search" }; - return this._connection - .sendToParent("stackQuery", options) - .then(onData) - .catch(onError); - } + } - /** - * This API allows you to retrieve data of a content type of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-single-content-type| Content Type API} requests. This method returns a Promise object. - * @param {string} uid Uid of the desired content type - * @param {Object} params Optional parameters for the GET call - * @return {Object} A promise object which will be resolved with content type details. - */ - getContentType(uid: string, params = {}): Promise<{ [key: string]: any }> { - if (!uid) { - return Promise.reject(new Error("uid is required")); - } - const options = { uid, params, action: "getContentType" }; - return this._connection - .sendToParent("stackQuery", options) - .then(onData) - .catch(onError); - } + /** + * Get the details of all the management tokens of the stack. + * Note: This API does not return the token value. + * @see {@link https://www.contentstack.com/docs/developers/apis/content-management-api/#get-all-management-tokens | Get all management tokens} + * @returns Details of all the management token of the stack + */ + async getManagementTokens(): Promise { + const options = { action: "getManagementTokens" }; + return this._connection + .sendToParent("stackQuery", options) + .then(async (response) => { + const data = await onData<{tokens: IManagementTokenDetails[]}>(response); + return data.tokens || []; + }) + .catch(onError); + } - /** - * This API allows you to retrieve data of a content types of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-content-types| Content Types API} requests. This method returns a Promise object. - * @param {Object} query Query for the GET call - * @param {Object} params Optional parameters for the GET call - * @return {Object} A promise object which will be resolved with details of the content type. - */ - getContentTypes( - query = {}, - params: { [key: string]: any } = {} - ): Promise<{ [key: string]: any }> { - const optionParams = params; - optionParams.query = query; - const options = { params: optionParams, action: "getContentTypes" }; - return this._connection - .sendToParent("stackQuery", options) - .then(onData) - .catch(onError); - } + /** + * Gets the results of the search based on user query + * @param queries Array of key value pair of query parameters + * @param apiKey API key of the stack + * @returns Result of the query + */ + search(queries: StackSearchQuery, apiKey: string | null = this._data.api_key) { + const options = { params: queries, api_key: apiKey, action: "search" }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .catch(onError); + } - /** - * This API allows you to retrieve environment details of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-single-environment| Environment API} requests. This method returns a Promise object. - * @param {string} name Name of the desired environment - * @param {Object} params Optional parameters for the GET call - * @return {Object} A promise object which will be resolved with environment details. - */ - getEnvironment(name: string, params = {}) { - if (!name) { - return Promise.reject(new Error("name is required")); - } - const options = { name, params, action: "getEnvironment" }; - return this._connection - .sendToParent("stackQuery", options) - .then(onData) - .catch(onError); - } - /** - * This API allows you to retrieve details of environments of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-environments| Environments API} requests. This method returns a Promise object. - * @param {Object} query Query for the GET call - * @param {Object} params Optional parameters for the GET call - * @return {Object} A Promise object which will be resolved with details of the environments. - */ - getEnvironments(query = {}, params = {}) { - const optionParams: { [key: string]: any } = params; - optionParams.query = query; - const options = { params: optionParams, action: "getEnvironments" }; - return this._connection - .sendToParent("stackQuery", options) - .then(onData) - .catch(onError); + /** + * This API allows you to retrieve data of a content type of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-single-content-type| Content Type API} requests. This method returns a Promise object. + * @param {string} uid Uid of the desired content type + * @param {Object} params Optional parameters for the GET call + * @return {Object} A promise object which will be resolved with content type details. + */ + getContentType(uid: string, params = {}): Promise<{ [key: string]: any }> { + if (!uid) { + return Promise.reject(new Error('uid is required')); } + const options = { uid, params, action: 'getContentType' }; + return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); + } - /** - * This API allows you to retrieve details of releases of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api/#get-all-releases| Releases API} requests. This method returns a Promise object. - * @param {Object} query Query for the GET call - * @param {Object} params Optional parameters for the GET call - * @return {Object} A Promise object which will be resolved with details of the releases. - */ - getReleases(query = {}, params = {}) { - const optionParams: { [key: string]: any } = params; - optionParams.query = query; - const options = { params: optionParams, action: "getReleases" }; - return this._connection - .sendToParent("stackQuery", options) - .then(onData) - .catch(onError); - } + /** + * This API allows you to retrieve data of a content types of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-content-types| Content Types API} requests. This method returns a Promise object. + * @param {Object} query Query for the GET call + * @param {Object} params Optional parameters for the GET call + * @return {Object} A promise object which will be resolved with details of the content type. + */ + getContentTypes(query = {}, params: { [key: string]: any } = {}): Promise<{ [key: string]: any }> { + const optionParams = params; + optionParams.query = query; + const options = { params: optionParams, action: 'getContentTypes' }; + return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); + } - /** - * This API allows you to retrieve details of publish queue of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api/#get-publish-queue| Publish Queue API} requests. This method returns a Promise object. - * @param {Object} query Query for the GET call - * @param {Object} params Optional parameters for the GET call - * @return {Object} A Promise object which will be resolved with details of the publish queue. - */ - getPublishes(query = {}, params = {}) { - const optionParams: { [key: string]: any } = params; - optionParams.query = query; - const options = { params: optionParams, action: "getPublishes" }; - return this._connection - .sendToParent("stackQuery", options) - .then(onData) - .catch(onError); + /** + * This API allows you to retrieve environment details of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-single-environment| Environment API} requests. This method returns a Promise object. + * @param {string} name Name of the desired environment + * @param {Object} params Optional parameters for the GET call + * @return {Object} A promise object which will be resolved with environment details. + */ + getEnvironment(name: string, params = {}) { + if (!name) { + return Promise.reject(new Error('name is required')); } + const options = { name, params, action: 'getEnvironment' }; + return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); + } - /** - * This API allows you to retrive a locale of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-language| Language API} requests. Method returns a Promise object. - * @param {string} code Code of the desired locale - * @param {Object} params Optional parameters for the GET call - * @return {Object} A promise object which will be resolved with locale details. - */ - getLocale(code: string, params = {}) { - if (!code) { - return Promise.reject(new Error("code is required")); - } - const options = { code, params, action: "getLocale" }; - return this._connection - .sendToParent("stackQuery", options) - .then(onData) - .catch(onError); - } + /** + * This API allows you to retrieve details of environments of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-environments| Environments API} requests. This method returns a Promise object. + * @param {Object} query Query for the GET call + * @param {Object} params Optional parameters for the GET call + * @return {Object} A Promise object which will be resolved with details of the environments. + */ + getEnvironments(query = {}, params = {}) { + const optionParams: { [key: string]: any } = params; + optionParams.query = query; + const options = { params: optionParams, action: 'getEnvironments' }; + return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); + } - /** - * This API allows you to retrive the locales of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-content-types| Languages API} requests. Method returns a Promise object. - * @param {Object} query Query for the GET call - * @param {Object} params Optional parameters for the GET call - * @return {Object} A Promise object which will be resolved with details of the locales. - */ - getLocales(query = {}, params = {}) { - const optionParams: { [key: string]: any } = params; - optionParams.query = query; - const options = { params: optionParams, action: "getLocales" }; - return this._connection - .sendToParent("stackQuery", options) - .then(onData) - .catch(onError); + /** + * This API allows you to retrieve details of releases of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api/#get-all-releases| Releases API} requests. This method returns a Promise object. + * @param {Object} query Query for the GET call + * @param {Object} params Optional parameters for the GET call + * @return {Object} A Promise object which will be resolved with details of the releases. + */ + getReleases(query = {}, params = {}) { + const optionParams: { [key: string]: any } = params; + optionParams.query = query; + const options = { params: optionParams, action: 'getReleases' }; + return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); + } + + /** + * This API allows you to retrieve details of publish queue of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api/#get-publish-queue| Publish Queue API} requests. This method returns a Promise object. + * @param {Object} query Query for the GET call + * @param {Object} params Optional parameters for the GET call + * @return {Object} A Promise object which will be resolved with details of the publish queue. + */ + getPublishes(query = {}, params = {}) { + const optionParams: { [key: string]: any } = params; + optionParams.query = query; + const options = { params: optionParams, action: 'getPublishes' }; + return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); + } + + /** + * This API allows you to retrive a locale of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-language| Language API} requests. Method returns a Promise object. + * @param {string} code Code of the desired locale + * @param {Object} params Optional parameters for the GET call + * @return {Object} A promise object which will be resolved with locale details. + */ + getLocale(code: string, params = {}) { + if (!code) { + return Promise.reject(new Error('code is required')); } + const options = { code, params, action: 'getLocale' }; + return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); + } - /** - * This API allows you to retrive a workflow of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-workflow| Language API} requests. Method returns a Promise object. - * @param {string} code Code of the desired locale - * @param {Object} params Optional parameters for the GET call - * @return {Object} A promise object which will be resolved with locale details. - */ + /** + * This API allows you to retrive the locales of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-content-types| Languages API} requests. Method returns a Promise object. + * @param {Object} query Query for the GET call + * @param {Object} params Optional parameters for the GET call + * @return {Object} A Promise object which will be resolved with details of the locales. + */ + getLocales(query = {}, params = {}) { + const optionParams: { [key: string]: any } = params; + optionParams.query = query; + const options = { params: optionParams, action: 'getLocales' }; + return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); + } + + /** + * This API allows you to retrive a workflow of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-workflow| Language API} requests. Method returns a Promise object. + * @param {string} code Code of the desired locale + * @param {Object} params Optional parameters for the GET call + * @return {Object} A promise object which will be resolved with locale details. + */ getWorkflow(uid: string, params = {}) { - if (!uid) { - return Promise.reject(new Error("workflow uid is required")); - } - const options = { uid, params, action: "getWorkflow" }; - return this._connection - .sendToParent("stackQuery", options) - .then(onData) - .catch(onError); + if (!uid) { + return Promise.reject(new Error('workflow uid is required')); + } + const options = { uid, params, action: 'getWorkflow' }; + return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); } - + /** * This API allows you to retrive the locales of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-content-types| Languages API} requests. Method returns a Promise object. * @param {Object} query Query for the GET call @@ -291,13 +251,10 @@ class Stack { * @return {Object} A Promise object which will be resolved with details of the locales. */ getWorkflows(query = {}, params = {}) { - const optionParams: { [key: string]: any } = params; - optionParams.query = query; - const options = { params: optionParams, action: "getWorkflows" }; - return this._connection - .sendToParent("stackQuery", options) - .then(onData) - .catch(onError); + const optionParams: { [key: string]: any } = params; + optionParams.query = query; + const options = { params: optionParams, action: 'getWorkflows' }; + return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError); } /** @@ -305,7 +262,7 @@ class Stack { * @returns All branches of the current stack */ getAllBranches(): BranchDetail[] { - return this._data.branches || []; + return this._data.branches || []; } /** @@ -313,44 +270,8 @@ class Stack { * @returns current branch of the current stack if available */ getCurrentBranch(): BranchDetail | null { - return this._currentBranch; - } - - /** - * This API allows you to retrieve data of a single global field of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api#get-single-global-field| Global Field API} requests. This method returns a Promise object. - * @param {string} uid UID of the desired global field - * @param {Object} params Optional parameters for the GET call - * @return {Object} A promise object which will be resolved with global field details. - */ - getGlobalField(uid: string, params = {}): Promise<{ [key: string]: any }> { - if (!uid) { - return Promise.reject(new Error("uid is required")); - } - const options = { uid, params, action: "getGlobalField" }; - return this._connection - .sendToParent("stackQuery", options) - .then(onData) - .catch(onError); - } - - /** - * This API allows you to retrieve data of all global fields of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api#get-all-global-fields| Global Fields API} requests. This method returns a Promise object. - * @param {Object} query Query for the GET call - * @param {Object} params Optional parameters for the GET call - * @return {Object} A promise object which will be resolved with global field details. - */ - getGlobalFields( - query = {}, - params: { [key: string]: any } = {} - ): Promise<{ [key: string]: any }> { - const optionParams = params; - optionParams.query = query; - const options = { params: optionParams, action: "getGlobalFields" }; - return this._connection - .sendToParent("stackQuery", options) - .then(onData) - .catch(onError); + return this._currentBranch; } } -export default Stack; +export default Stack; \ No newline at end of file From 5344b5ca0be4de6e2b1fae6d05aae6e356a3180f Mon Sep 17 00:00:00 2001 From: Praveen Mohan Date: Fri, 24 May 2024 17:22:23 +0530 Subject: [PATCH 3/3] feat: DEVREL-22 Added global fields handler --- src/stack/index.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/stack/index.ts b/src/stack/index.ts index 192d0a4..0977815 100755 --- a/src/stack/index.ts +++ b/src/stack/index.ts @@ -272,6 +272,42 @@ class Stack { getCurrentBranch(): BranchDetail | null { return this._currentBranch; } + + /** + * This API allows you to retrieve data of a single global field of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api#get-single-global-field| Global Field API} requests. This method returns a Promise object. + * @param {string} uid UID of the desired global field + * @param {Object} params Optional parameters for the GET call + * @return {Object} A promise object which will be resolved with global field details. + */ + getGlobalField(uid: string, params = {}): Promise<{ [key: string]: any }> { + if (!uid) { + return Promise.reject(new Error("uid is required")); + } + const options = { uid, params, action: "getGlobalField" }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .catch(onError); + } + + /** + * This API allows you to retrieve data of all global fields of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api#get-all-global-fields| Global Fields API} requests. This method returns a Promise object. + * @param {Object} query Query for the GET call + * @param {Object} params Optional parameters for the GET call + * @return {Object} A promise object which will be resolved with global field details. + */ + getGlobalFields( + query = {}, + params: { [key: string]: any } = {} + ): Promise<{ [key: string]: any }> { + const optionParams = params; + optionParams.query = query; + const options = { params: optionParams, action: "getGlobalFields" }; + return this._connection + .sendToParent("stackQuery", options) + .then(onData) + .catch(onError); + } } export default Stack; \ No newline at end of file