Skip to content

Commit 0080a3e

Browse files
committed
feat: ♻️ refactored manifest api functions in marketplace app [cs-38336]
1 parent 4b1abb7 commit 0080a3e

File tree

10 files changed

+257
-103
lines changed

10 files changed

+257
-103
lines changed

README.md

Lines changed: 1 addition & 1 deletion
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-2022 [Contentstack](https://www.contentstack.com/). All Rights Reserved
107+
Copyright © 2012-2023 [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

Lines changed: 35 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { create, deleteEntity, fetch, fetchAll, update } from '../entity'
44
import { Authorization } from './authorization'
55
import { Hosting } from './hosting'
66
import { Installation } from './installation'
7+
import { Oauth } from './oauth'
78

89
export function App (http, data) {
910
http.defaults.versioningStrategy = undefined
@@ -78,85 +79,25 @@ export function App (http, data) {
7879
this.delete = deleteEntity(http, false, this.params)
7980

8081
/**
81-
* @description The get oauth call is used to fetch the OAuth details of the app.
82+
* @description Oauth will allow to get, update auth and get scopes.
8283
* @memberof App
83-
* @func fetchOAuth
84+
* @func oauth
8485
* @returns {Promise<AppOAuth>}
86+
* @returns {Oauth}
8587
*
8688
* @example
8789
* import * as contentstack from '@contentstack/management'
8890
* const client = contentstack.client({ authtoken: 'TOKEN'})
89-
*
90-
* client.organization('organization_uid').app('manifest_uid').fetchOAuth()
91-
* .then((oAuthConfig) => console.log(oAuthConfig))
91+
* client.organization('organization_uid').app('manifest_uid').oauth()
9292
*/
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-
}
93+
this.oauth = () => {
94+
return new Oauth(http, { app_uid: this.uid, organization_uid: this.organization_uid }, this.params)
15495
}
15596

15697
/**
15798
* @description The hosting will allow you get, update, deploy manifest.
15899
* @memberof App
159-
* @func updateOAuth
100+
* @func hosting
160101
* @returns {Promise<AppOAuth>}
161102
* @returns {Hosting}
162103
*
@@ -310,6 +251,25 @@ export function App (http, data) {
310251
this.authorization = () => {
311252
return new Authorization(http, { app_uid: this.uid }, this.params)
312253
}
254+
255+
/**
256+
* @description The list installation call is used to retrieve all installations of your Contentstack organization.
257+
* @memberof App
258+
* @func listInstallations
259+
* @returns {Promise<ContentstackCollection<Installation>>}
260+
*
261+
* @example
262+
* import * as contentstack from '@contentstack/management'
263+
* const client = contentstack.client({ authtoken: 'TOKEN'})
264+
*
265+
* client.organization('organization_uid').app('app_uid').listInstallations()
266+
* .then((collection) => console.log(collection))
267+
*
268+
*/
269+
this.listInstallations = () => {
270+
this.urlPath = `manifests/${data.app_uid}/installations`
271+
return fetchAll(http, InstallationCollection, this.params)
272+
}
313273
} else {
314274
/**
315275
* @description The create manifest call is used for creating a new app/manifest in your Contentstack organization.
@@ -352,7 +312,7 @@ export function App (http, data) {
352312
* import * as contentstack from '@contentstack/management'
353313
* const client = contentstack.client({ authtoken: 'TOKEN'})
354314
*
355-
* client.organization('organization_uid').app().fetchAll()
315+
* client.organization('organization_uid').app().findAll()
356316
* .then((collection) => console.log(collection))
357317
*
358318
*/
@@ -400,3 +360,10 @@ export function AppCollection (http, data) {
400360
return new App(http, { data: appData })
401361
})
402362
}
363+
364+
function InstallationCollection (http, data) {
365+
const obj = cloneDeep(data.data) || []
366+
return obj.map((installationData) => {
367+
return new Installation(http, { data: installationData })
368+
})
369+
}

lib/app/installation/index.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -222,32 +222,6 @@ export function Installation (http, data, params = {}) {
222222
throw error(err)
223223
}
224224
}
225-
} else {
226-
if (data.app_uid) {
227-
this.urlPath = `manifests/${data.app_uid}/installations`
228-
/**
229-
* @description The find installation call is used to retrieve all installations of your Contentstack organization.
230-
* @memberof Installation
231-
* @func findAll
232-
* @returns {Promise<ContentstackCollection<Installation>>}
233-
*
234-
* @example
235-
* import * as contentstack from '@contentstack/management'
236-
* const client = contentstack.client({ authtoken: 'TOKEN'})
237-
*
238-
* client.organization('organization_uid').app('app_uid').installation().fetchAll()
239-
* .then((collection) => console.log(collection))
240-
*
241-
*/
242-
this.findAll = fetchAll(http, InstallationCollection, this.params)
243-
}
244225
}
245226
return this
246227
}
247-
248-
export function InstallationCollection (http, data) {
249-
const obj = cloneDeep(data.data) || []
250-
return obj.map((installationData) => {
251-
return new Installation(http, { data: installationData })
252-
})
253-
}

