@uniformdev/search 0.0.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Uniform
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,66 @@
1
+ // src/constants.ts
2
+ var UNIFORM_SEARCH_QUERY_KEY = "search";
3
+ var UNIFORM_SEARCH_PAGE_KEY = "page";
4
+ var UNIFORM_SEARCH_PAGE_SIZE_KEY = "pageSize";
5
+ var UNIFORM_SEARCH_ORDER_BY_KEY = "orderBy";
6
+ var FIRST_PAGE = 0;
7
+ var DEFAULT_PAGE_SIZE = 10;
8
+
9
+ // src/utils.ts
10
+ var buildOrderByQuery = (orderBy) => {
11
+ return `${orderBy.field}_${orderBy.direction}`;
12
+ };
13
+ var getSearchParamsFromUrl = (urlString) => {
14
+ if (!urlString) {
15
+ return {};
16
+ }
17
+ const url = new URL(urlString);
18
+ const params = {};
19
+ url.searchParams.forEach((value, key) => {
20
+ if (params[key]) {
21
+ params[key] = Array.isArray(params[key]) ? [...params[key], value] : [params[key], value];
22
+ } else {
23
+ params[key] = value;
24
+ }
25
+ });
26
+ return params;
27
+ };
28
+
29
+ // src/flattenBlockParams.ts
30
+ function resolveFieldValue(field, locale = "en-us") {
31
+ var _a, _b;
32
+ if (!field || typeof field !== "object") return field;
33
+ if (field.type === "$block" && Array.isArray(field.value)) {
34
+ return flattenBlockParams(field.value, locale);
35
+ }
36
+ if (field.locales && typeof field.locales === "object") {
37
+ return (_b = (_a = field.locales[locale]) != null ? _a : Object.values(field.locales)[0]) != null ? _b : field.value;
38
+ }
39
+ return field.value;
40
+ }
41
+ function flattenBlockParams(items, locale = "en-us") {
42
+ if (!Array.isArray(items)) return [];
43
+ return items.map((item) => {
44
+ const result = {};
45
+ const blockItem = item;
46
+ if (blockItem.fields) {
47
+ for (const [key, field] of Object.entries(blockItem.fields)) {
48
+ result[key] = resolveFieldValue(field, locale);
49
+ }
50
+ }
51
+ return result;
52
+ });
53
+ }
54
+
55
+ export {
56
+ UNIFORM_SEARCH_QUERY_KEY,
57
+ UNIFORM_SEARCH_PAGE_KEY,
58
+ UNIFORM_SEARCH_PAGE_SIZE_KEY,
59
+ UNIFORM_SEARCH_ORDER_BY_KEY,
60
+ FIRST_PAGE,
61
+ DEFAULT_PAGE_SIZE,
62
+ buildOrderByQuery,
63
+ getSearchParamsFromUrl,
64
+ flattenBlockParams
65
+ };
66
+ //# sourceMappingURL=chunk-ZABEFR2D.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/constants.ts","../src/utils.ts","../src/flattenBlockParams.ts"],"sourcesContent":["export const UNIFORM_SEARCH_QUERY_KEY = 'search';\nexport const UNIFORM_SEARCH_PAGE_KEY = 'page';\nexport const UNIFORM_SEARCH_PAGE_SIZE_KEY = 'pageSize';\nexport const UNIFORM_SEARCH_ORDER_BY_KEY = 'orderBy';\nexport const FIRST_PAGE = 0;\nexport const DEFAULT_PAGE_SIZE = 10;\n","import type { OrderBy } from './types';\n\nexport const buildOrderByQuery = (orderBy: OrderBy) => {\n return `${orderBy.field}_${orderBy.direction}`;\n};\n\nexport const getSearchParamsFromUrl = (urlString: string): Record<string, string | string[]> => {\n if (!urlString) {\n return {};\n }\n\n const url = new URL(urlString);\n const params: Record<string, string | string[]> = {};\n\n url.searchParams.forEach((value, key) => {\n if (params[key]) {\n params[key] = Array.isArray(params[key])\n ? [...(params[key] as string[]), value]\n : [params[key] as string, value];\n } else {\n params[key] = value;\n }\n });\n\n return params;\n};\n","type RawField = {\n type?: string;\n value?: unknown;\n locales?: Record<string, unknown>;\n};\n\ntype RawBlockItem = {\n type?: string;\n fields?: Record<string, RawField>;\n};\n\nfunction resolveFieldValue(field: RawField, locale = 'en-us'): unknown {\n if (!field || typeof field !== 'object') return field;\n\n if (field.type === '$block' && Array.isArray(field.value)) {\n return flattenBlockParams(field.value as RawBlockItem[], locale);\n }\n\n if (field.locales && typeof field.locales === 'object') {\n return field.locales[locale] ?? Object.values(field.locales)[0] ?? field.value;\n }\n\n return field.value;\n}\n\nexport function flattenBlockParams<T = Record<string, unknown>>(\n items: unknown,\n locale = 'en-us'\n): T[] {\n if (!Array.isArray(items)) return [];\n return items.map(item => {\n const result: Record<string, unknown> = {};\n const blockItem = item as RawBlockItem;\n if (blockItem.fields) {\n for (const [key, field] of Object.entries(blockItem.fields)) {\n result[key] = resolveFieldValue(field, locale);\n }\n }\n return result as T;\n });\n}\n"],"mappings":";AAAO,IAAM,2BAA2B;AACjC,IAAM,0BAA0B;AAChC,IAAM,+BAA+B;AACrC,IAAM,8BAA8B;AACpC,IAAM,aAAa;AACnB,IAAM,oBAAoB;;;ACH1B,IAAM,oBAAoB,CAAC,YAAqB;AACrD,SAAO,GAAG,QAAQ,KAAK,IAAI,QAAQ,SAAS;AAC9C;AAEO,IAAM,yBAAyB,CAAC,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' | 'assets';
2
+ type SearchHit = {
3
+ id: string;
4
+ _collection: SearchCollection;
5
+ [key: string]: unknown;
6
+ };
7
+ type Pagination<Item> = {
8
+ items: Item[];
9
+ page: number;
10
+ perPage: number;
11
+ total: number;
12
+ totalPages: number;
13
+ };
14
+ type CollectionResult = {
15
+ data: Pagination<SearchHit>;
16
+ facets: Facets;
17
+ };
18
+ type MultiSearchResult = {
19
+ [K in SearchCollection]?: CollectionResult;
20
+ };
21
+ type FilterByItem = {
22
+ title: string;
23
+ value: string;
24
+ };
25
+ type FilterBy = {
26
+ type: 'select' | 'multiSelect' | 'range';
27
+ title: string;
28
+ fieldId: string;
29
+ fieldKey: string;
30
+ enableFaceting: boolean;
31
+ values: FilterByItem[];
32
+ };
33
+ type OrderBy = {
34
+ title: string;
35
+ field: string;
36
+ direction: 'ASC' | 'DESC';
37
+ };
38
+ type PageSize = {
39
+ size: number;
40
+ };
41
+ type Facets = {
42
+ [key: string]: {
43
+ [key: string]: number;
44
+ };
45
+ };
46
+ type FilterQuery = {
47
+ [K: string]: string[];
48
+ };
49
+
50
+ interface SearchParams {
51
+ projectId?: string;
52
+ page?: number;
53
+ perPage?: number;
54
+ filters?: Record<string, unknown>;
55
+ facetBy?: string;
56
+ search?: string;
57
+ orderBy?: string;
58
+ collections?: SearchCollection[];
59
+ }
60
+ interface SearchClient {
61
+ performSearch(params: SearchParams): Promise<{
62
+ results: MultiSearchResult;
63
+ }>;
64
+ }
65
+ declare function createSearchClient(config: {
66
+ apiUrl: string;
67
+ apiKey?: string;
68
+ projectId?: string;
69
+ }): SearchClient;
70
+
71
+ export { type CollectionResult as C, type Facets as F, type MultiSearchResult as M, type OrderBy as O, type PageSize as P, type SearchClient as S, type FilterBy as a, type FilterByItem as b, type FilterQuery as c, type Pagination as d, type SearchCollection as e, type SearchHit as f, type SearchParams as g, createSearchClient as h };
@@ -0,0 +1,71 @@
1
+ type SearchCollection = 'entries' | 'compositions' | 'assets';
2
+ type SearchHit = {
3
+ id: string;
4
+ _collection: SearchCollection;
5
+ [key: string]: unknown;
6
+ };
7
+ type Pagination<Item> = {
8
+ items: Item[];
9
+ page: number;
10
+ perPage: number;
11
+ total: number;
12
+ totalPages: number;
13
+ };
14
+ type CollectionResult = {
15
+ data: Pagination<SearchHit>;
16
+ facets: Facets;
17
+ };
18
+ type MultiSearchResult = {
19
+ [K in SearchCollection]?: CollectionResult;
20
+ };
21
+ type FilterByItem = {
22
+ title: string;
23
+ value: string;
24
+ };
25
+ type FilterBy = {
26
+ type: 'select' | 'multiSelect' | 'range';
27
+ title: string;
28
+ fieldId: string;
29
+ fieldKey: string;
30
+ enableFaceting: boolean;
31
+ values: FilterByItem[];
32
+ };
33
+ type OrderBy = {
34
+ title: string;
35
+ field: string;
36
+ direction: 'ASC' | 'DESC';
37
+ };
38
+ type PageSize = {
39
+ size: number;
40
+ };
41
+ type Facets = {
42
+ [key: string]: {
43
+ [key: string]: number;
44
+ };
45
+ };
46
+ type FilterQuery = {
47
+ [K: string]: string[];
48
+ };
49
+
50
+ interface SearchParams {
51
+ projectId?: string;
52
+ page?: number;
53
+ perPage?: number;
54
+ filters?: Record<string, unknown>;
55
+ facetBy?: string;
56
+ search?: string;
57
+ orderBy?: string;
58
+ collections?: SearchCollection[];
59
+ }
60
+ interface SearchClient {
61
+ performSearch(params: SearchParams): Promise<{
62
+ results: MultiSearchResult;
63
+ }>;
64
+ }
65
+ declare function createSearchClient(config: {
66
+ apiUrl: string;
67
+ apiKey?: string;
68
+ projectId?: string;
69
+ }): SearchClient;
70
+
71
+ export { type CollectionResult as C, type Facets as F, type MultiSearchResult as M, type OrderBy as O, type PageSize as P, type SearchClient as S, type FilterBy as a, type FilterByItem as b, type FilterQuery as c, type Pagination as d, type SearchCollection as e, type SearchHit as f, type SearchParams as g, createSearchClient as h };
@@ -0,0 +1,16 @@
1
+ import { O as OrderBy } from './client-DrL-8TfY.mjs';
2
+ export { C as CollectionResult, F as Facets, a as FilterBy, b as FilterByItem, c as FilterQuery, M as MultiSearchResult, P as PageSize, d as Pagination, S as SearchClient, e as SearchCollection, f as SearchHit, g as SearchParams, h as createSearchClient } from './client-DrL-8TfY.mjs';
3
+
4
+ declare const UNIFORM_SEARCH_QUERY_KEY = "search";
5
+ declare const UNIFORM_SEARCH_PAGE_KEY = "page";
6
+ declare const UNIFORM_SEARCH_PAGE_SIZE_KEY = "pageSize";
7
+ declare const UNIFORM_SEARCH_ORDER_BY_KEY = "orderBy";
8
+ declare const FIRST_PAGE = 0;
9
+ declare const DEFAULT_PAGE_SIZE = 10;
10
+
11
+ declare const buildOrderByQuery: (orderBy: OrderBy) => string;
12
+ declare const getSearchParamsFromUrl: (urlString: string) => Record<string, string | string[]>;
13
+
14
+ declare function flattenBlockParams<T = Record<string, unknown>>(items: unknown, locale?: string): T[];
15
+
16
+ export { DEFAULT_PAGE_SIZE, FIRST_PAGE, OrderBy, UNIFORM_SEARCH_ORDER_BY_KEY, UNIFORM_SEARCH_PAGE_KEY, UNIFORM_SEARCH_PAGE_SIZE_KEY, UNIFORM_SEARCH_QUERY_KEY, buildOrderByQuery, flattenBlockParams, getSearchParamsFromUrl };
@@ -0,0 +1,16 @@
1
+ import { O as OrderBy } from './client-DrL-8TfY.js';
2
+ export { C as CollectionResult, F as Facets, a as FilterBy, b as FilterByItem, c as FilterQuery, M as MultiSearchResult, P as PageSize, d as Pagination, S as SearchClient, e as SearchCollection, f as SearchHit, g as SearchParams, h as createSearchClient } from './client-DrL-8TfY.js';
3
+
4
+ declare const UNIFORM_SEARCH_QUERY_KEY = "search";
5
+ declare const UNIFORM_SEARCH_PAGE_KEY = "page";
6
+ declare const UNIFORM_SEARCH_PAGE_SIZE_KEY = "pageSize";
7
+ declare const UNIFORM_SEARCH_ORDER_BY_KEY = "orderBy";
8
+ declare const FIRST_PAGE = 0;
9
+ declare const DEFAULT_PAGE_SIZE = 10;
10
+
11
+ declare const buildOrderByQuery: (orderBy: OrderBy) => string;
12
+ declare const getSearchParamsFromUrl: (urlString: string) => Record<string, string | string[]>;
13
+
14
+ declare function flattenBlockParams<T = Record<string, unknown>>(items: unknown, locale?: string): T[];
15
+
16
+ export { DEFAULT_PAGE_SIZE, FIRST_PAGE, OrderBy, UNIFORM_SEARCH_ORDER_BY_KEY, UNIFORM_SEARCH_PAGE_KEY, UNIFORM_SEARCH_PAGE_SIZE_KEY, UNIFORM_SEARCH_QUERY_KEY, buildOrderByQuery, flattenBlockParams, getSearchParamsFromUrl };
package/dist/index.js ADDED
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ DEFAULT_PAGE_SIZE: () => DEFAULT_PAGE_SIZE,
24
+ FIRST_PAGE: () => FIRST_PAGE,
25
+ UNIFORM_SEARCH_ORDER_BY_KEY: () => UNIFORM_SEARCH_ORDER_BY_KEY,
26
+ UNIFORM_SEARCH_PAGE_KEY: () => UNIFORM_SEARCH_PAGE_KEY,
27
+ UNIFORM_SEARCH_PAGE_SIZE_KEY: () => UNIFORM_SEARCH_PAGE_SIZE_KEY,
28
+ UNIFORM_SEARCH_QUERY_KEY: () => UNIFORM_SEARCH_QUERY_KEY,
29
+ buildOrderByQuery: () => buildOrderByQuery,
30
+ createSearchClient: () => createSearchClient,
31
+ flattenBlockParams: () => flattenBlockParams,
32
+ getSearchParamsFromUrl: () => getSearchParamsFromUrl
33
+ });
34
+ module.exports = __toCommonJS(src_exports);
35
+
36
+ // src/constants.ts
37
+ var UNIFORM_SEARCH_QUERY_KEY = "search";
38
+ var UNIFORM_SEARCH_PAGE_KEY = "page";
39
+ var UNIFORM_SEARCH_PAGE_SIZE_KEY = "pageSize";
40
+ var UNIFORM_SEARCH_ORDER_BY_KEY = "orderBy";
41
+ var FIRST_PAGE = 0;
42
+ var DEFAULT_PAGE_SIZE = 10;
43
+
44
+ // src/client.ts
45
+ function createSearchClient(config) {
46
+ return {
47
+ async performSearch(params) {
48
+ const headers = {
49
+ "Content-Type": "application/json"
50
+ };
51
+ if (config.apiKey) {
52
+ headers["x-api-key"] = config.apiKey;
53
+ }
54
+ const body = {
55
+ ...params,
56
+ projectId: params.projectId || config.projectId
57
+ };
58
+ try {
59
+ const response = await fetch(`${config.apiUrl}/api/search`, {
60
+ method: "POST",
61
+ headers,
62
+ body: JSON.stringify(body)
63
+ });
64
+ if (!response.ok) {
65
+ console.error(`[alex] Search API error: ${response.status}`);
66
+ return { results: {} };
67
+ }
68
+ return response.json();
69
+ } catch (error) {
70
+ console.error("[alex] Search fetch error:", error);
71
+ return { results: {} };
72
+ }
73
+ }
74
+ };
75
+ }
76
+
77
+ // src/utils.ts
78
+ var buildOrderByQuery = (orderBy) => {
79
+ return `${orderBy.field}_${orderBy.direction}`;
80
+ };
81
+ var getSearchParamsFromUrl = (urlString) => {
82
+ if (!urlString) {
83
+ return {};
84
+ }
85
+ const url = new URL(urlString);
86
+ const params = {};
87
+ url.searchParams.forEach((value, key) => {
88
+ if (params[key]) {
89
+ params[key] = Array.isArray(params[key]) ? [...params[key], value] : [params[key], value];
90
+ } else {
91
+ params[key] = value;
92
+ }
93
+ });
94
+ return params;
95
+ };
96
+
97
+ // src/flattenBlockParams.ts
98
+ function resolveFieldValue(field, locale = "en-us") {
99
+ var _a, _b;
100
+ if (!field || typeof field !== "object") return field;
101
+ if (field.type === "$block" && Array.isArray(field.value)) {
102
+ return flattenBlockParams(field.value, locale);
103
+ }
104
+ if (field.locales && typeof field.locales === "object") {
105
+ return (_b = (_a = field.locales[locale]) != null ? _a : Object.values(field.locales)[0]) != null ? _b : field.value;
106
+ }
107
+ return field.value;
108
+ }
109
+ function flattenBlockParams(items, locale = "en-us") {
110
+ if (!Array.isArray(items)) return [];
111
+ return items.map((item) => {
112
+ const result = {};
113
+ const blockItem = item;
114
+ if (blockItem.fields) {
115
+ for (const [key, field] of Object.entries(blockItem.fields)) {
116
+ result[key] = resolveFieldValue(field, locale);
117
+ }
118
+ }
119
+ return result;
120
+ });
121
+ }
122
+ // Annotate the CommonJS export names for ESM import in node:
123
+ 0 && (module.exports = {
124
+ DEFAULT_PAGE_SIZE,
125
+ FIRST_PAGE,
126
+ UNIFORM_SEARCH_ORDER_BY_KEY,
127
+ UNIFORM_SEARCH_PAGE_KEY,
128
+ UNIFORM_SEARCH_PAGE_SIZE_KEY,
129
+ UNIFORM_SEARCH_QUERY_KEY,
130
+ buildOrderByQuery,
131
+ createSearchClient,
132
+ flattenBlockParams,
133
+ getSearchParamsFromUrl
134
+ });
135
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/constants.ts","../src/client.ts","../src/utils.ts","../src/flattenBlockParams.ts"],"sourcesContent":["export * from './types';\nexport * from './constants';\nexport * from './client';\nexport * from './utils';\nexport * from './flattenBlockParams';\n","export const UNIFORM_SEARCH_QUERY_KEY = 'search';\nexport const UNIFORM_SEARCH_PAGE_KEY = 'page';\nexport const UNIFORM_SEARCH_PAGE_SIZE_KEY = 'pageSize';\nexport const UNIFORM_SEARCH_ORDER_BY_KEY = 'orderBy';\nexport const FIRST_PAGE = 0;\nexport const DEFAULT_PAGE_SIZE = 10;\n","import type { SearchCollection, MultiSearchResult } from './types';\n\nexport interface SearchParams {\n projectId?: string;\n page?: number;\n perPage?: number;\n filters?: Record<string, unknown>;\n facetBy?: string;\n search?: string;\n orderBy?: string;\n collections?: SearchCollection[];\n}\n\nexport interface SearchClient {\n performSearch(params: SearchParams): Promise<{ results: MultiSearchResult }>;\n}\n\nexport function createSearchClient(config: {\n apiUrl: string;\n apiKey?: string;\n projectId?: string;\n}): SearchClient {\n return {\n async performSearch(params) {\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n };\n if (config.apiKey) {\n headers['x-api-key'] = config.apiKey;\n }\n\n const body = {\n ...params,\n projectId: params.projectId || config.projectId,\n };\n\n try {\n const response = await fetch(`${config.apiUrl}/api/search`, {\n method: 'POST',\n headers,\n body: JSON.stringify(body),\n });\n\n if (!response.ok) {\n console.error(`[alex] Search API error: ${response.status}`);\n return { results: {} };\n }\n\n return response.json();\n } catch (error) {\n console.error('[alex] Search fetch error:', error);\n return { results: {} };\n }\n },\n };\n}\n","import type { OrderBy } from './types';\n\nexport const buildOrderByQuery = (orderBy: OrderBy) => {\n return `${orderBy.field}_${orderBy.direction}`;\n};\n\nexport const getSearchParamsFromUrl = (urlString: string): Record<string, string | string[]> => {\n if (!urlString) {\n return {};\n }\n\n const url = new URL(urlString);\n const params: Record<string, string | string[]> = {};\n\n url.searchParams.forEach((value, key) => {\n if (params[key]) {\n params[key] = Array.isArray(params[key])\n ? [...(params[key] as string[]), value]\n : [params[key] as string, value];\n } else {\n params[key] = value;\n }\n });\n\n return params;\n};\n","type RawField = {\n type?: string;\n value?: unknown;\n locales?: Record<string, unknown>;\n};\n\ntype RawBlockItem = {\n type?: string;\n fields?: Record<string, RawField>;\n};\n\nfunction resolveFieldValue(field: RawField, locale = 'en-us'): unknown {\n if (!field || typeof field !== 'object') return field;\n\n if (field.type === '$block' && Array.isArray(field.value)) {\n return flattenBlockParams(field.value as RawBlockItem[], locale);\n }\n\n if (field.locales && typeof field.locales === 'object') {\n return field.locales[locale] ?? Object.values(field.locales)[0] ?? field.value;\n }\n\n return field.value;\n}\n\nexport function flattenBlockParams<T = Record<string, unknown>>(\n items: unknown,\n locale = 'en-us'\n): T[] {\n if (!Array.isArray(items)) return [];\n return items.map(item => {\n const result: Record<string, unknown> = {};\n const blockItem = item as RawBlockItem;\n if (blockItem.fields) {\n for (const [key, field] of Object.entries(blockItem.fields)) {\n result[key] = resolveFieldValue(field, locale);\n }\n }\n return result as T;\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,2BAA2B;AACjC,IAAM,0BAA0B;AAChC,IAAM,+BAA+B;AACrC,IAAM,8BAA8B;AACpC,IAAM,aAAa;AACnB,IAAM,oBAAoB;;;ACY1B,SAAS,mBAAmB,QAIlB;AACf,SAAO;AAAA,IACL,MAAM,cAAc,QAAQ;AAC1B,YAAM,UAAkC;AAAA,QACtC,gBAAgB;AAAA,MAClB;AACA,UAAI,OAAO,QAAQ;AACjB,gBAAQ,WAAW,IAAI,OAAO;AAAA,MAChC;AAEA,YAAM,OAAO;AAAA,QACX,GAAG;AAAA,QACH,WAAW,OAAO,aAAa,OAAO;AAAA,MACxC;AAEA,UAAI;AACF,cAAM,WAAW,MAAM,MAAM,GAAG,OAAO,MAAM,eAAe;AAAA,UAC1D,QAAQ;AAAA,UACR;AAAA,UACA,MAAM,KAAK,UAAU,IAAI;AAAA,QAC3B,CAAC;AAED,YAAI,CAAC,SAAS,IAAI;AAChB,kBAAQ,MAAM,4BAA4B,SAAS,MAAM,EAAE;AAC3D,iBAAO,EAAE,SAAS,CAAC,EAAE;AAAA,QACvB;AAEA,eAAO,SAAS,KAAK;AAAA,MACvB,SAAS,OAAO;AACd,gBAAQ,MAAM,8BAA8B,KAAK;AACjD,eAAO,EAAE,SAAS,CAAC,EAAE;AAAA,MACvB;AAAA,IACF;AAAA,EACF;AACF;;;ACrDO,IAAM,oBAAoB,CAAC,YA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;AACA,WAAO;AAAA,EACT,CAAC;AACH;","names":[]}
package/dist/index.mjs ADDED
@@ -0,0 +1,57 @@
1
+ import {
2
+ DEFAULT_PAGE_SIZE,
3
+ FIRST_PAGE,
4
+ UNIFORM_SEARCH_ORDER_BY_KEY,
5
+ UNIFORM_SEARCH_PAGE_KEY,
6
+ UNIFORM_SEARCH_PAGE_SIZE_KEY,
7
+ UNIFORM_SEARCH_QUERY_KEY,
8
+ buildOrderByQuery,
9
+ flattenBlockParams,
10
+ getSearchParamsFromUrl
11
+ } from "./chunk-ZABEFR2D.mjs";
12
+
13
+ // src/client.ts
14
+ function createSearchClient(config) {
15
+ return {
16
+ async performSearch(params) {
17
+ const headers = {
18
+ "Content-Type": "application/json"
19
+ };
20
+ if (config.apiKey) {
21
+ headers["x-api-key"] = config.apiKey;
22
+ }
23
+ const body = {
24
+ ...params,
25
+ projectId: params.projectId || config.projectId
26
+ };
27
+ try {
28
+ const response = await fetch(`${config.apiUrl}/api/search`, {
29
+ method: "POST",
30
+ headers,
31
+ body: JSON.stringify(body)
32
+ });
33
+ if (!response.ok) {
34
+ console.error(`[alex] Search API error: ${response.status}`);
35
+ return { results: {} };
36
+ }
37
+ return response.json();
38
+ } catch (error) {
39
+ console.error("[alex] Search fetch error:", error);
40
+ return { results: {} };
41
+ }
42
+ }
43
+ };
44
+ }
45
+ export {
46
+ DEFAULT_PAGE_SIZE,
47
+ FIRST_PAGE,
48
+ UNIFORM_SEARCH_ORDER_BY_KEY,
49
+ UNIFORM_SEARCH_PAGE_KEY,
50
+ UNIFORM_SEARCH_PAGE_SIZE_KEY,
51
+ UNIFORM_SEARCH_QUERY_KEY,
52
+ buildOrderByQuery,
53
+ createSearchClient,
54
+ flattenBlockParams,
55
+ getSearchParamsFromUrl
56
+ };
57
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/client.ts"],"sourcesContent":["import type { SearchCollection, MultiSearchResult } from './types';\n\nexport interface SearchParams {\n projectId?: string;\n page?: number;\n perPage?: number;\n filters?: Record<string, unknown>;\n facetBy?: string;\n search?: string;\n orderBy?: string;\n collections?: SearchCollection[];\n}\n\nexport interface SearchClient {\n performSearch(params: SearchParams): Promise<{ results: MultiSearchResult }>;\n}\n\nexport function createSearchClient(config: {\n apiUrl: string;\n apiKey?: string;\n projectId?: string;\n}): SearchClient {\n return {\n async performSearch(params) {\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n };\n if (config.apiKey) {\n headers['x-api-key'] = config.apiKey;\n }\n\n const body = {\n ...params,\n projectId: params.projectId || config.projectId,\n };\n\n try {\n const response = await fetch(`${config.apiUrl}/api/search`, {\n method: 'POST',\n headers,\n body: JSON.stringify(body),\n });\n\n if (!response.ok) {\n console.error(`[alex] Search API error: ${response.status}`);\n return { results: {} };\n }\n\n return response.json();\n } catch (error) {\n console.error('[alex] Search fetch error:', error);\n return { results: {} };\n }\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;AAiBO,SAAS,mBAAmB,QAIlB;AACf,SAAO;AAAA,IACL,MAAM,cAAc,QAAQ;AAC1B,YAAM,UAAkC;AAAA,QACtC,gBAAgB;AAAA,MAClB;AACA,UAAI,OAAO,QAAQ;AACjB,gBAAQ,WAAW,IAAI,OAAO;AAAA,MAChC;AAEA,YAAM,OAAO;AAAA,QACX,GAAG;AAAA,QACH,WAAW,OAAO,aAAa,OAAO;AAAA,MACxC;AAEA,UAAI;AACF,cAAM,WAAW,MAAM,MAAM,GAAG,OAAO,MAAM,eAAe;AAAA,UAC1D,QAAQ;AAAA,UACR;AAAA,UACA,MAAM,KAAK,UAAU,IAAI;AAAA,QAC3B,CAAC;AAED,YAAI,CAAC,SAAS,IAAI;AAChB,kBAAQ,MAAM,4BAA4B,SAAS,MAAM,EAAE;AAC3D,iBAAO,EAAE,SAAS,CAAC,EAAE;AAAA,QACvB;AAEA,eAAO,SAAS,KAAK;AAAA,MACvB,SAAS,OAAO;AACd,gBAAQ,MAAM,8BAA8B,KAAK;AACjD,eAAO,EAAE,SAAS,CAAC,EAAE;AAAA,MACvB;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
@@ -0,0 +1,68 @@
1
+ import { FC, ReactNode } from 'react';
2
+ import { g as SearchParams, M as MultiSearchResult, d as Pagination, f as SearchHit, F as Facets, P as PageSize, a as FilterBy, e as SearchCollection } from './client-DrL-8TfY.mjs';
3
+
4
+ interface SearchProviderProps {
5
+ children: ReactNode;
6
+ performSearch: (params: SearchParams) => Promise<{
7
+ results: MultiSearchResult;
8
+ }>;
9
+ contentType?: string;
10
+ filterBy?: unknown;
11
+ orderBy?: unknown;
12
+ baseFilters?: unknown;
13
+ pageSizes?: unknown;
14
+ collections?: string | string[];
15
+ searchDebounceMs?: number;
16
+ }
17
+ interface UseSearchReturn {
18
+ results: Pagination<SearchHit>;
19
+ facets: Facets | null;
20
+ isLoading: boolean;
21
+ collectionResults: MultiSearchResult;
22
+ searchBoxValue: string;
23
+ setSearchQuery: (value: string) => void;
24
+ page: number;
25
+ setPage: (page: number) => void;
26
+ pageSize: number;
27
+ setPageSize: (size: number) => void;
28
+ pageSizes: PageSize[];
29
+ orderByOptions: Array<{
30
+ title: string;
31
+ value: string;
32
+ }>;
33
+ selectedOrderBy: string;
34
+ setOrderBy: (query: string) => void;
35
+ filterOptions: FilterBy[];
36
+ selectedFilters: Record<string, string[]>;
37
+ setSelectedFilters: (filters: Record<string, string[]>) => void;
38
+ clearFilters: () => void;
39
+ selectedCollections: SearchCollection[];
40
+ setSelectedCollections: (collections: SearchCollection[]) => void;
41
+ formatResultsSummary: (template: string) => string;
42
+ }
43
+ declare const SearchProvider: FC<SearchProviderProps>;
44
+
45
+ declare function useSearch(): UseSearchReturn;
46
+
47
+ declare const DOTS = "...";
48
+ interface UsePaginationParams {
49
+ currentPage: number;
50
+ totalCount: number;
51
+ perPage: number;
52
+ siblingCount?: number;
53
+ }
54
+ declare const usePagination: ({ totalCount, perPage, siblingCount, currentPage }: UsePaginationParams) => (string | number)[] | undefined;
55
+
56
+ interface UseSearchPaginationReturn {
57
+ pages: (number | string)[];
58
+ currentPage: number;
59
+ hasPrev: boolean;
60
+ hasNext: boolean;
61
+ goToPage: (page: number) => void;
62
+ goToPrev: () => void;
63
+ goToNext: () => void;
64
+ isLoading: boolean;
65
+ }
66
+ declare function useSearchPagination(siblingCount?: number): UseSearchPaginationReturn;
67
+
68
+ export { DOTS, SearchProvider, type SearchProviderProps, type UseSearchPaginationReturn, type UseSearchReturn, usePagination, useSearch, useSearchPagination };
@@ -0,0 +1,68 @@
1
+ import { FC, ReactNode } from 'react';
2
+ import { g as SearchParams, M as MultiSearchResult, d as Pagination, f as SearchHit, F as Facets, P as PageSize, a as FilterBy, e as SearchCollection } from './client-DrL-8TfY.js';
3
+
4
+ interface SearchProviderProps {
5
+ children: ReactNode;
6
+ performSearch: (params: SearchParams) => Promise<{
7
+ results: MultiSearchResult;
8
+ }>;
9
+ contentType?: string;
10
+ filterBy?: unknown;
11
+ orderBy?: unknown;
12
+ baseFilters?: unknown;
13
+ pageSizes?: unknown;
14
+ collections?: string | string[];
15
+ searchDebounceMs?: number;
16
+ }
17
+ interface UseSearchReturn {
18
+ results: Pagination<SearchHit>;
19
+ facets: Facets | null;
20
+ isLoading: boolean;
21
+ collectionResults: MultiSearchResult;
22
+ searchBoxValue: string;
23
+ setSearchQuery: (value: string) => void;
24
+ page: number;
25
+ setPage: (page: number) => void;
26
+ pageSize: number;
27
+ setPageSize: (size: number) => void;
28
+ pageSizes: PageSize[];
29
+ orderByOptions: Array<{
30
+ title: string;
31
+ value: string;
32
+ }>;
33
+ selectedOrderBy: string;
34
+ setOrderBy: (query: string) => void;
35
+ filterOptions: FilterBy[];
36
+ selectedFilters: Record<string, string[]>;
37
+ setSelectedFilters: (filters: Record<string, string[]>) => void;
38
+ clearFilters: () => void;
39
+ selectedCollections: SearchCollection[];
40
+ setSelectedCollections: (collections: SearchCollection[]) => void;
41
+ formatResultsSummary: (template: string) => string;
42
+ }
43
+ declare const SearchProvider: FC<SearchProviderProps>;
44
+
45
+ declare function useSearch(): UseSearchReturn;
46
+
47
+ declare const DOTS = "...";
48
+ interface UsePaginationParams {
49
+ currentPage: number;
50
+ totalCount: number;
51
+ perPage: number;
52
+ siblingCount?: number;
53
+ }
54
+ declare const usePagination: ({ totalCount, perPage, siblingCount, currentPage }: UsePaginationParams) => (string | number)[] | undefined;
55
+
56
+ interface UseSearchPaginationReturn {
57
+ pages: (number | string)[];
58
+ currentPage: number;
59
+ hasPrev: boolean;
60
+ hasNext: boolean;
61
+ goToPage: (page: number) => void;
62
+ goToPrev: () => void;
63
+ goToNext: () => void;
64
+ isLoading: boolean;
65
+ }
66
+ declare function useSearchPagination(siblingCount?: number): UseSearchPaginationReturn;
67
+
68
+ export { DOTS, SearchProvider, type SearchProviderProps, type UseSearchPaginationReturn, type UseSearchReturn, usePagination, useSearch, useSearchPagination };