Core: add initOrama option to static client

This commit is contained in:
Fuma Nama
2025-02-19 16:33:00 +08:00
parent 78af2292e9
commit d95c21fc78
4 changed files with 32 additions and 23 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-core': patch
---
add `initOrama` option to static client
+7 -18
View File
@@ -1,10 +1,4 @@
import {
type AnyOrama,
create,
load,
type Orama,
type RawData,
} from '@orama/orama';
import { type AnyOrama, create, load, type Orama } from '@orama/orama';
import { type SortedResult } from '@/server';
import { searchSimple } from '@/search/orama/search/simple';
import { searchAdvanced } from '@/search/orama/search/advanced';
@@ -12,12 +6,15 @@ import {
type advancedSchema,
type simpleSchema,
} from '@/search/orama/create-db';
import type { ExportedData } from '@/search/server';
export interface StaticOptions {
/**
* Where to download exported search indexes (URL)
*/
from?: string;
initOrama?: (locale?: string) => Promise<Orama<unknown, never, never, never>>;
}
export interface StaticClient {
@@ -28,17 +25,9 @@ export interface StaticClient {
) => Promise<SortedResult[]>;
}
type Type = 'simple' | 'advanced';
type ExportedData =
| (RawData & { type: Type })
| {
type: 'i18n';
data: Record<string, RawData & { type: Type }>;
};
export function createStaticClient({
from = '/api/search',
initOrama = (locale) => create({ schema: { _: 'string' }, language: locale }),
}: StaticOptions): StaticClient {
// locale -> db
const dbs = new Map<
@@ -61,7 +50,7 @@ export function createStaticClient({
if (data.type === 'i18n') {
for (const [k, v] of Object.entries(data.data)) {
const db = await create({ schema: { _: 'string' } });
const db = await initOrama(k);
await load(db, v);
dbs.set(k, {
@@ -70,7 +59,7 @@ export function createStaticClient({
});
}
} else {
const db = await create({ schema: { _: 'string' } });
const db = await initOrama();
await load(db, data);
dbs.set('', {
@@ -138,7 +138,7 @@ export function createI18nSearchAPI<T extends 'simple' | 'advanced'>(
return {
type: 'i18n',
data: Object.fromEntries(await Promise.all(entries)) as object,
data: Object.fromEntries(await Promise.all(entries)),
};
},
async search(query, searchOptions) {
+19 -4
View File
@@ -1,4 +1,10 @@
import { type Orama, type SearchParams, save, create } from '@orama/orama';
import {
type Orama,
type SearchParams,
save,
create,
type RawData,
} from '@orama/orama';
import { type NextRequest } from 'next/server';
import type { StructuredData } from '@/mdx-plugins/remark-structure';
import type { SortedResult } from '@/server/types';
@@ -14,6 +20,15 @@ import {
import { searchSimple } from '@/search/orama/search/simple';
import { searchAdvanced } from '@/search/orama/search/advanced';
type SearchType = 'simple' | 'advanced';
export type ExportedData =
| (RawData & { type: SearchType })
| {
type: 'i18n';
data: Record<string, RawData & { type: SearchType }>;
};
export interface SearchServer {
search: (
query: string,
@@ -25,7 +40,7 @@ export interface SearchServer {
*
* You can reference the exported database to implement client-side search
*/
export: () => Promise<unknown>;
export: () => Promise<ExportedData>;
}
export interface SearchAPI extends SearchServer {
@@ -69,7 +84,7 @@ export interface AdvancedOptions extends SharedOptions {
>;
}
export function createSearchAPI<T extends 'simple' | 'advanced'>(
export function createSearchAPI<T extends SearchType>(
type: T,
options: T extends 'simple' ? SimpleOptions : AdvancedOptions,
): SearchAPI {
@@ -95,7 +110,7 @@ export function initSimpleSearch(options: SimpleOptions): SearchServer {
async export() {
return {
type: 'simple',
...save(await doc),
...(await save(await doc)),
};
},
async search(query) {