Skip to content

Commit 8e3b86c

Browse files
committed
fix: 🚧 app request with organization added
1 parent 31fb2b3 commit 8e3b86c

File tree

13 files changed

+228
-227
lines changed

13 files changed

+228
-227
lines changed

lib/app/index.js

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,36 @@ export function App (http, data) {
222222
this.installation = (uid = null) => {
223223
return new Installation(http, uid ? { data: { uid } } : { app_uid: this.uid }, this.params)
224224
}
225+
/**
226+
* @description The GET app requests of an app call is used to retrieve all requests of an app.
227+
* @returns Promise<Response>
228+
* @memberof App
229+
* @func getRequests
230+
*
231+
* @example
232+
* import * as contentstack from '@contentstack/management'
233+
* const client = contentstack.client({ authtoken: 'TOKEN'})
234+
*
235+
* client.organization('organization_uid').app('app_uid').getRequests()
236+
* .then((response) => console.log(response))
237+
*
238+
*/
239+
this.getRequests = async () => {
240+
try {
241+
const headers = {
242+
headers: { ...cloneDeep(this.params) }
243+
}
244+
245+
const response = await http.get(`${this.urlPath}/requests`, headers)
246+
if (response.data) {
247+
return response.data
248+
} else {
249+
throw error(response)
250+
}
251+
} catch (err) {
252+
throw error(err)
253+
}
254+
}
225255
} else {
226256
/**
227257
* @description The create manifest call is used for creating a new app/manifest in your Contentstack organization.
@@ -270,22 +300,6 @@ export function App (http, data) {
270300
*/
271301
this.findAll = fetchAll(http, AppCollection, this.params)
272302
}
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-
}
289303
}
290304
return this
291305
}

lib/app/request/index.js

Lines changed: 71 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import cloneDeep from 'lodash/cloneDeep'
22
import error from '../../core/contentstackError'
33

4-
export function Request (http, data, param) {
4+
export function AppRequest (http, data, param) {
55
http.defaults.versioningStrategy = undefined
6-
this.urlPath = '/manifests'
76
this.params = param || {}
87

98
if (data) {
@@ -13,20 +12,20 @@ export function Request (http, data, param) {
1312
}
1413
}
1514
/**
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-
*/
15+
* @description The Delete app request call is used to delete an app request of an app in target_uid.
16+
* @param {string} requestUID The ID of the request to be deleted
17+
* @returns Promise<Response>
18+
* @memberof AppRequest
19+
* @func delete
20+
*
21+
* @example
22+
* import * as contentstack from '@contentstack/management'
23+
* const client = contentstack.client({ authtoken: 'TOKEN'})
24+
*
25+
* client.organization('organization_uid').request().delete('request_uid`)
26+
* .then((response) => console.log(response))
27+
*
28+
*/
3029
this.delete = async (requestUid) => {
3130
try {
3231
const headers = {
@@ -43,101 +42,68 @@ export function Request (http, data, param) {
4342
throw error(err)
4443
}
4544
}
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)
45+
/**
46+
* @description The Create call is used to create a app request for an app.
47+
* @param {string} appUid The uid for the app for request
48+
* @param {string} targetUid The uid of the target, on which the app will be installed
49+
* @returns Promise<Response>
50+
* @memberof AppRequest
51+
* @func create
52+
*
53+
* @example
54+
* import * as contentstack from '@contentstack/management'
55+
* const client = contentstack.client({ authtoken: 'TOKEN'})
56+
*
57+
* client.organization('organization_uid').request().create({ appUid: 'app_uid', targetUid: 'target_uid' })
58+
* .then((response) => console.log(response))
59+
*
60+
*/
61+
this.create = async ({ appUid, targetUid }) => {
62+
try {
63+
const headers = {
64+
headers: { ...cloneDeep(this.params) }
7665
}
77-
}
7866

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)
67+
const response = await http.post(`/requests`, { app_uid: appUid, target_uid: targetUid }, headers)
68+
if (response.data) {
69+
return response.data
70+
} else {
71+
throw error(response)
10772
}
73+
} catch (err) {
74+
throw error(err)
10875
}
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-
}
76+
}
77+
/**
78+
* @description The GET all app requests call is used to retrieve all requests of all apps in an organization.
79+
* @param {object} param object for query params
80+
* @returns Promise<Response>
81+
* @memberof AppRequest
82+
* @func findAll
83+
*
84+
* @example
85+
* import * as contentstack from '@contentstack/management'
86+
* const client = contentstack.client({ authtoken: 'TOKEN'})
87+
*
88+
* client.organization('organization_uid').request().findAll()
89+
* .then((response) => console.log(response))
90+
*
91+
*/
92+
this.findAll = async (param = {}) => {
93+
try {
94+
const headers = {
95+
headers: { ...cloneDeep(this.params) },
96+
params: { ...param }
97+
}
13198

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)
99+
const response = await http.get(`/requests`, headers)
100+
if (response.data) {
101+
return response.data
102+
} else {
103+
throw error(response)
140104
}
105+
} catch (err) {
106+
throw error(err)
141107
}
142108
}
143109
}

