Skip to content

Commit 8703a95

Browse files
authored
🤖 Merge PR DefinitelyTyped#72471 feat(itemsjs): add missing ids parameter to SearchOptions by @zecka
1 parent 4c38110 commit 8703a95

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

‎types/itemsjs/index.d.ts‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
144167
declare 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

150177
export = itemsjs;

‎types/itemsjs/itemsjs-tests.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,31 @@ itemsIds.similar(0);
105105
itemsIds.similar(0, {});
106106

107107
itemsIds.similar(0, { field: "name" });
108+
109+
// Custom id field should be an existing key in the object
110+
itemsjs(myitems, {
111+
custom_id_field: "name",
112+
});
113+
114+
itemsjs(myitems, {
115+
// @ts-expect-error
116+
custom_id_field: "keyThatDoesNotExist",
117+
});
118+
119+
// Ids types should match the custom_id_field type
120+
const itemsCustomId = itemsjs(myitems, {
121+
custom_id_field: "name",
122+
});
123+
// @ts-expect-error
124+
itemsCustomId.search({ ids: [1, 2] });
125+
// @ts-expect-error
126+
itemsCustomId.similar(1, { field: "name" });
127+
itemsCustomId.similar("a", { field: "name" });
128+
itemsCustomId.search({ ids: ["foo", "bar"] });
129+
itemsCustomId.search({
130+
// @ts-expect-error
131+
ids: [1],
132+
});
133+
itemsCustomId.search({
134+
ids: ["a"],
135+
});

0 commit comments

Comments
 (0)