forked from typesense/typesense-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypes.ts
More file actions
355 lines (324 loc) · 10.3 KB
/
Types.ts
File metadata and controls
355 lines (324 loc) · 10.3 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import type { AxiosRequestConfig } from "axios";
import type { BaseStreamConfig, StreamConfig } from "./Configuration";
import type {
DocumentSchema,
SearchParamsWithPreset,
SearchOptions,
SearchResponse,
DocumentWriteParameters,
DeleteQuery,
DeleteResponse,
ImportResponse,
DocumentsExportParameters,
} from "./Documents";
export type DropTokensMode = "right_to_left" | "left_to_right" | "both_sides:3";
export type OperationMode = "off" | "always" | "fallback";
export type CommaSeparated<
T extends string,
ToExtend,
OriginalString extends string = T,
Previous extends string[] = [],
> = T extends `${infer Start},${infer Rest}`
? TrimString<Start> extends ToExtend
? CommaSeparated<
Rest,
ToExtend,
OriginalString,
[...Previous, TrimString<Start>]
>
: {
error: "Invalid operation mode";
value: TrimString<Start>;
}
: TrimString<T> extends ToExtend
? OriginalString
: {
error: "Invalid operation mode";
value: TrimString<T>;
};
export type MessageChunk = {
message: string;
conversation_id: string;
};
export type UnionArrayKeys<T> = {
[K in keyof T]: T[K] extends undefined
? never
: NonNullable<T[K]> extends infer R
? R extends R[]
? never
: R extends (infer U)[] | infer U
? U[] extends R
? K
: never
: never
: never;
}[keyof T] &
keyof T;
export type UnionArraySearchParams<T extends DocumentSchema = DocumentSchema> =
UnionArrayKeys<T>;
export type ArraybleParams<T extends DocumentSchema = DocumentSchema> = {
readonly [K in UnionArraySearchParams<T>]: string;
};
export type ExtractBaseTypes<T> = {
[K in keyof T]: K extends UnionArrayKeys<T>
? T[K] extends (infer U)[] | infer U
? U
: T[K]
: T[K];
};
export const arrayableParams: ArraybleParams = {
query_by: "query_by",
query_by_weights: "query_by_weights",
facet_by: "facet_by",
group_by: "group_by",
include_fields: "include_fields",
exclude_fields: "exclude_fields",
highlight_fields: "highlight_fields",
highlight_full_fields: "highlight_full_fields",
pinned_hits: "pinned_hits",
hidden_hits: "hidden_hits",
infix: "infix",
override_tags: "override_tags",
num_typos: "num_typos",
prefix: "prefix",
synonym_sets: "synonym_sets",
sort_by: "sort_by",
};
export interface SearchParams<
TDoc extends DocumentSchema,
Infix extends string = string,
> {
// From https://typesense.org/docs/latest/api/documents.html#arguments
// eslint-disable-next-line @typescript-eslint/ban-types -- Can't use `object` here, it needs to intersect with `{}`
q?: "*" | (string & {});
query_by?: string | string[];
query_by_weights?: string | number[];
prefix?: string | boolean | boolean[]; // default: true
filter_by?: string;
max_filter_by_candidates?: number; // default: 4
enable_synonyms?: boolean; // default: true
enable_analytics?: boolean; // default: true
filter_curated_hits?: boolean; // default: false
enable_lazy_filter?: boolean; // default: false
sort_by?: string | string[]; // default: text match desc
facet_by?: string | string[];
max_facet_values?: number;
facet_sample_threshold?: number;
facet_sample_percent?: number;
facet_query?: string;
facet_query_num_typos?: number;
facet_return_parent?: string;
facet_strategy?: "exhaustive" | "top_values" | "automatic";
page?: number; // default: 1
per_page?: number; // default: 10, max 250
group_by?: string | string[];
group_limit?: number; // default:
group_missing_values?: boolean;
include_fields?: string | string[];
exclude_fields?: string | string[];
highlight_fields?: string | string[]; // default: all queried fields
highlight_full_fields?: string | string[]; // default: all fields
highlight_affix_num_tokens?: number; // default: 4
highlight_start_tag?: string; // default: <mark>
highlight_end_tag?: string; // default: </mark>
enable_highlight_v1?: boolean;
snippet_threshold?: number; // default: 30
nl_query?: boolean;
nl_model_id?: string;
nl_query_debug?: boolean;
nl_query_prompt_cache_ttl?: number;
num_typos?: string | number | number[]; // default: 2
min_len_1typo?: number;
min_len_2typo?: number;
split_join_tokens?: OperationMode;
exhaustive_search?: boolean;
drop_tokens_threshold?: number; // default: 10
drop_tokens_mode?: DropTokensMode;
typo_tokens_threshold?: number; // default: 100
pinned_hits?: string | string[];
synonym_sets?: string[] | string;
hidden_hits?: string | string[];
limit_hits?: number; // default: no limit
pre_segmented_query?: boolean;
enable_overrides?: boolean;
override_tags?: string | string[];
prioritize_exact_match?: boolean; // default: true
prioritize_token_position?: boolean;
prioritize_num_matching_fields?: boolean;
search_cutoff_ms?: number;
use_cache?: boolean;
max_candidates?: number;
infix?:
| CommaSeparated<Infix, OperationMode>
| OperationMode[]
| OperationMode;
preset?: string;
text_match_type?: "max_score" | "max_weight" | "sum_score";
vector_query?: string;
"x-typesense-api-key"?: string;
"x-typesense-user-id"?: string;
offset?: number;
limit?: number;
stopwords?: string;
conversation?: boolean;
validate_field_names?: boolean;
conversation_stream?: boolean;
conversation_model_id?: string;
conversation_id?: string;
voice_query?: string;
streamConfig?: StreamConfig<TDoc>;
}
export interface SearchResponseRequestParams {
collection_name?: string;
q?: string;
page?: number;
per_page?: number;
first_q?: string;
voice_query?: {
transcribed_query?: string;
};
}
export interface SearchableDocuments<
T extends DocumentSchema = DocumentSchema,
Infix extends string = string,
> {
search(
searchParameters: SearchParams<T, Infix> | SearchParamsWithPreset<T, Infix>,
options: SearchOptions,
): Promise<SearchResponse<T>>;
clearCache(): void;
}
export interface WriteableDocuments<T> {
create(document: T, options: DocumentWriteParameters): Promise<T>;
upsert(document: T, options: DocumentWriteParameters): Promise<T>;
update(document: T, options: DocumentWriteParameters): Promise<T>;
delete(query: DeleteQuery): Promise<DeleteResponse>;
import(
documents: T[] | string,
options: DocumentWriteParameters,
): Promise<string | ImportResponse[]>;
export(options: DocumentsExportParameters): Promise<string>;
}
export interface MultiSearchUnionStreamConfig<T extends DocumentSchema>
extends BaseStreamConfig {
onComplete?: (data: UnionSearchResponse<T>) => void;
}
export interface MultiSearchResultsStreamConfig<T extends DocumentSchema[]>
extends BaseStreamConfig {
onComplete?: (data: {
results: { [Index in keyof T]: SearchResponse<T[Index]> } & {
length: T["length"];
};
}) => void;
}
export interface RequestParams<T extends DocumentSchema[]> {
path: string;
queryParams?: Record<string, unknown>;
body?: unknown;
headers?: Record<string, string>;
streamConfig?:
| StreamConfig<T[number]>
| MultiSearchResultsStreamConfig<T>
| MultiSearchUnionStreamConfig<T[number]>;
abortSignal?: AbortSignal | null;
responseType?: AxiosRequestConfig["responseType"] | undefined;
isStreamingRequest: boolean | undefined;
}
export interface MultiSearchRequestsWithUnionSchema<
T extends DocumentSchema,
Infix extends string,
> extends SearchesMultiSearchesRequestSchema<T, Infix> {
union: true;
}
export interface MultiSearchRequestsWithoutUnionSchema<
T extends DocumentSchema,
Infix extends string,
> extends SearchesMultiSearchesRequestSchema<T, Infix> {
union?: false | undefined;
}
export type MultiSearchRequestsSchema<
T extends DocumentSchema,
Infix extends string,
> =
| MultiSearchRequestsWithUnionSchema<T, Infix>
| MultiSearchRequestsWithoutUnionSchema<T, Infix>;
export interface UnionSearchResponse<T extends DocumentSchema>
extends Omit<SearchResponse<T>, "request_params"> {
union_request_params: SearchResponseRequestParams[];
}
export type MultiSearchResponse<
T extends DocumentSchema[],
Infix extends string,
R extends MultiSearchRequestsSchema<
T[number],
Infix
> = MultiSearchRequestsSchema<T[number], Infix>,
> =
R extends MultiSearchRequestsWithUnionSchema<T[number], Infix>
? UnionSearchResponse<T[number]>
: {
results: { [Index in keyof T]: SearchResponse<T[Index]> } & {
length: T["length"];
};
};
export interface MultiSearchUnionStreamConfig<T extends DocumentSchema>
extends BaseStreamConfig {
onComplete?: (data: UnionSearchResponse<T>) => void;
}
export interface MultiSearchResultsStreamConfig<T extends DocumentSchema[]>
extends BaseStreamConfig {
onComplete?: (data: {
results: { [Index in keyof T]: SearchResponse<T[Index]> } & {
length: T["length"];
};
}) => void;
}
interface SearchesMultiSearchesRequestSchema<
T extends DocumentSchema,
Infix extends string,
> {
searches: (
| MultiSearchRequestSchema<T, Infix>
| MultiSearchRequestWithPresetSchema<T, Infix>
)[];
}
interface BaseMultiSearchRequestSchema {
collection?: string;
rerank_hybrid_matches?: boolean;
"x-typesense-api-key"?: string;
}
type CommonMultiSearchParametersBase<
T extends DocumentSchema,
Infix extends string,
> = Partial<
BaseMultiSearchRequestSchema & Omit<SearchParams<T, Infix>, "streamConfig">
>;
export type MultiSearchRequestSchema<
T extends DocumentSchema,
Infix extends string,
> = BaseMultiSearchRequestSchema & Omit<SearchParams<T, Infix>, "streamConfig">;
export type MultiSearchRequestWithPresetSchema<
T extends DocumentSchema,
Infix extends string,
> = BaseMultiSearchRequestSchema &
Omit<SearchParamsWithPreset<T, Infix>, "streamConfig">;
export type MultiSearchUnionParameters<
T extends DocumentSchema,
Infix extends string,
> = CommonMultiSearchParametersBase<T, Infix> & {
streamConfig?: MultiSearchUnionStreamConfig<T>;
use_cache?: boolean;
};
export type MultiSearchResultsParameters<
T extends DocumentSchema[],
Infix extends string,
> = CommonMultiSearchParametersBase<T[number], Infix> & {
streamConfig?: MultiSearchResultsStreamConfig<T>;
use_cache?: boolean;
};
type Whitespace = " " | "\n" | "\t";
type TrimString<S extends string> = S extends `${Whitespace}${infer S}`
? TrimString<S>
: S extends `${infer S}${Whitespace}`
? TrimString<S>
: S;