diff --git a/packages/supersearch/src/lib/types/json.ts b/packages/supersearch/src/lib/types/json.ts new file mode 100644 index 000000000..467d6c263 --- /dev/null +++ b/packages/supersearch/src/lib/types/json.ts @@ -0,0 +1,8 @@ +type JSONPrimitive = string | number | boolean | null | undefined; + +export type JSONValue = + | JSONPrimitive + | JSONValue[] + | { + [key: string]: JSONValue; + }; diff --git a/packages/supersearch/src/lib/types/superSearch.ts b/packages/supersearch/src/lib/types/superSearch.ts index b5ddbe528..a0bd89bf8 100644 --- a/packages/supersearch/src/lib/types/superSearch.ts +++ b/packages/supersearch/src/lib/types/superSearch.ts @@ -1,18 +1,8 @@ +import type { JSONValue } from './json.js'; + export type QueryFunction = (value: string) => URLSearchParams; export type PaginationQueryFunction = ( searchParams: URLSearchParams, - data: QueryResponse + data: JSONValue ) => URLSearchParams | undefined; -export type TransformFunction = (data: unknown) => unknown; - -export interface ResultItem { - '@id'?: string; - heading: string; -} - -export interface QueryResponse { - '@id'?: string; - context?: string; - items: ResultItem[]; - totalItems: number; -} +export type TransformFunction = (data: JSONValue) => JSONValue; diff --git a/packages/supersearch/src/lib/utils/useSearchRequest.svelte.ts b/packages/supersearch/src/lib/utils/useSearchRequest.svelte.ts index 0e21e845d..ec88ab085 100644 --- a/packages/supersearch/src/lib/utils/useSearchRequest.svelte.ts +++ b/packages/supersearch/src/lib/utils/useSearchRequest.svelte.ts @@ -3,6 +3,7 @@ import type { PaginationQueryFunction, TransformFunction } from '$lib/types/superSearch.js'; +import type { JSONValue } from '$lib/types/json.js'; import debounce from '$lib/utils/debounce.js'; export function useSearchRequest({ @@ -38,7 +39,7 @@ export function useSearchRequest({ const response = await fetch(`${endpoint}?${searchParams.toString()}`, { signal: controller.signal }); - const jsonResponse = await response.json(); + const jsonResponse = (await response.json()) as JSONValue; const _data = transformFn?.(jsonResponse) || jsonResponse; moreSearchParams = paginationQueryFn?.(searchParams, _data); diff --git a/packages/supersearch/src/routes/+page.svelte b/packages/supersearch/src/routes/+page.svelte index 68bc92c59..c7d420778 100644 --- a/packages/supersearch/src/routes/+page.svelte +++ b/packages/supersearch/src/routes/+page.svelte @@ -1,21 +1,33 @@