11import cloneDeep from 'lodash/cloneDeep'
22import error from '../core/contentstackError'
33import { create , deleteEntity , fetch , fetchAll , update } from '../entity'
4+ import { Installation } from './installation'
45export function App ( http , data ) {
56 http . defaults . versioningStrategy = undefined
67 this . urlPath = '/apps'
@@ -29,23 +30,14 @@ export function App (http, data) {
2930 * @example
3031 * import * as contentstack from '@contentstack/management'
3132 * const client = contentstack.client({ authtoken: 'TOKEN'})
32- * const app = {
33+ * const updateApp = {
3334 * name: 'APP_NAME',
3435 * description: 'APP_DESCRIPTION',
3536 * target_type: 'stack'/'organization',
36- * webhook: // optional
37- * {
38- * target_url: 'TARGET_URL',
39- * channel: 'CHANNEL'
40- * },
41- * oauth: // optional
42- * {
43- * redirect_uri: 'REDIRECT_URI',
44- * enabled: true,
45- * }
4637 * }
47- *
48- * client.organization('organization_uid').app('app_uid').update({app})
38+ * const app = client.organization('organization_uid').app('app_uid')
39+ * app = Object.assign(app, updateApp)
40+ * app.update()
4941 * .then((app) => console.log(app))
5042 *
5143 */
@@ -71,7 +63,7 @@ export function App (http, data) {
7163 * @description The delete an app call is used to delete the app.
7264 * @memberof App
7365 * @func delete
74- * @returns {Promise<App > }
66+ * @returns {Promise<Response > }
7567 *
7668 * @example
7769 * import * as contentstack from '@contentstack/management'
@@ -157,6 +149,56 @@ export function App (http, data) {
157149 throw error ( err )
158150 }
159151 }
152+
153+ /**
154+ * @description The install call is used to initiate the installation of the app
155+ * @param {String } param.targetType - The target on which app needs to be installed, stack or ogranization.
156+ * @param {String } param.targetUid - The uid of the target, on which the app will be installed
157+ * @returns Promise<Installation>
158+ *
159+ * @example
160+ * import * as contentstack from '@contentstack/management'
161+ * const client = contentstack.client({ authtoken: 'TOKEN'})
162+ * client.organization('organization_uid').app('app_uid').install({ targetUid: 'STACK_API_KEY', targetType: 'stack' })
163+ * .then((installation) => console.log(installation))
164+ */
165+ this . install = async ( { targetUid, targetType } ) => {
166+ try {
167+ const headers = {
168+ headers : { ...cloneDeep ( this . params ) }
169+ } || { }
170+
171+ const response = await http . post ( `${ this . urlPath } /install` , { target_type : targetType , target_uid : targetUid } , headers )
172+ if ( response . data ) {
173+ return new Installation ( http , response . data , this . params ) || { }
174+ } else {
175+ throw error ( response )
176+ }
177+ } catch ( err ) {
178+ throw error ( err )
179+ }
180+ }
181+
182+ /**
183+ * @description The Installation will allow you to fetch, update and delete of the app installation.
184+ * @param {String } uid Installation uid
185+ * @returns Installation
186+ *
187+ * @example
188+ * import * as contentstack from '@contentstack/management'
189+ * const client = contentstack.client({ authtoken: 'TOKEN'})
190+ * client.organization('organization_uid').app('app_uid').installation().findAll()
191+ * .then((installations) => console.log(installations))
192+ *
193+ * @example
194+ * import * as contentstack from '@contentstack/management'
195+ * const client = contentstack.client({ authtoken: 'TOKEN'})
196+ * client.organization('organization_uid').app('app_uid').installation('installation_uid').fetch()
197+ * .then((installation) => console.log(installation))
198+ */
199+ this . installation = ( uid = null ) => {
200+ return new Installation ( http , uid ? { data : { uid } } : { app_uid : this . uid } , this . params )
201+ }
160202 } else {
161203 /**
162204 * @description The create an app call is used for creating a new app in your Contentstack organization.
@@ -193,7 +235,7 @@ export function App (http, data) {
193235 * @description The create an app call is used for creating a new app in your Contentstack organization.
194236 * @memberof App
195237 * @func create
196- * @returns {Promise<App> }
238+ * @returns {Promise<ContentstackCollection< App> > }
197239 *
198240 * @example
199241 * import * as contentstack from '@contentstack/management'
@@ -212,6 +254,6 @@ export function App (http, data) {
212254export function AppCollection ( http , data ) {
213255 const obj = cloneDeep ( data . data ) || [ ]
214256 return obj . map ( ( appData ) => {
215- return new App ( http , { data : appData , stackHeaders : data . stackHeaders } )
257+ return new App ( http , { data : appData } )
216258 } )
217259}
0 commit comments