Skip to content

Commit 42a59ed

Browse files
committed
feat: 🚧 marketplace app request api implementation [CS-35318]
1 parent a15570d commit 42a59ed

File tree

10 files changed

+449
-1
lines changed

10 files changed

+449
-1
lines changed

lib/app/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import error from '../core/contentstackError'
33
import { create, deleteEntity, fetch, fetchAll, update } from '../entity'
44
import { Hosting } from './hosting'
55
import { Installation } from './installation'
6+
import { Request } from './request'
67

78
export function App (http, data) {
89
http.defaults.versioningStrategy = undefined
@@ -269,6 +270,22 @@ export function App (http, data) {
269270
*/
270271
this.findAll = fetchAll(http, AppCollection, this.params)
271272
}
273+
274+
/**
275+
* @description The Create installation call is used to create a app request for an app.
276+
* @returns Request
277+
*
278+
* @example
279+
* import * as contentstack from '@contentstack/management'
280+
* const client = contentstack.client({ authtoken: 'TOKEN'})
281+
*
282+
* client.organization('organization_uid').app('app_uid').request().create('target_uid')
283+
* .then((response) => console.log(response))
284+
*
285+
*/
286+
this.request = () => {
287+
return new Request(http, this.uid ? { app_uid: this.uid, organization_uid: this.organization_uid } : { organization_uid: this.organization_uid }, this.params)
288+
}
272289
}
273290
return this
274291
}

lib/app/request/index.js

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import cloneDeep from 'lodash/cloneDeep'
2+
import error from '../../core/contentstackError'
3+
4+
export function Request (http, data, param) {
5+
http.defaults.versioningStrategy = undefined
6+
this.urlPath = '/manifests'
7+
this.params = param || {}
8+
9+
if (data) {
10+
if (data.organization_uid) {
11+
this.params = {
12+
organization_uid: data.organization_uid
13+
}
14+
}
15+
/**
16+
* @description The Delete app request call is used to delete an app request of an app in target_uid.
17+
* @param {string} requestUID The ID of the request to be deleted
18+
* @returns Promise<Response>
19+
* @memberof Request
20+
* @func delete
21+
*
22+
* @example
23+
* import * as contentstack from '@contentstack/management'
24+
* const client = contentstack.client({ authtoken: 'TOKEN'})
25+
*
26+
* client.organization('organization_uid').app().request().delete('request_uid`)
27+
* .then((response) => console.log(response))
28+
*
29+
*/
30+
this.delete = async (requestUid) => {
31+
try {
32+
const headers = {
33+
headers: { ...cloneDeep(this.params) }
34+
}
35+
36+
const response = await http.delete(`/requests/${requestUid}`, headers)
37+
if (response.data) {
38+
return response.data
39+
} else {
40+
throw error(response)
41+
}
42+
} catch (err) {
43+
throw error(err)
44+
}
45+
}
46+
if (data.app_uid) {
47+
/**
48+
* @description The Create installation call is used to create a app request for an app.
49+
* @param {string} targetUid The uid of the target, on which the app will be installed
50+
* @returns Promise<Response>
51+
* @memberof Request
52+
* @func create
53+
*
54+
* @example
55+
* import * as contentstack from '@contentstack/management'
56+
* const client = contentstack.client({ authtoken: 'TOKEN'})
57+
*
58+
* client.organization('organization_uid').app('app_uid').request().create('target_uid')
59+
* .then((response) => console.log(response))
60+
*
61+
*/
62+
this.create = async (targetUid) => {
63+
try {
64+
const headers = {
65+
headers: { ...cloneDeep(this.params) }
66+
}
67+
68+
const response = await http.post(`/requests`, { app_uid: data.app_uid, target_uid: targetUid }, headers)
69+
if (response.data) {
70+
return response.data
71+
} else {
72+
throw error(response)
73+
}
74+
} catch (err) {
75+
throw error(err)
76+
}
77+
}
78+
79+
/**
80+
* @description The GET app requests of an app call is used to retrieve all requests of an app.
81+
* @returns Promise<Response>
82+
* @memberof Request
83+
* @func fetch
84+
*
85+
* @example
86+
* import * as contentstack from '@contentstack/management'
87+
* const client = contentstack.client({ authtoken: 'TOKEN'})
88+
*
89+
* client.organization('organization_uid').app('app_uid').request().fetch()
90+
* .then((response) => console.log(response))
91+
*
92+
*/
93+
this.fetch = async () => {
94+
try {
95+
const headers = {
96+
headers: { ...cloneDeep(this.params) }
97+
}
98+
99+
const response = await http.get(`/manifests/${data.app_uid}/requests`, headers)
100+
if (response.data) {
101+
return response.data
102+
} else {
103+
throw error(response)
104+
}
105+
} catch (err) {
106+
throw error(err)
107+
}
108+
}
109+
} else {
110+
/**
111+
* @description The GET all app requests call is used to retrieve all requests of all apps in an organization.
112+
* @param {object} param object for query params
113+
* @returns Promise<Response>
114+
* @memberof Request
115+
* @func findAll
116+
*
117+
* @example
118+
* import * as contentstack from '@contentstack/management'
119+
* const client = contentstack.client({ authtoken: 'TOKEN'})
120+
*
121+
* client.organization('organization_uid').app('app_uid').request().findAll()
122+
* .then((response) => console.log(response))
123+
*
124+
*/
125+
this.findAll = async (param = {}) => {
126+
try {
127+
const headers = {
128+
headers: { ...cloneDeep(this.params) },
129+
params: { ...param }
130+
}
131+
132+
const response = await http.get(`/requests`, headers)
133+
if (response.data) {
134+
return response.data
135+
} else {
136+
throw error(response)
137+
}
138+
} catch (err) {
139+
throw error(err)
140+
}
141+
}
142+
}
143+
}
144+
}

