1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-08-31 01:30:01 +02:00

add exports to index.d.ts

This commit is contained in:
Thomas Wilkerling
2025-05-07 18:10:10 +02:00
parent 1a0bd73002
commit 650cf81d9e
2 changed files with 88 additions and 52 deletions

79
index.d.ts vendored
View File

@@ -75,7 +75,7 @@ declare module "flexsearch" {
* **Document:** * **Document:**
* * Search options: https://github.com/nextapps-de/flexsearch#search-options * * Search options: https://github.com/nextapps-de/flexsearch#search-options
*/ */
type SearchOptions<R extends boolean = true> = { export type SearchOptions<R extends boolean = true> = {
query?: string; query?: string;
limit?: number; limit?: number;
offset?: number; offset?: number;
@@ -93,8 +93,7 @@ declare module "flexsearch" {
* * Language: https://github.com/nextapps-de/flexsearch#languages * * Language: https://github.com/nextapps-de/flexsearch#languages
*/ */
global { export type EncoderOptions = {
type EncoderOptions = {
rtl?: boolean; rtl?: boolean;
dedupe?: boolean; dedupe?: boolean;
include?: EncoderSplitOptions; include?: EncoderSplitOptions;
@@ -112,9 +111,9 @@ declare module "flexsearch" {
minlength?: number; minlength?: number;
maxlength?: number; maxlength?: number;
cache?: boolean | number; cache?: boolean | number;
}} }
type EncoderSplitOptions = { export type EncoderSplitOptions = {
letter?: boolean; letter?: boolean;
number?: boolean; number?: boolean;
symbol?: boolean; symbol?: boolean;
@@ -154,7 +153,7 @@ declare module "flexsearch" {
* * Right-To-Left: https://github.com/nextapps-de/flexsearch/doc/encoder.md#right-to-left-support * * Right-To-Left: https://github.com/nextapps-de/flexsearch/doc/encoder.md#right-to-left-support
* * Language: https://github.com/nextapps-de/flexsearch/doc/encoder.md#built-in-language-packs * * Language: https://github.com/nextapps-de/flexsearch/doc/encoder.md#built-in-language-packs
*/ */
type IndexOptions< export type IndexOptions<
S extends StorageInterface = undefined, S extends StorageInterface = undefined,
R extends boolean = true R extends boolean = true
> = { > = {
@@ -189,9 +188,9 @@ declare module "flexsearch" {
/* Index Search */ /* Index Search */
/************************************/ /************************************/
type DefaultSearchResults = Id[]; export type DefaultSearchResults = Id[];
type IntermediateSearchResults = Array<Id[]>; export type IntermediateSearchResults = Array<Id[]>;
type SearchResults<R extends boolean = true> = R extends true ? DefaultSearchResults : Resolver; export type SearchResults<R extends boolean = true> = R extends true ? DefaultSearchResults : Resolver;
/** /**
* Basic usage and variants: https://github.com/nextapps-de/flexsearch#basic-usage-and-variants \ * Basic usage and variants: https://github.com/nextapps-de/flexsearch#basic-usage-and-variants \
@@ -199,7 +198,7 @@ declare module "flexsearch" {
* Usage: https://github.com/nextapps-de/flexsearch#usage * Usage: https://github.com/nextapps-de/flexsearch#usage
*/ */
type IndexSearchResultsWrapper< export type IndexSearchResultsWrapper<
W extends boolean = false, W extends boolean = false,
S extends StorageInterface = undefined, S extends StorageInterface = undefined,
R extends boolean = true R extends boolean = true
@@ -327,7 +326,7 @@ declare module "flexsearch" {
type WorkerConfigPath = string; type WorkerConfigPath = string;
type WorkerType = boolean | WorkerURL | WorkerPath; type WorkerType = boolean | WorkerURL | WorkerPath;
type WorkerIndexOptions = IndexOptions & { export type WorkerIndexOptions = IndexOptions & {
config?: WorkerConfigURL | WorkerConfigPath, config?: WorkerConfigURL | WorkerConfigPath,
export?: () => Promise<void>; export?: () => Promise<void>;
import?: () => Promise<void>; import?: () => Promise<void>;
@@ -351,9 +350,9 @@ declare module "flexsearch" {
/** /**
* The template to be applied on matches (e.g. <code>"\<b>$1\</b>"</code>), where <code>\$1</code> is a placeholder for the matched partial * The template to be applied on matches (e.g. <code>"\<b>$1\</b>"</code>), where <code>\$1</code> is a placeholder for the matched partial
*/ */
type TemplateResultHighlighting = string; export type TemplateResultHighlighting = string;
type TagName = string; type TagName = string;
type FieldName<D = DocumentData> = D extends object export type FieldName<D = DocumentData> = D extends object
? { ? {
[K in keyof D]: K extends string [K in keyof D]: K extends string
? D[K] extends Array<infer U> ? D[K] extends Array<infer U>
@@ -363,7 +362,7 @@ declare module "flexsearch" {
}[keyof D] }[keyof D]
: never; : never;
type FieldOptions<D extends DocumentData = DocumentData> = IndexOptions & { export type FieldOptions<D extends DocumentData = DocumentData> = IndexOptions & {
field: FieldName<D>, field: FieldName<D>,
filter?: (content: string) => boolean; filter?: (content: string) => boolean;
custom?: (content: string) => string | boolean; custom?: (content: string) => string | boolean;
@@ -371,14 +370,14 @@ declare module "flexsearch" {
db?: StorageInterface; db?: StorageInterface;
}; };
type TagOptions<D extends DocumentData> = { export type TagOptions<D extends DocumentData> = {
field: FieldName<D>; field: FieldName<D>;
filter?: (content: string) => boolean; filter?: (content: string) => boolean;
custom?: (content: string) => string | boolean; custom?: (content: string) => string | boolean;
db?: StorageInterface; db?: StorageInterface;
}; };
type StoreOptions = { export type StoreOptions = {
field: FieldName; field: FieldName;
filter?: (content: string) => boolean; filter?: (content: string) => boolean;
custom?: (content: string) => string | boolean; custom?: (content: string) => string | boolean;
@@ -400,7 +399,7 @@ declare module "flexsearch" {
* * Document options: https://github.com/nextapps-de/flexsearch#document-options * * Document options: https://github.com/nextapps-de/flexsearch#document-options
*/ */
type DocumentOptions< export type DocumentOptions<
D extends DocumentData = DocumentData, D extends DocumentData = DocumentData,
W extends WorkerType = false, W extends WorkerType = false,
S extends StorageInterface = undefined S extends StorageInterface = undefined
@@ -414,7 +413,7 @@ declare module "flexsearch" {
* **Document:** * **Document:**
* * The document descriptor: https://github.com/nextapps-de/flexsearch#the-document-descriptor * * The document descriptor: https://github.com/nextapps-de/flexsearch#the-document-descriptor
*/ */
type DocumentDescriptor<D extends DocumentData = DocumentData> = { export type DocumentDescriptor<D extends DocumentData = DocumentData> = {
id?: string | "id"; id?: string | "id";
field?: FieldName<D> | FieldName<D>[] | FieldOptions<D> | Array<FieldOptions<D>>; field?: FieldName<D> | FieldName<D>[] | FieldOptions<D> | Array<FieldOptions<D>>;
index?: FieldName<D> | FieldName<D>[] | FieldOptions<D> | Array<FieldOptions<D>>; index?: FieldName<D> | FieldName<D>[] | FieldOptions<D> | Array<FieldOptions<D>>;
@@ -422,25 +421,25 @@ declare module "flexsearch" {
store?: FieldName<D> | FieldName<D>[] | StoreOptions | Array<StoreOptions> | boolean; store?: FieldName<D> | FieldName<D>[] | StoreOptions | Array<StoreOptions> | boolean;
}; };
type DefaultDocumentSearchResults<D extends DocumentData = DocumentData> = Array<{ export type DefaultDocumentSearchResults<D extends DocumentData = DocumentData> = Array<{
field?: FieldName<D>; field?: FieldName<D>;
tag?: FieldName<D>; tag?: FieldName<D>;
result: DefaultSearchResults; result: DefaultSearchResults;
}>; }>;
type EnrichedResults<D extends DocumentData = DocumentData> = Array<{ export type EnrichedResults<D extends DocumentData = DocumentData> = Array<{
id: Id; id: Id;
doc: D | null; doc: D | null;
highlight?: string; highlight?: string;
}>; }>;
type EnrichedDocumentSearchResults<D extends DocumentData = DocumentData> = Array<{ export type EnrichedDocumentSearchResults<D extends DocumentData = DocumentData> = Array<{
field?: FieldName<D>; field?: FieldName<D>;
tag?: FieldName<D>; tag?: FieldName<D>;
result: EnrichedResults<D>; result: EnrichedResults<D>;
}>; }>;
type MergedDocumentSearchResults<D extends DocumentData = DocumentData> = Array<{ export type MergedDocumentSearchResults<D extends DocumentData = DocumentData> = Array<{
id: Id; id: Id;
doc?: D | null; doc?: D | null;
field?: FieldName[]; field?: FieldName[];
@@ -448,7 +447,7 @@ declare module "flexsearch" {
highlight?: {[field: FieldName]: string}; highlight?: {[field: FieldName]: string};
}>; }>;
type DocumentSearchResults< export type DocumentSearchResults<
D extends DocumentData = DocumentData, D extends DocumentData = DocumentData,
H extends HighlightOptions = undefined, H extends HighlightOptions = undefined,
P extends PluckOptions = undefined, P extends PluckOptions = undefined,
@@ -469,18 +468,18 @@ declare module "flexsearch" {
: DefaultDocumentSearchResults : DefaultDocumentSearchResults
: Resolver; : Resolver;
type PluckOptions< export type PluckOptions<
D extends DocumentData = DocumentData, D extends DocumentData = DocumentData,
H extends HighlightOptions = undefined, H extends HighlightOptions = undefined,
R extends boolean = true, R extends boolean = true,
E extends boolean = false E extends boolean = false
> = FieldName | DocumentSearchOptions<D, H, undefined, R, E>; > = FieldName<D> | DocumentSearchOptions<D, H, undefined, R, E>;
/** /**
* **Document:** * **Document:**
* * Document search options: https://github.com/nextapps-de/flexsearch#document-search-options * * Document search options: https://github.com/nextapps-de/flexsearch#document-search-options
*/ */
type DocumentSearchOptions< export type DocumentSearchOptions<
D extends DocumentData = DocumentData, D extends DocumentData = DocumentData,
H extends HighlightOptions = undefined, H extends HighlightOptions = undefined,
P extends PluckOptions = undefined, P extends PluckOptions = undefined,
@@ -497,18 +496,18 @@ declare module "flexsearch" {
merge?: M; merge?: M;
}; };
type DocumentValue = export type DocumentValue =
| string | string
| number | number
| boolean | boolean
| null | null
| DocumentData; | DocumentData;
type DocumentData = { export type DocumentData = {
[key: string]: DocumentValue | DocumentValue[]; [key: string]: DocumentValue | DocumentValue[];
}; };
type DocumentSearchResultsWrapper< export type DocumentSearchResultsWrapper<
D extends DocumentData = DocumentData, D extends DocumentData = DocumentData,
W extends WorkerType = false, W extends WorkerType = false,
S extends StorageInterface = undefined, S extends StorageInterface = undefined,
@@ -579,7 +578,7 @@ declare module "flexsearch" {
M extends boolean = false M extends boolean = false
>( >(
query: string, query: string,
options?: DocumentSearchOptions<D, H, P, R, E, M>, options: DocumentSearchOptions<D, H, P, R, E, M>,
): DocumentSearchResultsWrapper<D, W, S, H, P, R, E, M>; ): DocumentSearchResultsWrapper<D, W, S, H, P, R, E, M>;
searchCache< searchCache<
H extends HighlightOptions = undefined, H extends HighlightOptions = undefined,
@@ -589,7 +588,7 @@ declare module "flexsearch" {
M extends boolean = false M extends boolean = false
>( >(
query: string, query: string,
options?: DocumentSearchOptions<D, H, P, R, E, M>, options: DocumentSearchOptions<D, H, P, R, E, M>,
): DocumentSearchResultsWrapper<D, W, S, H, P, R, E, M>; ): DocumentSearchResultsWrapper<D, W, S, H, P, R, E, M>;
/** @deprecated Pass "limit" within options */ /** @deprecated Pass "limit" within options */
@@ -792,7 +791,7 @@ declare module "flexsearch" {
db?: any; db?: any;
}; };
type DefaultResolve< export type DefaultResolve<
D extends DocumentData = DocumentData, D extends DocumentData = DocumentData,
H extends HighlightOptions = undefined, H extends HighlightOptions = undefined,
E extends boolean = false E extends boolean = false
@@ -805,7 +804,7 @@ declare module "flexsearch" {
highlight?: D extends DocumentData ? H : undefined; highlight?: D extends DocumentData ? H : undefined;
}; };
type ResolverOptions< export type ResolverOptions<
D extends DocumentData = DocumentData, D extends DocumentData = DocumentData,
W extends WorkerType = false, W extends WorkerType = false,
S extends StorageInterface = undefined, S extends StorageInterface = undefined,
@@ -827,18 +826,18 @@ declare module "flexsearch" {
resolve?: R; resolve?: R;
}; };
type HighlightBoundaryOptions = { export type HighlightBoundaryOptions = {
before?: number; before?: number;
after?: number; after?: number;
total?: number; total?: number;
}; };
type HighlightEllipsisOptions = { export type HighlightEllipsisOptions = {
template?: TemplateResultHighlighting; template?: TemplateResultHighlighting;
pattern?: string | boolean; pattern?: string | boolean;
}; };
type HighlightOptions = TemplateResultHighlighting | { export type HighlightOptions = TemplateResultHighlighting | {
template?: TemplateResultHighlighting; template?: TemplateResultHighlighting;
boundary?: HighlightBoundaryOptions | number; boundary?: HighlightBoundaryOptions | number;
ellipsis?: HighlightEllipsisOptions | string | boolean; ellipsis?: HighlightEllipsisOptions | string | boolean;
@@ -886,8 +885,7 @@ declare module "flexsearch" {
resolve(options?: DefaultResolve): SearchResults; resolve(options?: DefaultResolve): SearchResults;
} }
global { export class StorageInterface {
class StorageInterface {
db: any; db: any;
constructor(name: string, config: PersistentOptions); constructor(name: string, config: PersistentOptions);
@@ -902,7 +900,7 @@ declare module "flexsearch" {
destroy(): Promise<void>; destroy(): Promise<void>;
clear(): Promise<void>; clear(): Promise<void>;
}} }
export class IndexedDB extends StorageInterface { export class IndexedDB extends StorageInterface {
db: IDBDatabase; db: IDBDatabase;
@@ -924,10 +922,12 @@ declare module "flexsearch" {
// ----------------------------------- // -----------------------------------
declare module "flexsearch/db/*" { declare module "flexsearch/db/*" {
import { StorageInterface } from "flexsearch";
export default StorageInterface; export default StorageInterface;
} }
declare module "flexsearch/db/indexeddb" { declare module "flexsearch/db/indexeddb" {
import { StorageInterface } from "flexsearch";
export default class IndexedDB extends StorageInterface{ export default class IndexedDB extends StorageInterface{
db: IDBDatabase db: IDBDatabase
} }
@@ -936,6 +936,7 @@ declare module "flexsearch/db/indexeddb" {
// ----------------------------------- // -----------------------------------
declare module "flexsearch/lang/*" { declare module "flexsearch/lang/*" {
import { EncoderOptions } from "flexsearch";
const Options: EncoderOptions; const Options: EncoderOptions;
export default Options; export default Options;
} }

View File

@@ -5,26 +5,29 @@ import {
Worker, Worker,
Resolver, Resolver,
StorageInterface, StorageInterface,
DefaultSearchResults,
} from "flexsearch"; } from "flexsearch";
import "../index";
async function test() { async function test() {
const document = new Document<{ const document = new Document<{
title: string id: number,
description: string title: string,
tags: { description: string,
name: string tags: string[]
id: number
}[]
}>({ }>({
document: { document: {
index: [ "tags" ], id: "id",
index: [ "title", "description" ],
tag: [ "tags" ],
store: [ "title", "description" ]
}, },
}); });
// The correct type // The correct type
const doc1 = await document.searchAsync({}); const doc1 = await document.searchAsync({});
const doc2: Resolver = await document.searchAsync({ const doc2: Resolver = await document.searchAsync({
resolve: true, resolve: false,
}); });
const doc3 = await document.searchAsync({ const doc3 = await document.searchAsync({
enrich: true, enrich: true,
@@ -34,14 +37,46 @@ async function test() {
merge: true, merge: true,
}); });
doc4[0].doc.title; doc4[0].doc.title;
// The wrong type // The wrong type
const t_1_1: DefaultSearchResults = document.search("test", { pluck: "title" });
// @ts-expect-error // @ts-expect-error
const docw1: Resolver = await document.searchAsync({}); const t_1_2: DefaultSearchResults = document.search("test", {});
const t_2_1: DefaultDocumentSearchResults = document.search("test", {});
// @ts-expect-error // @ts-expect-error
const docw2: DefaultDocumentSearchResults = await document.searchAsync({ const t_2_2: DefaultSearchResults = document.search("test", {});
enrich: true,
}); const t_3_1: DefaultDocumentSearchResults = document.search({});
const t_3_2: DefaultDocumentSearchResults = document.search({ resolve: true });
const t_4_1: Resolver = document.search({ resolve: false });
const t_4_2: Resolver = new Resolver();
// @ts-expect-error
const t_4_3: Resolver = document.search({});
// @ts-expect-error
const t_4_4: Resolver = document.search({ resolve: true });
// @ts-expect-error
const docw6: DefaultDocumentSearchResults = await document.searchAsync({});
// @ts-expect-error
const docw7: DefaultDocumentSearchResults = await document.searchAsync({ pluck: false });
// @ts-expect-error
const docw8: DefaultDocumentSearchResults = await document.searchAsync({ enrich: false });
// @ts-expect-error
const docw4: Resolver = await document.searchAsync({});
// @ts-expect-error
const docw5: Resolver = await document.searchAsync({ resolve: true });
// @ts-expect-error
const docw6: DefaultDocumentSearchResults = await document.searchAsync({});
// @ts-expect-error
const docw7: DefaultDocumentSearchResults = await document.searchAsync({ pluck: false });
// @ts-expect-error
const docw8: DefaultDocumentSearchResults = await document.searchAsync({ enrich: false });
// Promise? // Promise?
const documentNoWorker = new Document({}); const documentNoWorker = new Document({});
const doc5 = documentNoWorker.search({}); // No Promise const doc5 = documentNoWorker.search({}); // No Promise