UNPKG

12.4 kBJavaScriptView Raw
1var _excluded = ["url"];
2
3function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
4
5function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
6
7import { createRequestConfig } from 'contentful-sdk-core';
8import entities from './entities';
9
10/**
11 * @private
12 */
13export default function createClientApi(makeRequest) {
14 var _entities$space = entities.space,
15 wrapSpace = _entities$space.wrapSpace,
16 wrapSpaceCollection = _entities$space.wrapSpaceCollection;
17 var wrapUser = entities.user.wrapUser;
18 var _entities$personalAcc = entities.personalAccessToken,
19 wrapPersonalAccessToken = _entities$personalAcc.wrapPersonalAccessToken,
20 wrapPersonalAccessTokenCollection = _entities$personalAcc.wrapPersonalAccessTokenCollection;
21 var _entities$organizatio = entities.organization,
22 wrapOrganization = _entities$organizatio.wrapOrganization,
23 wrapOrganizationCollection = _entities$organizatio.wrapOrganizationCollection;
24 var wrapUsageCollection = entities.usage.wrapUsageCollection;
25 return {
26 /**
27 * Gets all spaces
28 * @return Promise for a collection of Spaces
29 * ```javascript
30 * const contentful = require('contentful-management')
31 *
32 * const client = contentful.createClient({
33 * accessToken: '<content_management_api_key>'
34 * })
35 *
36 * client.getSpaces()
37 * .then((response) => console.log(response.items))
38 * .catch(console.error)
39 * ```
40 */
41 getSpaces: function getSpaces() {
42 var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
43 return makeRequest({
44 entityType: 'Space',
45 action: 'getMany',
46 params: {
47 query: createRequestConfig({
48 query: query
49 }).params
50 }
51 }).then(function (data) {
52 return wrapSpaceCollection(makeRequest, data);
53 });
54 },
55
56 /**
57 * Gets a space
58 * @param spaceId - Space ID
59 * @return Promise for a Space
60 * ```javascript
61 * const contentful = require('contentful-management')
62 *
63 * const client = contentful.createClient({
64 * accessToken: '<content_management_api_key>'
65 * })
66 *
67 * client.getSpace('<space_id>')
68 * .then((space) => console.log(space))
69 * .catch(console.error)
70 * ```
71 */
72 getSpace: function getSpace(spaceId) {
73 return makeRequest({
74 entityType: 'Space',
75 action: 'get',
76 params: {
77 spaceId: spaceId
78 }
79 }).then(function (data) {
80 return wrapSpace(makeRequest, data);
81 });
82 },
83
84 /**
85 * Creates a space
86 * @param spaceData - Object representation of the Space to be created
87 * @param organizationId - Organization ID, if the associated token can manage more than one organization.
88 * @return Promise for the newly created Space
89 * @example ```javascript
90 * const contentful = require('contentful-management')
91 *
92 * const client = contentful.createClient({
93 * accessToken: '<content_management_api_key>'
94 * })
95 *
96 * client.createSpace({
97 * name: 'Name of new space'
98 * })
99 * .then((space) => console.log(space))
100 * .catch(console.error)
101 * ```
102 */
103 createSpace: function createSpace(spaceData, organizationId) {
104 return makeRequest({
105 entityType: 'Space',
106 action: 'create',
107 params: {
108 organizationId: organizationId
109 },
110 payload: spaceData
111 }).then(function (data) {
112 return wrapSpace(makeRequest, data);
113 });
114 },
115
116 /**
117 * Gets an organization
118 * @param id - Organization ID
119 * @return Promise for a Organization
120 * @example ```javascript
121 * const contentful = require('contentful-management')
122 *
123 * const client = contentful.createClient({
124 * accessToken: '<content_management_api_key>'
125 * })
126 *
127 * client.getOrganization('<org_id>')
128 * .then((org) => console.log(org))
129 * .catch(console.error)
130 * ```
131 */
132 getOrganization: function getOrganization(id) {
133 return makeRequest({
134 entityType: 'Organization',
135 action: 'get',
136 params: {
137 organizationId: id
138 }
139 }).then(function (data) {
140 return wrapOrganization(makeRequest, data);
141 });
142 },
143
144 /**
145 * Gets a collection of Organizations
146 * @return Promise for a collection of Organizations
147 * @example ```javascript
148 * const contentful = require('contentful-management')
149 *
150 * const client = contentful.createClient({
151 * accessToken: '<content_management_api_key>'
152 * })
153 *
154 * client.getOrganizations()
155 * .then(result => console.log(result.items))
156 * .catch(console.error)
157 * ```
158 */
159 getOrganizations: function getOrganizations() {
160 var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
161 return makeRequest({
162 entityType: 'Organization',
163 action: 'getMany',
164 params: {
165 query: createRequestConfig({
166 query: query
167 }).params
168 }
169 }).then(function (data) {
170 return wrapOrganizationCollection(makeRequest, data);
171 });
172 },
173
174 /**
175 * Gets the authenticated user
176 * @return Promise for a User
177 * @example ```javascript
178 * const contentful = require('contentful-management')
179 *
180 * const client = contentful.createClient({
181 * accessToken: '<content_management_api_key>'
182 * })
183 *
184 * client.getCurrentUser()
185 * .then(user => console.log(user.firstName))
186 * .catch(console.error)
187 * ```
188 */
189 getCurrentUser: function getCurrentUser(params) {
190 return makeRequest({
191 entityType: 'User',
192 action: 'getCurrent',
193 params: params
194 }).then(function (data) {
195 return wrapUser(makeRequest, data);
196 });
197 },
198
199 /**
200 * Creates a personal access token
201 * @param data - personal access token config
202 * @return Promise for a Token
203 * @example ```javascript
204 * const contentful = require('contentful-management')
205 *
206 * const client = contentful.createClient({
207 * accessToken: '<content_management_api_key>'
208 * })
209 *
210 * client.createPersonalAccessToken(
211 * {
212 * "name": "My Token",
213 * "scope": [
214 * "content_management_manage"
215 * ]
216 * }
217 * )
218 * .then(personalAccessToken => console.log(personalAccessToken.token))
219 * .catch(console.error)
220 * ```
221 */
222 createPersonalAccessToken: function createPersonalAccessToken(data) {
223 return makeRequest({
224 entityType: 'PersonalAccessToken',
225 action: 'create',
226 params: {},
227 payload: data
228 }).then(function (response) {
229 return wrapPersonalAccessToken(makeRequest, response);
230 });
231 },
232
233 /**
234 * Gets a personal access token
235 * @param data - personal access token config
236 * @return Promise for a Token
237 * @example ```javascript
238 * const contentful = require('contentful-management')
239 *
240 * const client = contentful.createClient({
241 * accessToken: '<content_management_api_key>'
242 * })
243 *
244 * client.getPersonalAccessToken(tokenId)
245 * .then(token => console.log(token.token))
246 * .catch(console.error)
247 * ```
248 */
249 getPersonalAccessToken: function getPersonalAccessToken(tokenId) {
250 return makeRequest({
251 entityType: 'PersonalAccessToken',
252 action: 'get',
253 params: {
254 tokenId: tokenId
255 }
256 }).then(function (data) {
257 return wrapPersonalAccessToken(makeRequest, data);
258 });
259 },
260
261 /**
262 * Gets all personal access tokens
263 * @return Promise for a Token
264 * @example ```javascript
265 * const contentful = require('contentful-management')
266 *
267 * const client = contentful.createClient({
268 * accessToken: '<content_management_api_key>'
269 * })
270 *
271 * client.getPersonalAccessTokens()
272 * .then(response => console.log(reponse.items))
273 * .catch(console.error)
274 * ```
275 */
276 getPersonalAccessTokens: function getPersonalAccessTokens() {
277 return makeRequest({
278 entityType: 'PersonalAccessToken',
279 action: 'getMany',
280 params: {}
281 }).then(function (data) {
282 return wrapPersonalAccessTokenCollection(makeRequest, data);
283 });
284 },
285
286 /**
287 * Get organization usage grouped by {@link UsageMetricEnum metric}
288 *
289 * @param organizationId - Id of an organization
290 * @param query - Query parameters
291 * @return Promise of a collection of usages
292 * @example ```javascript
293 *
294 * const contentful = require('contentful-management')
295 *
296 * const client = contentful.createClient({
297 * accessToken: '<content_management_api_key>'
298 * })
299 *
300 * client.getOrganizationUsage('<organizationId>', {
301 * 'metric[in]': 'cma,gql',
302 * 'dateRange.startAt': '2019-10-22',
303 * 'dateRange.endAt': '2019-11-10'
304 * }
305 * })
306 * .then(result => console.log(result.items))
307 * .catch(console.error)
308 * ```
309 */
310 getOrganizationUsage: function getOrganizationUsage(organizationId) {
311 var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
312 return makeRequest({
313 entityType: 'Usage',
314 action: 'getManyForOrganization',
315 params: {
316 organizationId: organizationId,
317 query: query
318 }
319 }).then(function (data) {
320 return wrapUsageCollection(makeRequest, data);
321 });
322 },
323
324 /**
325 * Get organization usage grouped by space and metric
326 *
327 * @param organizationId - Id of an organization
328 * @param query - Query parameters
329 * @return Promise of a collection of usages
330 * ```javascript
331 * const contentful = require('contentful-management')
332 *
333 * const client = contentful.createClient({
334 * accessToken: '<content_management_api_key>'
335 * })
336 *
337 * client.getSpaceUsage('<organizationId>', {
338 * skip: 0,
339 * limit: 10,
340 * 'metric[in]': 'cda,cpa,gql',
341 * 'dateRange.startAt': '2019-10-22',
342 * 'dateRange.endAt': '2020-11-30'
343 * }
344 * })
345 * .then(result => console.log(result.items))
346 * .catch(console.error)
347 * ```
348 */
349 getSpaceUsage: function getSpaceUsage(organizationId) {
350 var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
351 return makeRequest({
352 entityType: 'Usage',
353 action: 'getManyForSpace',
354 params: {
355 organizationId: organizationId,
356 query: query
357 }
358 }).then(function (data) {
359 return wrapUsageCollection(makeRequest, data);
360 });
361 },
362
363 /**
364 * Make a custom request to the Contentful management API's /spaces endpoint
365 * @param opts - axios request options (https://github.com/mzabriskie/axios)
366 * @return Promise for the response data
367 * ```javascript
368 * const contentful = require('contentful-management')
369 *
370 * const client = contentful.createClient({
371 * accessToken: '<content_management_api_key>'
372 * })
373 *
374 * client.rawRequest({
375 * method: 'GET',
376 * url: '/custom/path'
377 * })
378 * .then((responseData) => console.log(responseData))
379 * .catch(console.error)
380 * ```
381 */
382 rawRequest: function rawRequest(_ref) {
383 var url = _ref.url,
384 config = _objectWithoutProperties(_ref, _excluded);
385
386 return makeRequest({
387 entityType: 'Http',
388 action: 'request',
389 params: {
390 url: url,
391 config: config
392 }
393 });
394 }
395 };
396}
\No newline at end of file