@reactionary/provider-commercetools 0.0.82 → 0.0.83

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/core/client.js CHANGED
@@ -146,7 +146,10 @@ class CommercetoolsClient {
146
146
  })
147
147
  });
148
148
  const body = await response.json();
149
- const scopes = body.scope;
149
+ if (!body) {
150
+ return AnonymousIdentitySchema.parse({});
151
+ }
152
+ const scopes = body.scope + "";
150
153
  if (scopes.indexOf("anonymous_id") > -1) {
151
154
  const s = scopes.split(" ");
152
155
  const idScope = s.find((x) => x.startsWith("anonymous_id"));
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@reactionary/provider-commercetools",
3
- "version": "0.0.82",
3
+ "version": "0.0.83",
4
4
  "main": "index.js",
5
5
  "types": "src/index.d.ts",
6
6
  "dependencies": {
7
- "@reactionary/core": "0.0.82",
7
+ "@reactionary/core": "0.0.83",
8
8
  "debug": "^4.4.3",
9
9
  "zod": "4.1.9",
10
10
  "@commercetools/ts-client": "^4.2.1",
@@ -34,15 +34,98 @@ class CommercetoolsSearchProvider extends ProductSearchProvider {
34
34
  }
35
35
  async getClient() {
36
36
  const client = await this.client.getClient();
37
- return client.withProjectKey({ projectKey: this.config.projectKey }).productProjections();
37
+ return client.withProjectKey({ projectKey: this.config.projectKey }).products();
38
+ }
39
+ async getFacetQuery(payload, selectedFacetValue) {
40
+ return {
41
+ exact: {
42
+ field: selectedFacetValue.facet.key,
43
+ fieldType: "text",
44
+ value: selectedFacetValue.key
45
+ }
46
+ };
47
+ }
48
+ async getSearchTermExpression(payload) {
49
+ return {
50
+ or: [
51
+ {
52
+ fullText: {
53
+ field: "name",
54
+ language: `${this.context.languageContext.locale}`,
55
+ value: payload.search.term
56
+ }
57
+ },
58
+ {
59
+ fullText: {
60
+ field: "description",
61
+ language: `${this.context.languageContext.locale}`,
62
+ value: payload.search.term
63
+ }
64
+ },
65
+ {
66
+ fullText: {
67
+ field: "searchKeywords",
68
+ language: `${this.context.languageContext.locale}`,
69
+ value: payload.search.term
70
+ }
71
+ }
72
+ ]
73
+ };
74
+ }
75
+ async getFacetsQuery(payload) {
76
+ if (payload.search.facets.length === 0) {
77
+ return void 0;
78
+ }
79
+ const facetsToApply = await Promise.all(
80
+ payload.search.facets.map((facet) => this.getFacetQuery(payload, facet))
81
+ );
82
+ if (facetsToApply.length === 0) {
83
+ return void 0;
84
+ }
85
+ if (facetsToApply.length === 1) {
86
+ return facetsToApply[0];
87
+ }
88
+ return {
89
+ and: facetsToApply
90
+ };
91
+ }
92
+ async getFacetsToReturn(payload) {
93
+ const facetsToReturn = [];
94
+ const configFacets = this.config.facetFieldsForSearch || ["category.id"];
95
+ for (const facet of configFacets) {
96
+ facetsToReturn.push({
97
+ distinct: {
98
+ name: facet,
99
+ field: facet,
100
+ fieldType: "text",
101
+ limit: 50
102
+ }
103
+ });
104
+ }
105
+ return facetsToReturn;
38
106
  }
39
107
  async queryByTerm(payload) {
40
108
  const client = await this.getClient();
41
- const response = await client.search().get({
42
- queryArgs: {
109
+ const facetsToReturn = await this.getFacetsToReturn(payload);
110
+ const facetsToApply = await this.getFacetsQuery(payload);
111
+ const searchTermExpression = await this.getSearchTermExpression(payload);
112
+ let finalFilterExpression = void 0;
113
+ if (facetsToApply) {
114
+ finalFilterExpression = {
115
+ and: [searchTermExpression, facetsToApply]
116
+ };
117
+ } else {
118
+ finalFilterExpression = searchTermExpression;
119
+ }
120
+ const response = await client.search().post({
121
+ body: {
122
+ query: finalFilterExpression,
123
+ productProjectionParameters: {
124
+ storeProjection: this.context.storeIdentifier.key
125
+ },
43
126
  limit: payload.search.paginationOptions.pageSize,
44
127
  offset: (payload.search.paginationOptions.pageNumber - 1) * payload.search.paginationOptions.pageSize,
45
- [`text.${this.context.languageContext.locale}`]: payload.search.term
128
+ facets: [...facetsToReturn]
46
129
  }
47
130
  }).execute();
48
131
  const responseBody = response.body;
@@ -85,16 +168,14 @@ class CommercetoolsSearchProvider extends ProductSearchProvider {
85
168
  ...query.search
86
169
  };
87
170
  const products = body.results.map(
88
- (p) => this.parseSingle(p)
171
+ (p) => this.parseSingle(p.productProjection)
89
172
  );
90
173
  const facets = [];
91
- for (const id in body.facets) {
92
- const f = body.facets[id];
93
- const facetId = FacetIdentifierSchema.parse({
94
- key: id
174
+ for (const facet of body.facets) {
175
+ const facetIdentifier = FacetIdentifierSchema.parse({
176
+ key: facet.name
95
177
  });
96
- const facet = this.parseFacet(facetId, f);
97
- facets.push(facet);
178
+ facets.push(this.parseFacet(facetIdentifier, facet));
98
179
  }
99
180
  const result = {
100
181
  identifier,
@@ -111,22 +192,29 @@ class CommercetoolsSearchProvider extends ProductSearchProvider {
111
192
  };
112
193
  return result;
113
194
  }
114
- parseFacet(facetIdentifier, facetValue) {
195
+ /**
196
+ * See version 0.0.81 for ProductProjection based facet parsing
197
+ * @param facetIdentifier
198
+ * @param facetValue
199
+ * @returns
200
+ */
201
+ parseFacet(facetIdentifier, facet) {
115
202
  const result = ProductSearchResultFacetSchema.parse({
116
203
  identifier: facetIdentifier,
117
- name: facetIdentifier.key,
204
+ name: facet.name,
118
205
  values: []
119
206
  });
120
- if (facetValue.type === "terms") {
121
- for (const ft of facetValue.terms) {
207
+ const distinctFacet = facet;
208
+ if (distinctFacet) {
209
+ distinctFacet.buckets.forEach((bucket) => {
122
210
  const facetValueIdentifier = FacetValueIdentifierSchema.parse({
123
211
  facet: facetIdentifier,
124
- key: ft.term
212
+ key: bucket.key
125
213
  });
126
214
  result.values.push(
127
- this.parseFacetValue(facetValueIdentifier, ft.term, ft.count)
215
+ this.parseFacetValue(facetValueIdentifier, bucket.key, bucket.count)
128
216
  );
129
- }
217
+ });
130
218
  }
131
219
  return result;
132
220
  }
@@ -7,7 +7,8 @@ const CommercetoolsConfigurationSchema = z.looseObject({
7
7
  clientId: z.string(),
8
8
  clientSecret: z.string(),
9
9
  scopes: z.array(z.string()).default(() => []),
10
- paymentMethods: PaymentMethodSchema.array().optional().default(() => [])
10
+ paymentMethods: PaymentMethodSchema.array().optional().default(() => []),
11
+ facetFieldsForSearch: z.array(z.string()).default(() => [])
11
12
  });
12
13
  export {
13
14
  CommercetoolsConfigurationSchema
@@ -1,13 +1,45 @@
1
+ import type { ProductSearchFacetResult as CTProductSearchFacetResult, ProductVariant as CTProductVariant, ProductPagedSearchResponse, ProductProjection, ProductSearchFacetExpression } from '@commercetools/platform-sdk';
2
+ import type { Cache, FacetIdentifier, FacetValueIdentifier, ProductSearchQueryByTerm, ProductSearchResult, ProductSearchResultFacet, ProductSearchResultFacetValue, ProductSearchResultItemVariant, RequestContext } from '@reactionary/core';
1
3
  import { ProductSearchProvider } from '@reactionary/core';
2
- import type { Cache, ProductSearchQueryByTerm, RequestContext, ProductSearchResult, ProductSearchResultItemVariant, FacetIdentifier, ProductSearchResultFacet, FacetValueIdentifier, ProductSearchResultFacetValue } from '@reactionary/core';
3
4
  import type { CommercetoolsConfiguration } from '../schema/configuration.schema.js';
4
- import type { ProductVariant as CTProductVariant, FacetResult, ProductProjection, ProductProjectionPagedSearchResponse } from '@commercetools/platform-sdk';
5
5
  import type { CommercetoolsClient } from '../core/client.js';
6
6
  export declare class CommercetoolsSearchProvider extends ProductSearchProvider {
7
7
  protected config: CommercetoolsConfiguration;
8
8
  protected client: CommercetoolsClient;
9
9
  constructor(config: CommercetoolsConfiguration, cache: Cache, context: RequestContext, client: CommercetoolsClient);
10
- protected getClient(): Promise<import("@commercetools/platform-sdk").ByProjectKeyProductProjectionsRequestBuilder>;
10
+ protected getClient(): Promise<import("@commercetools/platform-sdk").ByProjectKeyProductsRequestBuilder>;
11
+ protected getFacetQuery(payload: ProductSearchQueryByTerm, selectedFacetValue: FacetValueIdentifier): Promise<{
12
+ exact: {
13
+ field: string;
14
+ fieldType: string;
15
+ value: string;
16
+ };
17
+ }>;
18
+ protected getSearchTermExpression(payload: ProductSearchQueryByTerm): Promise<{
19
+ or: {
20
+ fullText: {
21
+ field: string;
22
+ language: string;
23
+ value: string;
24
+ };
25
+ }[];
26
+ }>;
27
+ protected getFacetsQuery(payload: ProductSearchQueryByTerm): Promise<{
28
+ exact: {
29
+ field: string;
30
+ fieldType: string;
31
+ value: string;
32
+ };
33
+ } | {
34
+ and: {
35
+ exact: {
36
+ field: string;
37
+ fieldType: string;
38
+ value: string;
39
+ };
40
+ }[];
41
+ } | undefined>;
42
+ protected getFacetsToReturn(payload: ProductSearchQueryByTerm): Promise<ProductSearchFacetExpression[]>;
11
43
  queryByTerm(payload: ProductSearchQueryByTerm): Promise<ProductSearchResult>;
12
44
  protected parseSingle(body: ProductProjection): {
13
45
  identifier: {
@@ -49,7 +81,7 @@ export declare class CommercetoolsSearchProvider extends ProductSearchProvider {
49
81
  placeholder: false;
50
82
  };
51
83
  };
52
- protected parsePaginatedResult(body: ProductProjectionPagedSearchResponse, query: ProductSearchQueryByTerm): {
84
+ protected parsePaginatedResult(body: ProductPagedSearchResponse, query: ProductSearchQueryByTerm): {
53
85
  identifier: {
54
86
  term: string;
55
87
  facets: {
@@ -133,7 +165,13 @@ export declare class CommercetoolsSearchProvider extends ProductSearchProvider {
133
165
  }[];
134
166
  }[];
135
167
  };
136
- protected parseFacet(facetIdentifier: FacetIdentifier, facetValue: FacetResult): ProductSearchResultFacet;
168
+ /**
169
+ * See version 0.0.81 for ProductProjection based facet parsing
170
+ * @param facetIdentifier
171
+ * @param facetValue
172
+ * @returns
173
+ */
174
+ protected parseFacet(facetIdentifier: FacetIdentifier, facet: CTProductSearchFacetResult): ProductSearchResultFacet;
137
175
  protected parseFacetValue(facetValueIdentifier: FacetValueIdentifier, label: string, count: number): ProductSearchResultFacetValue;
138
176
  protected parseVariant(variant: CTProductVariant, product: ProductProjection): ProductSearchResultItemVariant;
139
177
  }
@@ -28,5 +28,6 @@ export declare const CommercetoolsConfigurationSchema: z.ZodObject<{
28
28
  description: z.ZodString;
29
29
  isPunchOut: z.ZodBoolean;
30
30
  }, z.core.$loose>>>>;
31
+ facetFieldsForSearch: z.ZodDefault<z.ZodArray<z.ZodString>>;
31
32
  }, z.core.$loose>;
32
33
  export type CommercetoolsConfiguration = z.infer<typeof CommercetoolsConfigurationSchema>;