@reactionary/commercetools 0.6.3
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/README.md +11 -0
- package/capabilities/cart.capability.js +324 -0
- package/capabilities/category.capability.js +198 -0
- package/capabilities/checkout.capability.js +374 -0
- package/capabilities/identity.capability.js +95 -0
- package/capabilities/index.js +15 -0
- package/capabilities/inventory.capability.js +63 -0
- package/capabilities/order-search.capability.js +106 -0
- package/capabilities/order.capability.js +48 -0
- package/capabilities/price.capability.js +114 -0
- package/capabilities/product-associations.capability.js +120 -0
- package/capabilities/product-list.capability.js +393 -0
- package/capabilities/product-reviews.capability.js +193 -0
- package/capabilities/product-search.capability.js +297 -0
- package/capabilities/product.capability.js +103 -0
- package/capabilities/profile.capability.js +337 -0
- package/capabilities/store.capability.js +51 -0
- package/core/capability-descriptors.js +274 -0
- package/core/client.js +318 -0
- package/core/initialize.js +47 -0
- package/core/initialize.types.js +8 -0
- package/core/token-cache.js +36 -0
- package/factories/cart/cart.factory.js +110 -0
- package/factories/category/category.factory.js +40 -0
- package/factories/checkout/checkout-initializer-overrides.example.js +67 -0
- package/factories/checkout/checkout.factory.js +235 -0
- package/factories/identity/identity.factory.js +14 -0
- package/factories/inventory/inventory.factory.js +30 -0
- package/factories/order/order.factory.js +114 -0
- package/factories/order-search/order-search.factory.js +68 -0
- package/factories/price/price.factory.js +52 -0
- package/factories/product/product-capability-custom-method-only.example.js +38 -0
- package/factories/product/product-capability-schema-signature-extension.example.js +46 -0
- package/factories/product/product-factory-baseline.example.js +10 -0
- package/factories/product/product-factory-schema-and-parse-extension.example.js +25 -0
- package/factories/product/product-factory-schema-extension.example.js +18 -0
- package/factories/product/product-initializer-factory-extension.example.js +33 -0
- package/factories/product/product.factory.js +151 -0
- package/factories/product/utils.example.js +8 -0
- package/factories/product-associations/product-associations.factory.js +63 -0
- package/factories/product-list/product-list.factory.js +65 -0
- package/factories/product-reviews/product-reviews.factory.js +42 -0
- package/factories/product-search/product-search.factory.js +114 -0
- package/factories/profile/profile.factory.js +62 -0
- package/factories/store/store.factory.js +29 -0
- package/index.js +28 -0
- package/package.json +17 -0
- package/schema/capabilities.schema.js +46 -0
- package/schema/commercetools.schema.js +30 -0
- package/schema/configuration.schema.js +19 -0
- package/schema/session.schema.js +9 -0
- package/src/capabilities/cart.capability.d.ts +34 -0
- package/src/capabilities/category.capability.d.ts +47 -0
- package/src/capabilities/checkout.capability.d.ts +39 -0
- package/src/capabilities/identity.capability.d.ts +14 -0
- package/src/capabilities/index.d.ts +15 -0
- package/src/capabilities/inventory.capability.d.ts +13 -0
- package/src/capabilities/order-search.capability.d.ts +13 -0
- package/src/capabilities/order.capability.d.ts +13 -0
- package/src/capabilities/price.capability.d.ts +18 -0
- package/src/capabilities/product-associations.capability.d.ts +17 -0
- package/src/capabilities/product-list.capability.d.ts +29 -0
- package/src/capabilities/product-reviews.capability.d.ts +18 -0
- package/src/capabilities/product-search.capability.d.ts +82 -0
- package/src/capabilities/product.capability.d.ts +17 -0
- package/src/capabilities/profile.capability.d.ts +30 -0
- package/src/capabilities/store.capability.d.ts +13 -0
- package/src/core/capability-descriptors.d.ts +16 -0
- package/src/core/client.d.ts +55 -0
- package/src/core/initialize.d.ts +5 -0
- package/src/core/initialize.types.d.ts +118 -0
- package/src/core/token-cache.d.ts +9 -0
- package/src/factories/cart/cart.factory.d.ts +14 -0
- package/src/factories/category/category.factory.d.ts +11 -0
- package/src/factories/checkout/checkout-initializer-overrides.example.d.ts +1 -0
- package/src/factories/checkout/checkout.factory.d.ts +18 -0
- package/src/factories/identity/identity.factory.d.ts +8 -0
- package/src/factories/inventory/inventory.factory.d.ts +9 -0
- package/src/factories/order/order.factory.d.ts +9 -0
- package/src/factories/order-search/order-search.factory.d.ts +11 -0
- package/src/factories/price/price.factory.d.ts +11 -0
- package/src/factories/product/product-capability-custom-method-only.example.d.ts +1 -0
- package/src/factories/product/product-capability-schema-signature-extension.example.d.ts +1 -0
- package/src/factories/product/product-factory-baseline.example.d.ts +1 -0
- package/src/factories/product/product-factory-schema-and-parse-extension.example.d.ts +1 -0
- package/src/factories/product/product-factory-schema-extension.example.d.ts +1 -0
- package/src/factories/product/product-initializer-factory-extension.example.d.ts +1 -0
- package/src/factories/product/product.factory.d.ts +18 -0
- package/src/factories/product/utils.example.d.ts +4 -0
- package/src/factories/product-associations/product-associations.factory.d.ts +15 -0
- package/src/factories/product-list/product-list.factory.d.ts +17 -0
- package/src/factories/product-reviews/product-reviews.factory.d.ts +14 -0
- package/src/factories/product-search/product-search.factory.d.ts +13 -0
- package/src/factories/profile/profile.factory.d.ts +11 -0
- package/src/factories/store/store.factory.d.ts +9 -0
- package/src/index.d.ts +28 -0
- package/src/schema/capabilities.schema.d.ts +104 -0
- package/src/schema/commercetools.schema.d.ts +30 -0
- package/src/schema/configuration.schema.d.ts +30 -0
- package/src/schema/session.schema.d.ts +7 -0
- package/src/test/client-builder-merge-extensions.example.d.ts +1 -0
- package/src/test/test-utils.d.ts +27 -0
- package/test/client-builder-merge-extensions.example.js +94 -0
- package/test/test-utils.js +27 -0
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# commercetools
|
|
2
|
+
|
|
3
|
+
This library was generated with [Nx](https://nx.dev).
|
|
4
|
+
|
|
5
|
+
## Building
|
|
6
|
+
|
|
7
|
+
Run `nx build commercetools` to build the library.
|
|
8
|
+
|
|
9
|
+
## Running unit tests
|
|
10
|
+
|
|
11
|
+
Run `nx test commercetools` to execute the unit tests via [Vitest](https://vitest.dev/).
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
4
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
5
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
6
|
+
if (decorator = decorators[i])
|
|
7
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
8
|
+
if (kind && result)
|
|
9
|
+
__defProp(target, key, result);
|
|
10
|
+
return result;
|
|
11
|
+
};
|
|
12
|
+
import {
|
|
13
|
+
CartIdentifierSchema,
|
|
14
|
+
CartMutationApplyCouponSchema,
|
|
15
|
+
CartMutationChangeCurrencySchema,
|
|
16
|
+
CartMutationDeleteCartSchema,
|
|
17
|
+
CartMutationItemAddSchema,
|
|
18
|
+
CartMutationItemQuantityChangeSchema,
|
|
19
|
+
CartMutationItemRemoveSchema,
|
|
20
|
+
CartMutationRemoveCouponSchema,
|
|
21
|
+
CartCapability,
|
|
22
|
+
CartQueryByIdSchema,
|
|
23
|
+
CartSchema,
|
|
24
|
+
Reactionary,
|
|
25
|
+
success,
|
|
26
|
+
error,
|
|
27
|
+
unwrapValue,
|
|
28
|
+
assertSuccess
|
|
29
|
+
} from "@reactionary/core";
|
|
30
|
+
import { CommercetoolsCartIdentifierSchema } from "../schema/commercetools.schema.js";
|
|
31
|
+
class CommercetoolsCartCapability extends CartCapability {
|
|
32
|
+
config;
|
|
33
|
+
commercetools;
|
|
34
|
+
expandedCartFields = ["discountCodes[*].discountCode"];
|
|
35
|
+
factory;
|
|
36
|
+
constructor(config, cache, context, commercetools, factory) {
|
|
37
|
+
super(cache, context);
|
|
38
|
+
this.config = config;
|
|
39
|
+
this.commercetools = commercetools;
|
|
40
|
+
this.factory = factory;
|
|
41
|
+
}
|
|
42
|
+
async getById(payload) {
|
|
43
|
+
const client = await this.getClient();
|
|
44
|
+
const ctId = payload.cart;
|
|
45
|
+
try {
|
|
46
|
+
const remote = await client.carts.withId({ ID: ctId.key }).get({
|
|
47
|
+
queryArgs: {
|
|
48
|
+
expand: this.expandedCartFields
|
|
49
|
+
}
|
|
50
|
+
}).execute();
|
|
51
|
+
return success(this.factory.parseCart(this.context, remote.body));
|
|
52
|
+
} catch (err) {
|
|
53
|
+
return error({
|
|
54
|
+
type: "NotFound",
|
|
55
|
+
identifier: ctId
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async add(payload) {
|
|
60
|
+
let cartIdentifier = payload.cart;
|
|
61
|
+
if (!cartIdentifier) {
|
|
62
|
+
cartIdentifier = await this.createCart();
|
|
63
|
+
}
|
|
64
|
+
const channelId = await this.commercetools.resolveChannelIdByRole("Primary");
|
|
65
|
+
const result = await this.applyActions(cartIdentifier, [
|
|
66
|
+
{
|
|
67
|
+
action: "addLineItem",
|
|
68
|
+
quantity: payload.quantity,
|
|
69
|
+
sku: payload.variant.sku,
|
|
70
|
+
// FIXME: This should be dynamic, probably as part of the context...
|
|
71
|
+
distributionChannel: {
|
|
72
|
+
typeId: "channel",
|
|
73
|
+
id: channelId
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
action: "recalculate"
|
|
78
|
+
}
|
|
79
|
+
]);
|
|
80
|
+
return success(result);
|
|
81
|
+
}
|
|
82
|
+
async remove(payload) {
|
|
83
|
+
const result = await this.applyActions(payload.cart, [
|
|
84
|
+
{
|
|
85
|
+
action: "removeLineItem",
|
|
86
|
+
lineItemId: payload.item.key
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
action: "recalculate"
|
|
90
|
+
}
|
|
91
|
+
]);
|
|
92
|
+
return success(result);
|
|
93
|
+
}
|
|
94
|
+
async changeQuantity(payload) {
|
|
95
|
+
if (payload.quantity === 0) {
|
|
96
|
+
const existing = await this.getById({ cart: payload.cart });
|
|
97
|
+
assertSuccess(existing);
|
|
98
|
+
return existing;
|
|
99
|
+
}
|
|
100
|
+
const result = await this.applyActions(payload.cart, [
|
|
101
|
+
{
|
|
102
|
+
action: "changeLineItemQuantity",
|
|
103
|
+
lineItemId: payload.item.key,
|
|
104
|
+
quantity: payload.quantity
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
action: "recalculate"
|
|
108
|
+
}
|
|
109
|
+
]);
|
|
110
|
+
return success(result);
|
|
111
|
+
}
|
|
112
|
+
async getActiveCartId() {
|
|
113
|
+
const client = await this.getClient();
|
|
114
|
+
try {
|
|
115
|
+
const carts = await client.activeCart.get().execute();
|
|
116
|
+
const result = this.factory.parseCartIdentifier(this.context, {
|
|
117
|
+
key: carts.body.id,
|
|
118
|
+
version: carts.body.version || 0
|
|
119
|
+
});
|
|
120
|
+
return success(result);
|
|
121
|
+
} catch (e) {
|
|
122
|
+
return error({
|
|
123
|
+
type: "NotFound",
|
|
124
|
+
identifier: {}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async deleteCart(payload) {
|
|
129
|
+
const client = await this.getClient();
|
|
130
|
+
if (payload.cart.key) {
|
|
131
|
+
const ctId = payload.cart;
|
|
132
|
+
await client.carts.withId({ ID: ctId.key }).delete({
|
|
133
|
+
queryArgs: {
|
|
134
|
+
version: ctId.version,
|
|
135
|
+
dataErasure: false
|
|
136
|
+
}
|
|
137
|
+
}).execute();
|
|
138
|
+
}
|
|
139
|
+
return success(void 0);
|
|
140
|
+
}
|
|
141
|
+
async applyCouponCode(payload) {
|
|
142
|
+
const result = await this.applyActions(payload.cart, [
|
|
143
|
+
{
|
|
144
|
+
action: "addDiscountCode",
|
|
145
|
+
code: payload.couponCode
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
action: "recalculate"
|
|
149
|
+
}
|
|
150
|
+
]);
|
|
151
|
+
return success(result);
|
|
152
|
+
}
|
|
153
|
+
async removeCouponCode(payload) {
|
|
154
|
+
const client = await this.getClient();
|
|
155
|
+
const currentCart = await client.carts.withId({ ID: payload.cart.key }).get({
|
|
156
|
+
queryArgs: {
|
|
157
|
+
expand: this.expandedCartFields
|
|
158
|
+
}
|
|
159
|
+
}).execute();
|
|
160
|
+
const discountCodeReference = currentCart.body.discountCodes?.find(
|
|
161
|
+
(dc) => dc.discountCode.obj?.code === payload.couponCode
|
|
162
|
+
)?.discountCode;
|
|
163
|
+
if (!discountCodeReference) {
|
|
164
|
+
return success(this.factory.parseCart(this.context, currentCart.body));
|
|
165
|
+
}
|
|
166
|
+
const result = await this.applyActions(payload.cart, [
|
|
167
|
+
{
|
|
168
|
+
action: "removeDiscountCode",
|
|
169
|
+
discountCode: {
|
|
170
|
+
id: discountCodeReference.id,
|
|
171
|
+
typeId: "discount-code"
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
action: "recalculate"
|
|
176
|
+
}
|
|
177
|
+
]);
|
|
178
|
+
return success(result);
|
|
179
|
+
}
|
|
180
|
+
async changeCurrency(payload) {
|
|
181
|
+
const client = await this.getClient();
|
|
182
|
+
const currentCart = await client.carts.withId({ ID: payload.cart.key }).get().execute();
|
|
183
|
+
const newCart = await client.carts.post({
|
|
184
|
+
body: {
|
|
185
|
+
currency: payload.newCurrency,
|
|
186
|
+
locale: this.context.languageContext.locale
|
|
187
|
+
}
|
|
188
|
+
}).execute();
|
|
189
|
+
const newCartId = CommercetoolsCartIdentifierSchema.parse({
|
|
190
|
+
key: newCart.body.id,
|
|
191
|
+
version: newCart.body.version || 0
|
|
192
|
+
});
|
|
193
|
+
const cartItemAdds = currentCart.body.lineItems.map(
|
|
194
|
+
(item) => ({
|
|
195
|
+
action: "addLineItem",
|
|
196
|
+
sku: item.variant.sku || "",
|
|
197
|
+
quantity: item.quantity
|
|
198
|
+
})
|
|
199
|
+
);
|
|
200
|
+
const response = await this.applyActions(newCartId, [
|
|
201
|
+
...cartItemAdds,
|
|
202
|
+
{
|
|
203
|
+
action: "recalculate"
|
|
204
|
+
}
|
|
205
|
+
]);
|
|
206
|
+
await client.carts.withId({ ID: payload.cart.key }).delete({
|
|
207
|
+
queryArgs: {
|
|
208
|
+
version: currentCart.body.version || 0,
|
|
209
|
+
dataErasure: false
|
|
210
|
+
}
|
|
211
|
+
}).execute();
|
|
212
|
+
return success(response);
|
|
213
|
+
}
|
|
214
|
+
async createCart() {
|
|
215
|
+
const client = await this.getClient();
|
|
216
|
+
const response = await client.carts.post({
|
|
217
|
+
body: {
|
|
218
|
+
currency: this.context.languageContext.currencyCode || "USD",
|
|
219
|
+
country: this.context.taxJurisdiction.countryCode || "US",
|
|
220
|
+
locale: this.context.languageContext.locale
|
|
221
|
+
},
|
|
222
|
+
queryArgs: {
|
|
223
|
+
expand: this.expandedCartFields
|
|
224
|
+
}
|
|
225
|
+
}).execute();
|
|
226
|
+
return this.factory.parseCartIdentifier(this.context, {
|
|
227
|
+
key: response.body.id,
|
|
228
|
+
version: response.body.version || 0
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
async applyActions(cart, actions) {
|
|
232
|
+
const client = await this.getClient();
|
|
233
|
+
const ctId = cart;
|
|
234
|
+
try {
|
|
235
|
+
const response = await client.carts.withId({ ID: ctId.key }).post({
|
|
236
|
+
body: {
|
|
237
|
+
version: ctId.version,
|
|
238
|
+
actions
|
|
239
|
+
},
|
|
240
|
+
queryArgs: {
|
|
241
|
+
expand: this.expandedCartFields
|
|
242
|
+
}
|
|
243
|
+
}).execute();
|
|
244
|
+
if (response.error) {
|
|
245
|
+
console.error(response.error);
|
|
246
|
+
}
|
|
247
|
+
return this.factory.parseCart(this.context, response.body);
|
|
248
|
+
} catch (e) {
|
|
249
|
+
console.error("Error applying actions to cart:", e);
|
|
250
|
+
throw e;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Creates a new Commercetools client, optionally upgrading it from Anonymous mode to Guest mode.
|
|
255
|
+
* For now, any Query or Mutation will require an upgrade to Guest mode.
|
|
256
|
+
* In the future, maybe we can delay this upgrade until we actually need it.
|
|
257
|
+
*/
|
|
258
|
+
async getClient() {
|
|
259
|
+
const client = await this.commercetools.getClient();
|
|
260
|
+
const clientWithProject = client.withProjectKey({
|
|
261
|
+
projectKey: this.config.projectKey
|
|
262
|
+
});
|
|
263
|
+
return {
|
|
264
|
+
carts: clientWithProject.me().carts(),
|
|
265
|
+
activeCart: clientWithProject.me().activeCart(),
|
|
266
|
+
orders: clientWithProject.me().orders()
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
__decorateClass([
|
|
271
|
+
Reactionary({
|
|
272
|
+
inputSchema: CartQueryByIdSchema,
|
|
273
|
+
outputSchema: CartSchema
|
|
274
|
+
})
|
|
275
|
+
], CommercetoolsCartCapability.prototype, "getById", 1);
|
|
276
|
+
__decorateClass([
|
|
277
|
+
Reactionary({
|
|
278
|
+
inputSchema: CartMutationItemAddSchema,
|
|
279
|
+
outputSchema: CartSchema
|
|
280
|
+
})
|
|
281
|
+
], CommercetoolsCartCapability.prototype, "add", 1);
|
|
282
|
+
__decorateClass([
|
|
283
|
+
Reactionary({
|
|
284
|
+
inputSchema: CartMutationItemRemoveSchema,
|
|
285
|
+
outputSchema: CartSchema
|
|
286
|
+
})
|
|
287
|
+
], CommercetoolsCartCapability.prototype, "remove", 1);
|
|
288
|
+
__decorateClass([
|
|
289
|
+
Reactionary({
|
|
290
|
+
inputSchema: CartMutationItemQuantityChangeSchema,
|
|
291
|
+
outputSchema: CartSchema
|
|
292
|
+
})
|
|
293
|
+
], CommercetoolsCartCapability.prototype, "changeQuantity", 1);
|
|
294
|
+
__decorateClass([
|
|
295
|
+
Reactionary({
|
|
296
|
+
outputSchema: CartIdentifierSchema
|
|
297
|
+
})
|
|
298
|
+
], CommercetoolsCartCapability.prototype, "getActiveCartId", 1);
|
|
299
|
+
__decorateClass([
|
|
300
|
+
Reactionary({
|
|
301
|
+
inputSchema: CartMutationDeleteCartSchema
|
|
302
|
+
})
|
|
303
|
+
], CommercetoolsCartCapability.prototype, "deleteCart", 1);
|
|
304
|
+
__decorateClass([
|
|
305
|
+
Reactionary({
|
|
306
|
+
inputSchema: CartMutationApplyCouponSchema,
|
|
307
|
+
outputSchema: CartSchema
|
|
308
|
+
})
|
|
309
|
+
], CommercetoolsCartCapability.prototype, "applyCouponCode", 1);
|
|
310
|
+
__decorateClass([
|
|
311
|
+
Reactionary({
|
|
312
|
+
inputSchema: CartMutationRemoveCouponSchema,
|
|
313
|
+
outputSchema: CartSchema
|
|
314
|
+
})
|
|
315
|
+
], CommercetoolsCartCapability.prototype, "removeCouponCode", 1);
|
|
316
|
+
__decorateClass([
|
|
317
|
+
Reactionary({
|
|
318
|
+
inputSchema: CartMutationChangeCurrencySchema,
|
|
319
|
+
outputSchema: CartSchema
|
|
320
|
+
})
|
|
321
|
+
], CommercetoolsCartCapability.prototype, "changeCurrency", 1);
|
|
322
|
+
export {
|
|
323
|
+
CommercetoolsCartCapability
|
|
324
|
+
};
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
4
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
5
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
6
|
+
if (decorator = decorators[i])
|
|
7
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
8
|
+
if (kind && result)
|
|
9
|
+
__defProp(target, key, result);
|
|
10
|
+
return result;
|
|
11
|
+
};
|
|
12
|
+
import {
|
|
13
|
+
CategoryPaginatedResultSchema,
|
|
14
|
+
CategoryCapability,
|
|
15
|
+
CategoryQueryByIdSchema,
|
|
16
|
+
CategoryQueryBySlugSchema,
|
|
17
|
+
CategoryQueryForBreadcrumbSchema,
|
|
18
|
+
CategoryQueryForChildCategoriesSchema,
|
|
19
|
+
CategoryQueryForTopCategoriesSchema,
|
|
20
|
+
CategorySchema,
|
|
21
|
+
Reactionary,
|
|
22
|
+
success,
|
|
23
|
+
error
|
|
24
|
+
} from "@reactionary/core";
|
|
25
|
+
import * as z from "zod";
|
|
26
|
+
class CommercetoolsCategoryCapability extends CategoryCapability {
|
|
27
|
+
config;
|
|
28
|
+
commercetools;
|
|
29
|
+
factory;
|
|
30
|
+
constructor(config, cache, context, commercetools, factory) {
|
|
31
|
+
super(cache, context);
|
|
32
|
+
this.config = config;
|
|
33
|
+
this.commercetools = commercetools;
|
|
34
|
+
this.factory = factory;
|
|
35
|
+
}
|
|
36
|
+
async getClient() {
|
|
37
|
+
const client = await this.commercetools.getClient();
|
|
38
|
+
return client.withProjectKey({ projectKey: this.config.projectKey }).categories();
|
|
39
|
+
}
|
|
40
|
+
async getById(payload) {
|
|
41
|
+
const client = await this.getClient();
|
|
42
|
+
try {
|
|
43
|
+
const response = await client.withKey({ key: payload.id.key }).get().execute();
|
|
44
|
+
return success(this.factory.parseCategory(this.context, response.body));
|
|
45
|
+
} catch (err) {
|
|
46
|
+
return error({
|
|
47
|
+
type: "NotFound",
|
|
48
|
+
identifier: payload.id
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async getBySlug(payload) {
|
|
53
|
+
const client = await this.getClient();
|
|
54
|
+
try {
|
|
55
|
+
const response = await client.get({
|
|
56
|
+
queryArgs: {
|
|
57
|
+
where: `slug(${this.context.languageContext.locale}=:slug)`,
|
|
58
|
+
"var.slug": payload.slug,
|
|
59
|
+
storeProjection: this.context.storeIdentifier.key,
|
|
60
|
+
limit: 1,
|
|
61
|
+
withTotal: false
|
|
62
|
+
}
|
|
63
|
+
}).execute();
|
|
64
|
+
if (response.body.results.length === 0) {
|
|
65
|
+
return error({
|
|
66
|
+
type: "NotFound",
|
|
67
|
+
identifier: payload.slug
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return success(this.factory.parseCategory(this.context, response.body.results[0]));
|
|
71
|
+
} catch (err) {
|
|
72
|
+
console.error(`Error fetching category by slug:`, error);
|
|
73
|
+
return error({
|
|
74
|
+
type: "NotFound",
|
|
75
|
+
identifier: payload.slug
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async getBreadcrumbPathToCategory(payload) {
|
|
80
|
+
const client = await this.getClient();
|
|
81
|
+
const path = [];
|
|
82
|
+
try {
|
|
83
|
+
const response = await client.withKey({ key: payload.id.key }).get({
|
|
84
|
+
queryArgs: {
|
|
85
|
+
expand: "ancestors[*]"
|
|
86
|
+
}
|
|
87
|
+
}).execute();
|
|
88
|
+
const category = this.factory.parseCategory(this.context, response.body);
|
|
89
|
+
for (const anc of response.body.ancestors || []) {
|
|
90
|
+
if (anc.obj) {
|
|
91
|
+
const parsedAnc = this.factory.parseCategory(this.context, anc.obj);
|
|
92
|
+
path.push(parsedAnc);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
path.push(category);
|
|
96
|
+
} catch (error2) {
|
|
97
|
+
console.error(
|
|
98
|
+
`Error fetching category path for ${payload.id.key}:`,
|
|
99
|
+
error2
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
return success(path);
|
|
103
|
+
}
|
|
104
|
+
async findChildCategories(payload) {
|
|
105
|
+
const client = await this.getClient();
|
|
106
|
+
try {
|
|
107
|
+
const parentCategory = await client.withKey({ key: payload.parentId.key }).get().execute();
|
|
108
|
+
const response = await client.get({
|
|
109
|
+
queryArgs: {
|
|
110
|
+
where: "parent(id = :parentId)",
|
|
111
|
+
"var.parentId": parentCategory.body.id,
|
|
112
|
+
limit: payload.paginationOptions.pageSize,
|
|
113
|
+
offset: (payload.paginationOptions.pageNumber - 1) * payload.paginationOptions.pageSize,
|
|
114
|
+
sort: "orderHint asc",
|
|
115
|
+
storeProjection: this.context.storeIdentifier.key
|
|
116
|
+
}
|
|
117
|
+
}).execute();
|
|
118
|
+
const result = this.factory.parseCategoryPaginatedResult(this.context, response.body);
|
|
119
|
+
return success(result);
|
|
120
|
+
} catch (error2) {
|
|
121
|
+
console.error(
|
|
122
|
+
`Error fetching category path for ${payload.parentId.key}:`,
|
|
123
|
+
error2
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
const empty = {
|
|
127
|
+
items: [],
|
|
128
|
+
pageNumber: payload.paginationOptions.pageNumber,
|
|
129
|
+
pageSize: payload.paginationOptions.pageSize,
|
|
130
|
+
totalCount: 0,
|
|
131
|
+
totalPages: 0
|
|
132
|
+
};
|
|
133
|
+
return success(empty);
|
|
134
|
+
}
|
|
135
|
+
async findTopCategories(payload) {
|
|
136
|
+
const client = await this.getClient();
|
|
137
|
+
try {
|
|
138
|
+
const response = await client.get({
|
|
139
|
+
queryArgs: {
|
|
140
|
+
where: "parent is not defined",
|
|
141
|
+
limit: payload.paginationOptions.pageSize,
|
|
142
|
+
offset: (payload.paginationOptions.pageNumber - 1) * payload.paginationOptions.pageSize,
|
|
143
|
+
sort: "orderHint asc",
|
|
144
|
+
storeProjection: this.context.storeIdentifier.key
|
|
145
|
+
}
|
|
146
|
+
}).execute();
|
|
147
|
+
const result = this.factory.parseCategoryPaginatedResult(this.context, response.body);
|
|
148
|
+
return success(result);
|
|
149
|
+
} catch (error2) {
|
|
150
|
+
console.error(`Error fetching category top categories:`, error2);
|
|
151
|
+
}
|
|
152
|
+
const empty = {
|
|
153
|
+
items: [],
|
|
154
|
+
pageNumber: payload.paginationOptions.pageNumber,
|
|
155
|
+
pageSize: payload.paginationOptions.pageSize,
|
|
156
|
+
totalCount: 0,
|
|
157
|
+
totalPages: 0
|
|
158
|
+
};
|
|
159
|
+
return success(empty);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Handler for parsing a response from a remote capability implementation and converting it
|
|
163
|
+
* into the typed domain model.
|
|
164
|
+
*/
|
|
165
|
+
}
|
|
166
|
+
__decorateClass([
|
|
167
|
+
Reactionary({
|
|
168
|
+
inputSchema: CategoryQueryByIdSchema,
|
|
169
|
+
outputSchema: CategorySchema
|
|
170
|
+
})
|
|
171
|
+
], CommercetoolsCategoryCapability.prototype, "getById", 1);
|
|
172
|
+
__decorateClass([
|
|
173
|
+
Reactionary({
|
|
174
|
+
inputSchema: CategoryQueryBySlugSchema,
|
|
175
|
+
outputSchema: CategorySchema
|
|
176
|
+
})
|
|
177
|
+
], CommercetoolsCategoryCapability.prototype, "getBySlug", 1);
|
|
178
|
+
__decorateClass([
|
|
179
|
+
Reactionary({
|
|
180
|
+
inputSchema: CategoryQueryForBreadcrumbSchema,
|
|
181
|
+
outputSchema: z.array(CategorySchema)
|
|
182
|
+
})
|
|
183
|
+
], CommercetoolsCategoryCapability.prototype, "getBreadcrumbPathToCategory", 1);
|
|
184
|
+
__decorateClass([
|
|
185
|
+
Reactionary({
|
|
186
|
+
inputSchema: CategoryQueryForChildCategoriesSchema,
|
|
187
|
+
outputSchema: CategoryPaginatedResultSchema
|
|
188
|
+
})
|
|
189
|
+
], CommercetoolsCategoryCapability.prototype, "findChildCategories", 1);
|
|
190
|
+
__decorateClass([
|
|
191
|
+
Reactionary({
|
|
192
|
+
inputSchema: CategoryQueryForTopCategoriesSchema,
|
|
193
|
+
outputSchema: CategoryPaginatedResultSchema
|
|
194
|
+
})
|
|
195
|
+
], CommercetoolsCategoryCapability.prototype, "findTopCategories", 1);
|
|
196
|
+
export {
|
|
197
|
+
CommercetoolsCategoryCapability
|
|
198
|
+
};
|