@reactionary/hcl 0.9.8 → 0.9.12
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/capabilities/cart.capability.js +411 -0
- package/capabilities/category.capability.js +111 -35
- package/capabilities/checkout.capability.js +337 -0
- package/capabilities/identity.capability.js +214 -0
- package/capabilities/inventory.capability.js +75 -0
- package/capabilities/order-search.capability.js +79 -0
- package/capabilities/order.capability.js +62 -0
- package/capabilities/price.capability.js +122 -0
- package/capabilities/product-search.capability.js +64 -13
- package/capabilities/product.capability.js +114 -28
- package/capabilities/profile.capability.js +215 -0
- package/core/client.js +81 -90
- package/core/initialize.js +170 -7
- package/core/session-keys.js +14 -0
- package/factories/cart/cart.factory.js +135 -0
- package/factories/checkout/checkout.factory.js +131 -0
- package/factories/identity/identity.factory.js +12 -0
- package/factories/index.js +8 -0
- package/factories/inventory/inventory.factory.js +38 -0
- package/factories/order/order.factory.js +124 -0
- package/factories/order-search/order-search.factory.js +59 -0
- package/factories/price/price.factory.js +39 -0
- package/factories/profile/profile.factory.js +49 -0
- package/index.js +8 -0
- package/package.json +8 -5
- package/schema/capabilities.schema.js +10 -2
- package/schema/configuration.schema.js +9 -1
- package/src/capabilities/cart.capability.d.ts +69 -0
- package/src/capabilities/category.capability.d.ts +20 -1
- package/src/capabilities/checkout.capability.d.ts +41 -0
- package/src/capabilities/identity.capability.d.ts +29 -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 +16 -0
- package/src/capabilities/product-search.capability.d.ts +7 -2
- package/src/capabilities/product.capability.d.ts +14 -2
- package/src/capabilities/profile.capability.d.ts +31 -0
- package/src/core/client.d.ts +41 -14
- package/src/core/initialize.types.d.ts +17 -5
- package/src/core/session-keys.d.ts +14 -0
- package/src/factories/cart/cart.factory.d.ts +11 -0
- package/src/factories/checkout/checkout.factory.d.ts +11 -0
- package/src/factories/identity/identity.factory.d.ts +7 -0
- package/src/factories/index.d.ts +8 -0
- package/src/factories/inventory/inventory.factory.d.ts +29 -0
- package/src/factories/order/order.factory.d.ts +11 -0
- package/src/factories/order-search/order-search.factory.d.ts +9 -0
- package/src/factories/price/price.factory.d.ts +26 -0
- package/src/factories/profile/profile.factory.d.ts +9 -0
- package/src/index.d.ts +8 -0
- package/src/schema/capabilities.schema.d.ts +25 -1
- package/src/schema/configuration.schema.d.ts +6 -0
- package/src/schema/hcl.schema.d.ts +384 -0
- package/src/test/test-utils.d.ts +5 -1
|
@@ -0,0 +1,215 @@
|
|
|
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
|
+
ProfileCapability,
|
|
14
|
+
ProfileMutationAddShippingAddressSchema,
|
|
15
|
+
ProfileMutationMakeShippingAddressDefaultSchema,
|
|
16
|
+
ProfileMutationRemoveShippingAddressSchema,
|
|
17
|
+
ProfileMutationSetBillingAddressSchema,
|
|
18
|
+
ProfileMutationUpdateSchema,
|
|
19
|
+
ProfileMutationUpdateShippingAddressSchema,
|
|
20
|
+
ProfileQueryByIdSchema,
|
|
21
|
+
ProfileSchema,
|
|
22
|
+
Reactionary,
|
|
23
|
+
error,
|
|
24
|
+
success
|
|
25
|
+
} from "@reactionary/core";
|
|
26
|
+
import createDebug from "debug";
|
|
27
|
+
const debug = createDebug("reactionary:hcl:profile");
|
|
28
|
+
const BILLING_NICK_NAME = "Billing";
|
|
29
|
+
class HclProfileCapability extends ProfileCapability {
|
|
30
|
+
config;
|
|
31
|
+
client;
|
|
32
|
+
factory;
|
|
33
|
+
constructor(cache, context, config, client, factory) {
|
|
34
|
+
super(cache, context);
|
|
35
|
+
this.config = config;
|
|
36
|
+
this.client = client;
|
|
37
|
+
this.factory = factory;
|
|
38
|
+
}
|
|
39
|
+
async fetchPerson() {
|
|
40
|
+
return this.client.callGet(this.personSelfUrl());
|
|
41
|
+
}
|
|
42
|
+
async fetchProfile() {
|
|
43
|
+
const person = await this.fetchPerson();
|
|
44
|
+
return this.factory.parseProfile(this.context, person);
|
|
45
|
+
}
|
|
46
|
+
toWcsContact(address) {
|
|
47
|
+
const lines = [address.streetAddress, address.streetNumber].filter(Boolean);
|
|
48
|
+
return {
|
|
49
|
+
nickName: address.identifier.nickName,
|
|
50
|
+
firstName: address.firstName,
|
|
51
|
+
lastName: address.lastName,
|
|
52
|
+
addressLine: lines.length ? lines : void 0,
|
|
53
|
+
city: address.city,
|
|
54
|
+
stateOrProvinceName: address.region,
|
|
55
|
+
zipCode: address.postalCode,
|
|
56
|
+
country: address.countryCode
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
personSelfUrl() {
|
|
60
|
+
return `${this.client.transactionBaseUrl}/person/@self`;
|
|
61
|
+
}
|
|
62
|
+
updatePersonPayload(email, phone) {
|
|
63
|
+
return { email1: email, phone1: phone };
|
|
64
|
+
}
|
|
65
|
+
personContactListUrl() {
|
|
66
|
+
return `${this.client.transactionBaseUrl}/person/@self/contact`;
|
|
67
|
+
}
|
|
68
|
+
contactUrl(nickName) {
|
|
69
|
+
return `${this.client.transactionBaseUrl}/person/@self/contact/${encodeURIComponent(nickName)}`;
|
|
70
|
+
}
|
|
71
|
+
makeShippingAddressDefaultPayload() {
|
|
72
|
+
return { primary: "1" };
|
|
73
|
+
}
|
|
74
|
+
async getById(_payload) {
|
|
75
|
+
debug("getById");
|
|
76
|
+
return success(await this.fetchProfile());
|
|
77
|
+
}
|
|
78
|
+
async update(payload) {
|
|
79
|
+
debug("update %o", payload.identifier);
|
|
80
|
+
await this.client.callPut(
|
|
81
|
+
this.personSelfUrl(),
|
|
82
|
+
this.updatePersonPayload(payload.email, payload.phone)
|
|
83
|
+
);
|
|
84
|
+
return success(await this.fetchProfile());
|
|
85
|
+
}
|
|
86
|
+
async addShippingAddress(payload) {
|
|
87
|
+
debug("addShippingAddress %s", payload.address.identifier.nickName);
|
|
88
|
+
await this.client.callPost(
|
|
89
|
+
this.personContactListUrl(),
|
|
90
|
+
this.toWcsContact(payload.address)
|
|
91
|
+
);
|
|
92
|
+
return success(await this.fetchProfile());
|
|
93
|
+
}
|
|
94
|
+
async updateShippingAddress(payload) {
|
|
95
|
+
debug("updateShippingAddress %s", payload.address.identifier.nickName);
|
|
96
|
+
try {
|
|
97
|
+
await this.client.callPut(
|
|
98
|
+
this.contactUrl(payload.address.identifier.nickName),
|
|
99
|
+
this.toWcsContact(payload.address)
|
|
100
|
+
);
|
|
101
|
+
} catch (err) {
|
|
102
|
+
if (err instanceof Error && err.message.includes("404")) {
|
|
103
|
+
return error({
|
|
104
|
+
type: "NotFound",
|
|
105
|
+
identifier: payload.address.identifier
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
throw err;
|
|
109
|
+
}
|
|
110
|
+
return success(await this.fetchProfile());
|
|
111
|
+
}
|
|
112
|
+
async removeShippingAddress(payload) {
|
|
113
|
+
debug("removeShippingAddress %s", payload.addressIdentifier.nickName);
|
|
114
|
+
try {
|
|
115
|
+
await this.client.callDelete(
|
|
116
|
+
this.contactUrl(payload.addressIdentifier.nickName)
|
|
117
|
+
);
|
|
118
|
+
} catch (err) {
|
|
119
|
+
if (err instanceof Error && err.message.includes("404")) {
|
|
120
|
+
return error({
|
|
121
|
+
type: "NotFound",
|
|
122
|
+
identifier: payload.addressIdentifier
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
throw err;
|
|
126
|
+
}
|
|
127
|
+
return success(await this.fetchProfile());
|
|
128
|
+
}
|
|
129
|
+
async makeShippingAddressDefault(payload) {
|
|
130
|
+
debug("makeShippingAddressDefault %s", payload.addressIdentifier.nickName);
|
|
131
|
+
try {
|
|
132
|
+
await this.client.callPut(
|
|
133
|
+
this.contactUrl(payload.addressIdentifier.nickName),
|
|
134
|
+
this.makeShippingAddressDefaultPayload()
|
|
135
|
+
);
|
|
136
|
+
} catch (err) {
|
|
137
|
+
if (err instanceof Error && err.message.includes("404")) {
|
|
138
|
+
return error({
|
|
139
|
+
type: "NotFound",
|
|
140
|
+
identifier: payload.addressIdentifier
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
throw err;
|
|
144
|
+
}
|
|
145
|
+
return success(await this.fetchProfile());
|
|
146
|
+
}
|
|
147
|
+
async setBillingAddress(payload) {
|
|
148
|
+
debug("setBillingAddress");
|
|
149
|
+
const person = await this.fetchPerson();
|
|
150
|
+
const billingContact = (person.contact ?? []).find(
|
|
151
|
+
(c) => c.nickName === BILLING_NICK_NAME
|
|
152
|
+
);
|
|
153
|
+
const contactData = {
|
|
154
|
+
...this.toWcsContact(payload.address),
|
|
155
|
+
nickName: BILLING_NICK_NAME
|
|
156
|
+
};
|
|
157
|
+
if (billingContact) {
|
|
158
|
+
await this.client.callPut(
|
|
159
|
+
this.contactUrl(BILLING_NICK_NAME),
|
|
160
|
+
contactData
|
|
161
|
+
);
|
|
162
|
+
} else {
|
|
163
|
+
await this.client.callPost(
|
|
164
|
+
this.personContactListUrl(),
|
|
165
|
+
contactData
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
return success(await this.fetchProfile());
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
__decorateClass([
|
|
172
|
+
Reactionary({
|
|
173
|
+
inputSchema: ProfileQueryByIdSchema,
|
|
174
|
+
outputSchema: ProfileSchema
|
|
175
|
+
})
|
|
176
|
+
], HclProfileCapability.prototype, "getById", 1);
|
|
177
|
+
__decorateClass([
|
|
178
|
+
Reactionary({
|
|
179
|
+
inputSchema: ProfileMutationUpdateSchema,
|
|
180
|
+
outputSchema: ProfileSchema
|
|
181
|
+
})
|
|
182
|
+
], HclProfileCapability.prototype, "update", 1);
|
|
183
|
+
__decorateClass([
|
|
184
|
+
Reactionary({
|
|
185
|
+
inputSchema: ProfileMutationAddShippingAddressSchema,
|
|
186
|
+
outputSchema: ProfileSchema
|
|
187
|
+
})
|
|
188
|
+
], HclProfileCapability.prototype, "addShippingAddress", 1);
|
|
189
|
+
__decorateClass([
|
|
190
|
+
Reactionary({
|
|
191
|
+
inputSchema: ProfileMutationUpdateShippingAddressSchema,
|
|
192
|
+
outputSchema: ProfileSchema
|
|
193
|
+
})
|
|
194
|
+
], HclProfileCapability.prototype, "updateShippingAddress", 1);
|
|
195
|
+
__decorateClass([
|
|
196
|
+
Reactionary({
|
|
197
|
+
inputSchema: ProfileMutationRemoveShippingAddressSchema,
|
|
198
|
+
outputSchema: ProfileSchema
|
|
199
|
+
})
|
|
200
|
+
], HclProfileCapability.prototype, "removeShippingAddress", 1);
|
|
201
|
+
__decorateClass([
|
|
202
|
+
Reactionary({
|
|
203
|
+
inputSchema: ProfileMutationMakeShippingAddressDefaultSchema,
|
|
204
|
+
outputSchema: ProfileSchema
|
|
205
|
+
})
|
|
206
|
+
], HclProfileCapability.prototype, "makeShippingAddressDefault", 1);
|
|
207
|
+
__decorateClass([
|
|
208
|
+
Reactionary({
|
|
209
|
+
inputSchema: ProfileMutationSetBillingAddressSchema,
|
|
210
|
+
outputSchema: ProfileSchema
|
|
211
|
+
})
|
|
212
|
+
], HclProfileCapability.prototype, "setBillingAddress", 1);
|
|
213
|
+
export {
|
|
214
|
+
HclProfileCapability
|
|
215
|
+
};
|
package/core/client.js
CHANGED
|
@@ -1,120 +1,111 @@
|
|
|
1
|
+
import { getLocaleParams } from "./locale-params.js";
|
|
2
|
+
import {
|
|
3
|
+
SESSION_KEY_PERSONALIZATION_ID,
|
|
4
|
+
SESSION_KEY_WC_TOKEN,
|
|
5
|
+
SESSION_KEY_WC_TRUSTED_TOKEN
|
|
6
|
+
} from "./session-keys.js";
|
|
7
|
+
class HclCartNotFoundError extends Error {
|
|
8
|
+
constructor() {
|
|
9
|
+
super("No active cart found");
|
|
10
|
+
this.name = "HclCartNotFoundError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
1
13
|
class HclClient {
|
|
2
|
-
constructor(config) {
|
|
14
|
+
constructor(config, context) {
|
|
3
15
|
this.config = config;
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
16
|
+
this.context = context;
|
|
17
|
+
const origin = config.apiUrl.replace(/\/+$/, "");
|
|
18
|
+
const searchOrigin = (config.searchApiUrl ?? config.apiUrl).replace(/\/+$/, "");
|
|
19
|
+
this.catalogBaseUrl = `${searchOrigin}/search/resources`;
|
|
20
|
+
this.transactionBaseUrl = `${origin}/wcs/resources/store/${config.storeId}`;
|
|
7
21
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
params.set("langId", query.langId);
|
|
17
|
-
if (query.currency)
|
|
18
|
-
params.set("currency", query.currency);
|
|
19
|
-
if (query.categoryId)
|
|
20
|
-
params.set("categoryId", query.categoryId);
|
|
21
|
-
if (query.searchTerm)
|
|
22
|
-
params.set("searchTerm", query.searchTerm);
|
|
23
|
-
if (query.contractId)
|
|
24
|
-
params.set("contractId", query.contractId);
|
|
25
|
-
if (query.profileName)
|
|
26
|
-
params.set("profileName", query.profileName);
|
|
27
|
-
if (query.limit !== void 0)
|
|
28
|
-
params.set("limit", String(query.limit));
|
|
29
|
-
if (query.offset !== void 0)
|
|
30
|
-
params.set("offset", String(query.offset));
|
|
31
|
-
if (query.checkEntitlement !== void 0) {
|
|
32
|
-
params.set("checkEntitlement", String(query.checkEntitlement));
|
|
33
|
-
}
|
|
34
|
-
for (const id of query.id ?? []) {
|
|
35
|
-
params.append("id", id);
|
|
36
|
-
}
|
|
37
|
-
for (const partNumber of query.partNumber ?? []) {
|
|
38
|
-
params.append("partNumber", partNumber);
|
|
39
|
-
}
|
|
40
|
-
for (const facet of query.facets ?? []) {
|
|
41
|
-
params.append("facet", facet);
|
|
42
|
-
}
|
|
43
|
-
const url = `${this.baseUrl}/products?${params.toString()}`;
|
|
44
|
-
const response = await fetch(url, {
|
|
22
|
+
/** Base URL for the Query Service (catalog / search). */
|
|
23
|
+
catalogBaseUrl;
|
|
24
|
+
/** Base URL for the Transaction Service (WCS REST). */
|
|
25
|
+
transactionBaseUrl;
|
|
26
|
+
async callGet(url, params, opts) {
|
|
27
|
+
const merged = this.buildParams(params);
|
|
28
|
+
const query = merged.toString() ? `?${merged.toString()}` : "";
|
|
29
|
+
const response = await fetch(`${url}${query}`, {
|
|
45
30
|
method: "GET",
|
|
46
|
-
headers:
|
|
31
|
+
headers: this.buildHeaders()
|
|
47
32
|
});
|
|
33
|
+
if (opts?.allowUndefined && response.status === 404)
|
|
34
|
+
return void 0;
|
|
48
35
|
if (!response.ok) {
|
|
49
36
|
throw new Error(
|
|
50
|
-
`HCL
|
|
37
|
+
`HCL GET ${url} error ${response.status} ${response.statusText}`
|
|
51
38
|
);
|
|
52
39
|
}
|
|
53
40
|
return response.json();
|
|
54
41
|
}
|
|
55
|
-
|
|
56
|
-
* Resolve a URL slug to an HCL token (product partNumber, category ID, etc.).
|
|
57
|
-
* Calls GET /api/v2/urls?storeId=X&identifier=<slug>
|
|
58
|
-
* Returns undefined when the slug is not found (404).
|
|
59
|
-
*/
|
|
60
|
-
async resolveSlug(slug, langId) {
|
|
61
|
-
const params = new URLSearchParams();
|
|
62
|
-
params.set("storeId", this.config.storeId);
|
|
63
|
-
params.append("identifier", slug);
|
|
64
|
-
if (langId)
|
|
65
|
-
params.set("langId", langId);
|
|
66
|
-
const url = `${this.baseUrl}/urls?${params.toString()}`;
|
|
42
|
+
async callPost(url, body = {}) {
|
|
67
43
|
const response = await fetch(url, {
|
|
68
|
-
method: "
|
|
69
|
-
headers: {
|
|
44
|
+
method: "POST",
|
|
45
|
+
headers: { ...this.buildHeaders(), "Content-Type": "application/json" },
|
|
46
|
+
body: JSON.stringify(body)
|
|
70
47
|
});
|
|
71
|
-
if (response.status === 404)
|
|
72
|
-
return void 0;
|
|
73
48
|
if (!response.ok) {
|
|
49
|
+
const text = await response.text();
|
|
74
50
|
throw new Error(
|
|
75
|
-
`HCL
|
|
51
|
+
`HCL POST ${url} error ${response.status} ${response.statusText}: ${text}`
|
|
76
52
|
);
|
|
77
53
|
}
|
|
78
|
-
|
|
79
|
-
return data.contents?.[0];
|
|
54
|
+
return response.json();
|
|
80
55
|
}
|
|
81
|
-
|
|
82
|
-
* Query categories from the HCL Commerce Query Service.
|
|
83
|
-
* Calls GET /api/v2/categories with the given query parameters.
|
|
84
|
-
*/
|
|
85
|
-
async findCategories(query) {
|
|
86
|
-
const params = new URLSearchParams();
|
|
87
|
-
params.set("storeId", query.storeId ?? this.config.storeId);
|
|
88
|
-
const catalogId = query.catalogId ?? this.config.catalogId;
|
|
89
|
-
if (catalogId)
|
|
90
|
-
params.set("catalogId", catalogId);
|
|
91
|
-
if (query.langId)
|
|
92
|
-
params.set("langId", query.langId);
|
|
93
|
-
if (query.parentCategoryId)
|
|
94
|
-
params.set("parentCategoryId", query.parentCategoryId);
|
|
95
|
-
if (query.depthAndLimit)
|
|
96
|
-
params.set("depthAndLimit", query.depthAndLimit);
|
|
97
|
-
if (query.profileName)
|
|
98
|
-
params.set("profileName", query.profileName);
|
|
99
|
-
for (const id of query.id ?? []) {
|
|
100
|
-
params.append("id", id);
|
|
101
|
-
}
|
|
102
|
-
for (const identifier of query.identifier ?? []) {
|
|
103
|
-
params.append("identifier", identifier);
|
|
104
|
-
}
|
|
105
|
-
const url = `${this.baseUrl}/categories?${params.toString()}`;
|
|
56
|
+
async callPut(url, body) {
|
|
106
57
|
const response = await fetch(url, {
|
|
107
|
-
method: "
|
|
108
|
-
headers: {
|
|
58
|
+
method: "PUT",
|
|
59
|
+
headers: { ...this.buildHeaders(), "Content-Type": "application/json" },
|
|
60
|
+
body: JSON.stringify(body)
|
|
109
61
|
});
|
|
110
62
|
if (!response.ok) {
|
|
63
|
+
const text = await response.text();
|
|
111
64
|
throw new Error(
|
|
112
|
-
`HCL
|
|
65
|
+
`HCL PUT ${url} error ${response.status} ${response.statusText}: ${text}`
|
|
113
66
|
);
|
|
114
67
|
}
|
|
115
68
|
return response.json();
|
|
116
69
|
}
|
|
70
|
+
async callDelete(url, opts) {
|
|
71
|
+
const response = await fetch(url, {
|
|
72
|
+
method: "DELETE",
|
|
73
|
+
headers: this.buildHeaders()
|
|
74
|
+
});
|
|
75
|
+
if (!response.ok && !(opts?.ignore404 && response.status === 404)) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
`HCL DELETE ${url} error ${response.status} ${response.statusText}`
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Merges caller-supplied params with locale defaults (langId, currency)
|
|
83
|
+
* derived from the request context. Caller-supplied values always win.
|
|
84
|
+
*/
|
|
85
|
+
buildParams(params) {
|
|
86
|
+
const merged = new URLSearchParams(params);
|
|
87
|
+
const { langId, currency } = getLocaleParams(this.config, this.context);
|
|
88
|
+
if (langId && !merged.has("langId"))
|
|
89
|
+
merged.set("langId", langId);
|
|
90
|
+
if (currency && !merged.has("currency"))
|
|
91
|
+
merged.set("currency", currency);
|
|
92
|
+
return merged;
|
|
93
|
+
}
|
|
94
|
+
buildHeaders() {
|
|
95
|
+
const headers = { Accept: "application/json" };
|
|
96
|
+
const token = this.context.session[SESSION_KEY_WC_TOKEN];
|
|
97
|
+
const trustedToken = this.context.session[SESSION_KEY_WC_TRUSTED_TOKEN];
|
|
98
|
+
const personalizationId = this.context.session[SESSION_KEY_PERSONALIZATION_ID];
|
|
99
|
+
if (token)
|
|
100
|
+
headers["WCToken"] = token;
|
|
101
|
+
if (trustedToken)
|
|
102
|
+
headers["WCTrustedToken"] = trustedToken;
|
|
103
|
+
if (personalizationId)
|
|
104
|
+
headers["WCPersonalization"] = personalizationId;
|
|
105
|
+
return headers;
|
|
106
|
+
}
|
|
117
107
|
}
|
|
118
108
|
export {
|
|
109
|
+
HclCartNotFoundError,
|
|
119
110
|
HclClient
|
|
120
111
|
};
|
package/core/initialize.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
CategoryPaginatedResultSchema,
|
|
3
|
-
|
|
3
|
+
IdentitySchema,
|
|
4
|
+
OrderSchema,
|
|
5
|
+
OrderSearchResultSchema,
|
|
6
|
+
ProductSchema,
|
|
7
|
+
ProfileSchema
|
|
4
8
|
} from "@reactionary/core";
|
|
5
9
|
import { HclCategorySchema } from "../schema/category.schema.js";
|
|
6
10
|
import {
|
|
@@ -14,20 +18,46 @@ import {
|
|
|
14
18
|
} from "./initialize.types.js";
|
|
15
19
|
import { HclClient } from "./client.js";
|
|
16
20
|
import {
|
|
21
|
+
HclCartFactory,
|
|
17
22
|
HclCategoryFactory,
|
|
23
|
+
HclCheckoutFactory,
|
|
24
|
+
HclIdentityFactory,
|
|
25
|
+
HclInventoryFactory,
|
|
26
|
+
HclOrderFactory,
|
|
27
|
+
HclOrderSearchFactory,
|
|
28
|
+
HclPriceFactory,
|
|
18
29
|
HclProductFactory,
|
|
19
|
-
HclProductSearchFactory
|
|
30
|
+
HclProductSearchFactory,
|
|
31
|
+
HclProfileFactory
|
|
20
32
|
} from "../factories/index.js";
|
|
33
|
+
import { HclCartCapability } from "../capabilities/cart.capability.js";
|
|
34
|
+
import { HclCheckoutCapability } from "../capabilities/checkout.capability.js";
|
|
21
35
|
import { HclProductCapability } from "../capabilities/product.capability.js";
|
|
22
36
|
import { HclCategoryCapability } from "../capabilities/category.capability.js";
|
|
23
37
|
import { HclProductSearchCapability } from "../capabilities/product-search.capability.js";
|
|
24
|
-
import {
|
|
38
|
+
import { HclPriceCapability } from "../capabilities/price.capability.js";
|
|
39
|
+
import { HclInventoryCapability } from "../capabilities/inventory.capability.js";
|
|
40
|
+
import { HclIdentityCapability } from "../capabilities/identity.capability.js";
|
|
41
|
+
import { HclProfileCapability } from "../capabilities/profile.capability.js";
|
|
42
|
+
import { HclOrderCapability } from "../capabilities/order.capability.js";
|
|
43
|
+
import { HclOrderSearchCapability } from "../capabilities/order-search.capability.js";
|
|
44
|
+
import {
|
|
45
|
+
CartIdentifierSchema,
|
|
46
|
+
CartPaginatedSearchResultSchema,
|
|
47
|
+
CartSchema,
|
|
48
|
+
CheckoutSchema,
|
|
49
|
+
InventorySchema,
|
|
50
|
+
PaymentMethodSchema,
|
|
51
|
+
PriceSchema,
|
|
52
|
+
ProductSearchResultSchema,
|
|
53
|
+
ShippingMethodSchema
|
|
54
|
+
} from "@reactionary/core";
|
|
25
55
|
function withHclCapabilities(configuration, capabilities) {
|
|
26
56
|
return (cache, context) => {
|
|
27
57
|
const client = {};
|
|
28
58
|
const config = HclConfigurationSchema.parse(configuration);
|
|
29
59
|
const caps = HclCapabilitiesSchema.parse(capabilities);
|
|
30
|
-
const hclClient = new HclClient(config);
|
|
60
|
+
const hclClient = new HclClient(config, context);
|
|
31
61
|
const buildCapabilityArgs = (factory) => ({
|
|
32
62
|
cache,
|
|
33
63
|
context,
|
|
@@ -35,9 +65,48 @@ function withHclCapabilities(configuration, capabilities) {
|
|
|
35
65
|
hclClient,
|
|
36
66
|
factory
|
|
37
67
|
});
|
|
68
|
+
if (caps.cart?.enabled) {
|
|
69
|
+
client.cart = resolveCapabilityWithFactory(
|
|
70
|
+
capabilities.cart,
|
|
71
|
+
{
|
|
72
|
+
factory: new HclCartFactory(
|
|
73
|
+
CartSchema,
|
|
74
|
+
CartIdentifierSchema,
|
|
75
|
+
CartPaginatedSearchResultSchema
|
|
76
|
+
),
|
|
77
|
+
capability: (args) => new HclCartCapability(
|
|
78
|
+
args.cache,
|
|
79
|
+
args.context,
|
|
80
|
+
args.config,
|
|
81
|
+
args.hclClient,
|
|
82
|
+
args.factory
|
|
83
|
+
)
|
|
84
|
+
},
|
|
85
|
+
buildCapabilityArgs
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
if (caps.checkout?.enabled) {
|
|
89
|
+
client.checkout = resolveCapabilityWithFactory(
|
|
90
|
+
capabilities.checkout,
|
|
91
|
+
{
|
|
92
|
+
factory: new HclCheckoutFactory(
|
|
93
|
+
CheckoutSchema,
|
|
94
|
+
ShippingMethodSchema,
|
|
95
|
+
PaymentMethodSchema
|
|
96
|
+
),
|
|
97
|
+
capability: (args) => new HclCheckoutCapability(
|
|
98
|
+
args.cache,
|
|
99
|
+
args.context,
|
|
100
|
+
args.config,
|
|
101
|
+
args.hclClient,
|
|
102
|
+
args.factory
|
|
103
|
+
)
|
|
104
|
+
},
|
|
105
|
+
buildCapabilityArgs
|
|
106
|
+
);
|
|
107
|
+
}
|
|
38
108
|
if (caps.product?.enabled) {
|
|
39
109
|
client.product = resolveCapabilityWithFactory(
|
|
40
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
41
110
|
capabilities.product,
|
|
42
111
|
{
|
|
43
112
|
factory: new HclProductFactory(ProductSchema),
|
|
@@ -54,7 +123,6 @@ function withHclCapabilities(configuration, capabilities) {
|
|
|
54
123
|
}
|
|
55
124
|
if (caps.category?.enabled) {
|
|
56
125
|
client.category = resolveCapabilityWithFactory(
|
|
57
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
58
126
|
capabilities.category,
|
|
59
127
|
{
|
|
60
128
|
factory: new HclCategoryFactory(
|
|
@@ -74,7 +142,6 @@ function withHclCapabilities(configuration, capabilities) {
|
|
|
74
142
|
}
|
|
75
143
|
if (caps.productSearch?.enabled) {
|
|
76
144
|
client.productSearch = resolveCapabilityWithFactory(
|
|
77
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
78
145
|
capabilities.productSearch,
|
|
79
146
|
{
|
|
80
147
|
factory: new HclProductSearchFactory(ProductSearchResultSchema),
|
|
@@ -89,6 +156,102 @@ function withHclCapabilities(configuration, capabilities) {
|
|
|
89
156
|
buildCapabilityArgs
|
|
90
157
|
);
|
|
91
158
|
}
|
|
159
|
+
if (caps.price?.enabled) {
|
|
160
|
+
client.price = resolveCapabilityWithFactory(
|
|
161
|
+
capabilities.price,
|
|
162
|
+
{
|
|
163
|
+
factory: new HclPriceFactory(PriceSchema),
|
|
164
|
+
capability: (args) => new HclPriceCapability(
|
|
165
|
+
args.cache,
|
|
166
|
+
args.context,
|
|
167
|
+
args.config,
|
|
168
|
+
args.hclClient,
|
|
169
|
+
args.factory
|
|
170
|
+
)
|
|
171
|
+
},
|
|
172
|
+
buildCapabilityArgs
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
if (caps.inventory?.enabled) {
|
|
176
|
+
client.inventory = resolveCapabilityWithFactory(
|
|
177
|
+
capabilities.inventory,
|
|
178
|
+
{
|
|
179
|
+
factory: new HclInventoryFactory(InventorySchema),
|
|
180
|
+
capability: (args) => new HclInventoryCapability(
|
|
181
|
+
args.cache,
|
|
182
|
+
args.context,
|
|
183
|
+
args.config,
|
|
184
|
+
args.hclClient,
|
|
185
|
+
args.factory
|
|
186
|
+
)
|
|
187
|
+
},
|
|
188
|
+
buildCapabilityArgs
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
if (caps.identity?.enabled) {
|
|
192
|
+
client.identity = resolveCapabilityWithFactory(
|
|
193
|
+
capabilities.identity,
|
|
194
|
+
{
|
|
195
|
+
factory: new HclIdentityFactory(IdentitySchema),
|
|
196
|
+
capability: (args) => new HclIdentityCapability(
|
|
197
|
+
args.cache,
|
|
198
|
+
args.context,
|
|
199
|
+
args.config,
|
|
200
|
+
args.hclClient,
|
|
201
|
+
args.factory
|
|
202
|
+
)
|
|
203
|
+
},
|
|
204
|
+
buildCapabilityArgs
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
if (caps.profile?.enabled) {
|
|
208
|
+
client.profile = resolveCapabilityWithFactory(
|
|
209
|
+
capabilities.profile,
|
|
210
|
+
{
|
|
211
|
+
factory: new HclProfileFactory(ProfileSchema),
|
|
212
|
+
capability: (args) => new HclProfileCapability(
|
|
213
|
+
args.cache,
|
|
214
|
+
args.context,
|
|
215
|
+
args.config,
|
|
216
|
+
args.hclClient,
|
|
217
|
+
args.factory
|
|
218
|
+
)
|
|
219
|
+
},
|
|
220
|
+
buildCapabilityArgs
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
if (caps.order?.enabled) {
|
|
224
|
+
client.order = resolveCapabilityWithFactory(
|
|
225
|
+
capabilities.order,
|
|
226
|
+
{
|
|
227
|
+
factory: new HclOrderFactory(OrderSchema),
|
|
228
|
+
capability: (args) => new HclOrderCapability(
|
|
229
|
+
args.cache,
|
|
230
|
+
args.context,
|
|
231
|
+
args.config,
|
|
232
|
+
args.hclClient,
|
|
233
|
+
args.factory
|
|
234
|
+
)
|
|
235
|
+
},
|
|
236
|
+
buildCapabilityArgs
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
if (caps.orderSearch?.enabled) {
|
|
240
|
+
client.orderSearch = resolveCapabilityWithFactory(
|
|
241
|
+
capabilities.orderSearch,
|
|
242
|
+
{
|
|
243
|
+
factory: new HclOrderSearchFactory(OrderSearchResultSchema),
|
|
244
|
+
capability: (args) => new HclOrderSearchCapability(
|
|
245
|
+
args.cache,
|
|
246
|
+
args.context,
|
|
247
|
+
args.config,
|
|
248
|
+
args.hclClient,
|
|
249
|
+
args.factory
|
|
250
|
+
)
|
|
251
|
+
},
|
|
252
|
+
buildCapabilityArgs
|
|
253
|
+
);
|
|
254
|
+
}
|
|
92
255
|
return client;
|
|
93
256
|
};
|
|
94
257
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const SESSION_KEY_WC_TOKEN = "hcl.WCToken";
|
|
2
|
+
const SESSION_KEY_WC_TRUSTED_TOKEN = "hcl.WCTrustedToken";
|
|
3
|
+
const SESSION_KEY_USER_ID = "hcl.userId";
|
|
4
|
+
const SESSION_KEY_IDENTITY_TYPE = "hcl.identityType";
|
|
5
|
+
const SESSION_KEY_PERSONALIZATION_ID = "hcl.personalizationID";
|
|
6
|
+
const SESSION_KEY_PRICE_RULE_ID = "hcl.priceRuleId";
|
|
7
|
+
export {
|
|
8
|
+
SESSION_KEY_IDENTITY_TYPE,
|
|
9
|
+
SESSION_KEY_PERSONALIZATION_ID,
|
|
10
|
+
SESSION_KEY_PRICE_RULE_ID,
|
|
11
|
+
SESSION_KEY_USER_ID,
|
|
12
|
+
SESSION_KEY_WC_TOKEN,
|
|
13
|
+
SESSION_KEY_WC_TRUSTED_TOKEN
|
|
14
|
+
};
|