Skip to content

Commit 1a96171

Browse files
committed
feat: 🚧 App installation Api implemented
1 parent 2f91d4d commit 1a96171

File tree

11 files changed

+448
-72
lines changed

11 files changed

+448
-72
lines changed

lib/app/index.js

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import cloneDeep from 'lodash/cloneDeep'
22
import error from '../core/contentstackError'
33
import { create, deleteEntity, fetch, fetchAll, update } from '../entity'
4+
import { Installation } from './installation'
45
export 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) {
212254
export 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
}

lib/app/installation/index.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import cloneDeep from 'lodash/cloneDeep'
2+
import { deleteEntity, fetch, fetchAll, update } from '../../entity'
3+
4+
export function Installation (http, data, params = {}) {
5+
this.params = params
6+
if (data.data) {
7+
Object.assign(this, cloneDeep(data.data))
8+
if (this.installation_uid) {
9+
this.uid = this.installation_uid
10+
}
11+
if (this.organization_uid) {
12+
this.params = {
13+
organization_uid: this.organization_uid
14+
}
15+
}
16+
this.urlPath = `/installations/${this.uid}`
17+
18+
/**
19+
* @description The GET installation call is used to retrieve a specific installation of an app
20+
* @memberof Installation
21+
* @func fetch
22+
* @returns {Promise<Installation>}
23+
*
24+
* @example
25+
* import * as contentstack from '@contentstack/management'
26+
* const client = contentstack.client({ authtoken: 'TOKEN'})
27+
*
28+
* client.organization('organization_uid').app('app_uid').installation('installation_uid').fetch()
29+
* .then((installation) => console.log(installation))
30+
*
31+
*/
32+
this.fetch = fetch(http, 'data', this.params)
33+
34+
/**
35+
* @description The Update installation call is used to update information of an app
36+
* @memberof Installation
37+
* @func update
38+
* @returns {Promise<Installation>}
39+
*
40+
* @example
41+
* import * as contentstack from '@contentstack/management'
42+
* const client = contentstack.client({ authtoken: 'TOKEN'})
43+
*
44+
* const updateInstallation = {
45+
* name: 'APP_NAME',
46+
* description: 'APP_DESCRIPTION',
47+
* target_type: 'stack'/'organization',
48+
* }
49+
*
50+
* const installation = client.organization('organization_uid').app('app_uid').installation('installation_uid')
51+
* installation = Object.assign(installation, updateInstallation)
52+
* installation.update()
53+
* .then((installation) => console.log(installation))
54+
*/
55+
this.update = update(http, 'data', this.params)
56+
57+
/**
58+
* @description The Uninstall installation call is used to uninstall the installation.
59+
* @memberof Installation
60+
* @func uninstall
61+
* @returns {Promise<Response>}
62+
*
63+
* @example
64+
* import * as contentstack from '@contentstack/management'
65+
* const client = contentstack.client({ authtoken: 'TOKEN'})
66+
*
67+
* client.organization('organization_uid').app('app_uid').installation('installation_uid').uninstall()
68+
* .then((app) => console.log(app))
69+
*/
70+
this.uninstall = deleteEntity(http, false, this.params)
71+
} else {
72+
if (data.app_uid) {
73+
this.urlPath = `apps/${data.app_uid}/installations`
74+
/**
75+
* @description The create an app call is used for creating a new app in your Contentstack organization.
76+
* @memberof Installation
77+
* @func create
78+
* @returns {Promise<ContentstackCollection<Installation>>}
79+
*
80+
* @example
81+
* import * as contentstack from '@contentstack/management'
82+
* const client = contentstack.client({ authtoken: 'TOKEN'})
83+
*
84+
* client.organization('organization_uid').app('app_uid').installation().fetchAll()
85+
* .then((app) => console.log(app))
86+
*
87+
*/
88+
this.findAll = fetchAll(http, InstallationCollection, this.params)
89+
}
90+
}
91+
return this
92+
}
93+
94+
export function InstallationCollection (http, data) {
95+
const obj = cloneDeep(data.data) || []
96+
return obj.map((installationData) => {
97+
return new Installation(http, { data: installationData })
98+
})
99+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/management",
3-
"version": "1.4.0",
3+
"version": "1.5.0",
44
"description": "The Content Management API is used to manage the content of your Contentstack account",
55
"main": "./dist/node/contentstack-management.js",
66
"browser": "./dist/web/contentstack-management.js",

test/api/app-test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dotenv.config()
99
// var stacks = {}
1010
var orgID = process.env.ORGANIZATION
1111
var client = {}
12-
var appUid = '***REMOVED***'
12+
var appUid = ''
1313
const app = {
1414
name: 'My New App',
1515
description: 'My new test app',
@@ -81,7 +81,6 @@ describe('Apps api Test', () => {
8181
it('Update OAuth app test', done => {
8282
client.organization(orgID).app(appUid).updateOAuth({ config })
8383
.then((appResponse) => {
84-
appUid = appResponse.uid
8584
expect(appResponse.redirect_uri).to.be.equal(config.redirect_uri)
8685
expect(appResponse.app_token_config.enabled).to.be.equal(config.app_token_config.enabled)
8786
expect(appResponse.user_token_config.enabled).to.be.equal(config.user_token_config.enabled)

test/test.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
// require('./api/user-test')
2-
// require('./api/organization-test')
3-
// require('./api/stack-test')
1+
require('./api/user-test')
2+
require('./api/organization-test')
3+
require('./api/stack-test')
44
require('./api/app-test')
5-
// require('./api/branch-test')
6-
// require('./api/branchAlias-test')
7-
// require('./api/locale-test')
8-
// require('./api/environment-test')
9-
// require('./api/deliveryToken-test')
10-
// require('./api/role-test')
11-
// require('./api/stack-share')
12-
// require('./api/contentType-test')
13-
// require('./api/asset-test')
14-
// require('./api/extension-test')
15-
// require('./api/entry-test')
16-
// require('./api/webhook-test')
17-
// require('./api/workflow-test')
18-
// require('./api/globalfield-test')
19-
// require('./api/release-test')
20-
// require('./api/label-test')
21-
// require('./api/contentType-delete-test')
22-
// require('./api/delete-test')
5+
require('./api/branch-test')
6+
require('./api/branchAlias-test')
7+
require('./api/locale-test')
8+
require('./api/environment-test')
9+
require('./api/deliveryToken-test')
10+
require('./api/role-test')
11+
require('./api/stack-share')
12+
require('./api/contentType-test')
13+
require('./api/asset-test')
14+
require('./api/extension-test')
15+
require('./api/entry-test')
16+
require('./api/webhook-test')
17+
require('./api/workflow-test')
18+
require('./api/globalfield-test')
19+
require('./api/release-test')
20+
require('./api/label-test')
21+
require('./api/contentType-delete-test')
22+
require('./api/delete-test')

test/unit/apps-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ describe('Contentstack apps test', () => {
1616
expect(app.delete).to.be.equal(undefined)
1717
expect(app.fetchOAuth).to.be.equal(undefined)
1818
expect(app.updateOAuth).to.be.equal(undefined)
19+
expect(app.install).to.be.equal(undefined)
20+
expect(app.installation).to.not.equal(undefined)
1921
done()
2022
})
2123

@@ -30,6 +32,8 @@ describe('Contentstack apps test', () => {
3032
expect(app.delete).to.not.equal(undefined)
3133
expect(app.fetchOAuth).to.not.equal(undefined)
3234
expect(app.updateOAuth).to.not.equal(undefined)
35+
expect(app.install).to.not.equal(undefined)
36+
expect(app.installation).to.not.equal(undefined)
3337
done()
3438
})
3539

test/unit/index.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
// require('./concurrency-Queue-test')
2-
// require('./ContentstackClient-test')
3-
// require('./ContentstackHTTPClient-test')
4-
// require('./Util-test')
5-
// require('./contentstack-test')
6-
// require('./contentstackError-test')
7-
// require('./contentstackCollection-test')
8-
// require('./user-test')
9-
// require('./organization-test')
10-
// require('./role-test')
11-
// require('./stack-test')
12-
// require('./asset-folder-test')
13-
// require('./extension-test')
14-
// require('./branch-test')
15-
// require('./branchAlias-test')
16-
// require('./release-test')
17-
// require('./asset-test')
18-
// require('./webhook-test')
19-
// require('./workflow-test')
20-
// require('./publishRules-test')
21-
// require('./contentType-test')
22-
// require('./globalField-test')
23-
// require('./query-test')
24-
// require('./label-test')
25-
// require('./environment-test')
26-
// require('./locale-test')
27-
// require('./deliveryToken-test')
28-
// require('./entry-test')
1+
require('./concurrency-Queue-test')
2+
require('./ContentstackClient-test')
3+
require('./ContentstackHTTPClient-test')
4+
require('./Util-test')
5+
require('./contentstack-test')
6+
require('./contentstackError-test')
7+
require('./contentstackCollection-test')
8+
require('./user-test')
9+
require('./organization-test')
10+
require('./role-test')
11+
require('./stack-test')
12+
require('./asset-folder-test')
13+
require('./extension-test')
14+
require('./branch-test')
15+
require('./branchAlias-test')
16+
require('./release-test')
17+
require('./asset-test')
18+
require('./webhook-test')
19+
require('./workflow-test')
20+
require('./publishRules-test')
21+
require('./contentType-test')
22+
require('./globalField-test')
23+
require('./query-test')
24+
require('./label-test')
25+
require('./environment-test')
26+
require('./locale-test')
27+
require('./deliveryToken-test')
28+
require('./entry-test')
2929
require('./apps-test')
30+
require('./installation-test')

0 commit comments

Comments
 (0)