@reactionary/provider-algolia 0.0.35 → 0.0.37

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.
@@ -0,0 +1,19 @@
1
+ import { ProductSchema } from "@reactionary/core";
2
+ import { AlgoliaProductProvider } from "../providers/product.provider";
3
+ import { AlgoliaSearchProvider } from "../providers/search.provider";
4
+ import { AlgoliaSearchResultSchema } from "../schema/search.schema";
5
+ function withAlgoliaCapabilities(configuration, capabilities) {
6
+ return (cache) => {
7
+ const client = {};
8
+ if (capabilities.product) {
9
+ client.product = new AlgoliaProductProvider(configuration, ProductSchema, cache);
10
+ }
11
+ if (capabilities.search) {
12
+ client.search = new AlgoliaSearchProvider(configuration, AlgoliaSearchResultSchema, cache);
13
+ }
14
+ return client;
15
+ };
16
+ }
17
+ export {
18
+ withAlgoliaCapabilities
19
+ };
package/index.js CHANGED
@@ -1,623 +1,5 @@
1
- // core/src/cache/redis-cache.ts
2
- import { Redis } from "@upstash/redis";
3
-
4
- // core/src/decorators/trpc.decorators.ts
5
- import "reflect-metadata";
6
- var TRPC_QUERY_METADATA_KEY = Symbol("trpc:query");
7
- var TRPC_MUTATION_METADATA_KEY = Symbol("trpc:mutation");
8
-
9
- // core/src/providers/base.provider.ts
10
- var BaseProvider = class {
11
- constructor(schema, cache) {
12
- this.schema = schema;
13
- this.cache = cache;
14
- }
15
- /**
16
- * Validates that the final domain model constructed by the provider
17
- * fulfills the schema as defined. This will throw an exception.
18
- */
19
- assert(value) {
20
- return this.schema.parse(value);
21
- }
22
- /**
23
- * Creates a new model entity based on the schema defaults.
24
- */
25
- newModel() {
26
- return this.schema.parse({});
27
- }
28
- /**
29
- * Handler for parsing a response from a remote provider and converting it
30
- * into the typed domain model.
31
- */
32
- parseSingle(_body) {
33
- const model = this.newModel();
34
- return this.assert(model);
35
- }
36
- };
37
-
38
- // core/src/providers/product.provider.ts
39
- var ProductProvider = class extends BaseProvider {
40
- };
41
-
42
- // core/src/providers/search.provider.ts
43
- var SearchProvider = class extends BaseProvider {
44
- };
45
-
46
- // core/src/schemas/capabilities.schema.ts
47
- import { z } from "zod";
48
- var CapabilitiesSchema = z.looseInterface({
49
- product: z.boolean(),
50
- search: z.boolean(),
51
- analytics: z.boolean(),
52
- identity: z.boolean(),
53
- cart: z.boolean(),
54
- inventory: z.boolean(),
55
- price: z.boolean()
56
- });
57
-
58
- // core/src/schemas/session.schema.ts
59
- import { z as z4 } from "zod";
60
-
61
- // core/src/schemas/models/identity.model.ts
62
- import { z as z3 } from "zod";
63
-
64
- // core/src/schemas/models/base.model.ts
65
- import { z as z2 } from "zod";
66
- var CacheInformationSchema = z2.looseInterface({
67
- hit: z2.boolean().default(false),
68
- key: z2.string().default("")
69
- });
70
- var MetaSchema = z2.looseInterface({
71
- cache: CacheInformationSchema.default(() => CacheInformationSchema.parse({})),
72
- placeholder: z2.boolean().default(false).describe("Whether or not the entity exists in a remote system, or is a default placeholder.")
73
- });
74
- var BaseModelSchema = z2.looseInterface({
75
- meta: MetaSchema.default(() => MetaSchema.parse({}))
76
- });
77
-
78
- // core/src/schemas/models/identity.model.ts
79
- var IdentityTypeSchema = z3.enum(["Anonymous", "Guest", "Registered"]);
80
- var IdentitySchema = BaseModelSchema.extend({
81
- id: z3.string().default(""),
82
- type: IdentityTypeSchema.default("Anonymous"),
83
- token: z3.string().optional(),
84
- issued: z3.coerce.date().default(/* @__PURE__ */ new Date()),
85
- expiry: z3.coerce.date().default(/* @__PURE__ */ new Date())
86
- });
87
-
88
- // core/src/schemas/session.schema.ts
89
- var SessionSchema = z4.looseObject({
90
- id: z4.string(),
91
- identity: IdentitySchema.default(() => IdentitySchema.parse({}))
92
- });
93
-
94
- // core/src/schemas/models/cart.model.ts
95
- import { z as z6 } from "zod";
96
-
97
- // core/src/schemas/models/identifiers.model.ts
98
- import { z as z5 } from "zod";
99
- var FacetIdentifierSchema = z5.looseInterface({
100
- key: z5.string().default("").nonoptional()
101
- });
102
- var FacetValueIdentifierSchema = z5.looseInterface({
103
- facet: FacetIdentifierSchema.default(() => FacetIdentifierSchema.parse({})),
104
- key: z5.string().default("")
105
- });
106
- var SKUIdentifierSchema = z5.looseInterface({
107
- key: z5.string().default("").nonoptional()
108
- });
109
- var ProductIdentifierSchema = z5.looseInterface({
110
- key: z5.string().default("")
111
- });
112
- var SearchIdentifierSchema = z5.looseInterface({
113
- term: z5.string().default(""),
114
- page: z5.number().default(0),
115
- pageSize: z5.number().default(20),
116
- facets: z5.array(FacetValueIdentifierSchema.required()).default(() => [])
117
- });
118
- var CartIdentifierSchema = z5.looseInterface({
119
- key: z5.string().default("")
120
- });
121
- var CartItemIdentifierSchema = z5.looseInterface({
122
- key: z5.string().default("")
123
- });
124
- var PriceIdentifierSchema = z5.looseInterface({
125
- sku: SKUIdentifierSchema.default(() => SKUIdentifierSchema.parse({}))
126
- });
127
-
128
- // core/src/schemas/models/cart.model.ts
129
- var CartItemSchema = z6.looseInterface({
130
- identifier: CartItemIdentifierSchema.default(() => CartItemIdentifierSchema.parse({})),
131
- product: ProductIdentifierSchema.default(() => ProductIdentifierSchema.parse({})),
132
- quantity: z6.number().default(0)
133
- });
134
- var CartSchema = BaseModelSchema.extend({
135
- identifier: CartIdentifierSchema.default(() => CartIdentifierSchema.parse({})),
136
- items: z6.array(CartItemSchema).default(() => [])
137
- });
138
-
139
- // core/src/schemas/models/currency.model.ts
140
- import { z as z7 } from "zod";
141
- var CurrencySchema = z7.enum([
142
- "AED",
143
- "AFN",
144
- "ALL",
145
- "AMD",
146
- "ANG",
147
- "AOA",
148
- "ARS",
149
- "AUD",
150
- "AWG",
151
- "AZN",
152
- "BAM",
153
- "BBD",
154
- "BDT",
155
- "BGN",
156
- "BHD",
157
- "BIF",
158
- "BMD",
159
- "BND",
160
- "BOB",
161
- "BOV",
162
- "BRL",
163
- "BSD",
164
- "BTN",
165
- "BWP",
166
- "BYN",
167
- "BZD",
168
- "CAD",
169
- "CDF",
170
- "CHE",
171
- "CHF",
172
- "CHW",
173
- "CLF",
174
- "CLP",
175
- "CNY",
176
- "COP",
177
- "COU",
178
- "CRC",
179
- "CUC",
180
- "CUP",
181
- "CVE",
182
- "CZK",
183
- "DJF",
184
- "DKK",
185
- "DOP",
186
- "DZD",
187
- "EGP",
188
- "ERN",
189
- "ETB",
190
- "EUR",
191
- "FJD",
192
- "FKP",
193
- "GBP",
194
- "GEL",
195
- "GHS",
196
- "GIP",
197
- "GMD",
198
- "GNF",
199
- "GTQ",
200
- "GYD",
201
- "HKD",
202
- "HNL",
203
- "HRK",
204
- "HTG",
205
- "HUF",
206
- "IDR",
207
- "ILS",
208
- "INR",
209
- "IQD",
210
- "IRR",
211
- "ISK",
212
- "JMD",
213
- "JOD",
214
- "JPY",
215
- "KES",
216
- "KGS",
217
- "KHR",
218
- "KMF",
219
- "KPW",
220
- "KRW",
221
- "KWD",
222
- "KYD",
223
- "KZT",
224
- "LAK",
225
- "LBP",
226
- "LKR",
227
- "LRD",
228
- "LSL",
229
- "LYD",
230
- "MAD",
231
- "MDL",
232
- "MGA",
233
- "MKD",
234
- "MMK",
235
- "MNT",
236
- "MOP",
237
- "MRU",
238
- "MUR",
239
- "MVR",
240
- "MWK",
241
- "MXN",
242
- "MXV",
243
- "MYR",
244
- "MZN",
245
- "NAD",
246
- "NGN",
247
- "NIO",
248
- "NOK",
249
- "NPR",
250
- "NZD",
251
- "OMR",
252
- "PAB",
253
- "PEN",
254
- "PGK",
255
- "PHP",
256
- "PKR",
257
- "PLN",
258
- "PYG",
259
- "QAR",
260
- "RON",
261
- "RSD",
262
- "RUB",
263
- "RWF",
264
- "SAR",
265
- "SBD",
266
- "SCR",
267
- "SDG",
268
- "SEK",
269
- "SGD",
270
- "SHP",
271
- "SLE",
272
- "SLL",
273
- "SOS",
274
- "SRD",
275
- "SSP",
276
- "STN",
277
- "SYP",
278
- "SZL",
279
- "THB",
280
- "TJS",
281
- "TMT",
282
- "TND",
283
- "TOP",
284
- "TRY",
285
- "TTD",
286
- "TVD",
287
- "TWD",
288
- "TZS",
289
- "UAH",
290
- "UGX",
291
- "USD",
292
- "USN",
293
- "UYI",
294
- "UYU",
295
- "UYW",
296
- "UZS",
297
- "VED",
298
- "VES",
299
- "VND",
300
- "VUV",
301
- "WST",
302
- "XAF",
303
- "XAG",
304
- "XAU",
305
- "XBA",
306
- "XBB",
307
- "XBC",
308
- "XBD",
309
- "XCD",
310
- "XDR",
311
- "XOF",
312
- "XPD",
313
- "XPF",
314
- "XPT",
315
- "XSU",
316
- "XTS",
317
- "XUA",
318
- "XXX",
319
- "YER",
320
- "ZAR",
321
- "ZMW",
322
- "ZWL"
323
- ]);
324
-
325
- // core/src/schemas/models/inventory.model.ts
326
- import { z as z8 } from "zod";
327
- var InventorySchema = BaseModelSchema.extend({
328
- quantity: z8.number().default(0)
329
- });
330
-
331
- // core/src/schemas/models/price.model.ts
332
- import { z as z9 } from "zod";
333
- var MonetaryAmountSchema = z9.looseInterface({
334
- cents: z9.number().default(0).describe("The monetary amount in cent-precision."),
335
- currency: CurrencySchema.default("XXX").describe("The currency associated with the amount, as a ISO 4217 standardized code.")
336
- });
337
- var PriceSchema = BaseModelSchema.extend({
338
- identifier: PriceIdentifierSchema.default(() => PriceIdentifierSchema.parse({})),
339
- value: MonetaryAmountSchema.default(() => MonetaryAmountSchema.parse({}))
340
- });
341
-
342
- // core/src/schemas/models/product.model.ts
343
- import { z as z10 } from "zod";
344
- var SKUSchema = z10.looseInterface({
345
- identifier: ProductIdentifierSchema.default(() => ProductIdentifierSchema.parse({}))
346
- });
347
- var ProductAttributeSchema = z10.looseInterface({
348
- id: z10.string(),
349
- name: z10.string(),
350
- value: z10.string()
351
- });
352
- var ProductSchema = BaseModelSchema.extend({
353
- identifier: ProductIdentifierSchema.default(() => ProductIdentifierSchema.parse({})),
354
- name: z10.string().default(""),
355
- slug: z10.string().default(""),
356
- description: z10.string().default(""),
357
- image: z10.string().url().default("https://placehold.co/400"),
358
- images: z10.string().url().array().default(() => []),
359
- attributes: z10.array(ProductAttributeSchema).default(() => []),
360
- skus: z10.array(SKUSchema).default(() => [])
361
- });
362
-
363
- // core/src/schemas/models/search.model.ts
364
- import { z as z11 } from "zod";
365
- var SearchResultProductSchema = z11.looseInterface({
366
- identifier: ProductIdentifierSchema.default(ProductIdentifierSchema.parse({})),
367
- name: z11.string().default(""),
368
- image: z11.string().url().default("https://placehold.co/400"),
369
- slug: z11.string().default("")
370
- });
371
- var SearchResultFacetValueSchema = z11.looseInterface({
372
- identifier: FacetValueIdentifierSchema.default(() => FacetValueIdentifierSchema.parse({})),
373
- name: z11.string().default(""),
374
- count: z11.number().default(0),
375
- active: z11.boolean().default(false)
376
- });
377
- var SearchResultFacetSchema = z11.looseInterface({
378
- identifier: FacetIdentifierSchema.default(() => FacetIdentifierSchema.parse({})),
379
- name: z11.string().default(""),
380
- values: z11.array(SearchResultFacetValueSchema).default(() => [])
381
- });
382
- var SearchResultSchema = BaseModelSchema.extend({
383
- identifier: SearchIdentifierSchema.default(() => SearchIdentifierSchema.parse({})),
384
- products: z11.array(SearchResultProductSchema).default(() => []),
385
- pages: z11.number().default(0),
386
- facets: z11.array(SearchResultFacetSchema).default(() => [])
387
- });
388
-
389
- // core/src/schemas/mutations/base.mutation.ts
390
- import { z as z12 } from "zod";
391
- var BaseMutationSchema = z12.looseInterface({});
392
-
393
- // core/src/schemas/mutations/cart.mutation.ts
394
- import { z as z13 } from "zod";
395
- var CartMutationItemAddSchema = BaseMutationSchema.extend({
396
- cart: CartIdentifierSchema.required(),
397
- product: ProductIdentifierSchema.required(),
398
- quantity: z13.number()
399
- });
400
- var CartMutationItemRemoveSchema = BaseMutationSchema.extend({
401
- cart: CartIdentifierSchema.required(),
402
- item: CartItemIdentifierSchema.required()
403
- });
404
- var CartMutationItemQuantityChangeSchema = BaseMutationSchema.extend({
405
- cart: CartIdentifierSchema.required(),
406
- item: CartItemIdentifierSchema.required(),
407
- quantity: z13.number()
408
- });
409
-
410
- // core/src/schemas/mutations/identity.mutation.ts
411
- import { z as z14 } from "zod";
412
- var IdentityMutationLoginSchema = BaseMutationSchema.extend({
413
- username: z14.string(),
414
- password: z14.string()
415
- });
416
- var IdentityMutationLogoutSchema = BaseMutationSchema.extend({});
417
-
418
- // core/src/schemas/mutations/inventory.mutation.ts
419
- import { z as z15 } from "zod";
420
- var InventoryMutationSchema = z15.union([]);
421
-
422
- // core/src/schemas/mutations/price.mutation.ts
423
- import { z as z16 } from "zod";
424
- var PriceMutationSchema = z16.union([]);
425
-
426
- // core/src/schemas/mutations/product.mutation.ts
427
- import { z as z17 } from "zod";
428
- var ProductMutationSchema = z17.union([]);
429
-
430
- // core/src/schemas/mutations/search.mutation.ts
431
- import { z as z18 } from "zod";
432
- var SearchMutationSchema = z18.union([]);
433
-
434
- // core/src/schemas/queries/base.query.ts
435
- import { z as z19 } from "zod";
436
- var BaseQuerySchema = z19.looseInterface({});
437
-
438
- // core/src/schemas/queries/cart.query.ts
439
- import { z as z20 } from "zod";
440
- var CartQueryByIdSchema = BaseQuerySchema.extend({
441
- cart: CartIdentifierSchema.required()
442
- });
443
- var CartQuerySchema = z20.union([CartQueryByIdSchema]);
444
-
445
- // core/src/schemas/queries/identity.query.ts
446
- var IdentityQuerySelfSchema = BaseQuerySchema.extend({});
447
-
448
- // core/src/schemas/queries/inventory.query.ts
449
- import { z as z21 } from "zod";
450
- var InventoryQuerySchema = BaseQuerySchema.extend({
451
- sku: z21.string()
452
- });
453
-
454
- // core/src/schemas/queries/price.query.ts
455
- var PriceQueryBySkuSchema = BaseQuerySchema.extend({
456
- sku: SKUIdentifierSchema.required()
457
- });
458
-
459
- // core/src/schemas/queries/product.query.ts
460
- import { z as z22 } from "zod";
461
- var ProductQueryBySlugSchema = BaseQuerySchema.extend({
462
- slug: z22.string()
463
- });
464
- var ProductQueryByIdSchema = BaseQuerySchema.extend({
465
- id: z22.string()
466
- });
467
-
468
- // core/src/schemas/queries/search.query.ts
469
- var SearchQueryByTermSchema = BaseQuerySchema.extend({
470
- search: SearchIdentifierSchema.required()
471
- });
472
-
473
- // providers/algolia/src/providers/product.provider.ts
474
- var AlgoliaProductProvider = class extends ProductProvider {
475
- constructor(config, schema, cache) {
476
- super(schema, cache);
477
- this.config = config;
478
- }
479
- async getById(payload, _session) {
480
- const result = this.newModel();
481
- result.identifier = { key: payload.id };
482
- result.name = `Algolia Product ${payload.id}`;
483
- result.slug = payload.id;
484
- result.description = "Product from Algolia";
485
- result.meta = {
486
- cache: { hit: false, key: payload.id },
487
- placeholder: true
488
- };
489
- return this.assert(result);
490
- }
491
- async getBySlug(payload, _session) {
492
- const result = this.newModel();
493
- result.identifier = { key: payload.slug };
494
- result.name = `Algolia Product ${payload.slug}`;
495
- result.slug = payload.slug;
496
- result.description = "Product from Algolia";
497
- result.meta = {
498
- cache: { hit: false, key: payload.slug },
499
- placeholder: true
500
- };
501
- return this.assert(result);
502
- }
503
- };
504
-
505
- // providers/algolia/src/providers/search.provider.ts
506
- import { algoliasearch } from "algoliasearch";
507
- var AlgoliaSearchProvider = class extends SearchProvider {
508
- constructor(config, schema, cache) {
509
- super(schema, cache);
510
- this.config = config;
511
- }
512
- async queryByTerm(payload, _session) {
513
- const client = algoliasearch(this.config.appId, this.config.apiKey);
514
- const remote = await client.search({
515
- requests: [
516
- {
517
- indexName: this.config.indexName,
518
- query: payload.search.term,
519
- page: payload.search.page,
520
- hitsPerPage: payload.search.pageSize,
521
- facets: ["*"],
522
- analytics: true,
523
- clickAnalytics: true,
524
- facetFilters: payload.search.facets.map(
525
- (x) => `${encodeURIComponent(x.facet.key)}:${x.key}`
526
- )
527
- }
528
- ]
529
- });
530
- return this.parseSearchResult(remote, payload);
531
- }
532
- parseSearchResult(remote, payload) {
533
- const result = this.newModel();
534
- const remoteData = remote;
535
- const remoteProducts = remoteData.results[0];
536
- for (const id in remoteProducts.facets) {
537
- const f = remoteProducts.facets[id];
538
- const facet = {
539
- identifier: { key: id },
540
- name: id,
541
- values: []
542
- };
543
- for (const vid in f) {
544
- const fv = f[vid];
545
- const isActive = payload.search.facets.find(
546
- (x) => x.facet.key === id && x.key === vid
547
- );
548
- facet.values.push({
549
- identifier: { key: vid, facet: { key: id } },
550
- count: fv,
551
- name: vid,
552
- active: !!isActive
553
- });
554
- }
555
- result.facets.push(facet);
556
- }
557
- for (const p of remoteProducts.hits) {
558
- result.products.push({
559
- identifier: { key: p.objectID },
560
- slug: p.slug,
561
- name: p.name,
562
- image: p.image
563
- });
564
- }
565
- result.identifier = {
566
- ...payload.search,
567
- index: remoteProducts.index,
568
- key: remoteProducts.queryID
569
- };
570
- result.pages = remoteProducts.nbPages;
571
- result.meta = {
572
- cache: { hit: false, key: payload.search.term },
573
- placeholder: false
574
- };
575
- return this.assert(result);
576
- }
577
- };
578
-
579
- // providers/algolia/src/schema/search.schema.ts
580
- import { z as z23 } from "zod";
581
- var AlgoliaSearchIdentifierSchema = SearchIdentifierSchema.extend({
582
- key: z23.string().default(""),
583
- index: z23.string().default("")
584
- });
585
- var AlgoliaSearchResultSchema = SearchResultSchema.extend({
586
- identifier: AlgoliaSearchIdentifierSchema.default(() => AlgoliaSearchIdentifierSchema.parse({}))
587
- });
588
-
589
- // providers/algolia/src/core/initialize.ts
590
- function withAlgoliaCapabilities(configuration, capabilities) {
591
- return (cache) => {
592
- const client = {};
593
- if (capabilities.product) {
594
- client.product = new AlgoliaProductProvider(configuration, ProductSchema, cache);
595
- }
596
- if (capabilities.search) {
597
- client.search = new AlgoliaSearchProvider(configuration, AlgoliaSearchResultSchema, cache);
598
- }
599
- return client;
600
- };
601
- }
602
-
603
- // providers/algolia/src/schema/configuration.schema.ts
604
- import { z as z24 } from "zod";
605
- var AlgoliaConfigurationSchema = z24.looseInterface({
606
- appId: z24.string(),
607
- apiKey: z24.string(),
608
- indexName: z24.string()
609
- });
610
-
611
- // providers/algolia/src/schema/capabilities.schema.ts
612
- var AlgoliaCapabilitiesSchema = CapabilitiesSchema.pick({
613
- product: true,
614
- search: true,
615
- analytics: true
616
- }).partial();
617
- export {
618
- AlgoliaCapabilitiesSchema,
619
- AlgoliaConfigurationSchema,
620
- AlgoliaProductProvider,
621
- AlgoliaSearchProvider,
622
- withAlgoliaCapabilities
623
- };
1
+ export * from "./core/initialize";
2
+ export * from "./providers/product.provider";
3
+ export * from "./providers/search.provider";
4
+ export * from "./schema/configuration.schema";
5
+ export * from "./schema/capabilities.schema";
package/package.json CHANGED
@@ -1,8 +1,10 @@
1
1
  {
2
2
  "name": "@reactionary/provider-algolia",
3
- "version": "0.0.35",
3
+ "version": "0.0.37",
4
+ "main": "index.js",
5
+ "types": "src/index.d.ts",
4
6
  "dependencies": {
5
- "@reactionary/core": "0.0.35",
7
+ "@reactionary/core": "0.0.37",
6
8
  "algoliasearch": "^5.23.4",
7
9
  "zod": "4.0.0-beta.20250430T185432"
8
10
  }
@@ -0,0 +1,36 @@
1
+ import {
2
+ ProductProvider
3
+ } from "@reactionary/core";
4
+ class AlgoliaProductProvider extends ProductProvider {
5
+ constructor(config, schema, cache) {
6
+ super(schema, cache);
7
+ this.config = config;
8
+ }
9
+ async getById(payload, _session) {
10
+ const result = this.newModel();
11
+ result.identifier = { key: payload.id };
12
+ result.name = `Algolia Product ${payload.id}`;
13
+ result.slug = payload.id;
14
+ result.description = "Product from Algolia";
15
+ result.meta = {
16
+ cache: { hit: false, key: payload.id },
17
+ placeholder: true
18
+ };
19
+ return this.assert(result);
20
+ }
21
+ async getBySlug(payload, _session) {
22
+ const result = this.newModel();
23
+ result.identifier = { key: payload.slug };
24
+ result.name = `Algolia Product ${payload.slug}`;
25
+ result.slug = payload.slug;
26
+ result.description = "Product from Algolia";
27
+ result.meta = {
28
+ cache: { hit: false, key: payload.slug },
29
+ placeholder: true
30
+ };
31
+ return this.assert(result);
32
+ }
33
+ }
34
+ export {
35
+ AlgoliaProductProvider
36
+ };
@@ -0,0 +1,78 @@
1
+ import {
2
+ SearchProvider
3
+ } from "@reactionary/core";
4
+ import { algoliasearch } from "algoliasearch";
5
+ class AlgoliaSearchProvider extends SearchProvider {
6
+ constructor(config, schema, cache) {
7
+ super(schema, cache);
8
+ this.config = config;
9
+ }
10
+ async queryByTerm(payload, _session) {
11
+ const client = algoliasearch(this.config.appId, this.config.apiKey);
12
+ const remote = await client.search({
13
+ requests: [
14
+ {
15
+ indexName: this.config.indexName,
16
+ query: payload.search.term,
17
+ page: payload.search.page,
18
+ hitsPerPage: payload.search.pageSize,
19
+ facets: ["*"],
20
+ analytics: true,
21
+ clickAnalytics: true,
22
+ facetFilters: payload.search.facets.map(
23
+ (x) => `${encodeURIComponent(x.facet.key)}:${x.key}`
24
+ )
25
+ }
26
+ ]
27
+ });
28
+ return this.parseSearchResult(remote, payload);
29
+ }
30
+ parseSearchResult(remote, payload) {
31
+ const result = this.newModel();
32
+ const remoteData = remote;
33
+ const remoteProducts = remoteData.results[0];
34
+ for (const id in remoteProducts.facets) {
35
+ const f = remoteProducts.facets[id];
36
+ const facet = {
37
+ identifier: { key: id },
38
+ name: id,
39
+ values: []
40
+ };
41
+ for (const vid in f) {
42
+ const fv = f[vid];
43
+ const isActive = payload.search.facets.find(
44
+ (x) => x.facet.key === id && x.key === vid
45
+ );
46
+ facet.values.push({
47
+ identifier: { key: vid, facet: { key: id } },
48
+ count: fv,
49
+ name: vid,
50
+ active: !!isActive
51
+ });
52
+ }
53
+ result.facets.push(facet);
54
+ }
55
+ for (const p of remoteProducts.hits) {
56
+ result.products.push({
57
+ identifier: { key: p.objectID },
58
+ slug: p.slug,
59
+ name: p.name,
60
+ image: p.image
61
+ });
62
+ }
63
+ result.identifier = {
64
+ ...payload.search,
65
+ index: remoteProducts.index,
66
+ key: remoteProducts.queryID
67
+ };
68
+ result.pages = remoteProducts.nbPages;
69
+ result.meta = {
70
+ cache: { hit: false, key: payload.search.term },
71
+ placeholder: false
72
+ };
73
+ return this.assert(result);
74
+ }
75
+ }
76
+ export {
77
+ AlgoliaSearchProvider
78
+ };
@@ -0,0 +1,9 @@
1
+ import { CapabilitiesSchema } from "@reactionary/core";
2
+ const AlgoliaCapabilitiesSchema = CapabilitiesSchema.pick({
3
+ product: true,
4
+ search: true,
5
+ analytics: true
6
+ }).partial();
7
+ export {
8
+ AlgoliaCapabilitiesSchema
9
+ };
@@ -0,0 +1,9 @@
1
+ import { z } from "zod";
2
+ const AlgoliaConfigurationSchema = z.looseInterface({
3
+ appId: z.string(),
4
+ apiKey: z.string(),
5
+ indexName: z.string()
6
+ });
7
+ export {
8
+ AlgoliaConfigurationSchema
9
+ };
@@ -0,0 +1,13 @@
1
+ import { SearchIdentifierSchema, SearchResultSchema } from "@reactionary/core";
2
+ import { z } from "zod";
3
+ const AlgoliaSearchIdentifierSchema = SearchIdentifierSchema.extend({
4
+ key: z.string().default(""),
5
+ index: z.string().default("")
6
+ });
7
+ const AlgoliaSearchResultSchema = SearchResultSchema.extend({
8
+ identifier: AlgoliaSearchIdentifierSchema.default(() => AlgoliaSearchIdentifierSchema.parse({}))
9
+ });
10
+ export {
11
+ AlgoliaSearchIdentifierSchema,
12
+ AlgoliaSearchResultSchema
13
+ };