lib/organization/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { RoleCollection } from '../stack/roles'
66
import { StackCollection } from '../stack'
77
import { UserCollection } from '../user'
88
import { App } from '../app'
9+
import { AppRequest } from '../app/request'
910
/**
1011
* Organization is the top-level entity in the hierarchy of Contentstack, consisting of stacks and stack resources, and users. Organization allows easy management of projects as well as users within the Organization. Read more about <a href='https://www.contentstack.com/docs/guide/organization'>Organizations.</a>.
1112
* @namespace Organization
@@ -218,6 +219,21 @@ export function Organization (http, data) {
218219
this.app = (uid = null) => {
219220
return new App(http, uid !== null ? { data: { uid, organization_uid: this.uid } } : { organization_uid: this.uid })
220221
}
222+
223+
/**
224+
* @description The Create request call is used to create a app request for an app.
225+
* @returns Request
226+
*
227+
* @example
228+
* import * as contentstack from '@contentstack/management'
229+
* const client = contentstack.client({ authtoken: 'TOKEN'})
230+
*
231+
* client.organization('organization_uid').appRequest()
232+
*
233+
*/
234+
this.appRequest = () => {
235+
return new AppRequest(http, { organization_uid: this.organization_uid }, this.params)
236+
}
221237
} else {
222238
/**
223239
* @description The Get all organizations call lists all organizations related to the system user in the order that they were created.

test/api/app-request-test.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ describe('Apps request api Test', () => {
2020
})
2121

2222
it('test create app request', done => {
23-
client.organization(orgID).app(apps.uid).request()
24-
.create(stack.api_key)
23+
client.organization(orgID).request()
24+
.create({ appUid: apps.uid, targetUid: stack.api_key })
2525
.then((response) => {
2626
requestUID = response.data.data.uid
2727
expect(response.data).to.not.equal(undefined)
@@ -30,18 +30,8 @@ describe('Apps request api Test', () => {
3030
.catch(done)
3131
})
3232

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-
4333
it('test get all request for oranization', done => {
44-
client.organization(orgID).app().request()
34+
client.organization(orgID).request()
4535
.findAll()
4636
.then((response) => {
4737
expect(response.data).to.not.equal(undefined)
@@ -51,7 +41,7 @@ describe('Apps request api Test', () => {
5141
})
5242

5343
it('test delete app request', done => {
54-
client.organization(orgID).app().request()
44+
client.organization(orgID).request()
5545
.delete(requestUID)
5646
.then((response) => {
5747
expect(response.data).to.not.equal(undefined)

test/api/app-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,13 @@ describe('Apps api Test', () => {
139139
done()
140140
}).catch(done)
141141
})
142+
it('test fetch app request', done => {
143+
client.organization(orgID).app(appUid)
144+
.getRequests()
145+
.then((response) => {
146+
expect(response.data).to.not.equal(undefined)
147+
done()
148+
})
149+
.catch(done)
150+
})
142151
})

test/typescript/app-request.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { expect } from 'chai'
22
import * as dotenv from 'dotenv'
33
import { Hosting } from '../../types/app/hosting'
4-
import { Request, Requests } from '../../types/app/request'
4+
import { AppRequest } from '../../types/app/request'
55
dotenv.config()
66
let requestUID = ''
77

8-
export function orgAppRequest (request: Requests) {
8+
export function orgAppRequest (request: AppRequest) {
99
describe('Org App request api', () => {
1010
test('test get all request for oranization', done => {
1111
request
@@ -26,30 +26,15 @@ export function orgAppRequest (request: Requests) {
2626
})
2727
.catch(done)
2828
})
29-
})
30-
}
31-
32-
export function appRequest (request: Request) {
33-
describe('App request', () => {
3429
test('test create app request', done => {
3530
request
36-
.create(process.env.APIKEY as string)
31+
.create({ appUid: '', targetUid: process.env.APIKEY as string})
3732
.then((response) => {
3833
requestUID = response.data.data.uid
3934
expect(response.data).to.not.equal(undefined)
4035
done()
4136
})
4237
.catch(done)
4338
})
44-
45-
test('test fetch app request', done => {
46-
request
47-
.fetch()
48-
.then((response) => {
49-
expect(response.data).to.not.equal(undefined)
50-
done()
51-
})
52-
.catch(done)
53-
})
5439
})
5540
}

0 commit comments

Comments
 (0)