@23blocks/block-search 1.0.2 → 1.0.4
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/dist/index.d.ts +1 -0
- package/dist/index.esm.js +497 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/lib/block-search.d.ts +2 -0
- package/dist/src/lib/block-search.d.ts.map +1 -0
- package/dist/{lib/mappers/index.js → src/lib/mappers/index.d.ts} +2 -3
- package/dist/src/lib/mappers/index.d.ts.map +1 -0
- package/dist/src/lib/mappers/search.mapper.d.ts +23 -0
- package/dist/src/lib/mappers/search.mapper.d.ts.map +1 -0
- package/dist/src/lib/mappers/utils.d.ts +21 -0
- package/dist/src/lib/mappers/utils.d.ts.map +1 -0
- package/dist/{lib/search.block.js → src/lib/search.block.d.ts} +37 -21
- package/dist/src/lib/search.block.d.ts.map +1 -0
- package/dist/src/lib/services/index.d.ts +2 -0
- package/dist/src/lib/services/index.d.ts.map +1 -0
- package/dist/src/lib/services/search.service.d.ts +79 -0
- package/dist/src/lib/services/search.service.d.ts.map +1 -0
- package/dist/src/lib/types/index.d.ts +2 -0
- package/dist/src/lib/types/index.d.ts.map +1 -0
- package/dist/src/lib/types/search.d.ts +152 -0
- package/dist/src/lib/types/search.d.ts.map +1 -0
- package/package.json +10 -8
- package/dist/index.js +0 -6
- package/dist/index.js.map +0 -1
- package/dist/lib/block-search.js +0 -5
- package/dist/lib/block-search.js.map +0 -1
- package/dist/lib/mappers/index.js.map +0 -1
- package/dist/lib/mappers/search.mapper.js +0 -174
- package/dist/lib/mappers/search.mapper.js.map +0 -1
- package/dist/lib/mappers/utils.js +0 -57
- package/dist/lib/mappers/utils.js.map +0 -1
- package/dist/lib/search.block.js.map +0 -1
- package/dist/lib/services/index.js +0 -3
- package/dist/lib/services/index.js.map +0 -1
- package/dist/lib/services/search.service.js +0 -209
- package/dist/lib/services/search.service.js.map +0 -1
- package/dist/lib/types/index.js +0 -3
- package/dist/lib/types/index.js.map +0 -1
- package/dist/lib/types/search.js +0 -5
- package/dist/lib/types/search.js.map +0 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/index";
|
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
import { decodeOne, decodePageResult, decodeMany } from '@23blocks/jsonapi-codec';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Parse a string value, returning null for empty/undefined
|
|
5
|
+
*/ function parseString(value) {
|
|
6
|
+
if (value === null || value === undefined) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
const str = String(value);
|
|
10
|
+
return str.length > 0 ? str : null;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Parse a date value
|
|
14
|
+
*/ function parseDate(value) {
|
|
15
|
+
if (value === null || value === undefined) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
if (value instanceof Date) {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
22
|
+
const date = new Date(value);
|
|
23
|
+
return isNaN(date.getTime()) ? null : date;
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Parse a boolean value
|
|
29
|
+
*/ function parseBoolean(value) {
|
|
30
|
+
if (typeof value === 'boolean') {
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
if (value === 'true' || value === '1' || value === 1) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Parse an array of strings
|
|
40
|
+
*/ function parseStringArray(value) {
|
|
41
|
+
if (value === null || value === undefined) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
if (Array.isArray(value)) {
|
|
45
|
+
return value.map(String);
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Parse a number value
|
|
51
|
+
*/ function parseNumber(value) {
|
|
52
|
+
if (value === null || value === undefined) {
|
|
53
|
+
return 0;
|
|
54
|
+
}
|
|
55
|
+
const num = Number(value);
|
|
56
|
+
return isNaN(num) ? 0 : num;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Mapper for SearchResult resources
|
|
61
|
+
*/ const searchResultMapper = {
|
|
62
|
+
type: 'SearchResult',
|
|
63
|
+
map (resource, _included) {
|
|
64
|
+
var _resource_attributes;
|
|
65
|
+
const attrs = (_resource_attributes = resource.attributes) != null ? _resource_attributes : {};
|
|
66
|
+
var _parseString, _parseDate, _parseDate1, _attrs_entity_unique_id, _attrs_entity_type, _parseString1;
|
|
67
|
+
return {
|
|
68
|
+
id: resource.id,
|
|
69
|
+
uniqueId: (_parseString = parseString(attrs.unique_id)) != null ? _parseString : resource.id,
|
|
70
|
+
createdAt: (_parseDate = parseDate(attrs.created_at)) != null ? _parseDate : new Date(),
|
|
71
|
+
updatedAt: (_parseDate1 = parseDate(attrs.updated_at)) != null ? _parseDate1 : new Date(),
|
|
72
|
+
partition: parseString(attrs.partition),
|
|
73
|
+
key: parseString(attrs.key),
|
|
74
|
+
weight: parseNumber(attrs.weight),
|
|
75
|
+
relevance: parseNumber(attrs.relevance),
|
|
76
|
+
uri: parseString(attrs.uri),
|
|
77
|
+
origin: parseString(attrs.origin),
|
|
78
|
+
entityUniqueId: String((_attrs_entity_unique_id = attrs.entity_unique_id) != null ? _attrs_entity_unique_id : ''),
|
|
79
|
+
entityAlias: parseString(attrs.entity_alias),
|
|
80
|
+
entityDescription: parseString(attrs.entity_description),
|
|
81
|
+
entityAvatarUrl: parseString(attrs.entity_avatar_url),
|
|
82
|
+
entityType: String((_attrs_entity_type = attrs.entity_type) != null ? _attrs_entity_type : ''),
|
|
83
|
+
entitySource: parseString(attrs.entity_source),
|
|
84
|
+
entityUrl: parseString(attrs.entity_url),
|
|
85
|
+
counter: parseNumber(attrs.counter),
|
|
86
|
+
favorite: parseBoolean(attrs.favorite),
|
|
87
|
+
status: (_parseString1 = parseString(attrs.status)) != null ? _parseString1 : 'active',
|
|
88
|
+
payload: attrs.payload
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Mapper for SearchQuery resources
|
|
94
|
+
*/ const searchQueryMapper = {
|
|
95
|
+
type: 'Query',
|
|
96
|
+
map (resource, _included) {
|
|
97
|
+
var _resource_attributes;
|
|
98
|
+
const attrs = (_resource_attributes = resource.attributes) != null ? _resource_attributes : {};
|
|
99
|
+
var _parseString, _parseDate, _parseDate1, _attrs_query;
|
|
100
|
+
return {
|
|
101
|
+
id: resource.id,
|
|
102
|
+
uniqueId: (_parseString = parseString(attrs.unique_id)) != null ? _parseString : resource.id,
|
|
103
|
+
createdAt: (_parseDate = parseDate(attrs.created_at)) != null ? _parseDate : new Date(),
|
|
104
|
+
updatedAt: (_parseDate1 = parseDate(attrs.updated_at)) != null ? _parseDate1 : new Date(),
|
|
105
|
+
partition: parseString(attrs.partition),
|
|
106
|
+
key: parseString(attrs.key),
|
|
107
|
+
query: String((_attrs_query = attrs.query) != null ? _attrs_query : ''),
|
|
108
|
+
include: parseStringArray(attrs.include),
|
|
109
|
+
exclude: parseStringArray(attrs.exclude),
|
|
110
|
+
payload: attrs.payload,
|
|
111
|
+
userUniqueId: parseString(attrs.user_unique_id),
|
|
112
|
+
userProviderName: parseString(attrs.user_provider_name),
|
|
113
|
+
userName: parseString(attrs.user_name),
|
|
114
|
+
userEmail: parseString(attrs.user_email),
|
|
115
|
+
userRole: parseString(attrs.user_role),
|
|
116
|
+
userRoleUniqueId: parseString(attrs.user_role_unique_id),
|
|
117
|
+
userRoleName: parseString(attrs.user_role_name),
|
|
118
|
+
queryString: parseString(attrs.query_string),
|
|
119
|
+
userAgent: parseString(attrs.user_agent),
|
|
120
|
+
url: parseString(attrs.url),
|
|
121
|
+
path: parseString(attrs.path),
|
|
122
|
+
ipaddress: parseString(attrs.ipaddress),
|
|
123
|
+
origin: parseString(attrs.origin),
|
|
124
|
+
source: parseString(attrs.source),
|
|
125
|
+
submittedAt: parseDate(attrs.submitted_at),
|
|
126
|
+
device: parseString(attrs.device),
|
|
127
|
+
browser: parseString(attrs.browser),
|
|
128
|
+
hash: parseString(attrs.hash),
|
|
129
|
+
elapsedTime: attrs.elapsed_time != null ? parseNumber(attrs.elapsed_time) : null,
|
|
130
|
+
startedAt: parseDate(attrs.started_at),
|
|
131
|
+
endedAt: parseDate(attrs.ended_at),
|
|
132
|
+
totalRecords: parseNumber(attrs.total_records),
|
|
133
|
+
totalRecordsReturned: parseNumber(attrs.total_records_returned),
|
|
134
|
+
queryOrigin: parseString(attrs.query_origin)
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* Mapper for LastQuery resources
|
|
140
|
+
*/ const lastQueryMapper = {
|
|
141
|
+
type: 'LastQuery',
|
|
142
|
+
map (resource, _included) {
|
|
143
|
+
var _resource_attributes;
|
|
144
|
+
const attrs = (_resource_attributes = resource.attributes) != null ? _resource_attributes : {};
|
|
145
|
+
var _parseString, _parseDate, _parseDate1, _attrs_query;
|
|
146
|
+
return {
|
|
147
|
+
id: resource.id,
|
|
148
|
+
uniqueId: (_parseString = parseString(attrs.unique_id)) != null ? _parseString : resource.id,
|
|
149
|
+
createdAt: (_parseDate = parseDate(attrs.created_at)) != null ? _parseDate : new Date(),
|
|
150
|
+
updatedAt: (_parseDate1 = parseDate(attrs.updated_at)) != null ? _parseDate1 : new Date(),
|
|
151
|
+
partition: parseString(attrs.partition),
|
|
152
|
+
key: parseString(attrs.key),
|
|
153
|
+
query: String((_attrs_query = attrs.query) != null ? _attrs_query : ''),
|
|
154
|
+
include: parseStringArray(attrs.include),
|
|
155
|
+
exclude: parseStringArray(attrs.exclude),
|
|
156
|
+
payload: attrs.payload,
|
|
157
|
+
userUniqueId: parseString(attrs.user_unique_id),
|
|
158
|
+
userProviderName: parseString(attrs.user_provider_name),
|
|
159
|
+
userName: parseString(attrs.user_name),
|
|
160
|
+
userEmail: parseString(attrs.user_email),
|
|
161
|
+
userRole: parseString(attrs.user_role),
|
|
162
|
+
userRoleUniqueId: parseString(attrs.user_role_unique_id),
|
|
163
|
+
userRoleName: parseString(attrs.user_role_name),
|
|
164
|
+
queryString: parseString(attrs.query_string),
|
|
165
|
+
userAgent: parseString(attrs.user_agent),
|
|
166
|
+
url: parseString(attrs.url),
|
|
167
|
+
path: parseString(attrs.path),
|
|
168
|
+
ipaddress: parseString(attrs.ipaddress),
|
|
169
|
+
origin: parseString(attrs.origin),
|
|
170
|
+
source: parseString(attrs.source),
|
|
171
|
+
submittedAt: parseDate(attrs.submitted_at),
|
|
172
|
+
device: parseString(attrs.device),
|
|
173
|
+
browser: parseString(attrs.browser),
|
|
174
|
+
hash: parseString(attrs.hash),
|
|
175
|
+
elapsedTime: attrs.elapsed_time != null ? parseNumber(attrs.elapsed_time) : null,
|
|
176
|
+
startedAt: parseDate(attrs.started_at),
|
|
177
|
+
endedAt: parseDate(attrs.ended_at),
|
|
178
|
+
totalRecords: parseNumber(attrs.total_records),
|
|
179
|
+
totalRecordsReturned: parseNumber(attrs.total_records_returned),
|
|
180
|
+
queryOrigin: parseString(attrs.query_origin)
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* Mapper for FavoriteEntity resources
|
|
186
|
+
*/ const favoriteEntityMapper = {
|
|
187
|
+
type: 'FavoriteEntity',
|
|
188
|
+
map (resource, _included) {
|
|
189
|
+
var _resource_attributes;
|
|
190
|
+
const attrs = (_resource_attributes = resource.attributes) != null ? _resource_attributes : {};
|
|
191
|
+
var _parseString, _parseDate, _parseDate1, _attrs_entity_unique_id, _attrs_entity_type, _parseString1;
|
|
192
|
+
return {
|
|
193
|
+
id: resource.id,
|
|
194
|
+
uniqueId: (_parseString = parseString(attrs.unique_id)) != null ? _parseString : resource.id,
|
|
195
|
+
createdAt: (_parseDate = parseDate(attrs.created_at)) != null ? _parseDate : new Date(),
|
|
196
|
+
updatedAt: (_parseDate1 = parseDate(attrs.updated_at)) != null ? _parseDate1 : new Date(),
|
|
197
|
+
partition: parseString(attrs.partition),
|
|
198
|
+
key: parseString(attrs.key),
|
|
199
|
+
weight: parseNumber(attrs.weight),
|
|
200
|
+
relevance: parseNumber(attrs.relevance),
|
|
201
|
+
uri: parseString(attrs.uri),
|
|
202
|
+
origin: parseString(attrs.origin),
|
|
203
|
+
entityUniqueId: String((_attrs_entity_unique_id = attrs.entity_unique_id) != null ? _attrs_entity_unique_id : ''),
|
|
204
|
+
entityType: String((_attrs_entity_type = attrs.entity_type) != null ? _attrs_entity_type : ''),
|
|
205
|
+
entityAlias: parseString(attrs.entity_alias),
|
|
206
|
+
entitySource: parseString(attrs.entity_source),
|
|
207
|
+
entityUrl: parseString(attrs.entity_url),
|
|
208
|
+
entityAvatarUrl: parseString(attrs.entity_avatar_url),
|
|
209
|
+
counter: parseNumber(attrs.counter),
|
|
210
|
+
favorite: parseBoolean(attrs.favorite),
|
|
211
|
+
status: (_parseString1 = parseString(attrs.status)) != null ? _parseString1 : 'active'
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* Mapper for EntityType resources
|
|
217
|
+
*/ const entityTypeMapper = {
|
|
218
|
+
type: 'EntityType',
|
|
219
|
+
map (resource, _included) {
|
|
220
|
+
var _resource_attributes;
|
|
221
|
+
const attrs = (_resource_attributes = resource.attributes) != null ? _resource_attributes : {};
|
|
222
|
+
var _attrs_entity_type;
|
|
223
|
+
return {
|
|
224
|
+
id: resource.id,
|
|
225
|
+
entityType: String((_attrs_entity_type = attrs.entity_type) != null ? _attrs_entity_type : ''),
|
|
226
|
+
entitySource: parseString(attrs.entity_source)
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Build filter params for list operations
|
|
233
|
+
*/ function buildListParams(params) {
|
|
234
|
+
if (!params) return {};
|
|
235
|
+
const queryParams = {};
|
|
236
|
+
if (params.page) {
|
|
237
|
+
queryParams['page[number]'] = params.page;
|
|
238
|
+
}
|
|
239
|
+
if (params.perPage) {
|
|
240
|
+
queryParams['page[size]'] = params.perPage;
|
|
241
|
+
}
|
|
242
|
+
if (params.sort) {
|
|
243
|
+
const sorts = Array.isArray(params.sort) ? params.sort : [
|
|
244
|
+
params.sort
|
|
245
|
+
];
|
|
246
|
+
queryParams['sort'] = sorts.map((s)=>s.direction === 'desc' ? `-${s.field}` : s.field).join(',');
|
|
247
|
+
}
|
|
248
|
+
if (params.filter) {
|
|
249
|
+
for (const [key, value] of Object.entries(params.filter)){
|
|
250
|
+
queryParams[`filter[${key}]`] = value;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return queryParams;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Create the search service
|
|
257
|
+
*/ function createSearchService(transport, _config) {
|
|
258
|
+
return {
|
|
259
|
+
async search (request) {
|
|
260
|
+
var _response_meta, _response_meta1, _response_meta2;
|
|
261
|
+
const response = await transport.post('/search', {
|
|
262
|
+
query: request.query,
|
|
263
|
+
entity_types: request.entityTypes,
|
|
264
|
+
include: request.include,
|
|
265
|
+
exclude: request.exclude,
|
|
266
|
+
limit: request.limit,
|
|
267
|
+
offset: request.offset
|
|
268
|
+
});
|
|
269
|
+
const results = decodeMany(response, searchResultMapper);
|
|
270
|
+
// Extract query from meta if available
|
|
271
|
+
let query;
|
|
272
|
+
if ((_response_meta = response.meta) == null ? void 0 : _response_meta.query) {
|
|
273
|
+
var _request_include, _request_exclude, _response_meta_elapsed_time, _response_meta_total_records;
|
|
274
|
+
// The query comes in meta for search responses
|
|
275
|
+
query = {
|
|
276
|
+
id: '',
|
|
277
|
+
uniqueId: '',
|
|
278
|
+
createdAt: new Date(),
|
|
279
|
+
updatedAt: new Date(),
|
|
280
|
+
partition: null,
|
|
281
|
+
key: null,
|
|
282
|
+
query: request.query,
|
|
283
|
+
include: (_request_include = request.include) != null ? _request_include : null,
|
|
284
|
+
exclude: (_request_exclude = request.exclude) != null ? _request_exclude : null,
|
|
285
|
+
payload: null,
|
|
286
|
+
userUniqueId: null,
|
|
287
|
+
userProviderName: null,
|
|
288
|
+
userName: null,
|
|
289
|
+
userEmail: null,
|
|
290
|
+
userRole: null,
|
|
291
|
+
userRoleUniqueId: null,
|
|
292
|
+
userRoleName: null,
|
|
293
|
+
queryString: null,
|
|
294
|
+
userAgent: null,
|
|
295
|
+
url: null,
|
|
296
|
+
path: null,
|
|
297
|
+
ipaddress: null,
|
|
298
|
+
origin: null,
|
|
299
|
+
source: null,
|
|
300
|
+
submittedAt: new Date(),
|
|
301
|
+
device: null,
|
|
302
|
+
browser: null,
|
|
303
|
+
hash: null,
|
|
304
|
+
elapsedTime: (_response_meta_elapsed_time = response.meta.elapsed_time) != null ? _response_meta_elapsed_time : null,
|
|
305
|
+
startedAt: null,
|
|
306
|
+
endedAt: null,
|
|
307
|
+
totalRecords: (_response_meta_total_records = response.meta.total_records) != null ? _response_meta_total_records : results.length,
|
|
308
|
+
totalRecordsReturned: results.length,
|
|
309
|
+
queryOrigin: null
|
|
310
|
+
};
|
|
311
|
+
} else {
|
|
312
|
+
var _request_include1, _request_exclude1;
|
|
313
|
+
query = {
|
|
314
|
+
id: '',
|
|
315
|
+
uniqueId: '',
|
|
316
|
+
createdAt: new Date(),
|
|
317
|
+
updatedAt: new Date(),
|
|
318
|
+
partition: null,
|
|
319
|
+
key: null,
|
|
320
|
+
query: request.query,
|
|
321
|
+
include: (_request_include1 = request.include) != null ? _request_include1 : null,
|
|
322
|
+
exclude: (_request_exclude1 = request.exclude) != null ? _request_exclude1 : null,
|
|
323
|
+
payload: null,
|
|
324
|
+
userUniqueId: null,
|
|
325
|
+
userProviderName: null,
|
|
326
|
+
userName: null,
|
|
327
|
+
userEmail: null,
|
|
328
|
+
userRole: null,
|
|
329
|
+
userRoleUniqueId: null,
|
|
330
|
+
userRoleName: null,
|
|
331
|
+
queryString: null,
|
|
332
|
+
userAgent: null,
|
|
333
|
+
url: null,
|
|
334
|
+
path: null,
|
|
335
|
+
ipaddress: null,
|
|
336
|
+
origin: null,
|
|
337
|
+
source: null,
|
|
338
|
+
submittedAt: new Date(),
|
|
339
|
+
device: null,
|
|
340
|
+
browser: null,
|
|
341
|
+
hash: null,
|
|
342
|
+
elapsedTime: null,
|
|
343
|
+
startedAt: null,
|
|
344
|
+
endedAt: null,
|
|
345
|
+
totalRecords: results.length,
|
|
346
|
+
totalRecordsReturned: results.length,
|
|
347
|
+
queryOrigin: null
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
var _response_meta_total_records1, _response_meta_elapsed_time1;
|
|
351
|
+
return {
|
|
352
|
+
results,
|
|
353
|
+
query,
|
|
354
|
+
totalRecords: (_response_meta_total_records1 = (_response_meta1 = response.meta) == null ? void 0 : _response_meta1.total_records) != null ? _response_meta_total_records1 : results.length,
|
|
355
|
+
elapsedTime: (_response_meta_elapsed_time1 = (_response_meta2 = response.meta) == null ? void 0 : _response_meta2.elapsed_time) != null ? _response_meta_elapsed_time1 : 0
|
|
356
|
+
};
|
|
357
|
+
},
|
|
358
|
+
async suggest (query, limit = 10) {
|
|
359
|
+
const response = await transport.get('/search/suggest', {
|
|
360
|
+
params: {
|
|
361
|
+
q: query,
|
|
362
|
+
limit
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
return decodeMany(response, searchResultMapper);
|
|
366
|
+
},
|
|
367
|
+
async entityTypes () {
|
|
368
|
+
const response = await transport.get('/search/entity_types');
|
|
369
|
+
return decodeMany(response, entityTypeMapper);
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Create the search history service
|
|
375
|
+
*/ function createSearchHistoryService(transport, _config) {
|
|
376
|
+
return {
|
|
377
|
+
async recent (limit = 20) {
|
|
378
|
+
const response = await transport.get('/search/history', {
|
|
379
|
+
params: {
|
|
380
|
+
limit
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
return decodeMany(response, lastQueryMapper);
|
|
384
|
+
},
|
|
385
|
+
async get (id) {
|
|
386
|
+
const response = await transport.get(`/search/queries/${id}`);
|
|
387
|
+
return decodeOne(response, searchQueryMapper);
|
|
388
|
+
},
|
|
389
|
+
async clear () {
|
|
390
|
+
await transport.delete('/search/history');
|
|
391
|
+
},
|
|
392
|
+
async delete (id) {
|
|
393
|
+
await transport.delete(`/search/history/${id}`);
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Create the favorites service
|
|
399
|
+
*/ function createFavoritesService(transport, _config) {
|
|
400
|
+
return {
|
|
401
|
+
async list (params) {
|
|
402
|
+
const response = await transport.get('/search/favorites', {
|
|
403
|
+
params: buildListParams(params)
|
|
404
|
+
});
|
|
405
|
+
return decodePageResult(response, favoriteEntityMapper);
|
|
406
|
+
},
|
|
407
|
+
async get (id) {
|
|
408
|
+
const response = await transport.get(`/search/favorites/${id}`);
|
|
409
|
+
return decodeOne(response, favoriteEntityMapper);
|
|
410
|
+
},
|
|
411
|
+
async add (request) {
|
|
412
|
+
const response = await transport.post('/search/favorites', {
|
|
413
|
+
entity_unique_id: request.entityUniqueId,
|
|
414
|
+
entity_type: request.entityType,
|
|
415
|
+
entity_alias: request.entityAlias,
|
|
416
|
+
entity_url: request.entityUrl,
|
|
417
|
+
entity_avatar_url: request.entityAvatarUrl
|
|
418
|
+
});
|
|
419
|
+
return decodeOne(response, favoriteEntityMapper);
|
|
420
|
+
},
|
|
421
|
+
async remove (id) {
|
|
422
|
+
await transport.delete(`/search/favorites/${id}`);
|
|
423
|
+
},
|
|
424
|
+
async isFavorite (entityUniqueId) {
|
|
425
|
+
try {
|
|
426
|
+
var _response_data;
|
|
427
|
+
const response = await transport.get(`/search/favorites/check/${entityUniqueId}`);
|
|
428
|
+
var _response_data_favorite;
|
|
429
|
+
return (_response_data_favorite = (_response_data = response.data) == null ? void 0 : _response_data.favorite) != null ? _response_data_favorite : false;
|
|
430
|
+
} catch (e) {
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Create the Search block
|
|
439
|
+
*
|
|
440
|
+
* @example
|
|
441
|
+
* ```typescript
|
|
442
|
+
* import { createSearchBlock } from '@23blocks/block-search';
|
|
443
|
+
* import { createHttpTransport } from '@23blocks/transport-http';
|
|
444
|
+
*
|
|
445
|
+
* const transport = createHttpTransport({
|
|
446
|
+
* baseUrl: 'https://api.example.com',
|
|
447
|
+
* headers: () => ({
|
|
448
|
+
* 'Authorization': `Bearer ${getToken()}`,
|
|
449
|
+
* 'appid': 'your-app-id',
|
|
450
|
+
* }),
|
|
451
|
+
* });
|
|
452
|
+
*
|
|
453
|
+
* const searchBlock = createSearchBlock(transport, { appId: 'your-app-id' });
|
|
454
|
+
*
|
|
455
|
+
* // Execute a search
|
|
456
|
+
* const { results, totalRecords } = await searchBlock.search.search({
|
|
457
|
+
* query: 'hello world',
|
|
458
|
+
* entityTypes: ['Product', 'Article'],
|
|
459
|
+
* limit: 20,
|
|
460
|
+
* });
|
|
461
|
+
*
|
|
462
|
+
* // Get suggestions
|
|
463
|
+
* const suggestions = await searchBlock.search.suggest('hel');
|
|
464
|
+
*
|
|
465
|
+
* // Get recent searches
|
|
466
|
+
* const recent = await searchBlock.history.recent(10);
|
|
467
|
+
*
|
|
468
|
+
* // Add to favorites
|
|
469
|
+
* await searchBlock.favorites.add({
|
|
470
|
+
* entityUniqueId: 'product-123',
|
|
471
|
+
* entityType: 'Product',
|
|
472
|
+
* entityAlias: 'My Favorite Product',
|
|
473
|
+
* });
|
|
474
|
+
* ```
|
|
475
|
+
*/ function createSearchBlock(transport, config) {
|
|
476
|
+
return {
|
|
477
|
+
search: createSearchService(transport),
|
|
478
|
+
history: createSearchHistoryService(transport),
|
|
479
|
+
favorites: createFavoritesService(transport)
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Block metadata
|
|
484
|
+
*/ const searchBlockMetadata = {
|
|
485
|
+
name: 'search',
|
|
486
|
+
version: '0.0.1',
|
|
487
|
+
description: 'Full-text search, suggestions, history, and favorites',
|
|
488
|
+
resourceTypes: [
|
|
489
|
+
'SearchResult',
|
|
490
|
+
'SearchQuery',
|
|
491
|
+
'LastQuery',
|
|
492
|
+
'FavoriteEntity',
|
|
493
|
+
'EntityType'
|
|
494
|
+
]
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
export { createSearchBlock, entityTypeMapper, favoriteEntityMapper, lastQueryMapper, searchBlockMetadata, searchQueryMapper, searchResultMapper };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { createSearchBlock, searchBlockMetadata, type SearchBlock, type SearchBlockConfig, } from './lib/search.block.js';
|
|
2
|
+
export type { SearchResult, SearchQuery, LastQuery, FavoriteEntity, EntityType, SearchRequest, SearchResponse, AddFavoriteRequest, } from './lib/types/index.js';
|
|
3
|
+
export type { SearchService, SearchHistoryService, FavoritesService, } from './lib/services/index.js';
|
|
4
|
+
export { searchResultMapper, searchQueryMapper, lastQueryMapper, favoriteEntityMapper, entityTypeMapper, } from './lib/mappers/index.js';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,KAAK,WAAW,EAChB,KAAK,iBAAiB,GACvB,MAAM,uBAAuB,CAAC;AAG/B,YAAY,EACV,YAAY,EACZ,WAAW,EACX,SAAS,EACT,cAAc,EACd,UAAU,EACV,aAAa,EACb,cAAc,EACd,kBAAkB,GACnB,MAAM,sBAAsB,CAAC;AAG9B,YAAY,EACV,aAAa,EACb,oBAAoB,EACpB,gBAAgB,GACjB,MAAM,yBAAyB,CAAC;AAGjC,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,eAAe,EACf,oBAAoB,EACpB,gBAAgB,GACjB,MAAM,wBAAwB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"block-search.d.ts","sourceRoot":"","sources":["../../../src/lib/block-search.ts"],"names":[],"mappings":"AAAA,wBAAgB,WAAW,IAAI,MAAM,CAEpC"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export { searchResultMapper, searchQueryMapper, lastQueryMapper, favoriteEntityMapper, entityTypeMapper } from './search.mapper.js';
|
|
1
|
+
export { searchResultMapper, searchQueryMapper, lastQueryMapper, favoriteEntityMapper, entityTypeMapper, } from './search.mapper.js';
|
|
2
2
|
export { parseString, parseDate, parseBoolean, parseNumber, parseStringArray } from './utils.js';
|
|
3
|
-
|
|
4
|
-
//# sourceMappingURL=index.js.map
|
|
3
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/lib/mappers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,eAAe,EACf,oBAAoB,EACpB,gBAAgB,GACjB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,YAAY,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ResourceMapper } from '@23blocks/jsonapi-codec';
|
|
2
|
+
import type { SearchResult, SearchQuery, LastQuery, FavoriteEntity, EntityType } from '../types/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Mapper for SearchResult resources
|
|
5
|
+
*/
|
|
6
|
+
export declare const searchResultMapper: ResourceMapper<SearchResult>;
|
|
7
|
+
/**
|
|
8
|
+
* Mapper for SearchQuery resources
|
|
9
|
+
*/
|
|
10
|
+
export declare const searchQueryMapper: ResourceMapper<SearchQuery>;
|
|
11
|
+
/**
|
|
12
|
+
* Mapper for LastQuery resources
|
|
13
|
+
*/
|
|
14
|
+
export declare const lastQueryMapper: ResourceMapper<LastQuery>;
|
|
15
|
+
/**
|
|
16
|
+
* Mapper for FavoriteEntity resources
|
|
17
|
+
*/
|
|
18
|
+
export declare const favoriteEntityMapper: ResourceMapper<FavoriteEntity>;
|
|
19
|
+
/**
|
|
20
|
+
* Mapper for EntityType resources
|
|
21
|
+
*/
|
|
22
|
+
export declare const entityTypeMapper: ResourceMapper<EntityType>;
|
|
23
|
+
//# sourceMappingURL=search.mapper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"search.mapper.d.ts","sourceRoot":"","sources":["../../../../src/lib/mappers/search.mapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAgC,MAAM,yBAAyB,CAAC;AAE5F,OAAO,KAAK,EACV,YAAY,EACZ,WAAW,EACX,SAAS,EACT,cAAc,EACd,UAAU,EACX,MAAM,mBAAmB,CAAC;AAG3B;;GAEG;AACH,eAAO,MAAM,kBAAkB,EAAE,cAAc,CAAC,YAAY,CA8B3D,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,EAAE,cAAc,CAAC,WAAW,CA2CzD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,eAAe,EAAE,cAAc,CAAC,SAAS,CA2CrD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,oBAAoB,EAAE,cAAc,CAAC,cAAc,CA4B/D,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,gBAAgB,EAAE,cAAc,CAAC,UAAU,CAYvD,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse a string value, returning null for empty/undefined
|
|
3
|
+
*/
|
|
4
|
+
export declare function parseString(value: unknown): string | null;
|
|
5
|
+
/**
|
|
6
|
+
* Parse a date value
|
|
7
|
+
*/
|
|
8
|
+
export declare function parseDate(value: unknown): Date | null;
|
|
9
|
+
/**
|
|
10
|
+
* Parse a boolean value
|
|
11
|
+
*/
|
|
12
|
+
export declare function parseBoolean(value: unknown): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Parse an array of strings
|
|
15
|
+
*/
|
|
16
|
+
export declare function parseStringArray(value: unknown): string[] | null;
|
|
17
|
+
/**
|
|
18
|
+
* Parse a number value
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseNumber(value: unknown): number;
|
|
21
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../../src/lib/mappers/utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAMzD;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,GAAG,IAAI,CAerD;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAQpD;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,EAAE,GAAG,IAAI,CAQhE;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAMlD"}
|
|
@@ -1,4 +1,31 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Transport, BlockConfig } from '@23blocks/contracts';
|
|
2
|
+
import { type SearchService, type SearchHistoryService, type FavoritesService } from './services/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for the Search block
|
|
5
|
+
*/
|
|
6
|
+
export interface SearchBlockConfig extends BlockConfig {
|
|
7
|
+
/** Application ID */
|
|
8
|
+
appId: string;
|
|
9
|
+
/** Default entity types to search (optional) */
|
|
10
|
+
defaultEntityTypes?: string[];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Search block interface
|
|
14
|
+
*/
|
|
15
|
+
export interface SearchBlock {
|
|
16
|
+
/**
|
|
17
|
+
* Core search operations
|
|
18
|
+
*/
|
|
19
|
+
search: SearchService;
|
|
20
|
+
/**
|
|
21
|
+
* Search history management
|
|
22
|
+
*/
|
|
23
|
+
history: SearchHistoryService;
|
|
24
|
+
/**
|
|
25
|
+
* Favorites/bookmarks management
|
|
26
|
+
*/
|
|
27
|
+
favorites: FavoritesService;
|
|
28
|
+
}
|
|
2
29
|
/**
|
|
3
30
|
* Create the Search block
|
|
4
31
|
*
|
|
@@ -37,26 +64,15 @@ import { createSearchService, createSearchHistoryService, createFavoritesService
|
|
|
37
64
|
* entityAlias: 'My Favorite Product',
|
|
38
65
|
* });
|
|
39
66
|
* ```
|
|
40
|
-
*/
|
|
41
|
-
|
|
42
|
-
search: createSearchService(transport, config),
|
|
43
|
-
history: createSearchHistoryService(transport, config),
|
|
44
|
-
favorites: createFavoritesService(transport, config)
|
|
45
|
-
};
|
|
46
|
-
}
|
|
67
|
+
*/
|
|
68
|
+
export declare function createSearchBlock(transport: Transport, config: SearchBlockConfig): SearchBlock;
|
|
47
69
|
/**
|
|
48
70
|
* Block metadata
|
|
49
|
-
*/
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
'SearchQuery',
|
|
56
|
-
'LastQuery',
|
|
57
|
-
'FavoriteEntity',
|
|
58
|
-
'EntityType'
|
|
59
|
-
]
|
|
71
|
+
*/
|
|
72
|
+
export declare const searchBlockMetadata: {
|
|
73
|
+
name: string;
|
|
74
|
+
version: string;
|
|
75
|
+
description: string;
|
|
76
|
+
resourceTypes: string[];
|
|
60
77
|
};
|
|
61
|
-
|
|
62
|
-
//# sourceMappingURL=search.block.js.map
|
|
78
|
+
//# sourceMappingURL=search.block.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"search.block.d.ts","sourceRoot":"","sources":["../../../src/lib/search.block.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClE,OAAO,EAIL,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,gBAAgB,EACtB,MAAM,qBAAqB,CAAC;AAE7B;;GAEG;AACH,MAAM,WAAW,iBAAkB,SAAQ,WAAW;IACpD,qBAAqB;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,gDAAgD;IAChD,kBAAkB,CAAC,EAAE,MAAM,EAAE,CAAC;CAC/B;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B;;OAEG;IACH,MAAM,EAAE,aAAa,CAAC;IAEtB;;OAEG;IACH,OAAO,EAAE,oBAAoB,CAAC;IAE9B;;OAEG;IACH,SAAS,EAAE,gBAAgB,CAAC;CAC7B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,wBAAgB,iBAAiB,CAC/B,SAAS,EAAE,SAAS,EACpB,MAAM,EAAE,iBAAiB,GACxB,WAAW,CAMb;AAED;;GAEG;AACH,eAAO,MAAM,mBAAmB;;;;;CAW/B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/lib/services/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,0BAA0B,EAC1B,sBAAsB,EACtB,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,gBAAgB,GACtB,MAAM,qBAAqB,CAAC"}
|