lib/app/oauth/index.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import error from '../../core/contentstackError'
2+
import cloneDeep from 'lodash/cloneDeep'
3+
export function Oauth (http, data, params) {
4+
http.defaults.versioningStrategy = undefined
5+
this.params = params || {}
6+
if (data) {
7+
if (data.organization_uid) {
8+
this.params = {
9+
organization_uid: data.organization_uid
10+
}
11+
}
12+
if (data.app_uid) {
13+
this.urlPath = `/manifests/${data.app_uid}/oauth`
14+
/**
15+
* @description The get oauth call is used to fetch the OAuth details of the app.
16+
* @memberof Oauth
17+
* @func fetch
18+
* @returns {Promise<AppOAuth>}
19+
*
20+
* @example
21+
* import * as contentstack from '@contentstack/management'
22+
* const client = contentstack.client({ authtoken: 'TOKEN'})
23+
*
24+
* client.organization('organization_uid').app('manifest_uid').oauth().fetch()
25+
* .then((oAuthConfig) => console.log(oAuthConfig))
26+
*/
27+
this.fetch = async (param = {}) => {
28+
try {
29+
const headers = {
30+
headers: { ...cloneDeep(this.params) },
31+
params: {
32+
...cloneDeep(param)
33+
}
34+
} || {}
35+
36+
const response = await http.get(this.urlPath, headers)
37+
if (response.data) {
38+
return response.data.data || {}
39+
} else {
40+
throw error(response)
41+
}
42+
} catch (err) {
43+
throw error(err)
44+
}
45+
}
46+
47+
/**
48+
* @description The change oauth details call is used to update the OAuth details, (redirect url and permission scope) of an app.
49+
* @memberof Oauth
50+
* @func update
51+
* @returns {Promise<AppOAuth>}
52+
*
53+
* @example
54+
* import * as contentstack from '@contentstack/management'
55+
* const client = contentstack.client({ authtoken: 'TOKEN'})
56+
* const config = {
57+
* redirect_uri: 'REDIRECT_URI',
58+
* app_token_config: {
59+
* enabled: true,
60+
* scopes: ['scope1', 'scope2']
61+
* },
62+
* user_token_config: {
63+
* enabled: true,
64+
* scopes: ['scope1', 'scope2']
65+
* }
66+
* }
67+
* client.organization('organization_uid').app('manifest_uid').oauth().update({ config })
68+
* .then((oAuthConfig) => console.log(oAuthConfig))
69+
*/
70+
this.update = async ({ config, param = {} }) => {
71+
try {
72+
const headers = {
73+
headers: { ...cloneDeep(this.params) },
74+
params: {
75+
...cloneDeep(param)
76+
}
77+
} || {}
78+
79+
const response = await http.put(this.urlPath, config, headers)
80+
if (response.data) {
81+
return response.data.data || {}
82+
} else {
83+
throw error(response)
84+
}
85+
} catch (err) {
86+
throw error(err)
87+
}
88+
}
89+
90+
/**
91+
* @description The change oauth details call is used to update the OAuth details, (redirect url and permission scope) of an app.
92+
* @memberof Oauth
93+
* @func getScopes
94+
* @returns {Promise<AppOAuth>}
95+
*
96+
* @example
97+
* import * as contentstack from '@contentstack/management'
98+
* const client = contentstack.client({ authtoken: 'TOKEN'})
99+
*
100+
* client.organization('organization_uid').app('manifest_uid').oauth().getScopes()
101+
* .then((scopes) => console.log(scopes))
102+
*/
103+
this.getScopes = async () => {
104+
try {
105+
const headers = {
106+
headers: { ...cloneDeep(this.params) }
107+
} || {}
108+
109+
const response = await http.get('/manifests/oauth/scopes', headers)
110+
if (response.data) {
111+
return response.data.data || {}
112+
} else {
113+
throw error(response)
114+
}
115+
} catch (err) {
116+
throw error(err)
117+
}
118+
}
119+
}
120+
}
121+
}