test/api/app-request-test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import dotenv from 'dotenv'
2+
import { describe, it, setup } from 'mocha'
3+
import { jsonReader } from '../utility/fileOperations/readwrite'
4+
import { contentstackClient } from '../utility/ContentstackClient.js'
5+
import { expect } from 'chai'
6+
7+
dotenv.config()
8+
9+
let apps = {}
10+
const orgID = process.env.ORGANIZATION
11+
let client = {}
12+
let stack = {}
13+
let requestUID = ''
14+
describe('Apps request api Test', () => {
15+
setup(() => {
16+
const user = jsonReader('loggedinuser.json')
17+
client = contentstackClient(user.authtoken)
18+
apps = jsonReader('apps.json')
19+
stack = jsonReader('stack.json')
20+
})
21+
22+
it('test create app request', done => {
23+
client.organization(orgID).app(apps.uid).request()
24+
.create(stack.api_key)
25+
.then((response) => {
26+
requestUID = response.data.data.uid
27+
expect(response.data).to.not.equal(undefined)
28+
done()
29+
})
30+
.catch(done)
31+
})
32+
33+
it('test fetch app request', done => {
34+
client.organization(orgID).app(apps.uid).request()
35+
.fetch()
36+
.then((response) => {
37+
expect(response.data).to.not.equal(undefined)
38+
done()
39+
})
40+
.catch(done)
41+
})
42+
43+
it('test get all request for oranization', done => {
44+
client.organization(orgID).app().request()
45+
.findAll()
46+
.then((response) => {
47+
expect(response.data).to.not.equal(undefined)
48+
done()
49+
})
50+
.catch(done)
51+
})
52+
53+
it('test delete app request', done => {
54+
client.organization(orgID).app().request()
55+
.delete(requestUID)
56+
.then((response) => {
57+
expect(response.data).to.not.equal(undefined)
58+
done()
59+
})
60+
.catch(done)
61+
})
62+
})

test/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ require('./api/stack-test')
44
require('./api/app-test')
55
require('./api/hosting-test')
66
require('./api/app-delete-test')
7+
require('./api/app-request-test')
78
require('./api/branch-test')
89
require('./api/branchAlias-test')
910
require('./api/locale-test')

test/unit/ContentstackHTTPClient-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe('Contentstack HTTP Client', () => {
103103
defaultHostName: 'defaulthost'
104104
})
105105
expect(axiosInstance.defaults.paramsSerializer({ skip: 1, limit: 1 })).to.be.equal('skip=1&limit=1')
106-
expect(axiosInstance.defaults.paramsSerializer({ query: { title: 'title' }, limit: 1 })).to.be.equal('limit=1&query=%7B%22title%22:%22title%22%7D')
106+
expect(axiosInstance.defaults.paramsSerializer({ query: { title: 'title' }, limit: 1 })).to.be.equal('limit=1&query=%7B%22title%22%3A%22title%22%7D')
107107
} catch (err) {
108108
expect(err.message).to.be.equal('Expected parameter accessToken')
109109
}

0 commit comments

Comments
 (0)