UNPKG

5.67 kBJavaScriptView Raw
1import entities from './entities';
2import { wrapAppDefinition } from './entities/app-definition';
3/**
4 * @private
5 */
6
7/**
8 * @private
9 */
10export default function createAppDefinitionApi(makeRequest) {
11 var _entities$appBundle = entities.appBundle,
12 wrapAppBundle = _entities$appBundle.wrapAppBundle,
13 wrapAppBundleCollection = _entities$appBundle.wrapAppBundleCollection;
14
15 var getParams = function getParams(data) {
16 return {
17 appDefinitionId: data.sys.id,
18 organizationId: data.sys.organization.sys.id
19 };
20 };
21
22 return {
23 /**
24 * Sends an update to the server with any changes made to the object's properties
25 * @return Object returned from the server with updated changes.
26 * @example ```javascript
27 * const contentful = require('contentful-management')
28 *
29 * const client = contentful.createClient({
30 * accessToken: '<content_management_api_key>'
31 * })
32 *
33 * client.getOrganization('<org_id>')
34 * .then((org) => org.getAppDefinition('<app_def_id>'))
35 * .then((appDefinition) => {
36 * appDefinition.name = 'New App Definition name'
37 * return appDefinition.update()
38 * })
39 * .then((appDefinition) => console.log(`App Definition ${appDefinition.sys.id} updated.`))
40 * .catch(console.error)
41 * ```
42 */
43 update: function update() {
44 var data = this.toPlainObject();
45 return makeRequest({
46 entityType: 'AppDefinition',
47 action: 'update',
48 params: getParams(data),
49 headers: {},
50 payload: data
51 }).then(function (data) {
52 return wrapAppDefinition(makeRequest, data);
53 });
54 },
55
56 /**
57 * Deletes this object on the server.
58 * @return Promise for the deletion. It contains no data, but the Promise error case should be handled.
59 * @example ```javascript
60 * const contentful = require('contentful-management')
61 *
62 * const client = contentful.createClient({
63 * accessToken: '<content_management_api_key>'
64 * })
65 *
66 * client.getOrganization('<org_id>')
67 * .then((org) => org.getAppDefinition('<app_def_id>'))
68 * .then((appDefinition) => appDefinition.delete())
69 * .then(() => console.log(`App Definition deleted.`))
70 * .catch(console.error)
71 * ```
72 */
73 "delete": function del() {
74 var data = this.toPlainObject();
75 return makeRequest({
76 entityType: 'AppDefinition',
77 action: 'delete',
78 params: getParams(data)
79 });
80 },
81
82 /**
83 * Gets an app bundle
84 * @param id - AppBundle ID
85 * @return Promise for an AppBundle
86 * @example ```javascript
87 * const contentful = require('contentful-management')
88 * const client = contentful.createClient({
89 * accessToken: '<content_management_api_key>'
90 * })
91 *
92 * client.getOrganization('<org_id>')
93 * .then((org) => org.getAppDefinition('<app_def_id>'))
94 * .then((appDefinition) => appDefinition.getAppBundle('<app_upload_id>')
95 * .then((appBundle) => console.log(appBundle))
96 * .catch(console.error)
97 * ```
98 */
99 getAppBundle: function getAppBundle(id) {
100 var raw = this.toPlainObject();
101 return makeRequest({
102 entityType: 'AppBundle',
103 action: 'get',
104 params: {
105 appBundleId: id,
106 appDefinitionId: raw.sys.id,
107 organizationId: raw.sys.organization.sys.id
108 }
109 }).then(function (data) {
110 return wrapAppBundle(makeRequest, data);
111 });
112 },
113
114 /**
115 * Gets a collection of AppBundles
116 * @return Promise for a collection of AppBundles
117 * @example ```javascript
118 * const contentful = require('contentful-management')
119 * const client = contentful.createClient({
120 * accessToken: '<content_management_api_key>'
121 * })
122 *
123 * client.getOrganization('<org_id>')
124 * .then((org) => org.getAppDefinition('<app_def_id>'))
125 * .then((appDefinition) => appDefinition.getAppBundles()
126 * .then((response) => console.log(response.items))
127 * .catch(console.error)
128 * ```
129 */
130 getAppBundles: function getAppBundles() {
131 var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
132 var raw = this.toPlainObject();
133 return makeRequest({
134 entityType: 'AppBundle',
135 action: 'getMany',
136 params: {
137 organizationId: raw.sys.organization.sys.id,
138 appDefinitionId: raw.sys.id,
139 query: query
140 }
141 }).then(function (data) {
142 return wrapAppBundleCollection(makeRequest, data);
143 });
144 },
145
146 /**
147 * Creates an app bundle
148 * @param Object representation of the App Bundle to be created
149 * @return Promise for the newly created AppBundle
150 * @example ```javascript
151 * const contentful = require('contentful-management')
152 * const client = contentful.createClient({
153 * accessToken: '<content_management_api_key>'
154 * })
155 * client.getOrganization('<org_id>')
156 * .then((org) => org.getAppDefinition('<app_def_id>'))
157 * .then((appDefinition) => appDefinition.createAppBundle('<app_upload_id>')
158 * .then((appBundle) => console.log(appBundle))
159 * .catch(console.error)
160 * ```
161 */
162 createAppBundle: function createAppBundle(data) {
163 var raw = this.toPlainObject();
164 return makeRequest({
165 entityType: 'AppBundle',
166 action: 'create',
167 params: {
168 appDefinitionId: raw.sys.id,
169 organizationId: raw.sys.organization.sys.id
170 },
171 payload: data
172 }).then(function (data) {
173 return wrapAppBundle(makeRequest, data);
174 });
175 }
176 };
177}
\No newline at end of file