-
Notifications
You must be signed in to change notification settings - Fork 197
/
entry.ts
336 lines (319 loc) · 13.4 KB
/
entry.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
import { Document as RichTextDocument } from '@contentful/rich-text-types'
import { Asset } from './asset'
import { ContentfulCollection } from './collection'
import { ContentTypeLink, UnresolvedLink } from './link'
import { LocaleCode } from './locale'
import { Metadata } from './metadata'
import { EntrySkeletonType } from './query'
import { EntitySys } from './sys'
import { JsonArray, JsonObject } from 'type-fest'
import { ResourceLink } from './resource-link'
import { ChainModifiers } from './client'
/**
* System managed metadata for entries
* @category Entry
*/
export interface EntrySys extends EntitySys {
contentType: { sys: ContentTypeLink }
type: 'Entry'
}
/**
* Definition of abstract entry field types with additional type properties
* Introduced to properly distinguish all fields to create consistent query types
* @category Entry
*/
export declare namespace EntryFieldTypes {
type Symbol<Values extends string = string> = { type: 'Symbol'; values: Values }
type Text<Values extends string = string> = { type: 'Text'; values: Values }
type Integer<Values extends number = number> = { type: 'Integer'; values: Values }
type Number<Values extends number = number> = { type: 'Number'; values: Values }
type Date = { type: 'Date' }
type Boolean = { type: 'Boolean' }
type Location = { type: 'Location' }
type RichText = { type: 'RichText' }
type EntryLink<EntrySkeleton extends EntrySkeletonType> = {
type: 'EntryLink'
entry: EntrySkeleton
}
type EntryResourceLink<EntrySkeleton extends EntrySkeletonType> = {
type: 'EntryResourceLink'
entry: EntrySkeleton
}
type AssetLink = { type: 'AssetLink' }
type Array<
Item extends
| EntryFieldTypes.Symbol
| EntryFieldTypes.AssetLink
| EntryFieldTypes.EntryLink<EntrySkeletonType>
| EntryFieldTypes.EntryResourceLink<EntrySkeletonType>,
> = { type: 'Array'; item: Item }
type Object<Data extends JsonObject | JsonArray | null = JsonObject | JsonArray | null> = {
type: 'Object'
data: Data
}
}
/**
* Definition of entry fields
* @category Entry
*/
export declare namespace EntryFields {
type Symbol<Values extends string = string> = Values
type Text<Values extends string = string> = Values
type Integer<Values extends number = number> = Values
type Number<Values extends number = number> = Values
type Date = `${number}-${number}-${number}T${number}:${number}:${number}Z`
type Boolean = boolean
type Location = {
lat: number
lon: number
}
type EntryLink<EntrySkeleton extends EntrySkeletonType> = Entry<EntrySkeleton>
type AssetLink = Asset
type Link<EntrySkeleton extends EntrySkeletonType> = AssetLink | EntryLink<EntrySkeleton>
type Array<Item extends EntryFields.Symbol | AssetLink | EntryLink<EntrySkeletonType>> = Item[]
type Object<Data extends JsonObject | JsonArray | null = JsonObject | JsonArray | null> = Data
type RichText = RichTextDocument
}
/**
* All possible values for entry field types with additional type properties
* @category Entry
*/
export type EntryFieldType<EntrySkeleton extends EntrySkeletonType> =
| EntryFieldTypes.Symbol
| EntryFieldTypes.Text
| EntryFieldTypes.Integer
| EntryFieldTypes.Number
| EntryFieldTypes.Date
| EntryFieldTypes.Boolean
| EntryFieldTypes.Location
| EntryFieldTypes.RichText
| EntryFieldTypes.Object
| EntryFieldTypes.EntryLink<EntrySkeleton>
| EntryFieldTypes.EntryResourceLink<EntrySkeleton>
| EntryFieldTypes.AssetLink
| EntryFieldTypes.Array<EntryFieldTypes.Symbol>
| EntryFieldTypes.Array<EntryFieldTypes.AssetLink>
| EntryFieldTypes.Array<EntryFieldTypes.EntryLink<EntrySkeleton>>
| EntryFieldTypes.Array<EntryFieldTypes.EntryResourceLink<EntrySkeleton>>
/**
* All possible values for entry field types
* @category Entry
*/
export type EntryField<EntrySkeleton extends EntrySkeletonType> =
| EntryFields.Symbol
| EntryFields.Text
| EntryFields.Integer
| EntryFields.Number
| EntryFields.Date
| EntryFields.Boolean
| EntryFields.Location
| EntryFields.RichText
| EntryFields.Object
| EntryFields.EntryLink<EntrySkeleton>
| EntryFields.AssetLink
| EntryFields.Array<EntryFields.Symbol>
| EntryFields.Array<EntryFields.AssetLink>
| EntryFields.Array<EntryFields.EntryLink<EntrySkeleton>>
/**
* @category Entry
*/
export type BaseEntry = {
sys: EntrySys
metadata: Metadata
}
/**
* Mapping between abstract entry field types with additional type information
* and entry field types
* @category Entry
*/
export type BaseFieldMap<Field extends EntryFieldType<EntrySkeletonType>> =
Field extends EntryFieldTypes.Symbol<infer Values>
? EntryFields.Symbol<Values>
: Field extends EntryFieldTypes.Text<infer Values>
? EntryFields.Text<Values>
: Field extends EntryFieldTypes.Integer<infer Values>
? EntryFields.Integer<Values>
: Field extends EntryFieldTypes.Number<infer Values>
? EntryFields.Number<Values>
: Field extends EntryFieldTypes.Date
? EntryFields.Date
: Field extends EntryFieldTypes.Boolean
? EntryFields.Boolean
: Field extends EntryFieldTypes.Location
? EntryFields.Location
: Field extends EntryFieldTypes.RichText
? EntryFields.RichText
: Field extends EntryFieldTypes.Object<infer Data>
? EntryFields.Object<Data>
: never
/**
* A single resolved link to another entry in the same space
* If the current client configuration includes `withoutLinkResolution` chain option,
* the returned type will not resolve linked entities, but keep them as objects
* If the current client configuration includes `withoutUnresolvableLinks` chain option,
* the returned type will not include non-resolvable linked entities
* @category Entry
* @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 entry field values.
* @typeParam LinkedEntry - Shape of the linked entry used to calculate dynamic keys
* @internal
*/
export type ResolvedEntryLink<
Modifiers extends ChainModifiers,
Locales extends LocaleCode,
LinkedEntry extends EntrySkeletonType,
> = ChainModifiers extends Modifiers
? Entry<LinkedEntry, Modifiers, Locales> | UnresolvedLink<'Entry'> | undefined
: 'WITHOUT_LINK_RESOLUTION' extends Modifiers
? UnresolvedLink<'Entry'>
: 'WITHOUT_UNRESOLVABLE_LINKS' extends Modifiers
? Entry<LinkedEntry, Modifiers, Locales> | undefined
: Entry<LinkedEntry, Modifiers, Locales> | UnresolvedLink<'Entry'>
/**
* A single resolved reference link to another entry in a different space
* If the current client configuration includes `withoutLinkResolution` chain option,
* the returned type will not resolve linked entities, but keep them as objects
* If the current client configuration includes `withoutUnresolvableLinks` chain option,
* the returned type will not include non-resolvable linked entities
* @category Entry
* @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 entry field values.
* @typeParam LinkedEntry - Shape of the linked entry used to calculate dynamic keys
* @internal
*/
export type ResolvedEntryResourceLink<
Modifiers extends ChainModifiers,
Locales extends LocaleCode,
LinkedEntry extends EntrySkeletonType,
> = ChainModifiers extends Modifiers
? Entry<LinkedEntry, Modifiers, Locales> | { sys: ResourceLink } | undefined
: 'WITHOUT_LINK_RESOLUTION' extends Modifiers
? { sys: ResourceLink }
: 'WITHOUT_UNRESOLVABLE_LINKS' extends Modifiers
? Entry<LinkedEntry, Modifiers, Locales> | undefined
: Entry<LinkedEntry, Modifiers, Locales> | { sys: ResourceLink }
/**
* A single resolved link to another asset
* If the current client configuration includes `withoutLinkResolution` chain option,
* the returned type will not resolve linked entities, but keep them as objects
* If the current client configuration includes `withoutUnresolvableLinks` chain option,
* the returned type will not include non-resolvable linked entities
* @category Entry
* @typeParam Modifiers - The chain modifiers used to configure the client. They’re set automatically when using the client chain modifiers.
* @internal
*/
export type ResolvedAssetLink<
Modifiers extends ChainModifiers,
Locales extends LocaleCode,
> = ChainModifiers extends Modifiers
? Asset<Modifiers, Locales> | UnresolvedLink<'Asset'> | undefined
: 'WITHOUT_LINK_RESOLUTION' extends Modifiers
? UnresolvedLink<'Asset'>
: 'WITHOUT_UNRESOLVABLE_LINKS' extends Modifiers
? Asset<Modifiers, Locales> | undefined
: Asset<Modifiers, Locales> | UnresolvedLink<'Asset'>
/**
* A single resolved link to another resource
* @category Entry
* @typeParam Field - Shape of an entry 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 entry field values.
* @internal
*/
export type ResolvedLink<
Field extends EntryFieldType<EntrySkeletonType>,
Modifiers extends ChainModifiers = ChainModifiers,
Locales extends LocaleCode = LocaleCode,
> =
Field extends EntryFieldTypes.EntryLink<infer LinkedEntry>
? ResolvedEntryLink<Modifiers, Locales, LinkedEntry>
: Field extends EntryFieldTypes.EntryResourceLink<infer LinkedEntry>
? ResolvedEntryResourceLink<Modifiers, Locales, LinkedEntry>
: Field extends EntryFieldTypes.AssetLink
? ResolvedAssetLink<Modifiers, Locales>
: BaseFieldMap<Field>
/**
* A collection or single resolved link to another resource
* @category Entry
* @typeParam Field - Shape of an entry 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 entry field values.
* @see {@link https://www.contentful.com/developers/docs/concepts/links/ | Documentation}
*/
export type ResolvedField<
Field extends EntryFieldType<EntrySkeletonType>,
Modifiers extends ChainModifiers,
Locales extends LocaleCode = LocaleCode,
> =
Field extends EntryFieldTypes.Array<infer Item>
? Array<ResolvedLink<Item, Modifiers, Locales>>
: ResolvedLink<Field, Modifiers, Locales>
/**
* Entry represents anything defined as a Content Type in a space
* @category Entry
* @typeParam EntrySkeleton - Shape of entry 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 entry field values.
* @see {@link https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/entries | Documentation}
*/
export type Entry<
EntrySkeleton extends EntrySkeletonType = EntrySkeletonType,
Modifiers extends ChainModifiers = ChainModifiers,
Locales extends LocaleCode = LocaleCode,
> = BaseEntry & {
sys: { contentType: { sys: { id: EntrySkeleton['contentTypeId'] } } }
fields: ChainModifiers extends Modifiers
?
| {
[FieldName in keyof EntrySkeleton['fields']]: {
[LocaleName in Locales]?: ResolvedField<
EntrySkeleton['fields'][FieldName],
Modifiers,
Locales
>
}
}
| {
[FieldName in keyof EntrySkeleton['fields']]: ResolvedField<
EntrySkeleton['fields'][FieldName],
Modifiers,
Locales
>
}
: 'WITH_ALL_LOCALES' extends Modifiers
? {
[FieldName in keyof EntrySkeleton['fields']]: {
[LocaleName in Locales]?: ResolvedField<
EntrySkeleton['fields'][FieldName],
Modifiers,
Locales
>
}
}
: {
[FieldName in keyof EntrySkeleton['fields']]: ResolvedField<
EntrySkeleton['fields'][FieldName],
Modifiers,
Locales
>
}
}
/**
* A collection of entries
* @category Entry
* @typeParam EntrySkeleton - Shape of entry 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 entry field values.
* @see {@link https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/entries | Documentation}
*/
export type EntryCollection<
EntrySkeleton extends EntrySkeletonType,
Modifiers extends ChainModifiers = ChainModifiers,
Locales extends LocaleCode = LocaleCode,
> = ContentfulCollection<Entry<EntrySkeleton, Modifiers, Locales>> & {
errors?: Array<any>
includes?: {
Entry?: any[]
Asset?: any[]
}
}