@btst/plugin-search 0.0.0-stage → 1.0.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/LICENSE.md +17 -0
- package/dist/api/index.cjs +17 -0
- package/dist/api/index.d.cts +62 -0
- package/dist/api/index.d.mts +62 -0
- package/dist/api/index.d.ts +62 -0
- package/dist/api/index.mjs +4 -0
- package/dist/api/operations.cjs +127 -0
- package/dist/api/operations.mjs +125 -0
- package/dist/api/plugin.cjs +23 -0
- package/dist/api/plugin.mjs +21 -0
- package/dist/client/components/index.cjs +11 -0
- package/dist/client/components/index.d.cts +35 -0
- package/dist/client/components/index.d.mts +35 -0
- package/dist/client/components/index.d.ts +35 -0
- package/dist/client/components/index.mjs +3 -0
- package/dist/client/components/pages/search-page.cjs +39 -0
- package/dist/client/components/pages/search-page.internal.cjs +100 -0
- package/dist/client/components/pages/search-page.internal.mjs +98 -0
- package/dist/client/components/pages/search-page.mjs +37 -0
- package/dist/client/components/search-dialog.cjs +148 -0
- package/dist/client/components/search-dialog.mjs +145 -0
- package/dist/client/components/search-results.cjs +100 -0
- package/dist/client/components/search-results.mjs +96 -0
- package/dist/client/components/source-filter.cjs +31 -0
- package/dist/client/components/source-filter.mjs +29 -0
- package/dist/client/constants.cjs +5 -0
- package/dist/client/constants.mjs +3 -0
- package/dist/client/hooks/index.cjs +10 -0
- package/dist/client/hooks/index.d.cts +340 -0
- package/dist/client/hooks/index.d.mts +340 -0
- package/dist/client/hooks/index.d.ts +340 -0
- package/dist/client/hooks/index.mjs +3 -0
- package/dist/client/hooks/use-search-state.cjs +36 -0
- package/dist/client/hooks/use-search-state.mjs +34 -0
- package/dist/client/hooks/use-search.cjs +44 -0
- package/dist/client/hooks/use-search.mjs +42 -0
- package/dist/client/index.cjs +7 -0
- package/dist/client/index.d.cts +55 -0
- package/dist/client/index.d.mts +55 -0
- package/dist/client/index.d.ts +55 -0
- package/dist/client/index.mjs +1 -0
- package/dist/client/plugin.cjs +76 -0
- package/dist/client/plugin.mjs +74 -0
- package/dist/client/search-state.cjs +20 -0
- package/dist/client/search-state.mjs +17 -0
- package/dist/permissions.cjs +12 -0
- package/dist/permissions.d.cts +12 -0
- package/dist/permissions.d.mts +12 -0
- package/dist/permissions.d.ts +12 -0
- package/dist/permissions.mjs +10 -0
- package/dist/providers.cjs +122 -0
- package/dist/providers.d.cts +82 -0
- package/dist/providers.d.mts +82 -0
- package/dist/providers.d.ts +82 -0
- package/dist/providers.mjs +116 -0
- package/dist/query-keys.cjs +35 -0
- package/dist/query-keys.d.cts +71 -0
- package/dist/query-keys.d.mts +71 -0
- package/dist/query-keys.d.ts +71 -0
- package/dist/query-keys.mjs +32 -0
- package/dist/schemas.cjs +23 -0
- package/dist/schemas.d.cts +14 -0
- package/dist/schemas.d.mts +14 -0
- package/dist/schemas.d.ts +14 -0
- package/dist/schemas.mjs +17 -0
- package/dist/shared/plugin-search.5GvThkZW.d.cts +20 -0
- package/dist/shared/plugin-search.5GvThkZW.d.mts +20 -0
- package/dist/shared/plugin-search.5GvThkZW.d.ts +20 -0
- package/dist/style.css +2 -0
- package/dist/types.cjs +2 -0
- package/dist/types.d.cts +53 -0
- package/dist/types.d.mts +53 -0
- package/dist/types.d.ts +53 -0
- package/dist/types.mjs +1 -0
- package/dist/ui/src/components/button.cjs +56 -0
- package/dist/ui/src/components/button.mjs +53 -0
- package/dist/ui/src/components/command.cjs +88 -0
- package/dist/ui/src/components/command.mjs +83 -0
- package/dist/ui/src/components/dialog.cjs +127 -0
- package/dist/ui/src/components/dialog.mjs +105 -0
- package/dist/ui/src/components/input.cjs +23 -0
- package/dist/ui/src/components/input.mjs +21 -0
- package/dist/ui/src/lib/utils.cjs +10 -0
- package/dist/ui/src/lib/utils.mjs +8 -0
- package/package.json +181 -3
- package/README.md +0 -3
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function searchSnippet(value) {
|
|
4
|
+
return value.slice(0, 2e3).replace(/<[^>]*>/g, " ").replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1").replace(/[`*_#>~]/g, "").replace(/\s+/g, " ").trim().slice(0, 240);
|
|
5
|
+
}
|
|
6
|
+
function destination(basePath, slug) {
|
|
7
|
+
return `${basePath.replace(/\/$/, "")}/${encodeURIComponent(slug)}`;
|
|
8
|
+
}
|
|
9
|
+
function blogSearchProvider(options) {
|
|
10
|
+
return {
|
|
11
|
+
id: "blog",
|
|
12
|
+
label: "Blog",
|
|
13
|
+
async search({ q, limit, request }) {
|
|
14
|
+
const operations = options.operations(request);
|
|
15
|
+
const items = [];
|
|
16
|
+
let omittedDestination = false;
|
|
17
|
+
let total = 0;
|
|
18
|
+
for (let offset = 0; offset < limit; offset += 100) {
|
|
19
|
+
const page = await operations.listPosts({
|
|
20
|
+
query: q,
|
|
21
|
+
published: true,
|
|
22
|
+
limit: Math.min(100, limit - offset),
|
|
23
|
+
offset
|
|
24
|
+
});
|
|
25
|
+
total = page.total;
|
|
26
|
+
for (const post of page.items) {
|
|
27
|
+
if (!post.published) continue;
|
|
28
|
+
const href = destination(
|
|
29
|
+
options.basePath ?? "/pages/blog",
|
|
30
|
+
post.slug
|
|
31
|
+
);
|
|
32
|
+
if (href.length > 2e3) {
|
|
33
|
+
omittedDestination = true;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
items.push({
|
|
37
|
+
resourceId: post.id,
|
|
38
|
+
title: post.title.slice(0, 500),
|
|
39
|
+
snippet: searchSnippet(post.excerpt || post.content),
|
|
40
|
+
href
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
if (offset + page.items.length >= total || page.items.length === 0)
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
return { items, hasMore: total > limit || omittedDestination };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function schoolSearchProvider(options) {
|
|
51
|
+
return {
|
|
52
|
+
id: "school",
|
|
53
|
+
label: "Courses",
|
|
54
|
+
async search({ q, limit, request }) {
|
|
55
|
+
const page = await options.operations(request).listCourses({ query: q });
|
|
56
|
+
const courses = page.items.filter((course) => course.status === "published").sort((a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
57
|
+
return {
|
|
58
|
+
items: courses.slice(0, limit).map((course) => ({
|
|
59
|
+
resourceId: course.id,
|
|
60
|
+
title: course.title,
|
|
61
|
+
snippet: searchSnippet(course.description),
|
|
62
|
+
href: destination(options.basePath ?? "/pages/learn", course.slug)
|
|
63
|
+
})),
|
|
64
|
+
hasMore: courses.length > limit
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function ecommerceSearchProvider(options) {
|
|
70
|
+
return {
|
|
71
|
+
id: "ecommerce",
|
|
72
|
+
label: "Products",
|
|
73
|
+
async search({ q, limit, request }) {
|
|
74
|
+
const operations = options.operations(request);
|
|
75
|
+
const items = [];
|
|
76
|
+
let total = 0;
|
|
77
|
+
for (let offset = 0; offset < limit; offset += 100) {
|
|
78
|
+
const page = await operations.listProducts({
|
|
79
|
+
search: q,
|
|
80
|
+
limit: Math.min(100, limit - offset),
|
|
81
|
+
offset
|
|
82
|
+
});
|
|
83
|
+
total = page.total;
|
|
84
|
+
for (const product of page.items) {
|
|
85
|
+
if (product.status !== "published") continue;
|
|
86
|
+
items.push({
|
|
87
|
+
resourceId: product.id,
|
|
88
|
+
title: product.name,
|
|
89
|
+
snippet: searchSnippet(product.description),
|
|
90
|
+
href: destination(
|
|
91
|
+
options.basePath ?? "/pages/shop/products",
|
|
92
|
+
product.slug
|
|
93
|
+
)
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
if (offset + page.items.length >= total || page.items.length === 0)
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
return { items, hasMore: total > limit };
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function cmsSearchProvider(options) {
|
|
104
|
+
if (/^(school-|ecommerce-|job-|helpdesk-|crm-|ai-chat|knowledge-base)/.test(
|
|
105
|
+
options.typeSlug
|
|
106
|
+
)) {
|
|
107
|
+
throw new Error(
|
|
108
|
+
"Domain-owned content types require their public domain projection"
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
id: `cms-${options.typeSlug}`,
|
|
113
|
+
label: options.label,
|
|
114
|
+
search: options.search
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
exports.blogSearchProvider = blogSearchProvider;
|
|
119
|
+
exports.cmsSearchProvider = cmsSearchProvider;
|
|
120
|
+
exports.ecommerceSearchProvider = ecommerceSearchProvider;
|
|
121
|
+
exports.schoolSearchProvider = schoolSearchProvider;
|
|
122
|
+
exports.searchSnippet = searchSnippet;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { SearchProvider } from './types.cjs';
|
|
2
|
+
import '@btst/stack/plugins/api';
|
|
3
|
+
|
|
4
|
+
interface PublicPost {
|
|
5
|
+
readonly id: string;
|
|
6
|
+
readonly slug: string;
|
|
7
|
+
readonly title: string;
|
|
8
|
+
readonly content: string;
|
|
9
|
+
readonly excerpt: string;
|
|
10
|
+
readonly published: boolean;
|
|
11
|
+
}
|
|
12
|
+
interface PublicCourse {
|
|
13
|
+
readonly id: string;
|
|
14
|
+
readonly slug: string;
|
|
15
|
+
readonly title: string;
|
|
16
|
+
readonly description: string;
|
|
17
|
+
readonly status: string;
|
|
18
|
+
}
|
|
19
|
+
interface PublicProduct {
|
|
20
|
+
readonly id: string;
|
|
21
|
+
readonly slug: string;
|
|
22
|
+
readonly name: string;
|
|
23
|
+
readonly description: string;
|
|
24
|
+
readonly status: string;
|
|
25
|
+
}
|
|
26
|
+
/** Request-bound source operations, not raw getters or trusted operations. */
|
|
27
|
+
interface BlogSearchOperations {
|
|
28
|
+
listPosts(input: {
|
|
29
|
+
query: string;
|
|
30
|
+
published: true;
|
|
31
|
+
limit: number;
|
|
32
|
+
offset: number;
|
|
33
|
+
}): Promise<{
|
|
34
|
+
readonly items: readonly PublicPost[];
|
|
35
|
+
readonly total: number;
|
|
36
|
+
}>;
|
|
37
|
+
}
|
|
38
|
+
interface SchoolSearchOperations {
|
|
39
|
+
listCourses(input: {
|
|
40
|
+
query: string;
|
|
41
|
+
}): Promise<{
|
|
42
|
+
readonly items: readonly PublicCourse[];
|
|
43
|
+
}>;
|
|
44
|
+
}
|
|
45
|
+
interface EcommerceSearchOperations {
|
|
46
|
+
listProducts(input: {
|
|
47
|
+
search: string;
|
|
48
|
+
limit: number;
|
|
49
|
+
offset: number;
|
|
50
|
+
}): Promise<{
|
|
51
|
+
readonly items: readonly PublicProduct[];
|
|
52
|
+
readonly total: number;
|
|
53
|
+
}>;
|
|
54
|
+
}
|
|
55
|
+
interface SourceOptions<Operations> {
|
|
56
|
+
operations(request: Request): Operations;
|
|
57
|
+
/** Complete site-relative mount, including the framework page prefix. */
|
|
58
|
+
basePath?: string;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Convert the first 2,000 UTF-16 code units of source markup to plain text.
|
|
62
|
+
* Bound input before regex work so malformed markup cannot trigger unbounded scans.
|
|
63
|
+
*/
|
|
64
|
+
declare function searchSnippet(value: string): string;
|
|
65
|
+
/** Reuses Blog's publication filter and domain text matching. */
|
|
66
|
+
declare function blogSearchProvider(options: SourceOptions<BlogSearchOperations>): SearchProvider;
|
|
67
|
+
/** Uses only the public School catalog; lessons never participate. */
|
|
68
|
+
declare function schoolSearchProvider(options: SourceOptions<SchoolSearchOperations>): SearchProvider;
|
|
69
|
+
/** Uses Ecommerce's public product projection, including its visibility rules. */
|
|
70
|
+
declare function ecommerceSearchProvider(options: SourceOptions<EcommerceSearchOperations>): SearchProvider;
|
|
71
|
+
/**
|
|
72
|
+
* An explicit application-owned CMS public projection. No content types are
|
|
73
|
+
* searchable by default; domain-owned types must use their domain provider.
|
|
74
|
+
*/
|
|
75
|
+
declare function cmsSearchProvider(options: {
|
|
76
|
+
typeSlug: string;
|
|
77
|
+
label: string;
|
|
78
|
+
search: SearchProvider["search"];
|
|
79
|
+
}): SearchProvider;
|
|
80
|
+
|
|
81
|
+
export { blogSearchProvider, cmsSearchProvider, ecommerceSearchProvider, schoolSearchProvider, searchSnippet };
|
|
82
|
+
export type { BlogSearchOperations, EcommerceSearchOperations, SchoolSearchOperations };
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { SearchProvider } from './types.mjs';
|
|
2
|
+
import '@btst/stack/plugins/api';
|
|
3
|
+
|
|
4
|
+
interface PublicPost {
|
|
5
|
+
readonly id: string;
|
|
6
|
+
readonly slug: string;
|
|
7
|
+
readonly title: string;
|
|
8
|
+
readonly content: string;
|
|
9
|
+
readonly excerpt: string;
|
|
10
|
+
readonly published: boolean;
|
|
11
|
+
}
|
|
12
|
+
interface PublicCourse {
|
|
13
|
+
readonly id: string;
|
|
14
|
+
readonly slug: string;
|
|
15
|
+
readonly title: string;
|
|
16
|
+
readonly description: string;
|
|
17
|
+
readonly status: string;
|
|
18
|
+
}
|
|
19
|
+
interface PublicProduct {
|
|
20
|
+
readonly id: string;
|
|
21
|
+
readonly slug: string;
|
|
22
|
+
readonly name: string;
|
|
23
|
+
readonly description: string;
|
|
24
|
+
readonly status: string;
|
|
25
|
+
}
|
|
26
|
+
/** Request-bound source operations, not raw getters or trusted operations. */
|
|
27
|
+
interface BlogSearchOperations {
|
|
28
|
+
listPosts(input: {
|
|
29
|
+
query: string;
|
|
30
|
+
published: true;
|
|
31
|
+
limit: number;
|
|
32
|
+
offset: number;
|
|
33
|
+
}): Promise<{
|
|
34
|
+
readonly items: readonly PublicPost[];
|
|
35
|
+
readonly total: number;
|
|
36
|
+
}>;
|
|
37
|
+
}
|
|
38
|
+
interface SchoolSearchOperations {
|
|
39
|
+
listCourses(input: {
|
|
40
|
+
query: string;
|
|
41
|
+
}): Promise<{
|
|
42
|
+
readonly items: readonly PublicCourse[];
|
|
43
|
+
}>;
|
|
44
|
+
}
|
|
45
|
+
interface EcommerceSearchOperations {
|
|
46
|
+
listProducts(input: {
|
|
47
|
+
search: string;
|
|
48
|
+
limit: number;
|
|
49
|
+
offset: number;
|
|
50
|
+
}): Promise<{
|
|
51
|
+
readonly items: readonly PublicProduct[];
|
|
52
|
+
readonly total: number;
|
|
53
|
+
}>;
|
|
54
|
+
}
|
|
55
|
+
interface SourceOptions<Operations> {
|
|
56
|
+
operations(request: Request): Operations;
|
|
57
|
+
/** Complete site-relative mount, including the framework page prefix. */
|
|
58
|
+
basePath?: string;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Convert the first 2,000 UTF-16 code units of source markup to plain text.
|
|
62
|
+
* Bound input before regex work so malformed markup cannot trigger unbounded scans.
|
|
63
|
+
*/
|
|
64
|
+
declare function searchSnippet(value: string): string;
|
|
65
|
+
/** Reuses Blog's publication filter and domain text matching. */
|
|
66
|
+
declare function blogSearchProvider(options: SourceOptions<BlogSearchOperations>): SearchProvider;
|
|
67
|
+
/** Uses only the public School catalog; lessons never participate. */
|
|
68
|
+
declare function schoolSearchProvider(options: SourceOptions<SchoolSearchOperations>): SearchProvider;
|
|
69
|
+
/** Uses Ecommerce's public product projection, including its visibility rules. */
|
|
70
|
+
declare function ecommerceSearchProvider(options: SourceOptions<EcommerceSearchOperations>): SearchProvider;
|
|
71
|
+
/**
|
|
72
|
+
* An explicit application-owned CMS public projection. No content types are
|
|
73
|
+
* searchable by default; domain-owned types must use their domain provider.
|
|
74
|
+
*/
|
|
75
|
+
declare function cmsSearchProvider(options: {
|
|
76
|
+
typeSlug: string;
|
|
77
|
+
label: string;
|
|
78
|
+
search: SearchProvider["search"];
|
|
79
|
+
}): SearchProvider;
|
|
80
|
+
|
|
81
|
+
export { blogSearchProvider, cmsSearchProvider, ecommerceSearchProvider, schoolSearchProvider, searchSnippet };
|
|
82
|
+
export type { BlogSearchOperations, EcommerceSearchOperations, SchoolSearchOperations };
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { SearchProvider } from './types.js';
|
|
2
|
+
import '@btst/stack/plugins/api';
|
|
3
|
+
|
|
4
|
+
interface PublicPost {
|
|
5
|
+
readonly id: string;
|
|
6
|
+
readonly slug: string;
|
|
7
|
+
readonly title: string;
|
|
8
|
+
readonly content: string;
|
|
9
|
+
readonly excerpt: string;
|
|
10
|
+
readonly published: boolean;
|
|
11
|
+
}
|
|
12
|
+
interface PublicCourse {
|
|
13
|
+
readonly id: string;
|
|
14
|
+
readonly slug: string;
|
|
15
|
+
readonly title: string;
|
|
16
|
+
readonly description: string;
|
|
17
|
+
readonly status: string;
|
|
18
|
+
}
|
|
19
|
+
interface PublicProduct {
|
|
20
|
+
readonly id: string;
|
|
21
|
+
readonly slug: string;
|
|
22
|
+
readonly name: string;
|
|
23
|
+
readonly description: string;
|
|
24
|
+
readonly status: string;
|
|
25
|
+
}
|
|
26
|
+
/** Request-bound source operations, not raw getters or trusted operations. */
|
|
27
|
+
interface BlogSearchOperations {
|
|
28
|
+
listPosts(input: {
|
|
29
|
+
query: string;
|
|
30
|
+
published: true;
|
|
31
|
+
limit: number;
|
|
32
|
+
offset: number;
|
|
33
|
+
}): Promise<{
|
|
34
|
+
readonly items: readonly PublicPost[];
|
|
35
|
+
readonly total: number;
|
|
36
|
+
}>;
|
|
37
|
+
}
|
|
38
|
+
interface SchoolSearchOperations {
|
|
39
|
+
listCourses(input: {
|
|
40
|
+
query: string;
|
|
41
|
+
}): Promise<{
|
|
42
|
+
readonly items: readonly PublicCourse[];
|
|
43
|
+
}>;
|
|
44
|
+
}
|
|
45
|
+
interface EcommerceSearchOperations {
|
|
46
|
+
listProducts(input: {
|
|
47
|
+
search: string;
|
|
48
|
+
limit: number;
|
|
49
|
+
offset: number;
|
|
50
|
+
}): Promise<{
|
|
51
|
+
readonly items: readonly PublicProduct[];
|
|
52
|
+
readonly total: number;
|
|
53
|
+
}>;
|
|
54
|
+
}
|
|
55
|
+
interface SourceOptions<Operations> {
|
|
56
|
+
operations(request: Request): Operations;
|
|
57
|
+
/** Complete site-relative mount, including the framework page prefix. */
|
|
58
|
+
basePath?: string;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Convert the first 2,000 UTF-16 code units of source markup to plain text.
|
|
62
|
+
* Bound input before regex work so malformed markup cannot trigger unbounded scans.
|
|
63
|
+
*/
|
|
64
|
+
declare function searchSnippet(value: string): string;
|
|
65
|
+
/** Reuses Blog's publication filter and domain text matching. */
|
|
66
|
+
declare function blogSearchProvider(options: SourceOptions<BlogSearchOperations>): SearchProvider;
|
|
67
|
+
/** Uses only the public School catalog; lessons never participate. */
|
|
68
|
+
declare function schoolSearchProvider(options: SourceOptions<SchoolSearchOperations>): SearchProvider;
|
|
69
|
+
/** Uses Ecommerce's public product projection, including its visibility rules. */
|
|
70
|
+
declare function ecommerceSearchProvider(options: SourceOptions<EcommerceSearchOperations>): SearchProvider;
|
|
71
|
+
/**
|
|
72
|
+
* An explicit application-owned CMS public projection. No content types are
|
|
73
|
+
* searchable by default; domain-owned types must use their domain provider.
|
|
74
|
+
*/
|
|
75
|
+
declare function cmsSearchProvider(options: {
|
|
76
|
+
typeSlug: string;
|
|
77
|
+
label: string;
|
|
78
|
+
search: SearchProvider["search"];
|
|
79
|
+
}): SearchProvider;
|
|
80
|
+
|
|
81
|
+
export { blogSearchProvider, cmsSearchProvider, ecommerceSearchProvider, schoolSearchProvider, searchSnippet };
|
|
82
|
+
export type { BlogSearchOperations, EcommerceSearchOperations, SchoolSearchOperations };
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
function searchSnippet(value) {
|
|
2
|
+
return value.slice(0, 2e3).replace(/<[^>]*>/g, " ").replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1").replace(/[`*_#>~]/g, "").replace(/\s+/g, " ").trim().slice(0, 240);
|
|
3
|
+
}
|
|
4
|
+
function destination(basePath, slug) {
|
|
5
|
+
return `${basePath.replace(/\/$/, "")}/${encodeURIComponent(slug)}`;
|
|
6
|
+
}
|
|
7
|
+
function blogSearchProvider(options) {
|
|
8
|
+
return {
|
|
9
|
+
id: "blog",
|
|
10
|
+
label: "Blog",
|
|
11
|
+
async search({ q, limit, request }) {
|
|
12
|
+
const operations = options.operations(request);
|
|
13
|
+
const items = [];
|
|
14
|
+
let omittedDestination = false;
|
|
15
|
+
let total = 0;
|
|
16
|
+
for (let offset = 0; offset < limit; offset += 100) {
|
|
17
|
+
const page = await operations.listPosts({
|
|
18
|
+
query: q,
|
|
19
|
+
published: true,
|
|
20
|
+
limit: Math.min(100, limit - offset),
|
|
21
|
+
offset
|
|
22
|
+
});
|
|
23
|
+
total = page.total;
|
|
24
|
+
for (const post of page.items) {
|
|
25
|
+
if (!post.published) continue;
|
|
26
|
+
const href = destination(
|
|
27
|
+
options.basePath ?? "/pages/blog",
|
|
28
|
+
post.slug
|
|
29
|
+
);
|
|
30
|
+
if (href.length > 2e3) {
|
|
31
|
+
omittedDestination = true;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
items.push({
|
|
35
|
+
resourceId: post.id,
|
|
36
|
+
title: post.title.slice(0, 500),
|
|
37
|
+
snippet: searchSnippet(post.excerpt || post.content),
|
|
38
|
+
href
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
if (offset + page.items.length >= total || page.items.length === 0)
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
return { items, hasMore: total > limit || omittedDestination };
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function schoolSearchProvider(options) {
|
|
49
|
+
return {
|
|
50
|
+
id: "school",
|
|
51
|
+
label: "Courses",
|
|
52
|
+
async search({ q, limit, request }) {
|
|
53
|
+
const page = await options.operations(request).listCourses({ query: q });
|
|
54
|
+
const courses = page.items.filter((course) => course.status === "published").sort((a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
55
|
+
return {
|
|
56
|
+
items: courses.slice(0, limit).map((course) => ({
|
|
57
|
+
resourceId: course.id,
|
|
58
|
+
title: course.title,
|
|
59
|
+
snippet: searchSnippet(course.description),
|
|
60
|
+
href: destination(options.basePath ?? "/pages/learn", course.slug)
|
|
61
|
+
})),
|
|
62
|
+
hasMore: courses.length > limit
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function ecommerceSearchProvider(options) {
|
|
68
|
+
return {
|
|
69
|
+
id: "ecommerce",
|
|
70
|
+
label: "Products",
|
|
71
|
+
async search({ q, limit, request }) {
|
|
72
|
+
const operations = options.operations(request);
|
|
73
|
+
const items = [];
|
|
74
|
+
let total = 0;
|
|
75
|
+
for (let offset = 0; offset < limit; offset += 100) {
|
|
76
|
+
const page = await operations.listProducts({
|
|
77
|
+
search: q,
|
|
78
|
+
limit: Math.min(100, limit - offset),
|
|
79
|
+
offset
|
|
80
|
+
});
|
|
81
|
+
total = page.total;
|
|
82
|
+
for (const product of page.items) {
|
|
83
|
+
if (product.status !== "published") continue;
|
|
84
|
+
items.push({
|
|
85
|
+
resourceId: product.id,
|
|
86
|
+
title: product.name,
|
|
87
|
+
snippet: searchSnippet(product.description),
|
|
88
|
+
href: destination(
|
|
89
|
+
options.basePath ?? "/pages/shop/products",
|
|
90
|
+
product.slug
|
|
91
|
+
)
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
if (offset + page.items.length >= total || page.items.length === 0)
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
return { items, hasMore: total > limit };
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function cmsSearchProvider(options) {
|
|
102
|
+
if (/^(school-|ecommerce-|job-|helpdesk-|crm-|ai-chat|knowledge-base)/.test(
|
|
103
|
+
options.typeSlug
|
|
104
|
+
)) {
|
|
105
|
+
throw new Error(
|
|
106
|
+
"Domain-owned content types require their public domain projection"
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
id: `cms-${options.typeSlug}`,
|
|
111
|
+
label: options.label,
|
|
112
|
+
search: options.search
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export { blogSearchProvider, cmsSearchProvider, ecommerceSearchProvider, schoolSearchProvider, searchSnippet };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const client = require('@btst/stack/plugins/client');
|
|
4
|
+
|
|
5
|
+
const searchResources = {
|
|
6
|
+
searchResults: {
|
|
7
|
+
queries: {
|
|
8
|
+
list: {
|
|
9
|
+
path: "/search",
|
|
10
|
+
query: (params, _identity) => ({
|
|
11
|
+
q: params.q.trim(),
|
|
12
|
+
sources: params.sources?.join(","),
|
|
13
|
+
page: params.page ?? 1,
|
|
14
|
+
limit: params.limit ?? 20
|
|
15
|
+
}),
|
|
16
|
+
key: (params, identity) => [
|
|
17
|
+
{
|
|
18
|
+
q: params.q.trim(),
|
|
19
|
+
sources: params.sources ? [...params.sources].sort() : void 0,
|
|
20
|
+
page: params.page ?? 1,
|
|
21
|
+
limit: params.limit ?? 20
|
|
22
|
+
},
|
|
23
|
+
identity
|
|
24
|
+
],
|
|
25
|
+
select: (data) => data
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
function createSearchQueryKeys(client$1, headers) {
|
|
31
|
+
return client.createResourceQueryKeys(client$1, searchResources, headers);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
exports.createSearchQueryKeys = createSearchQueryKeys;
|
|
35
|
+
exports.searchResources = searchResources;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import * as _btst_stack_plugins_client from '@btst/stack/plugins/client';
|
|
2
|
+
import { ResourceClient } from '@btst/stack/plugins/client';
|
|
3
|
+
import { StackIdentity } from '@btst/stack/context';
|
|
4
|
+
import { SearchResponse } from './types.cjs';
|
|
5
|
+
import '@btst/stack/plugins/api';
|
|
6
|
+
|
|
7
|
+
/** Identity is a cache discriminator only; it is never sent as HTTP input. */
|
|
8
|
+
type SearchIdentityPartition = StackIdentity | {
|
|
9
|
+
identity: StackIdentity | null;
|
|
10
|
+
generation: number;
|
|
11
|
+
} | `pending:${number}` | `error:${number}`;
|
|
12
|
+
/** Query arguments shared by the search route, dialog, and SSR loader. */
|
|
13
|
+
interface SearchQueryParams {
|
|
14
|
+
q: string;
|
|
15
|
+
sources?: readonly string[];
|
|
16
|
+
page?: number;
|
|
17
|
+
limit?: number;
|
|
18
|
+
}
|
|
19
|
+
declare const searchResources: {
|
|
20
|
+
searchResults: {
|
|
21
|
+
queries: {
|
|
22
|
+
list: {
|
|
23
|
+
path: string;
|
|
24
|
+
query: (params: SearchQueryParams, _identity?: SearchIdentityPartition) => {
|
|
25
|
+
q: string;
|
|
26
|
+
sources: string | undefined;
|
|
27
|
+
page: number;
|
|
28
|
+
limit: number;
|
|
29
|
+
};
|
|
30
|
+
key: (params: SearchQueryParams, identity?: SearchIdentityPartition) => (StackIdentity | {
|
|
31
|
+
identity: StackIdentity | null;
|
|
32
|
+
generation: number;
|
|
33
|
+
} | `pending:${number}` | `error:${number}` | {
|
|
34
|
+
q: string;
|
|
35
|
+
sources: string[] | undefined;
|
|
36
|
+
page: number;
|
|
37
|
+
limit: number;
|
|
38
|
+
} | undefined)[];
|
|
39
|
+
select: (data: unknown) => SearchResponse;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
declare function createSearchQueryKeys(client: ResourceClient, headers?: HeadersInit): _btst_stack_plugins_client.ResourceQueryKeys<{
|
|
45
|
+
searchResults: {
|
|
46
|
+
queries: {
|
|
47
|
+
list: {
|
|
48
|
+
path: string;
|
|
49
|
+
query: (params: SearchQueryParams, _identity?: SearchIdentityPartition) => {
|
|
50
|
+
q: string;
|
|
51
|
+
sources: string | undefined;
|
|
52
|
+
page: number;
|
|
53
|
+
limit: number;
|
|
54
|
+
};
|
|
55
|
+
key: (params: SearchQueryParams, identity?: SearchIdentityPartition) => (StackIdentity | {
|
|
56
|
+
identity: StackIdentity | null;
|
|
57
|
+
generation: number;
|
|
58
|
+
} | `pending:${number}` | `error:${number}` | {
|
|
59
|
+
q: string;
|
|
60
|
+
sources: string[] | undefined;
|
|
61
|
+
page: number;
|
|
62
|
+
limit: number;
|
|
63
|
+
} | undefined)[];
|
|
64
|
+
select: (data: unknown) => SearchResponse;
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
}>;
|
|
69
|
+
|
|
70
|
+
export { createSearchQueryKeys, searchResources };
|
|
71
|
+
export type { SearchIdentityPartition, SearchQueryParams };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import * as _btst_stack_plugins_client from '@btst/stack/plugins/client';
|
|
2
|
+
import { ResourceClient } from '@btst/stack/plugins/client';
|
|
3
|
+
import { StackIdentity } from '@btst/stack/context';
|
|
4
|
+
import { SearchResponse } from './types.mjs';
|
|
5
|
+
import '@btst/stack/plugins/api';
|
|
6
|
+
|
|
7
|
+
/** Identity is a cache discriminator only; it is never sent as HTTP input. */
|
|
8
|
+
type SearchIdentityPartition = StackIdentity | {
|
|
9
|
+
identity: StackIdentity | null;
|
|
10
|
+
generation: number;
|
|
11
|
+
} | `pending:${number}` | `error:${number}`;
|
|
12
|
+
/** Query arguments shared by the search route, dialog, and SSR loader. */
|
|
13
|
+
interface SearchQueryParams {
|
|
14
|
+
q: string;
|
|
15
|
+
sources?: readonly string[];
|
|
16
|
+
page?: number;
|
|
17
|
+
limit?: number;
|
|
18
|
+
}
|
|
19
|
+
declare const searchResources: {
|
|
20
|
+
searchResults: {
|
|
21
|
+
queries: {
|
|
22
|
+
list: {
|
|
23
|
+
path: string;
|
|
24
|
+
query: (params: SearchQueryParams, _identity?: SearchIdentityPartition) => {
|
|
25
|
+
q: string;
|
|
26
|
+
sources: string | undefined;
|
|
27
|
+
page: number;
|
|
28
|
+
limit: number;
|
|
29
|
+
};
|
|
30
|
+
key: (params: SearchQueryParams, identity?: SearchIdentityPartition) => (StackIdentity | {
|
|
31
|
+
identity: StackIdentity | null;
|
|
32
|
+
generation: number;
|
|
33
|
+
} | `pending:${number}` | `error:${number}` | {
|
|
34
|
+
q: string;
|
|
35
|
+
sources: string[] | undefined;
|
|
36
|
+
page: number;
|
|
37
|
+
limit: number;
|
|
38
|
+
} | undefined)[];
|
|
39
|
+
select: (data: unknown) => SearchResponse;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
declare function createSearchQueryKeys(client: ResourceClient, headers?: HeadersInit): _btst_stack_plugins_client.ResourceQueryKeys<{
|
|
45
|
+
searchResults: {
|
|
46
|
+
queries: {
|
|
47
|
+
list: {
|
|
48
|
+
path: string;
|
|
49
|
+
query: (params: SearchQueryParams, _identity?: SearchIdentityPartition) => {
|
|
50
|
+
q: string;
|
|
51
|
+
sources: string | undefined;
|
|
52
|
+
page: number;
|
|
53
|
+
limit: number;
|
|
54
|
+
};
|
|
55
|
+
key: (params: SearchQueryParams, identity?: SearchIdentityPartition) => (StackIdentity | {
|
|
56
|
+
identity: StackIdentity | null;
|
|
57
|
+
generation: number;
|
|
58
|
+
} | `pending:${number}` | `error:${number}` | {
|
|
59
|
+
q: string;
|
|
60
|
+
sources: string[] | undefined;
|
|
61
|
+
page: number;
|
|
62
|
+
limit: number;
|
|
63
|
+
} | undefined)[];
|
|
64
|
+
select: (data: unknown) => SearchResponse;
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
}>;
|
|
69
|
+
|
|
70
|
+
export { createSearchQueryKeys, searchResources };
|
|
71
|
+
export type { SearchIdentityPartition, SearchQueryParams };
|