@rubixstudios/payload-typesense 1.6.13 → 1.8.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.
- package/README.md +109 -114
- package/dist/components/ThemeProvider.d.ts +4 -3
- package/dist/components/ThemeProvider.d.ts.map +1 -1
- package/dist/components/ThemeProvider.js +15 -5
- package/dist/components/icons/clockIcon.d.ts +2 -0
- package/dist/components/icons/clockIcon.d.ts.map +1 -0
- package/dist/components/icons/clockIcon.js +16 -0
- package/dist/components/icons/collectionIcon.d.ts +2 -0
- package/dist/components/icons/collectionIcon.d.ts.map +1 -0
- package/dist/components/icons/collectionIcon.js +16 -0
- package/dist/components/icons/errorIcon.d.ts +2 -0
- package/dist/components/icons/errorIcon.d.ts.map +1 -0
- package/dist/components/icons/errorIcon.js +16 -0
- package/dist/components/icons/index.d.ts +7 -0
- package/dist/components/icons/index.d.ts.map +1 -0
- package/dist/components/icons/index.js +6 -0
- package/dist/components/icons/noResultsIcon.d.ts +4 -0
- package/dist/components/icons/noResultsIcon.d.ts.map +1 -0
- package/dist/components/icons/noResultsIcon.js +19 -0
- package/dist/components/icons/searchIcon.d.ts +4 -0
- package/dist/components/icons/searchIcon.d.ts.map +1 -0
- package/dist/components/icons/searchIcon.js +16 -0
- package/dist/components/icons/xIcon.d.ts +2 -0
- package/dist/components/icons/xIcon.d.ts.map +1 -0
- package/dist/components/icons/xIcon.js +17 -0
- package/dist/components/index.d.ts +10 -5
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/index.js +6 -3
- package/dist/components/render/Content.d.ts +26 -0
- package/dist/components/render/Content.d.ts.map +1 -0
- package/dist/components/render/Content.js +144 -0
- package/dist/components/render/Dialog.d.ts +20 -0
- package/dist/components/render/Dialog.d.ts.map +1 -0
- package/dist/components/render/Dialog.js +43 -0
- package/dist/components/render/Header.d.ts +4 -4
- package/dist/components/render/Header.d.ts.map +1 -1
- package/dist/components/render/Header.js +20 -39
- package/dist/components/render/Highlight.d.ts +3 -0
- package/dist/components/render/Highlight.d.ts.map +1 -0
- package/dist/components/render/Highlight.js +22 -0
- package/dist/components/render/NoResults.d.ts +4 -4
- package/dist/components/render/NoResults.d.ts.map +1 -1
- package/dist/components/render/NoResults.js +21 -45
- package/dist/components/render/ResultError.d.ts +4 -4
- package/dist/components/render/ResultError.d.ts.map +1 -1
- package/dist/components/render/ResultError.js +14 -38
- package/dist/components/render/Results.d.ts +7 -6
- package/dist/components/render/Results.d.ts.map +1 -1
- package/dist/components/render/Results.js +70 -141
- package/dist/components/render/index.d.ts +7 -4
- package/dist/components/render/index.d.ts.map +1 -1
- package/dist/components/render/index.js +7 -4
- package/dist/components/search/HeadlessDialog.d.ts +5 -0
- package/dist/components/search/HeadlessDialog.d.ts.map +1 -0
- package/dist/components/search/HeadlessDialog.js +254 -0
- package/dist/components/search/HeadlessInput.d.ts +5 -0
- package/dist/components/search/HeadlessInput.d.ts.map +1 -0
- package/dist/components/search/HeadlessInput.js +193 -0
- package/dist/components/search/SearchStyles.d.ts +8 -0
- package/dist/components/search/SearchStyles.d.ts.map +1 -0
- package/dist/components/search/SearchStyles.js +9 -0
- package/dist/components/themes/constant.d.ts +3 -0
- package/dist/components/themes/constant.d.ts.map +1 -0
- package/dist/components/themes/constant.js +75 -0
- package/dist/components/themes/hooks.d.ts +4 -42
- package/dist/components/themes/hooks.d.ts.map +1 -1
- package/dist/components/themes/hooks.js +35 -95
- package/dist/components/themes/index.d.ts +1 -0
- package/dist/components/themes/index.d.ts.map +1 -1
- package/dist/components/themes/index.js +1 -0
- package/dist/components/themes/themes.d.ts.map +1 -1
- package/dist/components/themes/themes.js +66 -27
- package/dist/components/themes/types.d.ts +62 -2
- package/dist/components/themes/types.d.ts.map +1 -1
- package/dist/components/themes/utils.d.ts +13 -18
- package/dist/components/themes/utils.d.ts.map +1 -1
- package/dist/components/themes/utils.js +466 -344
- package/dist/components/utils/decodeEntities.d.ts +2 -0
- package/dist/components/utils/decodeEntities.d.ts.map +1 -0
- package/dist/components/utils/decodeEntities.js +23 -0
- package/dist/components/utils/index.d.ts +4 -0
- package/dist/components/utils/index.d.ts.map +1 -0
- package/dist/components/utils/index.js +3 -0
- package/dist/components/utils/toKebab.d.ts +2 -0
- package/dist/components/utils/toKebab.d.ts.map +1 -0
- package/dist/components/utils/toKebab.js +3 -0
- package/dist/components/utils/toText.d.ts +2 -0
- package/dist/components/utils/toText.d.ts.map +1 -0
- package/dist/components/utils/toText.js +3 -0
- package/dist/endpoints/handler/createAdvancedSearch.d.ts +1 -1
- package/dist/endpoints/handler/createAdvancedSearch.d.ts.map +1 -1
- package/dist/endpoints/handler/createAdvancedSearch.js +133 -11
- package/dist/endpoints/handler/createCollections.d.ts +1 -1
- package/dist/endpoints/handler/createCollections.d.ts.map +1 -1
- package/dist/endpoints/handler/createCollections.js +8 -8
- package/dist/endpoints/handler/createSearch.d.ts +1 -1
- package/dist/endpoints/handler/createSearch.d.ts.map +1 -1
- package/dist/endpoints/handler/createSearch.js +85 -110
- package/dist/endpoints/handler/createSuggest.d.ts +1 -1
- package/dist/endpoints/handler/createSuggest.d.ts.map +1 -1
- package/dist/endpoints/handler/createSuggest.js +39 -22
- package/dist/endpoints/health.d.ts +2 -2
- package/dist/endpoints/health.d.ts.map +1 -1
- package/dist/endpoints/health.js +58 -52
- package/dist/endpoints/search.d.ts +2 -9
- package/dist/endpoints/search.d.ts.map +1 -1
- package/dist/endpoints/search.js +17 -16
- package/dist/{utils → hooks}/useDebounce.d.ts.map +1 -1
- package/dist/{utils → hooks}/useDebounce.js +2 -2
- package/dist/hooks/useSearch.d.ts +21 -0
- package/dist/hooks/useSearch.d.ts.map +1 -0
- package/dist/hooks/useSearch.js +126 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +45 -34
- package/dist/lib/cache.d.ts.map +1 -1
- package/dist/lib/cache.js +54 -25
- package/dist/lib/client.d.ts +1 -1
- package/dist/lib/client.d.ts.map +1 -1
- package/dist/lib/client.js +35 -31
- package/dist/lib/hooks.d.ts +3 -11
- package/dist/lib/hooks.d.ts.map +1 -1
- package/dist/lib/hooks.js +15 -45
- package/dist/lib/initialization.d.ts +1 -1
- package/dist/lib/initialization.d.ts.map +1 -1
- package/dist/lib/initialization.js +54 -52
- package/dist/lib/schema-mapper.d.ts +3 -6
- package/dist/lib/schema-mapper.d.ts.map +1 -1
- package/dist/lib/schema-mapper.js +90 -84
- package/dist/lib/search-dialog.d.ts +169 -0
- package/dist/lib/search-dialog.d.ts.map +1 -0
- package/dist/lib/search-dialog.js +4 -0
- package/dist/lib/search-input.d.ts +93 -0
- package/dist/lib/search-input.d.ts.map +1 -0
- package/dist/lib/search-pagination.d.ts +38 -0
- package/dist/lib/search-pagination.d.ts.map +1 -0
- package/dist/lib/search-pagination.js +4 -0
- package/dist/lib/search.d.ts +25 -0
- package/dist/lib/search.d.ts.map +1 -0
- package/dist/lib/search.js +59 -0
- package/dist/lib/validation.d.ts +57 -39
- package/dist/lib/validation.d.ts.map +1 -1
- package/dist/lib/validation.js +92 -66
- package/dist/types.d.ts +193 -127
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/dist/utils/buildError.d.ts +1 -1
- package/dist/utils/buildError.d.ts.map +1 -1
- package/dist/utils/buildError.js +2 -2
- package/dist/utils/ensureCollection.d.ts +2 -2
- package/dist/utils/ensureCollection.d.ts.map +1 -1
- package/dist/utils/ensureCollection.js +138 -22
- package/dist/utils/extractText.d.ts.map +1 -1
- package/dist/utils/extractText.js +11 -8
- package/dist/utils/getCacheStats.d.ts.map +1 -1
- package/dist/utils/getCacheStats.js +1 -8
- package/dist/utils/getCollectionInfo.d.ts.map +1 -1
- package/dist/utils/getCollectionInfo.js +1 -1
- package/dist/utils/getCollections.d.ts +11 -0
- package/dist/utils/getCollections.d.ts.map +1 -0
- package/dist/utils/getCollections.js +152 -0
- package/dist/utils/keyboard.d.ts +4 -4
- package/dist/utils/keyboard.d.ts.map +1 -1
- package/dist/utils/keyboard.js +21 -23
- package/dist/utils/sortResults.d.ts +3 -0
- package/dist/utils/sortResults.d.ts.map +1 -0
- package/dist/utils/sortResults.js +103 -0
- package/dist/utils/testConnection.d.ts +1 -1
- package/dist/utils/testConnection.d.ts.map +1 -1
- package/dist/utils/testConnection.js +2 -2
- package/dist/utils/vectorSearch.d.ts +2 -5
- package/dist/utils/vectorSearch.d.ts.map +1 -1
- package/dist/utils/vectorSearch.js +24 -12
- package/package.json +16 -15
- package/dist/components/HeadlessSearchInput.d.ts +0 -5
- package/dist/components/HeadlessSearchInput.d.ts.map +0 -1
- package/dist/components/HeadlessSearchInput.js +0 -204
- package/dist/lib/headlessSearch.d.ts +0 -86
- package/dist/lib/headlessSearch.d.ts.map +0 -1
- package/dist/utils/getAllCollections.d.ts +0 -11
- package/dist/utils/getAllCollections.d.ts.map +0 -1
- package/dist/utils/getAllCollections.js +0 -103
- package/dist/utils/useSearch.d.ts +0 -18
- package/dist/utils/useSearch.d.ts.map +0 -1
- package/dist/utils/useSearch.js +0 -66
- /package/dist/{utils → hooks}/useDebounce.d.ts +0 -0
- /package/dist/lib/{headlessSearch.js → search-input.js} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decodeEntities.d.ts","sourceRoot":"","sources":["../../../src/components/utils/decodeEntities.ts"],"names":[],"mappings":"AA0BA,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEpD"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const entities = {
|
|
2
|
+
amp: '&',
|
|
3
|
+
lt: '<',
|
|
4
|
+
gt: '>',
|
|
5
|
+
quot: '"',
|
|
6
|
+
apos: "'",
|
|
7
|
+
'#39': "'"
|
|
8
|
+
};
|
|
9
|
+
const pattern = /&(amp|lt|gt|quot|apos|#39|#\d+|#x[\da-f]+);/gi;
|
|
10
|
+
function decodeNumeric(name) {
|
|
11
|
+
const hex = name[1]?.toLowerCase() === 'x';
|
|
12
|
+
const point = Number.parseInt(name.slice(hex ? 2 : 1), hex ? 16 : 10);
|
|
13
|
+
if (!Number.isInteger(point) || point <= 0 || point > 0x10ffff) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
return String.fromCodePoint(point);
|
|
17
|
+
}
|
|
18
|
+
function decodeEntity(entity, name) {
|
|
19
|
+
return entities[name.toLowerCase()] ?? decodeNumeric(name) ?? entity;
|
|
20
|
+
}
|
|
21
|
+
export function decodeEntities(value) {
|
|
22
|
+
return value.replace(pattern, decodeEntity);
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA;AACnC,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toKebab.d.ts","sourceRoot":"","sources":["../../../src/components/utils/toKebab.ts"],"names":[],"mappings":"AAAA,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE3C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toText.d.ts","sourceRoot":"","sources":["../../../src/components/utils/toText.ts"],"names":[],"mappings":"AAAA,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAE7C"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type PayloadHandler } from 'payload';
|
|
2
2
|
import type Typesense from 'typesense';
|
|
3
3
|
import { type TypesenseConfig } from '../../types.js';
|
|
4
|
-
export declare const createAdvancedSearch: (
|
|
4
|
+
export declare const createAdvancedSearch: (client: Typesense.Client, config: TypesenseConfig) => PayloadHandler;
|
|
5
5
|
//# sourceMappingURL=createAdvancedSearch.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAdvancedSearch.d.ts","sourceRoot":"","sources":["../../../src/endpoints/handler/createAdvancedSearch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAuB,MAAM,SAAS,CAAA;AAClE,OAAO,KAAK,SAAS,MAAM,WAAW,CAAA;
|
|
1
|
+
{"version":3,"file":"createAdvancedSearch.d.ts","sourceRoot":"","sources":["../../../src/endpoints/handler/createAdvancedSearch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAuB,MAAM,SAAS,CAAA;AAClE,OAAO,KAAK,SAAS,MAAM,WAAW,CAAA;AAItC,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAErD,eAAO,MAAM,oBAAoB,WACvB,SAAS,CAAC,MAAM,UAChB,eAAe,KACtB,cAsJF,CAAA"}
|
|
@@ -1,34 +1,156 @@
|
|
|
1
|
-
|
|
1
|
+
import { buildParameters, getCollectionName, getSearchMode } from '../../lib/search.js';
|
|
2
|
+
import { getErrors, validateParams } from '../../lib/validation.js';
|
|
3
|
+
export const createAdvancedSearch = (client, config)=>{
|
|
2
4
|
return async (request)=>{
|
|
3
5
|
if (!request.url) {
|
|
4
6
|
return Response.json({
|
|
5
|
-
error: '
|
|
7
|
+
error: 'Request URL missing'
|
|
6
8
|
}, {
|
|
7
9
|
status: 400
|
|
8
10
|
});
|
|
9
11
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
let url;
|
|
13
|
+
try {
|
|
14
|
+
url = new URL(request.url);
|
|
15
|
+
} catch {
|
|
16
|
+
return Response.json({
|
|
17
|
+
error: 'Invalid request URL'
|
|
18
|
+
}, {
|
|
19
|
+
status: 400
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
let collection;
|
|
23
|
+
try {
|
|
24
|
+
collection = getCollectionName(url);
|
|
25
|
+
} catch {
|
|
26
|
+
return Response.json({
|
|
27
|
+
error: 'Invalid collection name'
|
|
28
|
+
}, {
|
|
29
|
+
status: 400
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
const settings = collection ? config.collections?.[collection] : undefined;
|
|
33
|
+
if (!collection || !settings?.enabled) {
|
|
15
34
|
return Response.json({
|
|
16
35
|
error: 'Collection not enabled for search'
|
|
17
36
|
}, {
|
|
18
37
|
status: 400
|
|
19
38
|
});
|
|
20
39
|
}
|
|
21
|
-
if (!request.
|
|
40
|
+
if (request.data == null && !request.body) {
|
|
22
41
|
return Response.json({
|
|
23
42
|
error: 'Missing search parameters in request body'
|
|
24
43
|
}, {
|
|
25
44
|
status: 400
|
|
26
45
|
});
|
|
27
46
|
}
|
|
28
|
-
|
|
47
|
+
let body = request.data;
|
|
48
|
+
if (body == null) {
|
|
49
|
+
if (typeof request.json !== 'function') {
|
|
50
|
+
return Response.json({
|
|
51
|
+
error: 'Unable to read request body'
|
|
52
|
+
}, {
|
|
53
|
+
status: 400
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
body = await request.json();
|
|
58
|
+
} catch {
|
|
59
|
+
return Response.json({
|
|
60
|
+
error: 'Invalid JSON request body'
|
|
61
|
+
}, {
|
|
62
|
+
status: 400
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (!body || typeof body !== 'object' || Array.isArray(body)) {
|
|
67
|
+
return Response.json({
|
|
68
|
+
error: 'Invalid search parameters'
|
|
69
|
+
}, {
|
|
70
|
+
status: 400
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
const params = body;
|
|
74
|
+
if (params.page !== undefined && (typeof params.page !== 'number' || !Number.isInteger(params.page) || params.page < 1)) {
|
|
75
|
+
return Response.json({
|
|
76
|
+
error: 'Invalid page parameter'
|
|
77
|
+
}, {
|
|
78
|
+
status: 400
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
if (params.per_page !== undefined && (typeof params.per_page !== 'number' || !Number.isInteger(params.per_page) || params.per_page < 1 || params.per_page > 250)) {
|
|
82
|
+
return Response.json({
|
|
83
|
+
error: 'Invalid per_page parameter'
|
|
84
|
+
}, {
|
|
85
|
+
status: 400
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
if (params.q !== undefined && typeof params.q !== 'string') {
|
|
89
|
+
return Response.json({
|
|
90
|
+
error: 'Invalid query parameter'
|
|
91
|
+
}, {
|
|
92
|
+
status: 400
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (params.query_by !== undefined && typeof params.query_by !== 'string' && !(Array.isArray(params.query_by) && params.query_by.every((field)=>typeof field === 'string'))) {
|
|
96
|
+
return Response.json({
|
|
97
|
+
error: 'Invalid query_by parameter'
|
|
98
|
+
}, {
|
|
99
|
+
status: 400
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
const validation = validateParams({
|
|
103
|
+
...params,
|
|
104
|
+
q: typeof params.q === 'string' ? params.q.trim() : params.q
|
|
105
|
+
});
|
|
106
|
+
if (!validation.success || !validation.data) {
|
|
107
|
+
return Response.json({
|
|
108
|
+
error: 'Invalid search parameters',
|
|
109
|
+
details: getErrors(validation.errors ?? [])
|
|
110
|
+
}, {
|
|
111
|
+
status: 400
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
getSearchMode(config, validation.data);
|
|
116
|
+
if (params.vector_query !== undefined && !config.vectorSearch?.enabled) {
|
|
117
|
+
throw new Error('Vector search requires vectorSearch.enabled');
|
|
118
|
+
}
|
|
119
|
+
} catch (error) {
|
|
120
|
+
return Response.json({
|
|
121
|
+
error: error.message
|
|
122
|
+
}, {
|
|
123
|
+
status: 400
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
const requestedFields = typeof params.query_by === 'string' ? params.query_by.split(',') : params.query_by;
|
|
127
|
+
const fields = Array.isArray(requestedFields) && requestedFields.length ? requestedFields.map((field)=>String(field).trim()).filter(Boolean) : settings.searchFields?.length ? settings.searchFields : [
|
|
128
|
+
'title',
|
|
129
|
+
'content',
|
|
130
|
+
'description'
|
|
131
|
+
];
|
|
132
|
+
const { search_mode: _mode, vector: _vector, ...overrides } = params;
|
|
133
|
+
if (getSearchMode(config, validation.data) !== 'keyword') {
|
|
134
|
+
delete overrides.query_by;
|
|
135
|
+
}
|
|
136
|
+
const searchParams = {
|
|
137
|
+
...buildParameters(config, fields, validation.data),
|
|
138
|
+
...overrides,
|
|
139
|
+
q: validation.data.q,
|
|
140
|
+
page: validation.data.page,
|
|
141
|
+
per_page: validation.data.per_page
|
|
142
|
+
};
|
|
29
143
|
try {
|
|
30
|
-
const results = await
|
|
31
|
-
|
|
144
|
+
const results = await client.collections(collection).documents().search(searchParams);
|
|
145
|
+
const response = {
|
|
146
|
+
...results,
|
|
147
|
+
hits: results.hits?.map((hit)=>({
|
|
148
|
+
...hit,
|
|
149
|
+
collection,
|
|
150
|
+
displayName: settings.displayName ?? collection
|
|
151
|
+
})) ?? []
|
|
152
|
+
};
|
|
153
|
+
return Response.json(response);
|
|
32
154
|
} catch {
|
|
33
155
|
return Response.json({
|
|
34
156
|
error: 'Advanced search failed'
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { type PayloadHandler } from 'payload';
|
|
2
2
|
import { type TypesenseConfig } from '../../types.js';
|
|
3
|
-
export declare const createCollections: (
|
|
3
|
+
export declare const createCollections: (config: TypesenseConfig) => PayloadHandler;
|
|
4
4
|
//# sourceMappingURL=createCollections.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createCollections.d.ts","sourceRoot":"","sources":["../../../src/endpoints/handler/createCollections.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,SAAS,CAAA;AAE7C,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAErD,eAAO,MAAM,iBAAiB,
|
|
1
|
+
{"version":3,"file":"createCollections.d.ts","sourceRoot":"","sources":["../../../src/endpoints/handler/createCollections.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,SAAS,CAAA;AAE7C,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAErD,eAAO,MAAM,iBAAiB,WAAY,eAAe,KAAG,cAqB3D,CAAA"}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
export const createCollections = (
|
|
1
|
+
export const createCollections = (config)=>{
|
|
2
2
|
return ()=>{
|
|
3
3
|
try {
|
|
4
|
-
const
|
|
4
|
+
const enabled = Object.entries(config.collections ?? {}).filter(([, collection])=>collection?.enabled).map(([slug, collection])=>({
|
|
5
5
|
slug,
|
|
6
|
-
displayName:
|
|
7
|
-
facetFields:
|
|
8
|
-
icon:
|
|
9
|
-
searchFields:
|
|
6
|
+
displayName: collection?.displayName || slug.charAt(0).toUpperCase() + slug.slice(1),
|
|
7
|
+
facetFields: collection?.facetFields ?? [],
|
|
8
|
+
icon: collection?.icon ?? '📄',
|
|
9
|
+
searchFields: collection?.searchFields ?? []
|
|
10
10
|
}));
|
|
11
11
|
return Response.json({
|
|
12
|
-
categorized: Boolean(
|
|
13
|
-
collections:
|
|
12
|
+
categorized: Boolean(config.settings?.categorized),
|
|
13
|
+
collections: enabled
|
|
14
14
|
});
|
|
15
15
|
} catch {
|
|
16
16
|
return Response.json({
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type PayloadHandler } from 'payload';
|
|
2
2
|
import type Typesense from 'typesense';
|
|
3
3
|
import { type TypesenseConfig } from '../../types.js';
|
|
4
|
-
export declare const createSearch: (
|
|
4
|
+
export declare const createSearch: (client: Typesense.Client, config: TypesenseConfig) => PayloadHandler;
|
|
5
5
|
//# sourceMappingURL=createSearch.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createSearch.d.ts","sourceRoot":"","sources":["../../../src/endpoints/handler/createSearch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAuB,MAAM,SAAS,CAAA;AAClE,OAAO,KAAK,SAAS,MAAM,WAAW,CAAA;
|
|
1
|
+
{"version":3,"file":"createSearch.d.ts","sourceRoot":"","sources":["../../../src/endpoints/handler/createSearch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAuB,MAAM,SAAS,CAAA;AAClE,OAAO,KAAK,SAAS,MAAM,WAAW,CAAA;AAKtC,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAGrD,eAAO,MAAM,YAAY,WAAY,SAAS,CAAC,MAAM,UAAU,eAAe,KAAG,cA2GhF,CAAA"}
|
|
@@ -1,133 +1,108 @@
|
|
|
1
1
|
import { searchCache } from '../../lib/cache.js';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
export const createSearch = (
|
|
2
|
+
import { buildParameters, getCollectionName, getSearchMode } from '../../lib/search.js';
|
|
3
|
+
import { getErrors, validateParams } from '../../lib/validation.js';
|
|
4
|
+
import { getCollections } from '../../utils/getCollections.js';
|
|
5
|
+
export const createSearch = (client, config)=>{
|
|
6
6
|
return async (request)=>{
|
|
7
|
+
if (!request.url) return Response.json({
|
|
8
|
+
error: 'Request URL missing'
|
|
9
|
+
}, {
|
|
10
|
+
status: 400
|
|
11
|
+
});
|
|
12
|
+
let url;
|
|
13
|
+
let collection;
|
|
7
14
|
try {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const url = new URL(request.url);
|
|
16
|
-
const parts = url.pathname.split('/');
|
|
17
|
-
const searchIndex = parts.indexOf('search');
|
|
18
|
-
const collectionName = searchIndex >= 0 ? parts[searchIndex + 1] : '';
|
|
19
|
-
const collection = String(collectionName || '');
|
|
20
|
-
const q = url.searchParams.get('q') || '';
|
|
21
|
-
const page = Number(url.searchParams.get('page') || 1);
|
|
22
|
-
const per_page = Number(url.searchParams.get('per_page') || 10);
|
|
23
|
-
const collectionsParam = url.searchParams.get('collections');
|
|
24
|
-
const collections = collectionsParam ? collectionsParam.split(',').filter(Boolean) : undefined;
|
|
25
|
-
const vectorParams = url.searchParams.get('vector') === 'true';
|
|
26
|
-
const vectorConfig = pluginOptions.vectorSearch?.enabled === true;
|
|
27
|
-
const vector = vectorParams && vectorConfig;
|
|
28
|
-
const sort_by = url.searchParams.has('sort_by') ? url.searchParams.get('sort_by') || '' : undefined;
|
|
29
|
-
if (Number.isNaN(page) || page < 1) {
|
|
30
|
-
return Response.json({
|
|
31
|
-
error: 'Invalid page parameter'
|
|
32
|
-
}, {
|
|
33
|
-
status: 400
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
if (Number.isNaN(per_page) || per_page < 1 || per_page > 250) {
|
|
37
|
-
return Response.json({
|
|
38
|
-
error: 'Invalid per_page parameter'
|
|
39
|
-
}, {
|
|
40
|
-
status: 400
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
const validation = validateSearchParams({
|
|
44
|
-
page,
|
|
45
|
-
per_page,
|
|
46
|
-
q,
|
|
47
|
-
sort_by
|
|
15
|
+
url = new URL(request.url);
|
|
16
|
+
collection = getCollectionName(url);
|
|
17
|
+
} catch {
|
|
18
|
+
return Response.json({
|
|
19
|
+
error: 'Invalid request URL'
|
|
20
|
+
}, {
|
|
21
|
+
status: 400
|
|
48
22
|
});
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
if (!q.trim()) {
|
|
58
|
-
return Response.json({
|
|
59
|
-
details: 'Use /api/search?q=keyword',
|
|
60
|
-
error: 'Query parameter "q" is required'
|
|
61
|
-
}, {
|
|
62
|
-
status: 400
|
|
63
|
-
});
|
|
23
|
+
}
|
|
24
|
+
const value = (key)=>url.searchParams.get(key);
|
|
25
|
+
const number = (key)=>url.searchParams.has(key) ? Number(value(key)) : undefined;
|
|
26
|
+
const boolean = (key)=>{
|
|
27
|
+
if (!url.searchParams.has(key)) {
|
|
28
|
+
return undefined;
|
|
64
29
|
}
|
|
30
|
+
return value(key) === 'true' ? true : value(key) === 'false' ? false : value(key);
|
|
31
|
+
};
|
|
32
|
+
const validation = validateParams({
|
|
33
|
+
q: value('q')?.trim() ?? '',
|
|
34
|
+
page: number('page'),
|
|
35
|
+
per_page: number('per_page'),
|
|
36
|
+
sort_by: value('sort_by') ?? undefined,
|
|
37
|
+
num_typos: number('num_typos'),
|
|
38
|
+
min_len_1typo: number('min_len_1typo'),
|
|
39
|
+
min_len_2typo: number('min_len_2typo'),
|
|
40
|
+
typo_threshold: number('typo_tokens_threshold'),
|
|
41
|
+
numerical: boolean('numerical'),
|
|
42
|
+
alpha_numerical: boolean('alpha_numerical'),
|
|
43
|
+
search_mode: value('search_mode') ?? undefined,
|
|
44
|
+
vector: boolean('vector')
|
|
45
|
+
});
|
|
46
|
+
if (!validation.success || !validation.data) {
|
|
47
|
+
return Response.json({
|
|
48
|
+
error: 'Invalid search parameters',
|
|
49
|
+
details: getErrors(validation.errors ?? [])
|
|
50
|
+
}, {
|
|
51
|
+
status: 400
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
const params = validation.data;
|
|
55
|
+
try {
|
|
56
|
+
getSearchMode(config, params);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
return Response.json({
|
|
59
|
+
error: error.message
|
|
60
|
+
}, {
|
|
61
|
+
status: 400
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
65
|
if (!collection) {
|
|
66
|
-
|
|
66
|
+
const collections = value('collections')?.split(',').map((name)=>name.trim()).filter(Boolean);
|
|
67
|
+
return getCollections(client, config, params.q, {
|
|
68
|
+
...params,
|
|
67
69
|
collections,
|
|
68
|
-
filters: {}
|
|
69
|
-
page,
|
|
70
|
-
per_page,
|
|
71
|
-
sort_by,
|
|
72
|
-
vector
|
|
70
|
+
filters: {}
|
|
73
71
|
});
|
|
74
72
|
}
|
|
75
|
-
|
|
73
|
+
const settings = config.collections?.[collection];
|
|
74
|
+
if (!settings?.enabled) {
|
|
76
75
|
return Response.json({
|
|
77
76
|
error: 'Collection not enabled for search'
|
|
78
77
|
}, {
|
|
79
78
|
status: 400
|
|
80
79
|
});
|
|
81
80
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
81
|
+
const fields = settings.searchFields?.length ? settings.searchFields : [
|
|
82
|
+
'title',
|
|
83
|
+
'content',
|
|
84
|
+
'description'
|
|
85
|
+
];
|
|
86
|
+
const searchParams = buildParameters(config, fields, params);
|
|
87
|
+
const cached = searchCache.get(params.q, collection, searchParams);
|
|
88
|
+
const search = ()=>client.collections(collection).documents().search(searchParams);
|
|
89
|
+
const results = cached ? cached : await search();
|
|
90
|
+
const response = {
|
|
91
|
+
...results,
|
|
92
|
+
hits: results.hits?.map((hit)=>({
|
|
93
|
+
...hit,
|
|
85
94
|
collection,
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
});
|
|
89
|
-
return Response.json(results);
|
|
90
|
-
} catch (vectorError) {
|
|
91
|
-
return Response.json({
|
|
92
|
-
details: vectorError instanceof Error ? vectorError.message : 'Unknown vector search error',
|
|
93
|
-
error: 'Vector search failed'
|
|
94
|
-
}, {
|
|
95
|
-
status: 500
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
const fields = pluginOptions.collections?.[collection]?.searchFields?.join(',') || 'title,content';
|
|
100
|
-
const searchParams = {
|
|
101
|
-
highlight_full_fields: fields,
|
|
102
|
-
num_typos: 0,
|
|
103
|
-
page,
|
|
104
|
-
per_page,
|
|
105
|
-
q,
|
|
106
|
-
query_by: fields,
|
|
107
|
-
snippet_threshold: 30,
|
|
108
|
-
typo_tokens_threshold: 1,
|
|
109
|
-
...sort_by ? {
|
|
110
|
-
sort_by
|
|
111
|
-
} : {}
|
|
112
|
-
};
|
|
113
|
-
const cacheKey = {
|
|
114
|
-
fields,
|
|
115
|
-
page,
|
|
116
|
-
per_page,
|
|
117
|
-
sort_by,
|
|
118
|
-
vector
|
|
95
|
+
displayName: settings.displayName ?? collection
|
|
96
|
+
})) ?? []
|
|
119
97
|
};
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return Response.json(cached);
|
|
98
|
+
if (!cached) {
|
|
99
|
+
searchCache.set(params.q, response, collection, searchParams);
|
|
123
100
|
}
|
|
124
|
-
|
|
125
|
-
searchCache.set(q, results, collection, cacheKey);
|
|
126
|
-
return Response.json(results);
|
|
101
|
+
return Response.json(response);
|
|
127
102
|
} catch (error) {
|
|
128
103
|
return Response.json({
|
|
129
|
-
|
|
130
|
-
error: '
|
|
104
|
+
error: 'Search handler failed',
|
|
105
|
+
details: error instanceof Error ? error.message : 'Unknown error'
|
|
131
106
|
}, {
|
|
132
107
|
status: 500
|
|
133
108
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type PayloadHandler } from 'payload';
|
|
2
2
|
import type Typesense from 'typesense';
|
|
3
3
|
import { type TypesenseConfig } from '../../types.js';
|
|
4
|
-
export declare const createSuggest: (
|
|
4
|
+
export declare const createSuggest: (client: Typesense.Client, config: TypesenseConfig) => PayloadHandler;
|
|
5
5
|
//# sourceMappingURL=createSuggest.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createSuggest.d.ts","sourceRoot":"","sources":["../../../src/endpoints/handler/createSuggest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAuB,MAAM,SAAS,CAAA;AAClE,OAAO,KAAK,SAAS,MAAM,WAAW,CAAA;
|
|
1
|
+
{"version":3,"file":"createSuggest.d.ts","sourceRoot":"","sources":["../../../src/endpoints/handler/createSuggest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAuB,MAAM,SAAS,CAAA;AAClE,OAAO,KAAK,SAAS,MAAM,WAAW,CAAA;AAGtC,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAErD,eAAO,MAAM,aAAa,WAChB,SAAS,CAAC,MAAM,UAChB,eAAe,KACtB,cAmDF,CAAA"}
|
|
@@ -1,44 +1,61 @@
|
|
|
1
|
-
|
|
1
|
+
import { buildParameters, getCollectionName } from '../../lib/search.js';
|
|
2
|
+
export const createSuggest = (client, config)=>{
|
|
2
3
|
return async (request)=>{
|
|
3
|
-
|
|
4
|
+
let url;
|
|
5
|
+
let collection;
|
|
6
|
+
try {
|
|
7
|
+
if (!request.url) throw new Error('Request URL missing');
|
|
8
|
+
url = new URL(request.url);
|
|
9
|
+
collection = getCollectionName(url);
|
|
10
|
+
} catch {
|
|
4
11
|
return Response.json({
|
|
5
|
-
error: '
|
|
12
|
+
error: 'Invalid request URL'
|
|
6
13
|
}, {
|
|
7
14
|
status: 400
|
|
8
15
|
});
|
|
9
16
|
}
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
const searchIndex = parts.indexOf('search');
|
|
13
|
-
const collectionName = searchIndex >= 0 ? parts[searchIndex + 1] : '';
|
|
14
|
-
const collection = String(collectionName || '');
|
|
15
|
-
if (!collection || !pluginOptions.collections?.[collection]?.enabled) {
|
|
17
|
+
const settings = collection ? config.collections?.[collection] : undefined;
|
|
18
|
+
if (!collection || !settings?.enabled) {
|
|
16
19
|
return Response.json({
|
|
17
20
|
error: 'Collection not enabled for search'
|
|
18
21
|
}, {
|
|
19
22
|
status: 400
|
|
20
23
|
});
|
|
21
24
|
}
|
|
22
|
-
const q = url.searchParams.get('q');
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
const q = url.searchParams.get('q')?.trim();
|
|
26
|
+
const limit = Number(url.searchParams.get('limit') ?? 5);
|
|
27
|
+
if (!q) return Response.json({
|
|
28
|
+
error: 'Query parameter "q" is required'
|
|
29
|
+
}, {
|
|
30
|
+
status: 400
|
|
31
|
+
});
|
|
32
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 100) {
|
|
26
33
|
return Response.json({
|
|
27
|
-
error: '
|
|
34
|
+
error: 'Limit must be between 1 and 100'
|
|
28
35
|
}, {
|
|
29
36
|
status: 400
|
|
30
37
|
});
|
|
31
38
|
}
|
|
32
|
-
const fields = pluginOptions.collections?.[collection]?.searchFields?.join(',') || 'title,content';
|
|
33
39
|
try {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
40
|
+
const fields = settings.searchFields?.length ? settings.searchFields : [
|
|
41
|
+
'title',
|
|
42
|
+
'content',
|
|
43
|
+
'description'
|
|
44
|
+
];
|
|
45
|
+
const results = await client.collections(collection).documents().search(buildParameters(config, fields, {
|
|
37
46
|
q,
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
});
|
|
41
|
-
|
|
47
|
+
per_page: limit,
|
|
48
|
+
search_mode: 'keyword'
|
|
49
|
+
}));
|
|
50
|
+
const response = {
|
|
51
|
+
...results,
|
|
52
|
+
hits: results.hits?.map((hit)=>({
|
|
53
|
+
...hit,
|
|
54
|
+
collection,
|
|
55
|
+
displayName: settings.displayName ?? collection
|
|
56
|
+
})) ?? []
|
|
57
|
+
};
|
|
58
|
+
return Response.json(response);
|
|
42
59
|
} catch {
|
|
43
60
|
return Response.json({
|
|
44
61
|
error: 'Suggest failed'
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type PayloadHandler } from 'payload';
|
|
2
2
|
import type Typesense from 'typesense';
|
|
3
3
|
import { type TypesenseConfig } from '../types.js';
|
|
4
|
-
export declare const createHealthCheck: (
|
|
5
|
-
export declare const createDetailedHealthCheck: (
|
|
4
|
+
export declare const createHealthCheck: (client: Typesense.Client, _config: TypesenseConfig, lastSyncTime?: number) => PayloadHandler;
|
|
5
|
+
export declare const createDetailedHealthCheck: (client: Typesense.Client, config: TypesenseConfig, lastSyncTime?: number) => PayloadHandler;
|
|
6
6
|
//# sourceMappingURL=health.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"health.d.ts","sourceRoot":"","sources":["../../src/endpoints/health.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,SAAS,CAAA;AAC7C,OAAO,KAAK,SAAS,MAAM,WAAW,CAAA;AAGtC,OAAO,EAA4B,KAAK,eAAe,EAAE,MAAM,aAAa,CAAA;AAM5E,eAAO,MAAM,iBAAiB,
|
|
1
|
+
{"version":3,"file":"health.d.ts","sourceRoot":"","sources":["../../src/endpoints/health.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,SAAS,CAAA;AAC7C,OAAO,KAAK,SAAS,MAAM,WAAW,CAAA;AAGtC,OAAO,EAA4B,KAAK,eAAe,EAAE,MAAM,aAAa,CAAA;AAM5E,eAAO,MAAM,iBAAiB,WACpB,SAAS,CAAC,MAAM,WACf,eAAe,iBACT,MAAM,KACpB,cA0CF,CAAA;AAED,eAAO,MAAM,yBAAyB,WAC5B,SAAS,CAAC,MAAM,UAChB,eAAe,iBACR,MAAM,KACpB,cAoFF,CAAA"}
|