-
Notifications
You must be signed in to change notification settings - Fork 198
/
client.ts
396 lines (376 loc) · 13.4 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import { ContentType, ContentTypeCollection } from './content-type'
import { Space } from './space'
import { LocaleCode, LocaleCollection } from './locale'
import {
AssetQueries,
AssetsQueries,
EntriesQueries,
EntryQueries,
EntrySkeletonType,
TagQueries,
} from './query'
import { SyncCollection, SyncOptions, SyncQuery } from './sync'
import { Tag, TagCollection } from './tag'
import { AssetKey } from './asset-key'
import { Entry, EntryCollection } from './entry'
import { Asset, AssetCollection, AssetFields } from './asset'
/**
* Client chain modifiers used in all types that depend on the client configuration.
* @category Client
* @internal
*/
export type ChainModifiers =
| 'WITH_ALL_LOCALES'
| 'WITHOUT_LINK_RESOLUTION'
| 'WITHOUT_UNRESOLVABLE_LINKS'
| undefined
/**
* Adds a modifier to a list of client chain modifiers.
* @category Client
* @internal
*/
export type AddChainModifier<
Modifiers extends ChainModifiers,
AddedModifier extends Exclude<ChainModifiers, undefined>,
> = undefined extends Modifiers ? AddedModifier : Modifiers | AddedModifier
/**
* Contentful Delivery API Client. Contains methods which allow access to the different kinds of entities present in Contentful (Entries, Assets, etc).
* @category Client
* @typeParam Modifiers - The chain modifiers used to configure the client. They’re set automatically when using the client chain modifiers.
*/
export interface ContentfulClientApi<Modifiers extends ChainModifiers> {
/**
* Fetches a content type
* @param id - The content type’s ID
* @returns Promise for a content type
* @example
* ```typescript
* import * as contentful from 'contentful'
*
* const client = contentful.createClient({
* space: '<space_id>',
* accessToken: '<content_delivery_api_key>'
* })
*
* const contentType = await client.getContentType('<content_type_id>')
* console.log(contentType)
* ```
*/
getContentType(id: string): Promise<ContentType>
/**
* Fetches a collection of content types
* @returns Promise for a collection of content types
* @example
* ```typescript
* import * as contentful from 'contentful'
*
* const client = contentful.createClient({
* space: '<space_id>',
* accessToken: '<content_delivery_api_key>'
* })
*
* const response = await client.getContentTypes()
* console.log(response.items)
* ```
*/
getContentTypes(query?: { query?: string }): Promise<ContentTypeCollection>
/**
* Fetches the space which the client is currently configured to use
* @returns Promise for the space
* @example
* ```typescript
* import * as contentful from 'contentful'
*
* const client = contentful.createClient({
* space: '<space_id>',
* accessToken: '<content_delivery_api_key>'
* })
*
* const space = await client.getSpace()
* console.log(space)
* ```
*/
getSpace(): Promise<Space>
/**
* Fetches a collection of locales
* @returns Promise for a collection of locales
* @example
* ```typescript
* import * as contentful from 'contentful'
*
* const client = contentful.createClient({
* space: '<space_id>',
* accessToken: '<content_delivery_api_key>'
* })
*
* const response = await client.getLocales()
* console.log(response.items)
* ```
*/
getLocales(): Promise<LocaleCollection>
/**
* Synchronizes either all the content or only new content since last sync.
* <strong> Important note: </strong> The Sync API endpoint does not support include or link resolution.
* However, contentful.js can do link resolution on the client side for the initial sync.
* For the delta sync (using nextSyncToken) link resolution is not possible since the sdk won’t have access to all linked entities.
* @param query - Query object
* @param query.initial - Optional, unless first sync call
* @param query.limit - Optional, sets the page size for the number of retrieved entries
* @param query.nextSyncToken - Optional, used in subsequent sync calls
* @param query.nextPageToken - Optional, used in subsequent sync calls
* @param query.type - Optional, query for specific entities
* @param query.content_type - Query for specific content types; optional,
* unless `query.type` is defined as `Entry`
* @param syncOptions
* @param syncOptions.paginate - Configures the client to call the sync API recursively,
* collecting all items from responses into one collection
* @typeParam EntrySkeleton - Shape of entity fields used to calculate dynamic keys
* @typeParam Modifiers - The chain modifiers used to configure the client. They’re set automatically when using the client chain modifiers.
* @typeParam Locales - If provided for a client using `allLocales` modifier, response type defines locale keys for entity field values.
* @see {@link https://www.contentful.com/developers/docs/concepts/sync/ | Documentation}
* @see {@link https://www.contentful.com/developers/docs/javascript/tutorials/using-the-sync-api-with-js/ | Tutorial for using sync API}
* @example
* ```typescript
* import * as contentful from 'contentful'
*
* const client = contentful.createClient({
* space: '<space_id>',
* accessToken: '<content_delivery_api_key>'
* })
*
* const response = await client.sync({
* initial: true
* })
* console.log({
* entries: response.entries,
* assets: response.assets,
* nextSyncToken: response.nextSyncToken
* })
* ```
*/
sync<
EntrySkeleton extends EntrySkeletonType = EntrySkeletonType,
Modifiers extends ChainModifiers = ChainModifiers,
Locales extends LocaleCode = LocaleCode,
>(
query: SyncQuery,
syncOptions?: SyncOptions,
): Promise<SyncCollection<EntrySkeleton, Modifiers, Locales>>
/**
* Fetches a tag
* @param id - The tag’s ID
* @returns Promise for a tag
* @example
* ```typescript
* import * as contentful from 'contentful'
*
* const client = contentful.createClient({
* space: '<space_id>',
* accessToken: '<content_delivery_api_key>'
* })
*
* const tag = await client.getTag('<asset_id>')
* console.log(tag)
* ```
*/
getTag(id: string): Promise<Tag>
/**
* Gets a collection of Tags
* @returns Promise for a collection of tags
* @example
* ```typescript
* import * as contentful from 'contentful'
*
* const client = contentful.createClient({
* space: '<space_id>',
* accessToken: '<content_delivery_api_key>'
* })
*
* const response = await client.getTags()
* console.log(response.items)
* ```
*/
getTags(query?: TagQueries): Promise<TagCollection>
/**
* Creates an asset key for signing asset URLs (Embargoed Assets)
* @returns Promise for an asset key
* @example
* ```typescript
* import * as contentful from 'contentful'
*
* const client = contentful.createClient({
* space: '<space_id>',
* accessToken: '<content_delivery_api_key>'
* })
*
* const assetKey = await client.getAssetKey(<UNIX timestamp>)
* console.log(assetKey)
* ```
*/
createAssetKey(expiresAt: number): Promise<AssetKey>
/**
* Fetches an entry
* @param id - The entry’s ID
* @param query - Object with search parameters. In this method it's only used for `locale` when querying.
* @returns Promise for an entry
* @typeParam EntrySkeleton - Shape of entry fields used to calculate dynamic keys
* @typeParam Locales - If provided for a client using `allLocales` modifier, response type defines locale keys for entry field values.
* @example
* ```typescript
* const contentful = require('contentful')
*
* const client = contentful.createClient({
* space: '<space_id>',
* accessToken: '<content_delivery_api_key>'
* })
*
* const entry = await client.getEntry('<entry_id>')
* console.log(entry)
* ```
*/
getEntry<
EntrySkeleton extends EntrySkeletonType = EntrySkeletonType,
Locales extends LocaleCode = LocaleCode,
>(
id: string,
query?: EntryQueries<Modifiers>,
): Promise<Entry<EntrySkeleton, Modifiers, Locales>>
/**
* Fetches a collection of Entries
* @param query - Object with search parameters
* @returns Promise for a collection of Entries
* @typeParam EntrySkeleton - Shape of entry fields used to calculate dynamic keys
* @typeParam Locales - If provided for a client using `allLocales` modifier, response type defines locale keys for entry field values.
* @see {@link https://www.contentful.com/developers/docs/javascript/tutorials/using-js-cda-sdk/#retrieving-entries-with-search-parameters | JS SDK tutorial}
* @see {@link https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters | REST API reference}
* @example
* ```typescript
* const contentful = require('contentful')
*
* const client = contentful.createClient({
* space: '<space_id>',
* accessToken: '<content_delivery_api_key>'
* })
*
* const response = await client.getEntries()
* console.log(response.items)
* ```
*/
getEntries<
EntrySkeleton extends EntrySkeletonType = EntrySkeletonType,
Locales extends LocaleCode = LocaleCode,
>(
query?: EntriesQueries<EntrySkeleton, Modifiers>,
): Promise<EntryCollection<EntrySkeleton, Modifiers, Locales>>
/**
* Parse raw json data into a collection of entries. objects.Links will be resolved also
* @param data - json data
* @typeParam EntrySkeleton - Shape of entry fields used to calculate dynamic keys
* @typeParam Locales - If provided for a client using `allLocales` modifier, response type defines locale keys for entry field values.
* @example
* ```typescript
* const data = {items: [
* {
* sys: {type: 'Entry', locale: 'en-US'},
* fields: {
* animal: {sys: {type: 'Link', linkType: 'Animal', id: 'oink'}},
* anotheranimal: {sys: {type: 'Link', linkType: 'Animal', id: 'middle-parrot'}}
* }
* }
* ],
* includes: {
* Animal: [
* {
* sys: {type: 'Animal', id: 'oink', locale: 'en-US'},
* fields: {
* name: 'Pig',
* friend: {sys: {type: 'Link', linkType: 'Animal', id: 'groundhog'}}
* }
* }
* ]
* }
* }
* console.log( data.items[0].fields.foo ); // undefined
* const parsedData = client.parseEntries(data);
* console.log( parsedData.items[0].fields.foo ); // foo
* ```
*/
parseEntries<
EntrySkeleton extends EntrySkeletonType = EntrySkeletonType,
Locales extends LocaleCode = LocaleCode,
>(
data: EntryCollection<
EntrySkeleton,
AddChainModifier<Modifiers, 'WITHOUT_LINK_RESOLUTION'>,
Locales
>,
): EntryCollection<EntrySkeleton, Modifiers, Locales>
/**
* Fetches an asset
* @param id
* @param query - Object with search parameters. In this method it's only useful for `locale`.
* @returns Promise for an asset
* @typeParam Locales - If provided for a client using `allLocales` modifier, response type defines locale keys for asset field values.
* @example
* const contentful = require('contentful')
*
* const client = contentful.createClient({
* space: '<space_id>',
* accessToken: '<content_delivery_api_key>'
* })
*
* const asset = await client.getAsset('<asset_id>')
* console.log(asset)
*/
getAsset<Locales extends LocaleCode = LocaleCode>(
id: string,
query?: AssetQueries<Modifiers>,
): Promise<Asset<Modifiers, Locales>>
/**
* Fetches a collection of assets
* @param query - Object with search parameters
* @see {@link https://www.contentful.com/developers/docs/javascript/tutorials/using-js-cda-sdk/#retrieving-entries-with-search-parameters | JS SDK tutorial}
* @see {@link https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters | REST API reference}
* @returns Promise for a collection of Assets
* @typeParam Locales - If provided for a client using `allLocales` modifier, response type defines locale keys for asset field values.
* @example
* const contentful = require('contentful')
*
* const client = contentful.createClient({
* space: '<space_id>',
* accessToken: '<content_delivery_api_key>'
* })
*
* const response = await client.getAssets()
* console.log(response.items)
*/
getAssets<Locales extends LocaleCode = LocaleCode>(
query?: AssetsQueries<AssetFields, Modifiers>,
): Promise<AssetCollection<Modifiers, Locales>>
/**
* A client that will fetch assets and entries with all locales. Only available if not already enabled.
*/
withAllLocales: 'WITH_ALL_LOCALES' extends Modifiers
? never
: ContentfulClientApi<AddChainModifier<Modifiers, 'WITH_ALL_LOCALES'>>
/**
* A client that will not resolve links in entries. Only available if not already disabled.
*/
withoutLinkResolution: 'WITHOUT_LINK_RESOLUTION' extends Modifiers
? never
: 'WITHOUT_UNRESOLVABLE_LINKS' extends Modifiers
? never
: ContentfulClientApi<AddChainModifier<Modifiers, 'WITHOUT_LINK_RESOLUTION'>>
/**
* A client that will remove unresolvable links from entries. Only available if not already disabled.
*/
withoutUnresolvableLinks: 'WITHOUT_LINK_RESOLUTION' extends Modifiers
? never
: 'WITHOUT_UNRESOLVABLE_LINKS' extends Modifiers
? never
: ContentfulClientApi<AddChainModifier<Modifiers, 'WITHOUT_UNRESOLVABLE_LINKS'>>
/**
* The current Contentful.js version
*/
version: string
}