1 | import entities from './entities';
|
2 | import { wrapAppDefinition } from './entities/app-definition';
|
3 |
|
4 | /**
|
5 | * @private
|
6 | */
|
7 |
|
8 | /**
|
9 | * @private
|
10 | */
|
11 | export default function createAppDefinitionApi(makeRequest) {
|
12 | const {
|
13 | wrapAppBundle,
|
14 | wrapAppBundleCollection
|
15 | } = entities.appBundle;
|
16 | const {
|
17 | wrapResourceProvider
|
18 | } = entities.resourceProvider;
|
19 | const getParams = data => ({
|
20 | appDefinitionId: data.sys.id,
|
21 | organizationId: data.sys.organization.sys.id
|
22 | });
|
23 | return {
|
24 | /**
|
25 | * Sends an update to the server with any changes made to the object's properties
|
26 | * @return Object returned from the server with updated changes.
|
27 | * @example ```javascript
|
28 | * const contentful = require('contentful-management')
|
29 | *
|
30 | * const client = contentful.createClient({
|
31 | * accessToken: '<content_management_api_key>'
|
32 | * })
|
33 | *
|
34 | * client.getOrganization('<org_id>')
|
35 | * .then((org) => org.getAppDefinition('<app_def_id>'))
|
36 | * .then((appDefinition) => {
|
37 | * appDefinition.name = 'New App Definition name'
|
38 | * return appDefinition.update()
|
39 | * })
|
40 | * .then((appDefinition) => console.log(`App Definition ${appDefinition.sys.id} updated.`))
|
41 | * .catch(console.error)
|
42 | * ```
|
43 | */
|
44 | update: function update() {
|
45 | const data = this.toPlainObject();
|
46 | return makeRequest({
|
47 | entityType: 'AppDefinition',
|
48 | action: 'update',
|
49 | params: getParams(data),
|
50 | headers: {},
|
51 | payload: data
|
52 | }).then(data => wrapAppDefinition(makeRequest, data));
|
53 | },
|
54 | /**
|
55 | * Deletes this object on the server.
|
56 | * @return Promise for the deletion. It contains no data, but the Promise error case should be handled.
|
57 | * @example ```javascript
|
58 | * const contentful = require('contentful-management')
|
59 | *
|
60 | * const client = contentful.createClient({
|
61 | * accessToken: '<content_management_api_key>'
|
62 | * })
|
63 | *
|
64 | * client.getOrganization('<org_id>')
|
65 | * .then((org) => org.getAppDefinition('<app_def_id>'))
|
66 | * .then((appDefinition) => appDefinition.delete())
|
67 | * .then(() => console.log(`App Definition deleted.`))
|
68 | * .catch(console.error)
|
69 | * ```
|
70 | */
|
71 | delete: function del() {
|
72 | const data = this.toPlainObject();
|
73 | return makeRequest({
|
74 | entityType: 'AppDefinition',
|
75 | action: 'delete',
|
76 | params: getParams(data)
|
77 | });
|
78 | },
|
79 | /**
|
80 | * Gets an app bundle
|
81 | * @param id - AppBundle ID
|
82 | * @return Promise for an AppBundle
|
83 | * @example ```javascript
|
84 | * const contentful = require('contentful-management')
|
85 | * const client = contentful.createClient({
|
86 | * accessToken: '<content_management_api_key>'
|
87 | * })
|
88 | *
|
89 | * client.getOrganization('<org_id>')
|
90 | * .then((org) => org.getAppDefinition('<app_def_id>'))
|
91 | * .then((appDefinition) => appDefinition.getAppBundle('<app_upload_id>'))
|
92 | * .then((appBundle) => console.log(appBundle))
|
93 | * .catch(console.error)
|
94 | * ```
|
95 | */
|
96 | getAppBundle(id) {
|
97 | const raw = this.toPlainObject();
|
98 | return makeRequest({
|
99 | entityType: 'AppBundle',
|
100 | action: 'get',
|
101 | params: {
|
102 | appBundleId: id,
|
103 | appDefinitionId: raw.sys.id,
|
104 | organizationId: raw.sys.organization.sys.id
|
105 | }
|
106 | }).then(data => wrapAppBundle(makeRequest, data));
|
107 | },
|
108 | /**
|
109 | * Gets a collection of AppBundles
|
110 | * @return Promise for a collection of AppBundles
|
111 | * @example ```javascript
|
112 | * const contentful = require('contentful-management')
|
113 | * const client = contentful.createClient({
|
114 | * accessToken: '<content_management_api_key>'
|
115 | * })
|
116 | *
|
117 | * client.getOrganization('<org_id>')
|
118 | * .then((org) => org.getAppDefinition('<app_def_id>'))
|
119 | * .then((appDefinition) => appDefinition.getAppBundles())
|
120 | * .then((response) => console.log(response.items))
|
121 | * .catch(console.error)
|
122 | * ```
|
123 | */
|
124 | getAppBundles(query = {}) {
|
125 | const raw = this.toPlainObject();
|
126 | return makeRequest({
|
127 | entityType: 'AppBundle',
|
128 | action: 'getMany',
|
129 | params: {
|
130 | organizationId: raw.sys.organization.sys.id,
|
131 | appDefinitionId: raw.sys.id,
|
132 | query
|
133 | }
|
134 | }).then(data => wrapAppBundleCollection(makeRequest, data));
|
135 | },
|
136 | /**
|
137 | * Creates an app bundle
|
138 | * @param Object representation of the App Bundle to be created
|
139 | * @return Promise for the newly created AppBundle
|
140 | * @example ```javascript
|
141 | * const contentful = require('contentful-management')
|
142 | * const client = contentful.createClient({
|
143 | * accessToken: '<content_management_api_key>'
|
144 | * })
|
145 | * client.getOrganization('<org_id>')
|
146 | * .then((org) => org.getAppDefinition('<app_def_id>'))
|
147 | * .then((appDefinition) => appDefinition.createAppBundle('<app_upload_id>'))
|
148 | * .then((appBundle) => console.log(appBundle))
|
149 | * .catch(console.error)
|
150 | * ```
|
151 | */
|
152 | createAppBundle(data) {
|
153 | const raw = this.toPlainObject();
|
154 | return makeRequest({
|
155 | entityType: 'AppBundle',
|
156 | action: 'create',
|
157 | params: {
|
158 | appDefinitionId: raw.sys.id,
|
159 | organizationId: raw.sys.organization.sys.id
|
160 | },
|
161 | payload: data
|
162 | }).then(data => wrapAppBundle(makeRequest, data));
|
163 | },
|
164 | /**
|
165 | * Gets a list of App Installations across an org for given organization and App Definition
|
166 | * If a spaceId is provided in the query object, it will return the App Installations for that specific space.
|
167 | * @return Promise for the newly created AppBundle
|
168 | * @example ```javascript
|
169 | * const contentful = require('contentful-management')
|
170 | * const client = contentful.createClient({
|
171 | * accessToken: '<content_management_api_key>'
|
172 | * })
|
173 | * client.getAppDefinition('<organization_id>', '<app_definition_id>')
|
174 | * .then((appDefinition) => appDefinition.getInstallationsForOrg(
|
175 | * { spaceId: '<space_id>' } // optional
|
176 | * ))
|
177 | * .then((appInstallationsForOrg) => console.log(appInstallationsForOrg.items))
|
178 | * .catch(console.error)
|
179 | * ```
|
180 | */
|
181 | getInstallationsForOrg(query = {}) {
|
182 | const raw = this.toPlainObject();
|
183 | return makeRequest({
|
184 | entityType: 'AppDefinition',
|
185 | action: 'getInstallationsForOrg',
|
186 | params: {
|
187 | appDefinitionId: raw.sys.id,
|
188 | organizationId: raw.sys.organization.sys.id,
|
189 | query
|
190 | }
|
191 | });
|
192 | },
|
193 | /**
|
194 | * Creates or updates a resource provider
|
195 | * @param data representation of the ResourceProvider
|
196 | * @return Promise for the newly created or updated ResourceProvider
|
197 | * @example ```javascript
|
198 | * const contentful = require('contentful-management')
|
199 | * const client = contentful.createClient({
|
200 | * accessToken: '<content_management_api_key>'
|
201 | * })
|
202 | *
|
203 | * // You need a valid AppDefinition with an activated AppBundle that has a contentful function configured
|
204 | * client.getOrganization('<org_id>')
|
205 | * .then((org) => org.getAppDefinition('<app_def_id>'))
|
206 | * .then((appDefinition) => appDefinition.upsertResourceProvider({
|
207 | * sys: {
|
208 | * id: '<resource_provider_id>'
|
209 | * },
|
210 | * type: 'function',
|
211 | * function: {
|
212 | * sys: {
|
213 | * id: '<contentful_function_id>',
|
214 | * type: 'Link'
|
215 | * linkType: 'Function'
|
216 | * }
|
217 | * }
|
218 | * }))
|
219 | * .then((resourceProvider) => console.log(resourceProvider))
|
220 | * .catch(console.error)
|
221 | * ```
|
222 | */
|
223 | upsertResourceProvider(data) {
|
224 | const raw = this.toPlainObject();
|
225 | return makeRequest({
|
226 | entityType: 'ResourceProvider',
|
227 | action: 'upsert',
|
228 | params: {
|
229 | appDefinitionId: raw.sys.id,
|
230 | organizationId: raw.sys.organization.sys.id
|
231 | },
|
232 | payload: data
|
233 | }).then(payload => wrapResourceProvider(makeRequest, payload));
|
234 | },
|
235 | /**
|
236 | * Gets a Resource Provider
|
237 | * @return Promise for a Resource Provider
|
238 | * @example ```javascript
|
239 | * const contentful = require('contentful-management')
|
240 | * const client = contentful.createClient({
|
241 | * accessToken: '<content_management_api_key>'
|
242 | * })
|
243 | *
|
244 | * client.getOrganization('<org_id>')
|
245 | * .then((org) => org.getAppDefinition('<app_def_id>'))
|
246 | * .then((appDefinition) => appDefinition.getResourceProvider())
|
247 | * .then((resourceProvider) => console.log(resourceProvider))
|
248 | * .catch(console.error)
|
249 | * ```
|
250 | */
|
251 | getResourceProvider() {
|
252 | const raw = this.toPlainObject();
|
253 | return makeRequest({
|
254 | entityType: 'ResourceProvider',
|
255 | action: 'get',
|
256 | params: {
|
257 | appDefinitionId: raw.sys.id,
|
258 | organizationId: raw.sys.organization.sys.id
|
259 | }
|
260 | }).then(payload => wrapResourceProvider(makeRequest, payload));
|
261 | }
|
262 | };
|
263 | } |
\ | No newline at end of file |