Skip to content

Commit

Permalink
feat: give includes basic entry and asset types instead of any (#2363)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimBeyer authored Oct 25, 2024
1 parent f926c4e commit f9bf2cc
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 54 deletions.
4 changes: 2 additions & 2 deletions lib/types/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ export type EntryCollection<
> = ContentfulCollection<Entry<EntrySkeleton, Modifiers, Locales>> & {
errors?: Array<any>
includes?: {
Entry?: any[]
Asset?: any[]
Entry?: Entry<EntrySkeletonType, Modifiers, Locales>[]
Asset?: Asset<Modifiers, Locales>[]
}
}
191 changes: 139 additions & 52 deletions test/types/client/getEntries.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,105 +18,192 @@ type Fields = {
moreLinks: Entry<LinkedSkeleton>[]
}

type EntrySkeleton = EntrySkeletonType<Fields, 'content-type-id'>
// A skeleton with no fields
type BaseEntrySkeleton = EntrySkeletonType

// Skeleton with some specific fields
type TestEntrySkeleton = EntrySkeletonType<Fields, 'content-type-id'>

type Locale = 'en'

expectType<Entry<EntrySkeleton, undefined>>(await client.getEntry('entry-id'))
expectType<Entry<EntrySkeleton, undefined>>(await client.getEntry<EntrySkeleton>('entry-id'))
expectType<EntryCollection<EntrySkeleton, undefined>>(await client.getEntries())
expectType<EntryCollection<EntrySkeleton, undefined>>(await client.getEntries<EntrySkeleton>())
/**
* With no extra parameters
*/
expectType<Entry<TestEntrySkeleton, undefined>>(await client.getEntry('entry-id'))

expectType<Entry<TestEntrySkeleton, undefined>>(
await client.getEntry<TestEntrySkeleton>('entry-id'),
)
expectType<EntryCollection<TestEntrySkeleton, undefined>>(await client.getEntries())

expectType<EntryCollection<TestEntrySkeleton, undefined>>(
await client.getEntries<TestEntrySkeleton>(),
)
expectType<Entry<BaseEntrySkeleton, undefined>>((await client.getEntries()).includes!.Entry![0])

expectType<EntryCollection<EntrySkeleton, undefined>>(
await client.getEntries<EntrySkeleton>({ content_type: 'content-type-id' }),
expectType<EntryCollection<TestEntrySkeleton, undefined>>(
await client.getEntries<TestEntrySkeleton>({ content_type: 'content-type-id' }),
)

expectError(await client.getEntries<EntrySkeleton>({ content_type: 'unexpected' }))
expectType<EntryCollection<EntrySkeleton | LinkedSkeleton, undefined>>(
await client.getEntries<EntrySkeleton | LinkedSkeleton>({
expectError(await client.getEntries<TestEntrySkeleton>({ content_type: 'unexpected' }))

expectType<EntryCollection<TestEntrySkeleton | LinkedSkeleton, undefined>>(
await client.getEntries<TestEntrySkeleton | LinkedSkeleton>({
content_type: 'content-type-id',
}),
)

expectType<Entry<EntrySkeleton, 'WITHOUT_UNRESOLVABLE_LINKS'>>(
/**
* Without unresolvable Links
*/
expectType<Entry<TestEntrySkeleton, 'WITHOUT_UNRESOLVABLE_LINKS'>>(
await client.withoutUnresolvableLinks.getEntry('entry-id'),
)
expectType<Entry<EntrySkeleton, 'WITHOUT_UNRESOLVABLE_LINKS'>>(
await client.withoutUnresolvableLinks.getEntry<EntrySkeleton>('entry-id'),
expectType<Entry<TestEntrySkeleton, 'WITHOUT_UNRESOLVABLE_LINKS'>>(
await client.withoutUnresolvableLinks.getEntry<TestEntrySkeleton>('entry-id'),
)
expectType<EntryCollection<EntrySkeleton, 'WITHOUT_UNRESOLVABLE_LINKS'>>(

expectType<EntryCollection<TestEntrySkeleton, 'WITHOUT_UNRESOLVABLE_LINKS'>>(
await client.withoutUnresolvableLinks.getEntries(),
)
expectType<EntryCollection<EntrySkeleton, 'WITHOUT_UNRESOLVABLE_LINKS'>>(
await client.withoutUnresolvableLinks.getEntries<EntrySkeleton>(),

expectType<EntryCollection<TestEntrySkeleton, 'WITHOUT_UNRESOLVABLE_LINKS'>>(
await client.withoutUnresolvableLinks.getEntries<TestEntrySkeleton>(),
)

expectType<Entry<EntrySkeleton, 'WITHOUT_LINK_RESOLUTION'>>(
expectType<Entry<BaseEntrySkeleton, 'WITHOUT_UNRESOLVABLE_LINKS'>>(
(await client.withoutUnresolvableLinks.getEntries()).includes!.Entry![0],
)

/**
* Without link resolution
*/
expectType<Entry<TestEntrySkeleton, 'WITHOUT_LINK_RESOLUTION'>>(
await client.withoutLinkResolution.getEntry('entry-id'),
)
expectType<Entry<EntrySkeleton, 'WITHOUT_LINK_RESOLUTION'>>(
await client.withoutLinkResolution.getEntry<EntrySkeleton>('entry-id'),

expectType<Entry<TestEntrySkeleton, 'WITHOUT_LINK_RESOLUTION'>>(
await client.withoutLinkResolution.getEntry<TestEntrySkeleton>('entry-id'),
)
expectType<EntryCollection<EntrySkeleton, 'WITHOUT_LINK_RESOLUTION'>>(

expectType<EntryCollection<TestEntrySkeleton, 'WITHOUT_LINK_RESOLUTION'>>(
await client.withoutLinkResolution.getEntries(),
)
expectType<EntryCollection<EntrySkeleton, 'WITHOUT_LINK_RESOLUTION'>>(
await client.withoutLinkResolution.getEntries<EntrySkeleton>(),

expectType<EntryCollection<TestEntrySkeleton, 'WITHOUT_LINK_RESOLUTION'>>(
await client.withoutLinkResolution.getEntries<TestEntrySkeleton>(),
)

expectType<Entry<EntrySkeleton, 'WITH_ALL_LOCALES'>>(
expectType<Entry<BaseEntrySkeleton, 'WITHOUT_LINK_RESOLUTION'>>(
(await client.withoutLinkResolution.getEntries()).includes!.Entry![0],
)

/**
* With all Locales
*/
expectType<Entry<TestEntrySkeleton, 'WITH_ALL_LOCALES'>>(
await client.withAllLocales.getEntry('entry-id'),
)
expectType<Entry<EntrySkeleton, 'WITH_ALL_LOCALES'>>(
await client.withAllLocales.getEntry<EntrySkeleton>('entry-id'),

expectType<Entry<TestEntrySkeleton, 'WITH_ALL_LOCALES'>>(
await client.withAllLocales.getEntry<TestEntrySkeleton>('entry-id'),
)
expectType<Entry<EntrySkeleton, 'WITH_ALL_LOCALES', Locale>>(
await client.withAllLocales.getEntry<EntrySkeleton, Locale>('entry-id'),

expectType<Entry<TestEntrySkeleton, 'WITH_ALL_LOCALES', Locale>>(
await client.withAllLocales.getEntry<TestEntrySkeleton, Locale>('entry-id'),
)
expectType<EntryCollection<EntrySkeleton, 'WITH_ALL_LOCALES'>>(

expectType<EntryCollection<TestEntrySkeleton, 'WITH_ALL_LOCALES'>>(
await client.withAllLocales.getEntries(),
)
expectType<EntryCollection<EntrySkeleton, 'WITH_ALL_LOCALES'>>(
await client.withAllLocales.getEntries<EntrySkeleton>(),

expectType<EntryCollection<TestEntrySkeleton, 'WITH_ALL_LOCALES'>>(
await client.withAllLocales.getEntries<TestEntrySkeleton>(),
)

expectType<EntryCollection<TestEntrySkeleton, 'WITH_ALL_LOCALES', Locale>>(
await client.withAllLocales.getEntries<TestEntrySkeleton, Locale>(),
)

expectType<Entry<BaseEntrySkeleton, 'WITH_ALL_LOCALES', Locale>>(
(await client.withAllLocales.getEntries<TestEntrySkeleton, Locale>()).includes!.Entry![0],
)
expectType<EntryCollection<EntrySkeleton, 'WITH_ALL_LOCALES', Locale>>(
await client.withAllLocales.getEntries<EntrySkeleton, Locale>(),

expectType<Entry<BaseEntrySkeleton, 'WITH_ALL_LOCALES'>>(
(await client.withAllLocales.getEntries<TestEntrySkeleton>()).includes!.Entry![0],
)

expectType<Entry<EntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_UNRESOLVABLE_LINKS'>>(
/**
* With all Locales and without unresolvable Links
*/
expectType<Entry<TestEntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_UNRESOLVABLE_LINKS'>>(
await client.withAllLocales.withoutUnresolvableLinks.getEntry('entry-id'),
)
expectType<Entry<EntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_UNRESOLVABLE_LINKS'>>(
await client.withAllLocales.withoutUnresolvableLinks.getEntry<EntrySkeleton>('entry-id'),

expectType<Entry<TestEntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_UNRESOLVABLE_LINKS'>>(
await client.withAllLocales.withoutUnresolvableLinks.getEntry<TestEntrySkeleton>('entry-id'),
)
expectType<Entry<EntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_UNRESOLVABLE_LINKS', Locale>>(
await client.withAllLocales.withoutUnresolvableLinks.getEntry<EntrySkeleton, Locale>('entry-id'),

expectType<Entry<TestEntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_UNRESOLVABLE_LINKS', Locale>>(
await client.withAllLocales.withoutUnresolvableLinks.getEntry<TestEntrySkeleton, Locale>(
'entry-id',
),
)
expectType<EntryCollection<EntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_UNRESOLVABLE_LINKS'>>(

expectType<EntryCollection<TestEntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_UNRESOLVABLE_LINKS'>>(
await client.withAllLocales.withoutUnresolvableLinks.getEntries(),
)
expectType<EntryCollection<EntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_UNRESOLVABLE_LINKS'>>(
await client.withAllLocales.withoutUnresolvableLinks.getEntries<EntrySkeleton>(),

expectType<EntryCollection<TestEntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_UNRESOLVABLE_LINKS'>>(
await client.withAllLocales.withoutUnresolvableLinks.getEntries<TestEntrySkeleton>(),
)

expectType<
EntryCollection<EntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_UNRESOLVABLE_LINKS', Locale>
>(await client.withAllLocales.withoutUnresolvableLinks.getEntries<EntrySkeleton, Locale>())
EntryCollection<TestEntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_UNRESOLVABLE_LINKS', Locale>
>(await client.withAllLocales.withoutUnresolvableLinks.getEntries<TestEntrySkeleton, Locale>())

expectType<Entry<EntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_LINK_RESOLUTION'>>(
expectType<Entry<BaseEntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_UNRESOLVABLE_LINKS'>>(
(await client.withAllLocales.withoutUnresolvableLinks.getEntries<TestEntrySkeleton>()).includes!
.Entry![0],
)

expectType<Entry<BaseEntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_UNRESOLVABLE_LINKS', Locale>>(
(await client.withAllLocales.withoutUnresolvableLinks.getEntries<TestEntrySkeleton, Locale>())
.includes!.Entry![0],
)

/**
* With all Locales and without link resolution
*/
expectType<Entry<TestEntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_LINK_RESOLUTION'>>(
await client.withAllLocales.withoutLinkResolution.getEntry('entry-id'),
)
expectType<Entry<EntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_LINK_RESOLUTION'>>(
await client.withAllLocales.withoutLinkResolution.getEntry<EntrySkeleton>('entry-id'),

expectType<Entry<TestEntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_LINK_RESOLUTION'>>(
await client.withAllLocales.withoutLinkResolution.getEntry<TestEntrySkeleton>('entry-id'),
)
expectType<Entry<EntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_LINK_RESOLUTION', Locale>>(
await client.withAllLocales.withoutLinkResolution.getEntry<EntrySkeleton, Locale>('entry-id'),

expectType<Entry<TestEntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_LINK_RESOLUTION', Locale>>(
await client.withAllLocales.withoutLinkResolution.getEntry<TestEntrySkeleton, Locale>('entry-id'),
)
expectType<EntryCollection<EntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_LINK_RESOLUTION'>>(

expectType<EntryCollection<TestEntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_LINK_RESOLUTION'>>(
await client.withAllLocales.withoutLinkResolution.getEntries(),
)
expectType<EntryCollection<EntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_LINK_RESOLUTION'>>(
await client.withAllLocales.withoutLinkResolution.getEntries<EntrySkeleton>(),

expectType<EntryCollection<TestEntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_LINK_RESOLUTION'>>(
await client.withAllLocales.withoutLinkResolution.getEntries<TestEntrySkeleton>(),
)

expectType<
EntryCollection<TestEntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_LINK_RESOLUTION', Locale>
>(await client.withAllLocales.withoutLinkResolution.getEntries<TestEntrySkeleton, Locale>())

expectType<Entry<BaseEntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_LINK_RESOLUTION'>>(
(await client.withAllLocales.withoutLinkResolution.getEntries()).includes!.Entry![0],
)
expectType<EntryCollection<EntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_LINK_RESOLUTION', Locale>>(
await client.withAllLocales.withoutLinkResolution.getEntries<EntrySkeleton, Locale>(),

expectType<Entry<BaseEntrySkeleton, 'WITH_ALL_LOCALES' | 'WITHOUT_LINK_RESOLUTION', Locale>>(
(await client.withAllLocales.withoutLinkResolution.getEntries<TestEntrySkeleton, Locale>())
.includes!.Entry![0],
)

0 comments on commit f9bf2cc

Please sign in to comment.