11import cloneDeep from 'lodash/cloneDeep'
2- import error from '../../ core/contentstackError'
3- import { create , deleteEntity , fetch , update } from '../ ../entity'
4- import { Authorization } from '.. /authorization'
2+ import error from '../core/contentstackError'
3+ import { create , deleteEntity , fetch , fetchAll , update } from '../entity'
4+ import { Authorization } from './authorization'
55import { Hosting } from './hosting'
6- import { Installation , InstallationCollection } from '../installation'
7- import { Oauth } from './oauth'
8- import ContentstackCollection from '../../contentstackCollection'
6+ import { Installation } from './installation'
97
108export function App ( http , data ) {
119 http . defaults . versioningStrategy = undefined
@@ -27,25 +25,25 @@ export function App (http, data) {
2725 this . urlPath = `/manifests/${ this . uid } `
2826
2927 /**
30- * @description The update manifest call is used to update the app details such as name, description, icon, and so on.
31- * @memberof App
32- * @func update
33- * @returns {Promise<App> }
34- *
35- * @example
36- * import * as contentstack from '@contentstack/management'
37- * const client = contentstack.client({ authtoken: 'TOKEN'})
38- * const updateApp = {
39- * name: 'APP_NAME',
40- * description: 'APP_DESCRIPTION',
41- * target_type: 'stack'/'organization',
42- * }
43- * const app = client.organization('organization_uid').app('manifest_uid')
44- * app = Object.assign(app, updateApp)
45- * app.update()
46- * .then((app) => console.log(app))
47- *
48- */
28+ * @description The update manifest call is used to update the app details such as name, description, icon, and so on.
29+ * @memberof App
30+ * @func update
31+ * @returns {Promise<App> }
32+ *
33+ * @example
34+ * import * as contentstack from '@contentstack/management'
35+ * const client = contentstack.client({ authtoken: 'TOKEN'})
36+ * const updateApp = {
37+ * name: 'APP_NAME',
38+ * description: 'APP_DESCRIPTION',
39+ * target_type: 'stack'/'organization',
40+ * }
41+ * const app = client.organization('organization_uid').app('manifest_uid')
42+ * app = Object.assign(app, updateApp)
43+ * app.update()
44+ * .then((app) => console.log(app))
45+ *
46+ */
4947 this . update = update ( http , undefined , this . params )
5048
5149 /**
@@ -80,25 +78,85 @@ export function App (http, data) {
8078 this . delete = deleteEntity ( http , false , this . params )
8179
8280 /**
83- * @description Oauth will allow to get, update auth and get scopes .
81+ * @description The get oauth call is used to fetch the OAuth details of the app .
8482 * @memberof App
85- * @func oauth
83+ * @func fetchOAuth
8684 * @returns {Promise<AppOAuth> }
87- * @returns {Oauth }
8885 *
8986 * @example
9087 * import * as contentstack from '@contentstack/management'
9188 * const client = contentstack.client({ authtoken: 'TOKEN'})
92- * client.organization('organization_uid').app('manifest_uid').oauth()
89+ *
90+ * client.organization('organization_uid').app('manifest_uid').fetchOAuth()
91+ * .then((oAuthConfig) => console.log(oAuthConfig))
9392 */
94- this . oauth = ( ) => {
95- return new Oauth ( http , { app_uid : this . uid , organization_uid : this . organization_uid } , this . params )
93+ this . fetchOAuth = async ( param = { } ) => {
94+ try {
95+ const headers = {
96+ headers : { ...cloneDeep ( this . params ) } ,
97+ params : {
98+ ...cloneDeep ( param )
99+ }
100+ } || { }
101+
102+ const response = await http . get ( `${ this . urlPath } /oauth` , headers )
103+ if ( response . data ) {
104+ return response . data . data || { }
105+ } else {
106+ throw error ( response )
107+ }
108+ } catch ( err ) {
109+ throw error ( err )
110+ }
111+ }
112+
113+ /**
114+ * @description The change oauth details call is used to update the OAuth details, (redirect url and permission scope) of an app.
115+ * @memberof App
116+ * @func updateOAuth
117+ * @returns {Promise<AppOAuth> }
118+ *
119+ * @example
120+ * import * as contentstack from '@contentstack/management'
121+ * const client = contentstack.client({ authtoken: 'TOKEN'})
122+ * const config = {
123+ * redirect_uri: 'REDIRECT_URI',
124+ * app_token_config: {
125+ * enabled: true,
126+ * scopes: ['scope1', 'scope2']
127+ * },
128+ * user_token_config: {
129+ * enabled: true,
130+ * scopes: ['scope1', 'scope2']
131+ * }
132+ * }
133+ * client.organization('organization_uid').app('manifest_uid').updateOAuth({ config })
134+ * .then((oAuthConfig) => console.log(oAuthConfig))
135+ */
136+ this . updateOAuth = async ( { config, param = { } } ) => {
137+ try {
138+ const headers = {
139+ headers : { ...cloneDeep ( this . params ) } ,
140+ params : {
141+ ...cloneDeep ( param )
142+ }
143+ } || { }
144+
145+ const response = await http . put ( `${ this . urlPath } /oauth` , config , headers )
146+ if ( response . data ) {
147+ return response . data . data || { }
148+ } else {
149+ throw error ( response )
150+ }
151+ } catch ( err ) {
152+ throw error ( err )
153+ }
96154 }
97155
98156 /**
99157 * @description The hosting will allow you get, update, deploy manifest.
100158 * @memberof App
101- * @func hosting
159+ * @func updateOAuth
102160 * @returns {Promise<AppOAuth> }
103161 * @returns {Hosting }
104162 *
@@ -143,34 +201,26 @@ export function App (http, data) {
143201 }
144202
145203 /**
146- * @description The upgrade call is used to upgrade the installation of an app
204+ * @description The Installation will allow you to fetch, update and delete of the app installation.
147205 * @memberof App
148- * @func upgrade
149- * @param {String } param.targetType - The target on which app needs to be installed, stack or ogranization.
150- * @param {String } param.targetUid - The uid of the target, on which the app will be installed
151- * @returns Promise<Installation>
206+ * @func installation
207+ * @param {String } uid Installation uid
208+ * @returns Installation
152209 *
153210 * @example
154211 * import * as contentstack from '@contentstack/management'
155212 * const client = contentstack.client({ authtoken: 'TOKEN'})
156- * client.organization('organization_uid').app('manifest_uid').install({ targetUid: 'STACK_API_KEY', targetType: 'stack' })
213+ * client.organization('organization_uid').app('manifest_uid').installation().findAll()
214+ * .then((installations) => console.log(installations))
215+ *
216+ * @example
217+ * import * as contentstack from '@contentstack/management'
218+ * const client = contentstack.client({ authtoken: 'TOKEN'})
219+ * client.organization('organization_uid').app('manifest_uid').installation('installation_uid').fetch()
157220 * .then((installation) => console.log(installation))
158221 */
159- this . upgrade = async ( { targetUid, targetType } ) => {
160- try {
161- const headers = {
162- headers : { ...cloneDeep ( this . params ) }
163- } || { }
164-
165- const response = await http . put ( `${ this . urlPath } /reinstall` , { target_type : targetType , target_uid : targetUid } , headers )
166- if ( response . data ) {
167- return new Installation ( http , response . data , this . params ) || { }
168- } else {
169- throw error ( response )
170- }
171- } catch ( err ) {
172- throw error ( err )
173- }
222+ this . installation = ( uid = null ) => {
223+ return new Installation ( http , uid ? { data : { uid } } : { app_uid : this . uid } , this . params )
174224 }
175225 /**
176226 * @description The GET app requests of an app call is used to retrieve all requests of an app.
@@ -260,36 +310,6 @@ export function App (http, data) {
260310 this . authorization = ( ) => {
261311 return new Authorization ( http , { app_uid : this . uid } , this . params )
262312 }
263-
264- /**
265- * @description The list installation call is used to retrieve all installations of your Contentstack organization.
266- * @memberof App
267- * @func listInstallations
268- * @returns {Promise<ContentstackCollection<Installation>> }
269- *
270- * @example
271- * import * as contentstack from '@contentstack/management'
272- * const client = contentstack.client({ authtoken: 'TOKEN'})
273- *
274- * client.organization('organization_uid').app('app_uid').listInstallations()
275- * .then((collection) => console.log(collection))
276- *
277- */
278- this . listInstallations = async ( ) => {
279- try {
280- const headers = {
281- headers : { ...cloneDeep ( this . params ) , ...cloneDeep ( this . headers ) }
282- }
283- const response = await http . get ( `manifests/${ this . uid } /installations` , headers )
284- if ( response . data ) {
285- return new ContentstackCollection ( response , http , this . stackHeaders , InstallationCollection )
286- } else {
287- throw error ( response )
288- }
289- } catch ( err ) {
290- throw error ( err )
291- }
292- }
293313 } else {
294314 /**
295315 * @description The create manifest call is used for creating a new app/manifest in your Contentstack organization.
@@ -321,7 +341,62 @@ export function App (http, data) {
321341 *
322342 */
323343 this . create = create ( { http, params : this . params } )
344+
345+ /**
346+ * @description The get all manifest call is used to fetch all the apps in your Contentstack organization.
347+ * @memberof App
348+ * @func findAll
349+ * @returns {Promise<ContentstackCollection<App>> }
350+ *
351+ * @example
352+ * import * as contentstack from '@contentstack/management'
353+ * const client = contentstack.client({ authtoken: 'TOKEN'})
354+ *
355+ * client.organization('organization_uid').app().fetchAll()
356+ * .then((collection) => console.log(collection))
357+ *
358+ */
359+ this . findAll = fetchAll ( http , AppCollection , this . params )
360+
361+ /**
362+ * @description To get the apps list of authorized apps for the particular organization
363+ * @memberof Organization
364+ * @func authorizedApps
365+ * @param {number } skip - Offset for skipping content in the response.
366+ * @param {number } limit - Limit on api response to provide content in list.
367+ * @example
368+ * import * as contentstack from '@contentstack/management'
369+ * const client = contentstack.client()
370+ *
371+ * client.organization('organization_uid').authorizedApps({ skip: 10 })
372+ * .then((roles) => console.log(roles))
373+ *
374+ */
375+ this . findAllAuthorized = async ( param = { } ) => {
376+ const headers = {
377+ headers : { ...cloneDeep ( this . params ) }
378+ }
379+
380+ headers . params = { ...param }
381+ try {
382+ const response = await http . get ( `/authorized-apps` , headers )
383+ if ( response . data ) {
384+ return response . data
385+ } else {
386+ return error ( response )
387+ }
388+ } catch ( err ) {
389+ return error ( err )
390+ }
391+ }
324392 }
325393 }
326394 return this
327395}
396+
397+ export function AppCollection ( http , data ) {
398+ const obj = cloneDeep ( data . data ) || [ ]
399+ return obj . map ( ( appData ) => {
400+ return new App ( http , { data : appData } )
401+ } )
402+ }
0 commit comments