@rubixstudios/payload-typesense 1.2.4 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +55 -20
  2. package/dist/components/HeadlessSearchInput.d.ts.map +1 -1
  3. package/dist/components/HeadlessSearchInput.js +4 -3
  4. package/dist/components/index.d.ts +1 -0
  5. package/dist/components/index.d.ts.map +1 -1
  6. package/dist/components/index.js +1 -0
  7. package/dist/components/render/Header.d.ts +2 -1
  8. package/dist/components/render/Header.d.ts.map +1 -1
  9. package/dist/components/render/Header.js +1 -0
  10. package/dist/components/render/NoResults.d.ts +2 -1
  11. package/dist/components/render/NoResults.d.ts.map +1 -1
  12. package/dist/components/render/NoResults.js +1 -0
  13. package/dist/components/render/ResultError.d.ts +2 -1
  14. package/dist/components/render/ResultError.d.ts.map +1 -1
  15. package/dist/components/render/ResultError.js +1 -0
  16. package/dist/components/render/Results.d.ts +2 -1
  17. package/dist/components/render/Results.d.ts.map +1 -1
  18. package/dist/components/render/Results.js +3 -2
  19. package/dist/components/themes/utils.js +1 -1
  20. package/dist/endpoints/handler/createSearch.d.ts.map +1 -1
  21. package/dist/endpoints/handler/createSearch.js +32 -10
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +2 -6
  24. package/dist/lib/cache.d.ts +8 -2
  25. package/dist/lib/cache.d.ts.map +1 -1
  26. package/dist/lib/cache.js +60 -28
  27. package/dist/lib/client.d.ts.map +1 -1
  28. package/dist/lib/client.js +9 -16
  29. package/dist/lib/headlessSearch.d.ts +8 -3
  30. package/dist/lib/headlessSearch.d.ts.map +1 -1
  31. package/dist/lib/hooks.d.ts +2 -2
  32. package/dist/lib/hooks.d.ts.map +1 -1
  33. package/dist/lib/hooks.js +4 -3
  34. package/dist/lib/initialization.d.ts.map +1 -1
  35. package/dist/lib/initialization.js +9 -15
  36. package/dist/lib/schema-mapper.d.ts +3 -19
  37. package/dist/lib/schema-mapper.d.ts.map +1 -1
  38. package/dist/lib/schema-mapper.js +41 -9
  39. package/dist/types.d.ts +49 -2
  40. package/dist/types.d.ts.map +1 -1
  41. package/dist/utils/ensureCollection.d.ts +2 -1
  42. package/dist/utils/ensureCollection.d.ts.map +1 -1
  43. package/dist/utils/extractText.d.ts +1 -1
  44. package/dist/utils/extractText.d.ts.map +1 -1
  45. package/dist/utils/extractText.js +4 -6
  46. package/dist/utils/getAllCollections.d.ts +2 -0
  47. package/dist/utils/getAllCollections.d.ts.map +1 -1
  48. package/dist/utils/getAllCollections.js +33 -14
  49. package/dist/utils/keyboard.d.ts +1 -1
  50. package/dist/utils/keyboard.d.ts.map +1 -1
  51. package/dist/utils/keyboard.js +2 -6
  52. package/dist/utils/useSearch.d.ts +2 -1
  53. package/dist/utils/useSearch.d.ts.map +1 -1
  54. package/dist/utils/useSearch.js +25 -37
  55. package/dist/utils/vectorSearch.d.ts +7 -0
  56. package/dist/utils/vectorSearch.d.ts.map +1 -0
  57. package/dist/utils/vectorSearch.js +12 -0
  58. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"useSearch.d.ts","sourceRoot":"","sources":["../../src/utils/useSearch.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,aAAa,CAAA;AAEjD,KAAK,gBAAgB,CAAC,CAAC,IAAI;IACzB,OAAO,EAAE,MAAM,CAAA;IACf,WAAW,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;IAC/B,cAAc,EAAE,MAAM,CAAA;IACtB,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,KAAK,IAAI,CAAA;IAChD,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,KAAK,IAAI,CAAA;IAC9D,OAAO,EAAE,MAAM,CAAA;CAChB,CAAA;AAED,wBAAgB,SAAS,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,EACrD,OAAO,EACP,WAAW,EACX,cAAc,EACd,SAAS,EACT,QAAQ,EACR,OAAO,GACR,EAAE,gBAAgB,CAAC,CAAC,CAAC;;;;0BAsBE,MAAM;EA0E7B"}
