1
- /* @flow */
2
-
3
1
/**
4
2
* Contentful CDA API
5
3
* @namespace CDAClient
9
7
* @namespace Entities
10
8
*/
11
9
12
- /**
13
- * Link to another entity. See <a href="https://www.contentful.com/developers/docs/concepts/links/">Links</a> for more details.
14
- * @memberof Entities
15
- * @typedef Link
16
- * @prop {string } type - type of this entity. Always link.
17
- * @prop {string } id
18
- * @prop {string } linkType - type of this link. If defined, either Entry or Asset
19
- */
20
-
21
- /**
22
- * System metadata. See <a href="https://www.contentful.com/developers/docs/references/content-delivery-api/#/introduction/common-resource-attributes">Common Resource Attributes</a> for more details.
23
- * @memberof Entities
24
- * @typedef Sys
25
- * @prop {string } type
26
- * @prop {string } id
27
- * @prop {Entities.Link } space
28
- * @prop {string } createdAt
29
- * @prop {string } updatedAt
30
- * @prop {number } revision
31
- */
32
-
33
- import type { Space } from './entities/space'
34
10
import { wrapSpace } from './entities/space'
35
- import type { ContentType , ContentTypeCollection } from './entities/content-type'
36
11
import { wrapContentType , wrapContentTypeCollection } from './entities/content-type'
37
- import type { Entry , EntryCollection } from './entities/entry'
38
12
import { wrapEntry , wrapEntryCollection } from './entities/entry'
39
- import type { Asset , AssetCollection } from './entities/asset'
40
13
import { wrapAsset , wrapAssetCollection } from './entities/asset'
41
- import type { SyncCollection } from './sync'
42
14
import pagedSync from './sync'
43
15
import createRequestConfig from './create-request-config'
44
16
@@ -54,33 +26,32 @@ import createRequestConfig from './create-request-config'
54
26
* @prop {function } getAssets
55
27
* @prop {function } sync
56
28
*/
57
- export type ContentfulClient = {
58
- getSpace : ( id : string ) => Promise < Space > ,
59
- getContentType : ( id : string ) => Promise < ContentType > ,
60
- getContentTypes : ( query ? : Object ) => Promise < ContentTypeCollection > ,
61
- getEntry : ( id : string ) => Promise < Entry > ,
62
- getEntries : ( query ? : Object ) => Promise < EntryCollection > ,
63
- getAsset : ( id : string ) => Promise < Asset > ,
64
- getAssets : ( query ? : Object ) => Promise < AssetCollection > ,
65
- sync : ( query : Object ) => Promise < SyncCollection >
66
- }
67
- function errorHandler ( error : Object ) {
29
+ function errorHandler ( error ) {
68
30
if ( error . data ) {
69
31
throw error . data
70
32
}
71
33
throw error
72
34
}
73
35
74
36
/**
75
- * @private
76
37
* Link resolution can be turned off for the methods that use it, or it can
77
38
* be turned off globally. The local setting overrides the global setting.
39
+ * @private
40
+ * @param {Object } query - regular query object used for collection endpoints
41
+ * @param {boolean } globalSetting - Global library setting for link resolution
78
42
*/
79
- function shouldLinksResolve ( query : Object , globalSetting : boolean ) : boolean {
43
+ function shouldLinksResolve ( query , globalSetting ) {
80
44
return ! ! ( 'resolveLinks' in query ? query . resolveLinks : globalSetting )
81
45
}
82
46
83
- export default function createCdaApi ( http : Object , resolveLinksGlobalSetting : boolean ) : ContentfulClient {
47
+ /**
48
+ * Creates CDA API object
49
+ * @private
50
+ * @param {Object } http - HTTP client instance
51
+ * @param {boolean } resolveLinksGlobalSetting - Global library setting for link resolution
52
+ * @return {ClientAPI }
53
+ */
54
+ export default function createCdaApi ( http , resolveLinksGlobalSetting ) {
84
55
/**
85
56
* Gets the Space which the client is currently configured to use
86
57
* @memberof CDAClient
@@ -89,7 +60,7 @@ export default function createCdaApi (http: Object, resolveLinksGlobalSetting: b
89
60
* client.getSpace()
90
61
* .then(space => console.log(space))
91
62
*/
92
- function getSpace ( ) : Promise < Space > {
63
+ function getSpace ( ) {
93
64
return http . get ( '' )
94
65
. then ( response => wrapSpace ( response . data ) , errorHandler )
95
66
}
@@ -103,21 +74,21 @@ export default function createCdaApi (http: Object, resolveLinksGlobalSetting: b
103
74
* client.getContentType('contentTypeId')
104
75
* .then(contentType => console.log(contentType))
105
76
*/
106
- function getContentType ( id : string ) : Promise < ContentType > {
77
+ function getContentType ( id ) {
107
78
return http . get ( 'content_types/' + id )
108
79
. then ( response => wrapContentType ( response . data ) , errorHandler )
109
80
}
110
81
111
82
/**
112
83
* Gets a collection of Content Types
113
84
* @memberof CDAClient
114
- * @param {Entities.Query } query
85
+ * @param {Entities.Query= } query - Query object
115
86
* @return {Promise<Entities.ContentTypeCollection> } Promise for a collection of Content Types
116
87
* @example
117
88
* client.getContentTypes()
118
89
* .then(contentTypes => console.log(contentTypes.items))
119
90
*/
120
- function getContentTypes ( query ?: Object = { } ) : Promise < ContentTypeCollection > {
91
+ function getContentTypes ( query = { } ) {
121
92
return http . get ( 'content_types' , createRequestConfig ( { query : query } ) )
122
93
. then ( response => wrapContentTypeCollection ( response . data ) , errorHandler )
123
94
}
@@ -131,7 +102,7 @@ export default function createCdaApi (http: Object, resolveLinksGlobalSetting: b
131
102
* client.getEntry('entryId')
132
103
* .then(entry => console.log(entry))
133
104
*/
134
- function getEntry ( id : string ) : Promise < Entry > {
105
+ function getEntry ( id ) {
135
106
return http . get ( 'entries/' + id )
136
107
. then ( response => wrapEntry ( response . data ) , errorHandler )
137
108
}
@@ -146,7 +117,7 @@ export default function createCdaApi (http: Object, resolveLinksGlobalSetting: b
146
117
* client.getEntries({content_type: 'contentTypeId'})
147
118
* .then(entries => console.log(entries.items))
148
119
*/
149
- function getEntries ( query ?: Object = { } ) : Promise < EntryCollection > {
120
+ function getEntries ( query = { } ) {
150
121
const resolveLinks = shouldLinksResolve ( query , resolveLinksGlobalSetting )
151
122
return http . get ( 'entries' , createRequestConfig ( { query : query } ) )
152
123
. then ( response => wrapEntryCollection ( response . data , resolveLinks ) , errorHandler )
@@ -161,7 +132,7 @@ export default function createCdaApi (http: Object, resolveLinksGlobalSetting: b
161
132
* client.getAsset('assetId')
162
133
* .then(asset => console.log(asset))
163
134
*/
164
- function getAsset ( id : string ) : Promise < Asset > {
135
+ function getAsset ( id ) {
165
136
return http . get ( 'assets/' + id )
166
137
. then ( response => wrapAsset ( response . data ) , errorHandler )
167
138
}
@@ -175,7 +146,7 @@ export default function createCdaApi (http: Object, resolveLinksGlobalSetting: b
175
146
* client.getAssets()
176
147
* .then(assets => console.log(assets.items))
177
148
*/
178
- function getAssets ( query ?: Object = { } ) : Promise < AssetCollection > {
149
+ function getAssets ( query = { } ) {
179
150
return http . get ( 'assets' , createRequestConfig ( { query : query } ) )
180
151
. then ( response => wrapAssetCollection ( response . data ) , errorHandler )
181
152
}
@@ -195,7 +166,7 @@ export default function createCdaApi (http: Object, resolveLinksGlobalSetting: b
195
166
* client.sync()
196
167
* .then(response => console.log(response.entries, response.assets, response.nextSyncToken))
197
168
*/
198
- function sync ( query ?: Object = { } ) : Promise < SyncCollection > {
169
+ function sync ( query = { } ) {
199
170
const resolveLinks = shouldLinksResolve ( query , resolveLinksGlobalSetting )
200
171
return pagedSync ( http , query , resolveLinks )
201
172
}
0 commit comments