@@ -22,7 +22,12 @@ declare namespace itemsjs {
2222 total : number ;
2323 }
2424
25- interface SearchOptions < I extends { } , S extends string , A extends keyof I & string > {
25+ interface SearchOptions <
26+ I extends { } ,
27+ S extends string ,
28+ A extends keyof I & string ,
29+ IdField extends keyof I & string ,
30+ > {
2631 query ?: string | undefined ;
2732 /** @default 1 */
2833 page ?: number | undefined ;
@@ -39,6 +44,9 @@ declare namespace itemsjs {
3944 removeStopWordFilter ?: boolean | undefined ;
4045 /** @default false */
4146 is_all_filtered_items ?: boolean | undefined ;
47+
48+ /** Restricts results to items whose values match the ID field (`id` by default or as defined in `custom_id_field`). */
49+ ids ?: I [ IdField ] [ ] ;
4250 }
4351
4452 interface AggregationOptions < A extends string > {
@@ -61,9 +69,14 @@ declare namespace itemsjs {
6169 per_page ?: number | undefined ;
6270 }
6371
64- interface ItemsJs < I extends { } , S extends string , A extends keyof I & string > {
72+ interface ItemsJs <
73+ I extends { } ,
74+ S extends string ,
75+ A extends keyof I & string ,
76+ IdField extends keyof I & string ,
77+ > {
6578 /** Search items */
66- search ( options ?: SearchOptions < I , S , A > ) : {
79+ search ( options ?: SearchOptions < I , S , A , IdField > ) : {
6780 data : {
6881 items : I [ ] ;
6982 allFilteredItems : I [ ] | null ;
@@ -86,10 +99,10 @@ declare namespace itemsjs {
8699
87100 /**
88101 * Find similar items.
89- * Uses the `id` key on items to check for uniqueness
102+ * Uses the `id` key or the one set via `custom_id_field` to check for uniqueness..
90103 */
91104 similar (
92- id : I extends { id : infer ID } ? ID : unknown ,
105+ id : I [ IdField ] ,
93106 options : SimilarOptions < I > ,
94107 ) : {
95108 data : { items : Array < I & { _id : number ; intersection_length : number } > } ;
@@ -125,26 +138,40 @@ declare namespace itemsjs {
125138 }
126139
127140 /** Configuration for itemsjs */
128- interface Configuration < I extends { } , S extends string , A extends keyof I & string > {
141+ interface Configuration <
142+ I extends { } ,
143+ S extends string ,
144+ A extends keyof I & string ,
145+ IdField extends string = "id" ,
146+ > {
129147 sortings ?: Record < S , Sorting < I > > | undefined ;
130148 aggregations ?: Record < A , Aggregation > | undefined ;
131149 /** @default [] */
132150 searchableFields ?: Array < keyof I > | undefined ;
133151 /** @default true */
134152 native_search_enabled ?: boolean | undefined ;
153+ /** @default 'id' — defines which field represents the unique ID */
154+ custom_id_field ?: IdField ;
135155 }
136156}
137157
138158/**
139159 * Main itemsjs function
140160 * @param items The items to index
141- * @param configuration itemsjs
161+ * @param configuration Configuration options including sortings, aggregations, and optionally a custom ID field.
142162 * @template I The type of items being indexed
163+ * @template S The keys of sortings defined in the configuration.
164+ * @template A The keys of aggregations defined in the configuration.
165+ * @template IdField The field used as the unique identifier for items (defaults to "id").
143166 */
144167declare function itemsjs <
145- I extends { } = Record < any , unknown > ,
168+ I extends Record < string , any > = Record < string , any > ,
146169 S extends string = string ,
147170 A extends keyof I & string = keyof I & string ,
148- > ( items : I [ ] , configuration ?: itemsjs . Configuration < I , S , A > ) : itemsjs . ItemsJs < I , S , A > ;
171+ IdField extends keyof I & string = "id" ,
172+ > (
173+ items : I [ ] ,
174+ configuration ?: itemsjs . Configuration < I , S , A , IdField > ,
175+ ) : itemsjs . ItemsJs < I , S , A , IdField > ;
149176
150177export = itemsjs ;
0 commit comments