1
+ {"version":3,"file":"useSearch.d.ts","sourceRoot":"","sources":["../../src/utils/useSearch.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,aAAa,CAAA;AAEjD,KAAK,gBAAgB,CAAC,CAAC,IAAI;IACzB,OAAO,EAAE,MAAM,CAAA;IACf,WAAW,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;IAC/B,cAAc,EAAE,MAAM,CAAA;IACtB,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,KAAK,IAAI,CAAA;IAChD,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,KAAK,IAAI,CAAA;IAC9D,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB,CAAA;AAED,wBAAgB,SAAS,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,EACrD,OAAO,EACP,WAAW,EACX,cAAc,EACd,SAAS,EACT,QAAQ,EACR,OAAO,EACP,MAAc,GACf,EAAE,gBAAgB,CAAC,CAAC,CAAC;;;;0BAYE,MAAM;EA4D7B"}
@@ -1,27 +1,13 @@
1
1
  'use client';
2
- import { useCallback, useEffect, useRef, useState } from 'react';
3
- export function useSearch({ baseUrl, collections, minQueryLength, onResults, onSearch, perPage }) {
2
+ import { useCallback, useRef, useState } from 'react';
3
+ export function useSearch({ baseUrl, collections, minQueryLength, onResults, onSearch, perPage, vector = false }) {
4
4
  const [results, setResults] = useState(null);
5
5
  const [isLoading, setIsLoading] = useState(false);
6
6
  const [error, setError] = useState(null);
7
- const collectionsRef = useRef(collections);
8
7
  const onResultsRef = useRef(onResults);
9
8
  const onSearchRef = useRef(onSearch);
10
- useEffect(()=>{
11
- collectionsRef.current = collections;
12
- }, [
13
- collections
14
- ]);
15
- useEffect(()=>{
16
- onResultsRef.current = onResults;
17
- }, [
18
- onResults
19
- ]);
20
- useEffect(()=>{
21
- onSearchRef.current = onSearch;
22
- }, [
23
- onSearch
24
- ]);
9
+ onResultsRef.current = onResults;
10
+ onSearchRef.current = onSearch;
25
11
  const search = useCallback(async (searchQuery)=>{
26
12
  if (searchQuery.length < minQueryLength) {
27
13
  setResults(null);
@@ -31,43 +17,45 @@ export function useSearch({ baseUrl, collections, minQueryLength, onResults, onS
31
17
  setIsLoading(true);
32
18
  setError(null);
33
19
  try {
34
- const activeCollections = collectionsRef.current;
20
+ const params = new URLSearchParams({
21
+ per_page: String(perPage),
22
+ q: searchQuery
23
+ });
24
+ if (vector) {
25
+ params.append('vector', 'true');
26
+ }
35
27
  let searchUrl;
36
- if (typeof activeCollections === 'string' && activeCollections) {
37
- searchUrl = `${baseUrl}/api/search/${activeCollections}?q=${encodeURIComponent(searchQuery)}&per_page=${perPage}`;
38
- } else if (Array.isArray(activeCollections) && activeCollections.length > 0) {
39
- searchUrl = `${baseUrl}/api/search?q=${encodeURIComponent(searchQuery)}&per_page=${perPage * 2}`;
28
+ if (Array.isArray(collections) && collections.length > 0) {
29
+ if (collections.length === 1) {
30
+ searchUrl = `${baseUrl}/api/search/${collections[0]}?${params.toString()}`;
31
+ } else {
32
+ params.append('collections', collections.join(','));
33
+ searchUrl = `${baseUrl}/api/search?${params.toString()}`;
34
+ }
40
35
  } else {
41
- searchUrl = `${baseUrl}/api/search?q=${encodeURIComponent(searchQuery)}&per_page=${perPage}`;
36
+ searchUrl = `${baseUrl}/api/search?${params.toString()}`;
42
37
  }
43
38
  const response = await fetch(searchUrl);
44
39
  if (!response.ok) {
45
40
  throw new Error(`Search failed: ${response.status} ${response.statusText}`);
46
41
  }
47
- let searchResults = await response.json();
48
- if (Array.isArray(activeCollections) && activeCollections.length > 0) {
49
- const filteredHits = searchResults.hits?.filter((hit)=>hit.collection && activeCollections.includes(hit.collection)) || [];
50
- const filteredCollections = searchResults.collections?.filter((col)=>col.collection && activeCollections.includes(col.collection)) || [];
51
- searchResults = {
52
- ...searchResults,
53
- collections: filteredCollections,
54
- found: filteredHits.length,
55
- hits: filteredHits
56
- };
57
- }
42
+ const searchResults = await response.json();
58
43
  setResults(searchResults);
59
44
  onResultsRef.current?.(searchResults);
60
45
  onSearchRef.current?.(searchQuery, searchResults);
61
46
  } catch (err) {
62
- setError(err instanceof Error ? err.message : 'Search failed');
47
+ const errorMessage = err instanceof Error ? err.message : 'Search failed';
48
+ setError(errorMessage);
63
49
  setResults(null);
64
50
  } finally{
65
51
  setIsLoading(false);
66
52
  }
67
53
  }, [
68
54
  baseUrl,
55
+ collections,
69
56
  minQueryLength,
70
- perPage
57
+ perPage,
58
+ vector
71
59
  ]);
72
60
  return {
73
61
  error,
@@ -0,0 +1,7 @@
1
+ import type Typesense from 'typesense';
2
+ export declare function performVectorSearch(typesenseClient: Typesense.Client, query: string, options: {
3
+ collection: string;
4
+ page: number;
5
+ per_page: number;
6
+ }): Promise<any>;
7
+ //# sourceMappingURL=vectorSearch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vectorSearch.d.ts","sourceRoot":"","sources":["../../src/utils/vectorSearch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,WAAW,CAAA;AAEtC,wBAAsB,mBAAmB,CACvC,eAAe,EAAE,SAAS,CAAC,MAAM,EACjC,KAAK,EAAE,MAAM,EACb,OAAO,EAAE;IACP,UAAU,EAAE,MAAM,CAAA;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;CACjB,GACA,OAAO,CAAC,GAAG,CAAC,CAad"}
@@ -0,0 +1,12 @@
1
+ export async function performVectorSearch(typesenseClient, query, options) {
2
+ const { collection, page, per_page } = options;
3
+ const searchParams = {
4
+ num_typos: 0,
5
+ page,
6
+ per_page,
7
+ q: query,
8
+ query_by: 'embedding',
9
+ search_cutoff_ms: 5000
10
+ };
11
+ return await typesenseClient.collections(collection).documents().search(searchParams);
12
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rubixstudios/payload-typesense",
3
- "version": "1.2.4",
3
+ "version": "1.3.0",
4
4
  "description": "A production-ready search plugin that integrates Typesense with Payload CMS, offering fast, typo-tolerant search with real-time synchronization. This fork by Rubix Studios reduces bloat and introduces targeted changes for improved performance, maintainability, and flexibility.",
5
5
  "license": "MIT",
6
6
  "author": "Rubix Studios <hello@rubixstudios.com.au> (https://rubixstudios.com.au)",