test/unit/apps-test.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ describe('Contentstack apps test', () => {
1616
expect(app.fetch).to.be.equal(undefined)
1717
expect(app.update).to.be.equal(undefined)
1818
expect(app.delete).to.be.equal(undefined)
19-
expect(app.fetchOAuth).to.be.equal(undefined)
20-
expect(app.updateOAuth).to.be.equal(undefined)
2119
expect(app.hosting).to.be.equal(undefined)
2220
expect(app.install).to.be.equal(undefined)
2321
expect(app.installation).to.be.equal(undefined)
2422
expect(app.getRequests).to.be.equal(undefined)
2523
expect(app.authorize).to.be.equal(undefined)
24+
expect(app.oath()).to.be.equal(undefined)
2625
done()
2726
})
2827

@@ -36,8 +35,6 @@ describe('Contentstack apps test', () => {
3635
expect(app.fetch).to.not.equal(undefined)
3736
expect(app.update).to.not.equal(undefined)
3837
expect(app.delete).to.not.equal(undefined)
39-
expect(app.fetchOAuth).to.not.equal(undefined)
40-
expect(app.updateOAuth).to.not.equal(undefined)
4138
expect(app.hosting).to.not.equal(undefined)
4239
expect(app.install).to.not.equal(undefined)
4340
expect(app.installation).to.not.equal(undefined)
@@ -47,6 +44,7 @@ describe('Contentstack apps test', () => {
4744
expect(app.installation()).to.not.equal(undefined)
4845
expect(app.installation(uid)).to.not.equal(undefined)
4946
expect(app.authorization()).to.not.equal(undefined)
47+
expect(app.oauth()).to.not.equal(undefined)
5048
done()
5149
})
5250

@@ -61,8 +59,6 @@ describe('Contentstack apps test', () => {
6159
expect(app.fetch).to.not.equal(undefined)
6260
expect(app.update).to.not.equal(undefined)
6361
expect(app.delete).to.not.equal(undefined)
64-
expect(app.fetchOAuth).to.not.equal(undefined)
65-
expect(app.updateOAuth).to.not.equal(undefined)
6662
expect(app.hosting).to.not.equal(undefined)
6763
expect(app.install).to.not.equal(undefined)
6864
expect(app.installation).to.not.equal(undefined)

test/unit/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ require('./hosting-test')
3232
require('./deployment-test')
3333
require('./app-request-test')
3434
require('./authorization-test')
35+
require('./oauth-test')

test/unit/mock/objects.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,14 @@ const oAuthMock = {
535535
}
536536
}
537537

538+
const oAuthScopesMock = {
539+
scopes: [
540+
'user:read',
541+
'user:write',
542+
'user.profile:read'
543+
]
544+
}
545+
538546
const appInstallMock = {
539547
status: 'installed',
540548
installation_uid: 'installationUID',
@@ -694,6 +702,7 @@ function checkSystemFields (object) {
694702
export {
695703
appMock,
696704
oAuthMock,
705+
oAuthScopesMock,
697706
appInstallMock,
698707
installationMock,
699708
errorMock,

0 commit comments

Comments
 (0)