Skip to content

Commit 0c63a63

Browse files
authored
Merge pull request #27 from contentstack/feature/marketplace-app
Feature Developer Hub
2 parents 7294a64 + 545991c commit 0c63a63

23 files changed

+1406
-46
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Contentstack
3+
Copyright (c) 2012-2022 Contentstack
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ contentstackClient.stack({ api_key: 'API_KEY' }).asset().create({ asset })
104104
- [Content Management API Docs](https://www.contentstack.com/docs/developers/apis/content-management-api)
105105

106106
### The MIT License (MIT)
107-
Copyright © 2012-2021 [Contentstack](https://www.contentstack.com/). All Rights Reserved
107+
Copyright © 2012-2022 [Contentstack](https://www.contentstack.com/). All Rights Reserved
108108

109109
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
110110

lib/app/index.js

+263
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
import cloneDeep from 'lodash/cloneDeep'
2+
import error from '../core/contentstackError'
3+
import { create, deleteEntity, fetch, fetchAll, update } from '../entity'
4+
import { Installation } from './installation'
5+
export function App (http, data) {
6+
http.defaults.versioningStrategy = undefined
7+
this.urlPath = '/apps'
8+
this.params = {}
9+
if (data) {
10+
if (data.organization_uid) {
11+
this.params = {
12+
organization_uid: data.organization_uid
13+
}
14+
}
15+
if (data.data) {
16+
Object.assign(this, cloneDeep(data.data))
17+
if (this.organization_uid) {
18+
this.params = {
19+
organization_uid: this.organization_uid
20+
}
21+
}
22+
this.urlPath = `/apps/${this.uid}`
23+
24+
/**
25+
* @description The update an app call is used to update the app details such as name, description, icon, and so on.
26+
* @memberof App
27+
* @func update
28+
* @returns {Promise<App>}
29+
*
30+
* @example
31+
* import * as contentstack from '@contentstack/management'
32+
* const client = contentstack.client({ authtoken: 'TOKEN'})
33+
* const updateApp = {
34+
* name: 'APP_NAME',
35+
* description: 'APP_DESCRIPTION',
36+
* target_type: 'stack'/'organization',
37+
* }
38+
* const app = client.organization('organization_uid').app('app_uid')
39+
* app = Object.assign(app, updateApp)
40+
* app.update()
41+
* .then((app) => console.log(app))
42+
*
43+
*/
44+
this.update = update(http, undefined, this.params)
45+
46+
/**
47+
* @description The get app details call is used to fetch details of a particular app with its ID.
48+
* @memberof App
49+
* @func fetch
50+
* @returns {Promise<App>}
51+
*
52+
* @example
53+
* import * as contentstack from '@contentstack/management'
54+
* const client = contentstack.client({ authtoken: 'TOKEN'})
55+
*
56+
* client.organization('organization_uid').app('app_uid').fetch()
57+
* .then((app) => console.log(app))
58+
*
59+
*/
60+
this.fetch = fetch(http, 'data', this.params)
61+
62+
/**
63+
* @description The delete an app call is used to delete the app.
64+
* @memberof App
65+
* @func delete
66+
* @returns {Promise<Response>}
67+
*
68+
* @example
69+
* import * as contentstack from '@contentstack/management'
70+
* const client = contentstack.client({ authtoken: 'TOKEN'})
71+
*
72+
* client.organization('organization_uid').app('app_uid').delete()
73+
* .then((response) => console.log(response))
74+
*/
75+
this.delete = deleteEntity(http, false, this.params)
76+
77+
/**
78+
* @description The get oauth call is used to fetch the OAuth details of the app.
79+
* @memberof App
80+
* @func fetchOAuth
81+
* @returns {Promise<AppOAuth>}
82+
*
83+
* @example
84+
* import * as contentstack from '@contentstack/management'
85+
* const client = contentstack.client({ authtoken: 'TOKEN'})
86+
*
87+
* client.organization('organization_uid').app('app_uid').fetchOAuth()
88+
* .then((oAuthConfig) => console.log(oAuthConfig))
89+
*/
90+
this.fetchOAuth = async (param = {}) => {
91+
try {
92+
const headers = {
93+
headers: { ...cloneDeep(this.params) },
94+
params: {
95+
...cloneDeep(param)
96+
}
97+
} || {}
98+
99+
const response = await http.get(`${this.urlPath}/oauth`, headers)
100+
if (response.data) {
101+
return response.data.data || {}
102+
} else {
103+
throw error(response)
104+
}
105+
} catch (err) {
106+
throw error(err)
107+
}
108+
}
109+
110+
/**
111+
* @description The change oauth details call is used to update the OAuth details, (redirect url and permission scope) of an app.
112+
* @memberof App
113+
* @func updateOAuth
114+
* @returns {Promise<AppOAuth>}
115+
*
116+
* @example
117+
* import * as contentstack from '@contentstack/management'
118+
* const client = contentstack.client({ authtoken: 'TOKEN'})
119+
* const config = {
120+
* redirect_uri: 'REDIRECT_URI',
121+
* app_token_config: {
122+
* enabled: true,
123+
* scopes: ['scope1', 'scope2']
124+
* },
125+
* user_token_config: {
126+
* enabled: true,
127+
* scopes: ['scope1', 'scope2']
128+
* }
129+
* }
130+
* client.organization('organization_uid').app('app_uid').updateOAuth({ config })
131+
* .then((oAuthConfig) => console.log(oAuthConfig))
132+
*/
133+
this.updateOAuth = async ({ config, param = {} }) => {
134+
try {
135+
const headers = {
136+
headers: { ...cloneDeep(this.params) },
137+
params: {
138+
...cloneDeep(param)
139+
}
140+
} || {}
141+
142+
const response = await http.put(`${this.urlPath}/oauth`, config, headers)
143+
if (response.data) {
144+
return response.data.data || {}
145+
} else {
146+
throw error(response)
147+
}
148+
} catch (err) {
149+
throw error(err)
150+
}
151+
}
152+
153+
/**
154+
* @description The install call is used to initiate the installation of the app
155+
* @memberof App
156+
* @func install
157+
* @param {String} param.targetType - The target on which app needs to be installed, stack or ogranization.
158+
* @param {String} param.targetUid - The uid of the target, on which the app will be installed
159+
* @returns Promise<Installation>
160+
*
161+
* @example
162+
* import * as contentstack from '@contentstack/management'
163+
* const client = contentstack.client({ authtoken: 'TOKEN'})
164+
* client.organization('organization_uid').app('app_uid').install({ targetUid: 'STACK_API_KEY', targetType: 'stack' })
165+
* .then((installation) => console.log(installation))
166+
*/
167+
this.install = async ({ targetUid, targetType }) => {
168+
try {
169+
const headers = {
170+
headers: { ...cloneDeep(this.params) }
171+
} || {}
172+
173+
const response = await http.post(`${this.urlPath}/install`, { target_type: targetType, target_uid: targetUid }, headers)
174+
if (response.data) {
175+
return new Installation(http, response.data, this.params) || {}
176+
} else {
177+
throw error(response)
178+
}
179+
} catch (err) {
180+
throw error(err)
181+
}
182+
}
183+
184+
/**
185+
* @description The Installation will allow you to fetch, update and delete of the app installation.
186+
* @memberof App
187+
* @func installation
188+
* @param {String} uid Installation uid
189+
* @returns Installation
190+
*
191+
* @example
192+
* import * as contentstack from '@contentstack/management'
193+
* const client = contentstack.client({ authtoken: 'TOKEN'})
194+
* client.organization('organization_uid').app('app_uid').installation().findAll()
195+
* .then((installations) => console.log(installations))
196+
*
197+
* @example
198+
* import * as contentstack from '@contentstack/management'
199+
* const client = contentstack.client({ authtoken: 'TOKEN'})
200+
* client.organization('organization_uid').app('app_uid').installation('installation_uid').fetch()
201+
* .then((installation) => console.log(installation))
202+
*/
203+
this.installation = (uid = null) => {
204+
return new Installation(http, uid ? { data: { uid } } : { app_uid: this.uid }, this.params)
205+
}
206+
} else {
207+
/**
208+
* @description The create an app call is used for creating a new app in your Contentstack organization.
209+
* @memberof App
210+
* @func create
211+
* @returns {Promise<App>}
212+
*
213+
* @example
214+
* import * as contentstack from '@contentstack/management'
215+
* const client = contentstack.client({ authtoken: 'TOKEN'})
216+
* const app = {
217+
* name: 'APP_NAME',
218+
* description: 'APP_DESCRIPTION',
219+
* target_type: 'stack'/'organization',
220+
* webhook: // optional
221+
* {
222+
* target_url: 'TARGET_URL',
223+
* channel: 'CHANNEL'
224+
* },
225+
* oauth: // optional
226+
* {
227+
* redirect_uri: 'REDIRECT_URI',
228+
* enabled: true,
229+
* }
230+
* }
231+
*
232+
* client.organization('organization_uid').app().create(app)
233+
* .then((app) => console.log(app))
234+
*
235+
*/
236+
this.create = create({ http, params: this.params })
237+
238+
/**
239+
* @description The get all apps call is used to fetch all the apps in your Contentstack organization.
240+
* @memberof App
241+
* @func findAll
242+
* @returns {Promise<ContentstackCollection<App>>}
243+
*
244+
* @example
245+
* import * as contentstack from '@contentstack/management'
246+
* const client = contentstack.client({ authtoken: 'TOKEN'})
247+
*
248+
* client.organization('organization_uid').app().fetchAll()
249+
* .then((collection) => console.log(collection))
250+
*
251+
*/
252+
this.findAll = fetchAll(http, AppCollection, this.params)
253+
}
254+
}
255+
return this
256+
}
257+
258+
export function AppCollection (http, data) {
259+
const obj = cloneDeep(data.data) || []
260+
return obj.map((appData) => {
261+
return new App(http, { data: appData })
262+
})
263+
}

0 commit comments

Comments
 (0)