Skip to content

Commit

Permalink
feat: add reference search to query types (#1766)
Browse files Browse the repository at this point in the history
  • Loading branch information
veu committed Mar 15, 2023
1 parent f651239 commit 7da3ac4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/types/query/query.ts
Expand Up @@ -8,6 +8,7 @@ import { FullTextSearchFilters } from './search'
import { AssetSelectFilter, EntrySelectFilter, EntrySelectFilterWithFields } from './select'
import { SubsetFilters } from './subset'
import { FieldsType } from './util'
import { ReferenceSearchFilters } from './reference'

type FixedPagedOptions = {
skip?: number
Expand Down Expand Up @@ -40,6 +41,7 @@ export type EntryFieldsQueries<Fields extends FieldsType> =
| SubsetFilters<Fields, 'fields'>
| LocationSearchFilters<Fields, 'fields'>
| RangeFilters<Fields, 'fields'>
| ReferenceSearchFilters<Fields, 'fields'>

// TODO: create-contentful-api complained about non-optional fields when initialized with {}
export type EntriesQueries<Fields extends FieldsType> =
Expand Down
13 changes: 13 additions & 0 deletions lib/types/query/reference.ts
@@ -0,0 +1,13 @@
import { EntryFields } from '../entry'
import { ConditionalPick } from 'type-fest'

type SupportedTypes = EntryFields.EntryLink<any>

/**
* @desc search on references
* @see [Documentation]{@link https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/search-on-references}
*/
export type ReferenceSearchFilters<Fields, Prefix extends string> = {
[FieldName in keyof ConditionalPick<Fields, SupportedTypes> as `${Prefix}.${string &
FieldName}.${string}`]?: any
}
14 changes: 14 additions & 0 deletions test/types/queries/entry-queries.test-d.ts
Expand Up @@ -143,3 +143,17 @@ expectNotAssignable<EntriesQueries<{ locationField: EntryFields.Location }>>({
content_type: 'id',
'fields.unknownField[within]': [0, 1, 2, 3],
})

// search on references

expectNotAssignable<EntriesQueries<{ referenceField: EntryFields.EntryLink<any> }>>({
'fields.referenceField.sys.contentType.sys.id': 'id',
})
expectAssignable<EntriesQueries<{ referenceField: EntryFields.EntryLink<any> }>>({
content_type: 'id',
'fields.referenceField.sys.contentType.sys.id': 'id',
})
expectNotAssignable<EntriesQueries<{ referenceField: EntryFields.EntryLink<any> }>>({
content_type: 'id',
'fields.unknownField.sys.contentType.sys.id': 'id',
})
14 changes: 13 additions & 1 deletion test/types/query.test-d.ts
@@ -1,5 +1,5 @@
import { expectAssignable } from 'tsd'
import { EntriesQueries, EntryFields } from '../../lib'
import { EntriesQueries, EntryFields, EntryLink, FieldsType } from '../../lib'
import { EntryFieldsQueries } from '../../lib/types/query/query'
import { BLOCKS } from '@contentful/rich-text-types'

Expand Down Expand Up @@ -126,6 +126,18 @@ expectAssignable<Required<EntryFieldsQueries<{ arrayStringField: EntryFields.Arr
'fields.arrayStringField[match]': stringValue,
})

/*
* EntryFields: Type Reference
*/
expectAssignable<
Required<EntryFieldsQueries<{ referenceField: EntryFields.EntryLink<FieldsType> }>>
>({
select: ['fields.referenceField'],
'fields.referenceField[exists]': booleanValue,
'fields.referenceField.sys.contentType.sys.id': stringValue,
'fields.referenceField.fields.numberField': numberValue,
})

/*
* Entry
*/
Expand Down

0 comments on commit 7da3ac4

Please sign in to comment.