@reactionary/provider-commercetools 0.0.27
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/index.js +1107 -0
- package/package.json +10 -0
- package/src/core/client.d.ts +14 -0
- package/src/core/initialize.d.ts +4 -0
- package/src/index.d.ts +10 -0
- package/src/providers/cart.provider.d.ts +15 -0
- package/src/providers/identity.provider.d.ts +13 -0
- package/src/providers/inventory.provider.d.ts +12 -0
- package/src/providers/price.provider.d.ts +10 -0
- package/src/providers/product.provider.d.ts +11 -0
- package/src/providers/search.provider.d.ts +11 -0
- package/src/schema/capabilities.schema.d.ts +14 -0
- package/src/schema/configuration.schema.d.ts +13 -0
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# provider-commercetools
|
|
2
|
+
|
|
3
|
+
This library was generated with [Nx](https://nx.dev).
|
|
4
|
+
|
|
5
|
+
## Building
|
|
6
|
+
|
|
7
|
+
Run `nx build provider-commercetools` to build the library.
|
|
8
|
+
|
|
9
|
+
## Running unit tests
|
|
10
|
+
|
|
11
|
+
Run `nx test provider-commercetools` to execute the unit tests via [Jest](https://jestjs.io).
|
package/index.js
ADDED
|
@@ -0,0 +1,1107 @@
|
|
|
1
|
+
// providers/commercetools/src/core/client.ts
|
|
2
|
+
import { ClientBuilder } from "@commercetools/ts-client";
|
|
3
|
+
import { createApiBuilderFromCtpClient } from "@commercetools/platform-sdk";
|
|
4
|
+
var ANONYMOUS_SCOPES = ["view_published_products", "manage_shopping_lists", "view_shipping_methods", "manage_customers", "view_product_selections", "view_categories", "view_project_settings", "manage_order_edits", "view_sessions", "view_standalone_prices", "manage_orders", "view_tax_categories", "view_cart_discounts", "view_discount_codes", "create_anonymous_token", "manage_sessions", "view_products", "view_types"];
|
|
5
|
+
var GUEST_SCOPES = [...ANONYMOUS_SCOPES];
|
|
6
|
+
var REGISTERED_SCOPES = [...GUEST_SCOPES];
|
|
7
|
+
var CommercetoolsClient = class {
|
|
8
|
+
constructor(config) {
|
|
9
|
+
this.config = config;
|
|
10
|
+
}
|
|
11
|
+
getClient(token) {
|
|
12
|
+
if (token) {
|
|
13
|
+
return this.createClientWithToken(token);
|
|
14
|
+
}
|
|
15
|
+
return this.createAnonymousClient();
|
|
16
|
+
}
|
|
17
|
+
async login(username, password) {
|
|
18
|
+
const scopes = REGISTERED_SCOPES.map(
|
|
19
|
+
(scope) => `${scope}:${this.config.projectKey}`
|
|
20
|
+
).join(" ");
|
|
21
|
+
const queryParams = new URLSearchParams({
|
|
22
|
+
grant_type: "password",
|
|
23
|
+
username,
|
|
24
|
+
password,
|
|
25
|
+
scope: scopes
|
|
26
|
+
});
|
|
27
|
+
const url = `${this.config.authUrl}/oauth/${this.config.projectKey}/customers/token?${queryParams.toString()}`;
|
|
28
|
+
const headers = {
|
|
29
|
+
Authorization: "Basic " + btoa(this.config.clientId + ":" + this.config.clientSecret)
|
|
30
|
+
};
|
|
31
|
+
const remote = await fetch(url, { method: "POST", headers });
|
|
32
|
+
const json = await remote.json();
|
|
33
|
+
return json;
|
|
34
|
+
}
|
|
35
|
+
async guest() {
|
|
36
|
+
const scopes = GUEST_SCOPES.map(
|
|
37
|
+
(scope) => `${scope}:${this.config.projectKey}`
|
|
38
|
+
).join(" ");
|
|
39
|
+
const queryParams = new URLSearchParams({
|
|
40
|
+
grant_type: "client_credentials",
|
|
41
|
+
scope: scopes
|
|
42
|
+
});
|
|
43
|
+
const url = `${this.config.authUrl}/oauth/${this.config.projectKey}/anonymous/token?${queryParams.toString()}`;
|
|
44
|
+
const headers = {
|
|
45
|
+
Authorization: "Basic " + btoa(this.config.clientId + ":" + this.config.clientSecret)
|
|
46
|
+
};
|
|
47
|
+
const remote = await fetch(url, { method: "POST", headers });
|
|
48
|
+
const json = await remote.json();
|
|
49
|
+
return json;
|
|
50
|
+
}
|
|
51
|
+
async logout(token) {
|
|
52
|
+
const queryParams = new URLSearchParams({
|
|
53
|
+
token,
|
|
54
|
+
token_type_hint: "access_token"
|
|
55
|
+
});
|
|
56
|
+
const url = `${this.config.authUrl}/oauth/token/revoke?${queryParams.toString()}`;
|
|
57
|
+
const headers = {
|
|
58
|
+
Authorization: "Basic " + btoa(this.config.clientId + ":" + this.config.clientSecret)
|
|
59
|
+
};
|
|
60
|
+
const remote = await fetch(url, { method: "POST", headers });
|
|
61
|
+
return remote;
|
|
62
|
+
}
|
|
63
|
+
async introspect(token) {
|
|
64
|
+
const queryParams = new URLSearchParams({
|
|
65
|
+
token
|
|
66
|
+
});
|
|
67
|
+
const url = `${this.config.authUrl}/oauth/introspect?` + queryParams;
|
|
68
|
+
const headers = {
|
|
69
|
+
Authorization: "Basic " + btoa(this.config.clientId + ":" + this.config.clientSecret)
|
|
70
|
+
};
|
|
71
|
+
const remote = await fetch(url, { method: "POST", headers });
|
|
72
|
+
const json = await remote.json();
|
|
73
|
+
return json;
|
|
74
|
+
}
|
|
75
|
+
createAnonymousClient() {
|
|
76
|
+
const scopes = ANONYMOUS_SCOPES.map(
|
|
77
|
+
(scope) => `${scope}:${this.config.projectKey}`
|
|
78
|
+
).join(" ");
|
|
79
|
+
const builder = this.createBaseClientBuilder().withClientCredentialsFlow({
|
|
80
|
+
host: this.config.authUrl,
|
|
81
|
+
projectKey: this.config.projectKey,
|
|
82
|
+
credentials: {
|
|
83
|
+
clientId: this.config.clientId,
|
|
84
|
+
clientSecret: this.config.clientSecret
|
|
85
|
+
},
|
|
86
|
+
scopes: [scopes]
|
|
87
|
+
});
|
|
88
|
+
return createApiBuilderFromCtpClient(builder.build());
|
|
89
|
+
}
|
|
90
|
+
createClientWithToken(token) {
|
|
91
|
+
const builder = this.createBaseClientBuilder().withExistingTokenFlow(`Bearer ${token}`, { force: true });
|
|
92
|
+
return createApiBuilderFromCtpClient(builder.build());
|
|
93
|
+
}
|
|
94
|
+
createBaseClientBuilder() {
|
|
95
|
+
const builder = new ClientBuilder().withProjectKey(this.config.projectKey).withQueueMiddleware({
|
|
96
|
+
concurrency: 20
|
|
97
|
+
}).withHttpMiddleware({
|
|
98
|
+
retryConfig: {
|
|
99
|
+
backoff: true,
|
|
100
|
+
maxRetries: 3,
|
|
101
|
+
retryDelay: 500,
|
|
102
|
+
retryOnAbort: true,
|
|
103
|
+
retryCodes: [500, 429, 420],
|
|
104
|
+
maxDelay: 5e3
|
|
105
|
+
},
|
|
106
|
+
enableRetry: true,
|
|
107
|
+
includeResponseHeaders: true,
|
|
108
|
+
maskSensitiveHeaderData: false,
|
|
109
|
+
host: this.config.apiUrl,
|
|
110
|
+
httpClient: fetch
|
|
111
|
+
});
|
|
112
|
+
return builder;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// core/src/cache/caching-strategy.ts
|
|
117
|
+
var BaseCachingStrategy = class {
|
|
118
|
+
get(query, session) {
|
|
119
|
+
const q = query;
|
|
120
|
+
return {
|
|
121
|
+
key: q.sku,
|
|
122
|
+
cacheDurationInSeconds: 300,
|
|
123
|
+
canCache: true
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// core/src/cache/redis-cache.ts
|
|
129
|
+
import { Redis } from "@upstash/redis";
|
|
130
|
+
var RedisCache = class {
|
|
131
|
+
constructor(strategy) {
|
|
132
|
+
this.strategy = strategy;
|
|
133
|
+
this.redis = Redis.fromEnv();
|
|
134
|
+
}
|
|
135
|
+
async get(query, session, schema) {
|
|
136
|
+
let result = null;
|
|
137
|
+
const cacheInformation = this.strategy.get(query, session);
|
|
138
|
+
if (cacheInformation.canCache && cacheInformation.key) {
|
|
139
|
+
const unvalidated = await this.redis.get(cacheInformation.key);
|
|
140
|
+
const parsed = schema.safeParse(unvalidated);
|
|
141
|
+
if (parsed.success) {
|
|
142
|
+
result = parsed.data;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
147
|
+
put(query, session, value) {
|
|
148
|
+
const cacheInformation = this.strategy.get(query, session);
|
|
149
|
+
if (cacheInformation.canCache && cacheInformation.key) {
|
|
150
|
+
this.redis.set(cacheInformation.key, value, { ex: cacheInformation.cacheDurationInSeconds });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// core/src/providers/base.provider.ts
|
|
156
|
+
var BaseProvider = class {
|
|
157
|
+
constructor(schema, querySchema, mutationSchema) {
|
|
158
|
+
this.schema = schema;
|
|
159
|
+
this.querySchema = querySchema;
|
|
160
|
+
this.mutationSchema = mutationSchema;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Validates that the final domain model constructed by the provider
|
|
164
|
+
* fulfills the schema as defined. This will throw an exception.
|
|
165
|
+
*/
|
|
166
|
+
assert(value) {
|
|
167
|
+
return this.schema.parse(value);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Creates a new model entity based on the schema defaults.
|
|
171
|
+
*/
|
|
172
|
+
newModel() {
|
|
173
|
+
return this.schema.parse({});
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Retrieves a set of entities matching the list of queries. The size of
|
|
177
|
+
* the resulting list WILL always match the size of the query list. The
|
|
178
|
+
* result list will never contain nulls or undefined. The order
|
|
179
|
+
* of the results will match the order of the queries.
|
|
180
|
+
*/
|
|
181
|
+
async query(queries, session) {
|
|
182
|
+
const results = await this.fetch(queries, session);
|
|
183
|
+
for (const result of results) {
|
|
184
|
+
this.assert(result);
|
|
185
|
+
}
|
|
186
|
+
return results;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Executes the listed mutations in order and returns the final state
|
|
190
|
+
* resulting from that set of operations.
|
|
191
|
+
*/
|
|
192
|
+
async mutate(mutations, session) {
|
|
193
|
+
const result = await this.process(mutations, session);
|
|
194
|
+
this.assert(result);
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
// core/src/providers/cart.provider.ts
|
|
200
|
+
var CartProvider = class extends BaseProvider {
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
// core/src/providers/identity.provider.ts
|
|
204
|
+
var IdentityProvider = class extends BaseProvider {
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// core/src/providers/inventory.provider.ts
|
|
208
|
+
var InventoryProvider = class extends BaseProvider {
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// core/src/providers/price.provider.ts
|
|
212
|
+
var PriceProvider = class extends BaseProvider {
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
// core/src/providers/product.provider.ts
|
|
216
|
+
var ProductProvider = class extends BaseProvider {
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
// core/src/providers/search.provider.ts
|
|
220
|
+
var SearchProvider = class extends BaseProvider {
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
// core/src/schemas/capabilities.schema.ts
|
|
224
|
+
import { z } from "zod";
|
|
225
|
+
var CapabilitiesSchema = z.looseInterface({
|
|
226
|
+
product: z.boolean(),
|
|
227
|
+
search: z.boolean(),
|
|
228
|
+
analytics: z.boolean(),
|
|
229
|
+
identity: z.boolean(),
|
|
230
|
+
cart: z.boolean(),
|
|
231
|
+
inventory: z.boolean(),
|
|
232
|
+
price: z.boolean()
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
// core/src/schemas/session.schema.ts
|
|
236
|
+
import { z as z4 } from "zod";
|
|
237
|
+
|
|
238
|
+
// core/src/schemas/models/identity.model.ts
|
|
239
|
+
import { z as z3 } from "zod";
|
|
240
|
+
|
|
241
|
+
// core/src/schemas/models/base.model.ts
|
|
242
|
+
import { z as z2 } from "zod";
|
|
243
|
+
var CacheInformationSchema = z2.looseInterface({
|
|
244
|
+
hit: z2.boolean().default(false),
|
|
245
|
+
key: z2.string().default("")
|
|
246
|
+
});
|
|
247
|
+
var MetaSchema = z2.looseInterface({
|
|
248
|
+
cache: CacheInformationSchema.default(() => CacheInformationSchema.parse({})),
|
|
249
|
+
placeholder: z2.boolean().default(false).describe("Whether or not the entity exists in a remote system, or is a default placeholder.")
|
|
250
|
+
});
|
|
251
|
+
var BaseModelSchema = z2.looseInterface({
|
|
252
|
+
meta: MetaSchema.default(() => MetaSchema.parse({}))
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// core/src/schemas/models/identity.model.ts
|
|
256
|
+
var IdentityTypeSchema = z3.enum(["Anonymous", "Guest", "Registered"]);
|
|
257
|
+
var IdentitySchema = BaseModelSchema.extend({
|
|
258
|
+
id: z3.string().default(""),
|
|
259
|
+
type: IdentityTypeSchema.default("Anonymous"),
|
|
260
|
+
token: z3.string().optional(),
|
|
261
|
+
issued: z3.coerce.date().default(/* @__PURE__ */ new Date()),
|
|
262
|
+
expiry: z3.coerce.date().default(/* @__PURE__ */ new Date())
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
// core/src/schemas/session.schema.ts
|
|
266
|
+
var SessionSchema = z4.looseObject({
|
|
267
|
+
id: z4.string(),
|
|
268
|
+
identity: IdentitySchema.default(() => IdentitySchema.parse({}))
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
// core/src/schemas/models/cart.model.ts
|
|
272
|
+
import { z as z6 } from "zod";
|
|
273
|
+
|
|
274
|
+
// core/src/schemas/models/identifiers.model.ts
|
|
275
|
+
import { z as z5 } from "zod";
|
|
276
|
+
var FacetIdentifierSchema = z5.looseInterface({
|
|
277
|
+
key: z5.string().default("").nonoptional()
|
|
278
|
+
});
|
|
279
|
+
var FacetValueIdentifierSchema = z5.looseInterface({
|
|
280
|
+
facet: FacetIdentifierSchema.default(() => FacetIdentifierSchema.parse({})),
|
|
281
|
+
key: z5.string().default("")
|
|
282
|
+
});
|
|
283
|
+
var SKUIdentifierSchema = z5.looseInterface({
|
|
284
|
+
key: z5.string().default("").nonoptional()
|
|
285
|
+
});
|
|
286
|
+
var ProductIdentifierSchema = z5.looseInterface({
|
|
287
|
+
key: z5.string().default("")
|
|
288
|
+
});
|
|
289
|
+
var SearchIdentifierSchema = z5.looseInterface({
|
|
290
|
+
term: z5.string().default(""),
|
|
291
|
+
page: z5.number().default(0),
|
|
292
|
+
pageSize: z5.number().default(20),
|
|
293
|
+
facets: z5.array(FacetValueIdentifierSchema.required()).default(() => [])
|
|
294
|
+
});
|
|
295
|
+
var CartIdentifierSchema = z5.looseInterface({
|
|
296
|
+
key: z5.string().default("")
|
|
297
|
+
});
|
|
298
|
+
var CartItemIdentifierSchema = z5.looseInterface({
|
|
299
|
+
key: z5.string().default("")
|
|
300
|
+
});
|
|
301
|
+
var PriceIdentifierSchema = z5.looseInterface({
|
|
302
|
+
sku: SKUIdentifierSchema.default(() => SKUIdentifierSchema.parse({}))
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
// core/src/schemas/models/cart.model.ts
|
|
306
|
+
var CartItemSchema = z6.looseInterface({
|
|
307
|
+
identifier: CartItemIdentifierSchema.default(() => CartItemIdentifierSchema.parse({})),
|
|
308
|
+
product: ProductIdentifierSchema.default(() => ProductIdentifierSchema.parse({})),
|
|
309
|
+
quantity: z6.number().default(0)
|
|
310
|
+
});
|
|
311
|
+
var CartSchema = BaseModelSchema.extend({
|
|
312
|
+
identifier: CartIdentifierSchema.default(() => CartIdentifierSchema.parse({})),
|
|
313
|
+
items: z6.array(CartItemSchema).default(() => [])
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
// core/src/schemas/models/currency.model.ts
|
|
317
|
+
import { z as z7 } from "zod";
|
|
318
|
+
var CurrencySchema = z7.enum([
|
|
319
|
+
"AED",
|
|
320
|
+
"AFN",
|
|
321
|
+
"ALL",
|
|
322
|
+
"AMD",
|
|
323
|
+
"ANG",
|
|
324
|
+
"AOA",
|
|
325
|
+
"ARS",
|
|
326
|
+
"AUD",
|
|
327
|
+
"AWG",
|
|
328
|
+
"AZN",
|
|
329
|
+
"BAM",
|
|
330
|
+
"BBD",
|
|
331
|
+
"BDT",
|
|
332
|
+
"BGN",
|
|
333
|
+
"BHD",
|
|
334
|
+
"BIF",
|
|
335
|
+
"BMD",
|
|
336
|
+
"BND",
|
|
337
|
+
"BOB",
|
|
338
|
+
"BOV",
|
|
339
|
+
"BRL",
|
|
340
|
+
"BSD",
|
|
341
|
+
"BTN",
|
|
342
|
+
"BWP",
|
|
343
|
+
"BYN",
|
|
344
|
+
"BZD",
|
|
345
|
+
"CAD",
|
|
346
|
+
"CDF",
|
|
347
|
+
"CHE",
|
|
348
|
+
"CHF",
|
|
349
|
+
"CHW",
|
|
350
|
+
"CLF",
|
|
351
|
+
"CLP",
|
|
352
|
+
"CNY",
|
|
353
|
+
"COP",
|
|
354
|
+
"COU",
|
|
355
|
+
"CRC",
|
|
356
|
+
"CUC",
|
|
357
|
+
"CUP",
|
|
358
|
+
"CVE",
|
|
359
|
+
"CZK",
|
|
360
|
+
"DJF",
|
|
361
|
+
"DKK",
|
|
362
|
+
"DOP",
|
|
363
|
+
"DZD",
|
|
364
|
+
"EGP",
|
|
365
|
+
"ERN",
|
|
366
|
+
"ETB",
|
|
367
|
+
"EUR",
|
|
368
|
+
"FJD",
|
|
369
|
+
"FKP",
|
|
370
|
+
"GBP",
|
|
371
|
+
"GEL",
|
|
372
|
+
"GHS",
|
|
373
|
+
"GIP",
|
|
374
|
+
"GMD",
|
|
375
|
+
"GNF",
|
|
376
|
+
"GTQ",
|
|
377
|
+
"GYD",
|
|
378
|
+
"HKD",
|
|
379
|
+
"HNL",
|
|
380
|
+
"HRK",
|
|
381
|
+
"HTG",
|
|
382
|
+
"HUF",
|
|
383
|
+
"IDR",
|
|
384
|
+
"ILS",
|
|
385
|
+
"INR",
|
|
386
|
+
"IQD",
|
|
387
|
+
"IRR",
|
|
388
|
+
"ISK",
|
|
389
|
+
"JMD",
|
|
390
|
+
"JOD",
|
|
391
|
+
"JPY",
|
|
392
|
+
"KES",
|
|
393
|
+
"KGS",
|
|
394
|
+
"KHR",
|
|
395
|
+
"KMF",
|
|
396
|
+
"KPW",
|
|
397
|
+
"KRW",
|
|
398
|
+
"KWD",
|
|
399
|
+
"KYD",
|
|
400
|
+
"KZT",
|
|
401
|
+
"LAK",
|
|
402
|
+
"LBP",
|
|
403
|
+
"LKR",
|
|
404
|
+
"LRD",
|
|
405
|
+
"LSL",
|
|
406
|
+
"LYD",
|
|
407
|
+
"MAD",
|
|
408
|
+
"MDL",
|
|
409
|
+
"MGA",
|
|
410
|
+
"MKD",
|
|
411
|
+
"MMK",
|
|
412
|
+
"MNT",
|
|
413
|
+
"MOP",
|
|
414
|
+
"MRU",
|
|
415
|
+
"MUR",
|
|
416
|
+
"MVR",
|
|
417
|
+
"MWK",
|
|
418
|
+
"MXN",
|
|
419
|
+
"MXV",
|
|
420
|
+
"MYR",
|
|
421
|
+
"MZN",
|
|
422
|
+
"NAD",
|
|
423
|
+
"NGN",
|
|
424
|
+
"NIO",
|
|
425
|
+
"NOK",
|
|
426
|
+
"NPR",
|
|
427
|
+
"NZD",
|
|
428
|
+
"OMR",
|
|
429
|
+
"PAB",
|
|
430
|
+
"PEN",
|
|
431
|
+
"PGK",
|
|
432
|
+
"PHP",
|
|
433
|
+
"PKR",
|
|
434
|
+
"PLN",
|
|
435
|
+
"PYG",
|
|
436
|
+
"QAR",
|
|
437
|
+
"RON",
|
|
438
|
+
"RSD",
|
|
439
|
+
"RUB",
|
|
440
|
+
"RWF",
|
|
441
|
+
"SAR",
|
|
442
|
+
"SBD",
|
|
443
|
+
"SCR",
|
|
444
|
+
"SDG",
|
|
445
|
+
"SEK",
|
|
446
|
+
"SGD",
|
|
447
|
+
"SHP",
|
|
448
|
+
"SLE",
|
|
449
|
+
"SLL",
|
|
450
|
+
"SOS",
|
|
451
|
+
"SRD",
|
|
452
|
+
"SSP",
|
|
453
|
+
"STN",
|
|
454
|
+
"SYP",
|
|
455
|
+
"SZL",
|
|
456
|
+
"THB",
|
|
457
|
+
"TJS",
|
|
458
|
+
"TMT",
|
|
459
|
+
"TND",
|
|
460
|
+
"TOP",
|
|
461
|
+
"TRY",
|
|
462
|
+
"TTD",
|
|
463
|
+
"TVD",
|
|
464
|
+
"TWD",
|
|
465
|
+
"TZS",
|
|
466
|
+
"UAH",
|
|
467
|
+
"UGX",
|
|
468
|
+
"USD",
|
|
469
|
+
"USN",
|
|
470
|
+
"UYI",
|
|
471
|
+
"UYU",
|
|
472
|
+
"UYW",
|
|
473
|
+
"UZS",
|
|
474
|
+
"VED",
|
|
475
|
+
"VES",
|
|
476
|
+
"VND",
|
|
477
|
+
"VUV",
|
|
478
|
+
"WST",
|
|
479
|
+
"XAF",
|
|
480
|
+
"XAG",
|
|
481
|
+
"XAU",
|
|
482
|
+
"XBA",
|
|
483
|
+
"XBB",
|
|
484
|
+
"XBC",
|
|
485
|
+
"XBD",
|
|
486
|
+
"XCD",
|
|
487
|
+
"XDR",
|
|
488
|
+
"XOF",
|
|
489
|
+
"XPD",
|
|
490
|
+
"XPF",
|
|
491
|
+
"XPT",
|
|
492
|
+
"XSU",
|
|
493
|
+
"XTS",
|
|
494
|
+
"XUA",
|
|
495
|
+
"XXX",
|
|
496
|
+
"YER",
|
|
497
|
+
"ZAR",
|
|
498
|
+
"ZMW",
|
|
499
|
+
"ZWL"
|
|
500
|
+
]);
|
|
501
|
+
|
|
502
|
+
// core/src/schemas/models/inventory.model.ts
|
|
503
|
+
import { z as z8 } from "zod";
|
|
504
|
+
var InventorySchema = BaseModelSchema.extend({
|
|
505
|
+
quantity: z8.number().default(0)
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
// core/src/schemas/models/price.model.ts
|
|
509
|
+
import { z as z9 } from "zod";
|
|
510
|
+
var MonetaryAmountSchema = z9.looseInterface({
|
|
511
|
+
cents: z9.number().default(0).describe("The monetary amount in cent-precision."),
|
|
512
|
+
currency: CurrencySchema.default("XXX").describe("The currency associated with the amount, as a ISO 4217 standardized code.")
|
|
513
|
+
});
|
|
514
|
+
var PriceSchema = BaseModelSchema.extend({
|
|
515
|
+
identifier: PriceIdentifierSchema.default(() => PriceIdentifierSchema.parse({})),
|
|
516
|
+
value: MonetaryAmountSchema.default(() => MonetaryAmountSchema.parse({}))
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
// core/src/schemas/models/product.model.ts
|
|
520
|
+
import { z as z10 } from "zod";
|
|
521
|
+
var SKUSchema = z10.looseInterface({
|
|
522
|
+
identifier: ProductIdentifierSchema.default(() => ProductIdentifierSchema.parse({}))
|
|
523
|
+
});
|
|
524
|
+
var ProductAttributeSchema = z10.looseInterface({
|
|
525
|
+
id: z10.string(),
|
|
526
|
+
name: z10.string(),
|
|
527
|
+
value: z10.string()
|
|
528
|
+
});
|
|
529
|
+
var ProductSchema = BaseModelSchema.extend({
|
|
530
|
+
identifier: ProductIdentifierSchema.default(() => ProductIdentifierSchema.parse({})),
|
|
531
|
+
name: z10.string().default(""),
|
|
532
|
+
slug: z10.string().default(""),
|
|
533
|
+
description: z10.string().default(""),
|
|
534
|
+
image: z10.string().url().default("https://placehold.co/400"),
|
|
535
|
+
images: z10.string().url().array().default(() => []),
|
|
536
|
+
attributes: z10.array(ProductAttributeSchema).default(() => []),
|
|
537
|
+
skus: z10.array(SKUSchema).default(() => [])
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
// core/src/schemas/models/search.model.ts
|
|
541
|
+
import { z as z11 } from "zod";
|
|
542
|
+
var SearchResultProductSchema = z11.looseInterface({
|
|
543
|
+
identifier: ProductIdentifierSchema.default(ProductIdentifierSchema.parse({})),
|
|
544
|
+
name: z11.string().default(""),
|
|
545
|
+
image: z11.string().url().default("https://placehold.co/400"),
|
|
546
|
+
slug: z11.string().default("")
|
|
547
|
+
});
|
|
548
|
+
var SearchResultFacetValueSchema = z11.looseInterface({
|
|
549
|
+
identifier: FacetValueIdentifierSchema.default(() => FacetValueIdentifierSchema.parse({})),
|
|
550
|
+
name: z11.string().default(""),
|
|
551
|
+
count: z11.number().default(0),
|
|
552
|
+
active: z11.boolean().default(false)
|
|
553
|
+
});
|
|
554
|
+
var SearchResultFacetSchema = z11.looseInterface({
|
|
555
|
+
identifier: FacetIdentifierSchema.default(() => FacetIdentifierSchema.parse({})),
|
|
556
|
+
name: z11.string().default(""),
|
|
557
|
+
values: z11.array(SearchResultFacetValueSchema).default(() => [])
|
|
558
|
+
});
|
|
559
|
+
var SearchResultSchema = BaseModelSchema.extend({
|
|
560
|
+
identifier: SearchIdentifierSchema.default(() => SearchIdentifierSchema.parse({})),
|
|
561
|
+
products: z11.array(SearchResultProductSchema).default(() => []),
|
|
562
|
+
pages: z11.number().default(0),
|
|
563
|
+
facets: z11.array(SearchResultFacetSchema).default(() => [])
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
// core/src/schemas/mutations/base.mutation.ts
|
|
567
|
+
import { z as z12 } from "zod";
|
|
568
|
+
var BaseMutationSchema = z12.looseInterface({
|
|
569
|
+
mutation: z12.ZodLiteral
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
// core/src/schemas/mutations/cart.mutation.ts
|
|
573
|
+
import { z as z13 } from "zod";
|
|
574
|
+
var CartMutationItemAddSchema = BaseMutationSchema.extend({
|
|
575
|
+
mutation: z13.literal("add"),
|
|
576
|
+
cart: CartIdentifierSchema.required(),
|
|
577
|
+
product: ProductIdentifierSchema.required(),
|
|
578
|
+
quantity: z13.number()
|
|
579
|
+
});
|
|
580
|
+
var CartMutationItemRemoveSchema = BaseMutationSchema.extend({
|
|
581
|
+
mutation: z13.literal("remove"),
|
|
582
|
+
cart: CartIdentifierSchema.required(),
|
|
583
|
+
item: CartItemIdentifierSchema.required()
|
|
584
|
+
});
|
|
585
|
+
var CartMutationItemQuantityChangeSchema = BaseMutationSchema.extend({
|
|
586
|
+
mutation: z13.literal("adjustQuantity"),
|
|
587
|
+
cart: CartIdentifierSchema.required(),
|
|
588
|
+
item: CartItemIdentifierSchema.required(),
|
|
589
|
+
quantity: z13.number()
|
|
590
|
+
});
|
|
591
|
+
var CartMutationSchema = z13.union([CartMutationItemAddSchema, CartMutationItemRemoveSchema, CartMutationItemQuantityChangeSchema]);
|
|
592
|
+
|
|
593
|
+
// core/src/schemas/mutations/identity.mutation.ts
|
|
594
|
+
import { z as z14 } from "zod";
|
|
595
|
+
var IdentityMutationLoginSchema = BaseMutationSchema.extend({
|
|
596
|
+
mutation: z14.literal("login"),
|
|
597
|
+
username: z14.string(),
|
|
598
|
+
password: z14.string()
|
|
599
|
+
});
|
|
600
|
+
var IdentityMutationLogoutSchema = BaseMutationSchema.extend({
|
|
601
|
+
mutation: z14.literal("logout")
|
|
602
|
+
});
|
|
603
|
+
var IdentityMutationSchema = z14.union([IdentityMutationLoginSchema, IdentityMutationLogoutSchema]);
|
|
604
|
+
|
|
605
|
+
// core/src/schemas/mutations/inventory.mutation.ts
|
|
606
|
+
import { z as z15 } from "zod";
|
|
607
|
+
var InventoryMutationSchema = z15.union([]);
|
|
608
|
+
|
|
609
|
+
// core/src/schemas/mutations/price.mutation.ts
|
|
610
|
+
import { z as z16 } from "zod";
|
|
611
|
+
var PriceMutationSchema = z16.union([]);
|
|
612
|
+
|
|
613
|
+
// core/src/schemas/mutations/product.mutation.ts
|
|
614
|
+
import { z as z17 } from "zod";
|
|
615
|
+
var ProductMutationSchema = z17.union([]);
|
|
616
|
+
|
|
617
|
+
// core/src/schemas/mutations/search.mutation.ts
|
|
618
|
+
import { z as z18 } from "zod";
|
|
619
|
+
var SearchMutationSchema = z18.union([]);
|
|
620
|
+
|
|
621
|
+
// core/src/schemas/queries/base.query.ts
|
|
622
|
+
import { z as z19 } from "zod";
|
|
623
|
+
var BaseQuerySchema = z19.looseInterface({
|
|
624
|
+
query: z19.ZodLiteral
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
// core/src/schemas/queries/cart.query.ts
|
|
628
|
+
import { z as z20 } from "zod";
|
|
629
|
+
var CartQueryByIdSchema = BaseQuerySchema.extend({
|
|
630
|
+
query: z20.literal("id"),
|
|
631
|
+
cart: CartIdentifierSchema.required()
|
|
632
|
+
});
|
|
633
|
+
var CartQuerySchema = z20.union([CartQueryByIdSchema]);
|
|
634
|
+
|
|
635
|
+
// core/src/schemas/queries/identity.query.ts
|
|
636
|
+
import { z as z21 } from "zod";
|
|
637
|
+
var IdentityQuerySelfSchema = BaseQuerySchema.extend({
|
|
638
|
+
query: z21.literal("self")
|
|
639
|
+
});
|
|
640
|
+
var IdentityQuerySchema = z21.union([IdentityQuerySelfSchema]);
|
|
641
|
+
|
|
642
|
+
// core/src/schemas/queries/inventory.query.ts
|
|
643
|
+
import { z as z22 } from "zod";
|
|
644
|
+
var InventoryQuerySchema = BaseQuerySchema.extend({
|
|
645
|
+
query: z22.literal("sku"),
|
|
646
|
+
sku: z22.string()
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
// core/src/schemas/queries/price.query.ts
|
|
650
|
+
import { z as z23 } from "zod";
|
|
651
|
+
var PriceQueryBySkuSchema = BaseQuerySchema.extend({
|
|
652
|
+
query: z23.literal("sku"),
|
|
653
|
+
sku: SKUIdentifierSchema.required()
|
|
654
|
+
});
|
|
655
|
+
var PriceQuerySchema = z23.union([PriceQueryBySkuSchema]);
|
|
656
|
+
|
|
657
|
+
// core/src/schemas/queries/product.query.ts
|
|
658
|
+
import { z as z24 } from "zod";
|
|
659
|
+
var ProductQueryBySlugSchema = BaseQuerySchema.extend({
|
|
660
|
+
query: z24.literal("slug"),
|
|
661
|
+
slug: z24.string()
|
|
662
|
+
});
|
|
663
|
+
var ProductQueryByIdSchema = BaseQuerySchema.extend({
|
|
664
|
+
query: z24.literal("id"),
|
|
665
|
+
id: z24.string()
|
|
666
|
+
});
|
|
667
|
+
var ProductQuerySchema = z24.union([ProductQueryBySlugSchema, ProductQueryByIdSchema]);
|
|
668
|
+
|
|
669
|
+
// core/src/schemas/queries/search.query.ts
|
|
670
|
+
import { z as z25 } from "zod";
|
|
671
|
+
var SearchQueryByTermSchema = BaseQuerySchema.extend({
|
|
672
|
+
query: z25.literal("term"),
|
|
673
|
+
search: SearchIdentifierSchema.required()
|
|
674
|
+
});
|
|
675
|
+
var SearchQuerySchema = z25.union([SearchQueryByTermSchema]);
|
|
676
|
+
|
|
677
|
+
// providers/commercetools/src/providers/search.provider.ts
|
|
678
|
+
var CommercetoolsSearchProvider = class extends SearchProvider {
|
|
679
|
+
constructor(config, schema, querySchema, mutationSchema) {
|
|
680
|
+
super(schema, querySchema, mutationSchema);
|
|
681
|
+
this.config = config;
|
|
682
|
+
}
|
|
683
|
+
async fetch(queries, session) {
|
|
684
|
+
const results = [];
|
|
685
|
+
for (const query of queries) {
|
|
686
|
+
const result = await this.get(query.search);
|
|
687
|
+
results.push(result);
|
|
688
|
+
}
|
|
689
|
+
return results;
|
|
690
|
+
}
|
|
691
|
+
process(mutations, session) {
|
|
692
|
+
throw new Error("Method not implemented.");
|
|
693
|
+
}
|
|
694
|
+
async get(identifier) {
|
|
695
|
+
const client = new CommercetoolsClient(this.config).createAnonymousClient();
|
|
696
|
+
const remote = await client.withProjectKey({ projectKey: this.config.projectKey }).productProjections().search().get({
|
|
697
|
+
queryArgs: {
|
|
698
|
+
limit: identifier.pageSize,
|
|
699
|
+
offset: identifier.pageSize * identifier.page,
|
|
700
|
+
["text.en-US"]: identifier.term
|
|
701
|
+
}
|
|
702
|
+
}).execute();
|
|
703
|
+
const parsed = this.parse(remote, identifier);
|
|
704
|
+
return parsed;
|
|
705
|
+
}
|
|
706
|
+
parse(remote, query) {
|
|
707
|
+
const result = super.newModel();
|
|
708
|
+
result.identifier = query;
|
|
709
|
+
for (const p of remote.body.results) {
|
|
710
|
+
const product = SearchResultProductSchema.parse({});
|
|
711
|
+
product.identifier.key = p.id;
|
|
712
|
+
product.name = p.name["en-US"];
|
|
713
|
+
if (p.masterVariant.images) {
|
|
714
|
+
product.image = p.masterVariant.images[0].url;
|
|
715
|
+
}
|
|
716
|
+
result.products.push(product);
|
|
717
|
+
}
|
|
718
|
+
result.pages = Math.ceil((remote.body.total || 0) / query.pageSize);
|
|
719
|
+
return result;
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
|
|
723
|
+
// providers/commercetools/src/providers/product.provider.ts
|
|
724
|
+
var CommercetoolsProductProvider = class extends ProductProvider {
|
|
725
|
+
constructor(config, schema, querySchema, mutationSchema) {
|
|
726
|
+
super(schema, querySchema, mutationSchema);
|
|
727
|
+
this.config = config;
|
|
728
|
+
}
|
|
729
|
+
async fetch(queries, session) {
|
|
730
|
+
const ids = queries.filter((x) => x.query === "id").map((x) => x.id);
|
|
731
|
+
const slugs = queries.filter((x) => x.query === "slug").map((x) => x.slug);
|
|
732
|
+
const client = new CommercetoolsClient(this.config).createAnonymousClient();
|
|
733
|
+
const remote = await client.withProjectKey({ projectKey: this.config.projectKey }).productProjections().get({
|
|
734
|
+
queryArgs: {
|
|
735
|
+
where: "slug(en-US in :slugs)",
|
|
736
|
+
"var.slugs": slugs
|
|
737
|
+
}
|
|
738
|
+
}).execute();
|
|
739
|
+
console.log("remote: ", remote);
|
|
740
|
+
const results = new Array();
|
|
741
|
+
for (const r of remote.body.results) {
|
|
742
|
+
const result = this.parse(r);
|
|
743
|
+
results.push(result);
|
|
744
|
+
}
|
|
745
|
+
return results;
|
|
746
|
+
}
|
|
747
|
+
process(mutation, session) {
|
|
748
|
+
throw new Error("Method not implemented.");
|
|
749
|
+
}
|
|
750
|
+
parse(data) {
|
|
751
|
+
const base = this.newModel();
|
|
752
|
+
base.identifier.key = data.id;
|
|
753
|
+
base.name = data.name["en-US"];
|
|
754
|
+
base.slug = data.slug["en-US"];
|
|
755
|
+
if (data.description) {
|
|
756
|
+
base.description = data.description["en-US"];
|
|
757
|
+
}
|
|
758
|
+
if (data.masterVariant.images) {
|
|
759
|
+
base.image = data.masterVariant.images[0].url;
|
|
760
|
+
}
|
|
761
|
+
const variants = [data.masterVariant, ...data.variants];
|
|
762
|
+
for (const variant of variants) {
|
|
763
|
+
base.skus.push({
|
|
764
|
+
identifier: {
|
|
765
|
+
key: variant.sku || ""
|
|
766
|
+
}
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
return base;
|
|
770
|
+
}
|
|
771
|
+
};
|
|
772
|
+
|
|
773
|
+
// providers/commercetools/src/providers/identity.provider.ts
|
|
774
|
+
var CommercetoolsIdentityProvider = class extends IdentityProvider {
|
|
775
|
+
constructor(config, schema, querySchema, mutationSchema) {
|
|
776
|
+
super(schema, querySchema, mutationSchema);
|
|
777
|
+
this.config = config;
|
|
778
|
+
}
|
|
779
|
+
async fetch(queries, session) {
|
|
780
|
+
const results = [];
|
|
781
|
+
for (const query of queries) {
|
|
782
|
+
const result = await this.get(session);
|
|
783
|
+
results.push(result);
|
|
784
|
+
}
|
|
785
|
+
return results;
|
|
786
|
+
}
|
|
787
|
+
async process(mutations, session) {
|
|
788
|
+
let result = this.newModel();
|
|
789
|
+
for (const mutation of mutations) {
|
|
790
|
+
switch (mutation.mutation) {
|
|
791
|
+
case "login":
|
|
792
|
+
result = await this.login(mutation, session);
|
|
793
|
+
break;
|
|
794
|
+
case "logout":
|
|
795
|
+
result = await this.logout(session);
|
|
796
|
+
break;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
return result;
|
|
800
|
+
}
|
|
801
|
+
async get(session) {
|
|
802
|
+
const client = new CommercetoolsClient(this.config);
|
|
803
|
+
const base = this.newModel();
|
|
804
|
+
if (session.identity.token) {
|
|
805
|
+
const remote = await client.introspect(session.identity.token);
|
|
806
|
+
if (remote.active) {
|
|
807
|
+
const current = this.schema.safeParse(session.identity);
|
|
808
|
+
if (current.success) {
|
|
809
|
+
return current.data;
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
session.identity = base;
|
|
814
|
+
return base;
|
|
815
|
+
}
|
|
816
|
+
async login(payload, session) {
|
|
817
|
+
const client = new CommercetoolsClient(this.config);
|
|
818
|
+
const remote = await client.login(payload.username, payload.password);
|
|
819
|
+
const base = this.newModel();
|
|
820
|
+
if (remote && remote.access_token) {
|
|
821
|
+
base.issued = /* @__PURE__ */ new Date();
|
|
822
|
+
base.expiry = /* @__PURE__ */ new Date();
|
|
823
|
+
base.expiry.setSeconds(base.expiry.getSeconds() + remote.expires_in);
|
|
824
|
+
base.id = this.extractCustomerIdFromScopes(remote.scope);
|
|
825
|
+
base.token = remote.access_token;
|
|
826
|
+
base.type = "Registered";
|
|
827
|
+
}
|
|
828
|
+
session.identity = base;
|
|
829
|
+
return base;
|
|
830
|
+
}
|
|
831
|
+
async logout(session) {
|
|
832
|
+
const client = new CommercetoolsClient(this.config);
|
|
833
|
+
const base = this.newModel();
|
|
834
|
+
if (session.identity.token) {
|
|
835
|
+
const remote = await client.logout(session.identity.token);
|
|
836
|
+
}
|
|
837
|
+
session.identity = base;
|
|
838
|
+
return base;
|
|
839
|
+
}
|
|
840
|
+
extractCustomerIdFromScopes(scopes) {
|
|
841
|
+
const scopeList = scopes.split(" ");
|
|
842
|
+
const customerScope = scopeList.find((x) => x.startsWith("customer_id"));
|
|
843
|
+
const id = customerScope?.split(":")[1];
|
|
844
|
+
return id || "";
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
|
|
848
|
+
// providers/commercetools/src/providers/cart.provider.ts
|
|
849
|
+
var CommercetoolsCartProvider = class extends CartProvider {
|
|
850
|
+
constructor(config, schema, querySchema, mutationSchema) {
|
|
851
|
+
super(schema, querySchema, mutationSchema);
|
|
852
|
+
this.config = config;
|
|
853
|
+
}
|
|
854
|
+
async fetch(queries, session) {
|
|
855
|
+
const results = [];
|
|
856
|
+
for (const query of queries) {
|
|
857
|
+
if (query.cart.key) {
|
|
858
|
+
const client = this.getClient(session);
|
|
859
|
+
const remote = await client.withId({ ID: query.cart.key }).get().execute();
|
|
860
|
+
const result = this.composeCart(remote.body);
|
|
861
|
+
results.push(result);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
return results;
|
|
865
|
+
}
|
|
866
|
+
async process(mutations, session) {
|
|
867
|
+
let cart = this.newModel();
|
|
868
|
+
for (const mutation of mutations) {
|
|
869
|
+
switch (mutation.mutation) {
|
|
870
|
+
case "add":
|
|
871
|
+
cart = await this.add(mutation, session);
|
|
872
|
+
break;
|
|
873
|
+
case "adjustQuantity":
|
|
874
|
+
cart = await this.adjust(mutation, session);
|
|
875
|
+
break;
|
|
876
|
+
case "remove":
|
|
877
|
+
cart = await this.remove(mutation, session);
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
return cart;
|
|
881
|
+
}
|
|
882
|
+
async adjust(payload, session) {
|
|
883
|
+
const client = this.getClient(session);
|
|
884
|
+
const existing = await client.withId({ ID: payload.cart.key }).get().execute();
|
|
885
|
+
const remote = await client.withId({ ID: payload.cart.key }).post({
|
|
886
|
+
body: {
|
|
887
|
+
version: existing.body.version,
|
|
888
|
+
actions: [
|
|
889
|
+
{
|
|
890
|
+
action: "changeLineItemQuantity",
|
|
891
|
+
lineItemId: payload.item.key,
|
|
892
|
+
quantity: payload.quantity
|
|
893
|
+
}
|
|
894
|
+
]
|
|
895
|
+
}
|
|
896
|
+
}).execute();
|
|
897
|
+
const result = this.composeCart(remote.body);
|
|
898
|
+
return result;
|
|
899
|
+
}
|
|
900
|
+
async add(payload, session) {
|
|
901
|
+
const client = this.getClient(session);
|
|
902
|
+
let id = payload.cart.key;
|
|
903
|
+
let version = 0;
|
|
904
|
+
if (!id) {
|
|
905
|
+
const remoteCart = await client.post({
|
|
906
|
+
body: {
|
|
907
|
+
currency: "USD",
|
|
908
|
+
country: "US"
|
|
909
|
+
}
|
|
910
|
+
}).execute();
|
|
911
|
+
id = remoteCart.body.id;
|
|
912
|
+
version = remoteCart.body.version;
|
|
913
|
+
} else {
|
|
914
|
+
const existing = await client.withId({ ID: payload.cart.key }).get().execute();
|
|
915
|
+
version = existing.body.version;
|
|
916
|
+
}
|
|
917
|
+
const remoteAdd = await client.withId({ ID: id }).post({
|
|
918
|
+
body: {
|
|
919
|
+
version,
|
|
920
|
+
actions: [
|
|
921
|
+
{
|
|
922
|
+
action: "addLineItem",
|
|
923
|
+
quantity: payload.quantity,
|
|
924
|
+
productId: payload.product.key
|
|
925
|
+
}
|
|
926
|
+
]
|
|
927
|
+
}
|
|
928
|
+
}).execute();
|
|
929
|
+
const result = this.composeCart(remoteAdd.body);
|
|
930
|
+
return result;
|
|
931
|
+
}
|
|
932
|
+
async remove(payload, session) {
|
|
933
|
+
const client = this.getClient(session);
|
|
934
|
+
const existing = await client.withId({ ID: payload.cart.key }).get().execute();
|
|
935
|
+
const remote = await client.withId({ ID: payload.cart.key }).post({
|
|
936
|
+
body: {
|
|
937
|
+
version: existing.body.version,
|
|
938
|
+
actions: [
|
|
939
|
+
{
|
|
940
|
+
action: "removeLineItem",
|
|
941
|
+
lineItemId: payload.item.key
|
|
942
|
+
}
|
|
943
|
+
]
|
|
944
|
+
}
|
|
945
|
+
}).execute();
|
|
946
|
+
const result = this.composeCart(remote.body);
|
|
947
|
+
return result;
|
|
948
|
+
}
|
|
949
|
+
getClient(session) {
|
|
950
|
+
const client = new CommercetoolsClient(this.config).getClient(
|
|
951
|
+
session.identity.token
|
|
952
|
+
);
|
|
953
|
+
const cartClient = client.withProjectKey({ projectKey: this.config.projectKey }).carts();
|
|
954
|
+
return cartClient;
|
|
955
|
+
}
|
|
956
|
+
composeCart(remote) {
|
|
957
|
+
const result = this.newModel();
|
|
958
|
+
result.identifier.key = remote.id;
|
|
959
|
+
for (const remoteItem of remote.lineItems) {
|
|
960
|
+
const item = CartItemSchema.parse({});
|
|
961
|
+
item.identifier.key = remoteItem.id;
|
|
962
|
+
item.product.key = remoteItem.productId;
|
|
963
|
+
item.quantity = remoteItem.quantity;
|
|
964
|
+
result.items.push(item);
|
|
965
|
+
}
|
|
966
|
+
return result;
|
|
967
|
+
}
|
|
968
|
+
};
|
|
969
|
+
|
|
970
|
+
// providers/commercetools/src/providers/inventory.provider.ts
|
|
971
|
+
var CommercetoolsInventoryProvider = class extends InventoryProvider {
|
|
972
|
+
constructor(config, schema, querySchema, mutationSchema) {
|
|
973
|
+
super(schema, querySchema, mutationSchema);
|
|
974
|
+
this.cache = new RedisCache(new BaseCachingStrategy());
|
|
975
|
+
this.config = config;
|
|
976
|
+
}
|
|
977
|
+
async fetch(queries, session) {
|
|
978
|
+
const results = [];
|
|
979
|
+
for (const query of queries) {
|
|
980
|
+
let result = await this.cache.get(query, session, this.schema);
|
|
981
|
+
console.log("from cache: ", result);
|
|
982
|
+
if (!result) {
|
|
983
|
+
result = await this.get(query, session);
|
|
984
|
+
this.cache.put(query, session, result);
|
|
985
|
+
}
|
|
986
|
+
results.push(result);
|
|
987
|
+
}
|
|
988
|
+
return results;
|
|
989
|
+
}
|
|
990
|
+
process(mutations, session) {
|
|
991
|
+
throw new Error("Method not implemented.");
|
|
992
|
+
}
|
|
993
|
+
async get(query, session) {
|
|
994
|
+
const client = new CommercetoolsClient(this.config).getClient(
|
|
995
|
+
session.identity?.token
|
|
996
|
+
);
|
|
997
|
+
const remote = await client.withProjectKey({ projectKey: this.config.projectKey }).inventory().get({
|
|
998
|
+
queryArgs: {
|
|
999
|
+
where: "sku=:sku",
|
|
1000
|
+
"var.sku": query.sku
|
|
1001
|
+
}
|
|
1002
|
+
}).execute();
|
|
1003
|
+
const base = this.newModel();
|
|
1004
|
+
if (remote.body.results.length > 0) {
|
|
1005
|
+
const inv = remote.body.results[0];
|
|
1006
|
+
base.quantity = inv.availableQuantity;
|
|
1007
|
+
}
|
|
1008
|
+
return base;
|
|
1009
|
+
}
|
|
1010
|
+
};
|
|
1011
|
+
|
|
1012
|
+
// providers/commercetools/src/providers/price.provider.ts
|
|
1013
|
+
var CommercetoolsPriceProvider = class extends PriceProvider {
|
|
1014
|
+
constructor(config, schema, querySchema, mutationSchema) {
|
|
1015
|
+
super(schema, querySchema, mutationSchema);
|
|
1016
|
+
this.config = config;
|
|
1017
|
+
}
|
|
1018
|
+
async fetch(queries, session) {
|
|
1019
|
+
const client = new CommercetoolsClient(this.config).getClient(
|
|
1020
|
+
session.identity?.token
|
|
1021
|
+
);
|
|
1022
|
+
const queryArgs = {
|
|
1023
|
+
where: "sku in :skus",
|
|
1024
|
+
"var.skus": queries.map((x) => x.sku.key)
|
|
1025
|
+
};
|
|
1026
|
+
const remote = await client.withProjectKey({ projectKey: this.config.projectKey }).standalonePrices().get({
|
|
1027
|
+
queryArgs
|
|
1028
|
+
}).execute();
|
|
1029
|
+
const results = new Array();
|
|
1030
|
+
for (const query of queries) {
|
|
1031
|
+
const base = this.newModel();
|
|
1032
|
+
const matched = remote.body.results.find((x) => x.sku === query.sku.key);
|
|
1033
|
+
if (matched) {
|
|
1034
|
+
base.value = {
|
|
1035
|
+
cents: matched.value.centAmount,
|
|
1036
|
+
currency: "USD"
|
|
1037
|
+
};
|
|
1038
|
+
}
|
|
1039
|
+
base.identifier = {
|
|
1040
|
+
sku: {
|
|
1041
|
+
key: query.sku.key
|
|
1042
|
+
}
|
|
1043
|
+
};
|
|
1044
|
+
results.push(base);
|
|
1045
|
+
}
|
|
1046
|
+
return results;
|
|
1047
|
+
}
|
|
1048
|
+
process(mutation, session) {
|
|
1049
|
+
throw new Error("Method not implemented.");
|
|
1050
|
+
}
|
|
1051
|
+
};
|
|
1052
|
+
|
|
1053
|
+
// providers/commercetools/src/core/initialize.ts
|
|
1054
|
+
function withCommercetoolsCapabilities(configuration, capabilities) {
|
|
1055
|
+
const client = {};
|
|
1056
|
+
if (capabilities.product) {
|
|
1057
|
+
client.product = new CommercetoolsProductProvider(configuration, ProductSchema, ProductQuerySchema, ProductMutationSchema);
|
|
1058
|
+
}
|
|
1059
|
+
if (capabilities.search) {
|
|
1060
|
+
client.search = new CommercetoolsSearchProvider(configuration, SearchResultSchema, SearchQuerySchema, SearchMutationSchema);
|
|
1061
|
+
}
|
|
1062
|
+
if (capabilities.identity) {
|
|
1063
|
+
client.identity = new CommercetoolsIdentityProvider(configuration, IdentitySchema, IdentityQuerySchema, IdentityMutationSchema);
|
|
1064
|
+
}
|
|
1065
|
+
if (capabilities.cart) {
|
|
1066
|
+
client.cart = new CommercetoolsCartProvider(configuration, CartSchema, CartQuerySchema, CartMutationSchema);
|
|
1067
|
+
}
|
|
1068
|
+
if (capabilities.inventory) {
|
|
1069
|
+
client.inventory = new CommercetoolsInventoryProvider(configuration, InventorySchema, InventoryQuerySchema, InventoryMutationSchema);
|
|
1070
|
+
}
|
|
1071
|
+
if (capabilities.price) {
|
|
1072
|
+
client.price = new CommercetoolsPriceProvider(configuration, PriceSchema, PriceQuerySchema, PriceMutationSchema);
|
|
1073
|
+
}
|
|
1074
|
+
return client;
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
// providers/commercetools/src/schema/capabilities.schema.ts
|
|
1078
|
+
var CommercetoolsCapabilitiesSchema = CapabilitiesSchema.pick({
|
|
1079
|
+
product: true,
|
|
1080
|
+
search: true,
|
|
1081
|
+
identity: true,
|
|
1082
|
+
cart: true,
|
|
1083
|
+
inventory: true,
|
|
1084
|
+
price: true
|
|
1085
|
+
}).partial();
|
|
1086
|
+
|
|
1087
|
+
// providers/commercetools/src/schema/configuration.schema.ts
|
|
1088
|
+
import { z as z26 } from "zod";
|
|
1089
|
+
var CommercetoolsConfigurationSchema = z26.looseInterface({
|
|
1090
|
+
projectKey: z26.string(),
|
|
1091
|
+
authUrl: z26.string(),
|
|
1092
|
+
apiUrl: z26.string(),
|
|
1093
|
+
clientId: z26.string(),
|
|
1094
|
+
clientSecret: z26.string()
|
|
1095
|
+
});
|
|
1096
|
+
export {
|
|
1097
|
+
CommercetoolsCapabilitiesSchema,
|
|
1098
|
+
CommercetoolsCartProvider,
|
|
1099
|
+
CommercetoolsClient,
|
|
1100
|
+
CommercetoolsConfigurationSchema,
|
|
1101
|
+
CommercetoolsIdentityProvider,
|
|
1102
|
+
CommercetoolsInventoryProvider,
|
|
1103
|
+
CommercetoolsPriceProvider,
|
|
1104
|
+
CommercetoolsProductProvider,
|
|
1105
|
+
CommercetoolsSearchProvider,
|
|
1106
|
+
withCommercetoolsCapabilities
|
|
1107
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ClientBuilder } from '@commercetools/ts-client';
|
|
2
|
+
import { CommercetoolsConfiguration } from '../schema/configuration.schema';
|
|
3
|
+
export declare class CommercetoolsClient {
|
|
4
|
+
protected config: CommercetoolsConfiguration;
|
|
5
|
+
constructor(config: CommercetoolsConfiguration);
|
|
6
|
+
getClient(token?: string): import("@commercetools/platform-sdk").ApiRoot;
|
|
7
|
+
login(username: string, password: string): Promise<any>;
|
|
8
|
+
guest(): Promise<any>;
|
|
9
|
+
logout(token: string): Promise<Response>;
|
|
10
|
+
introspect(token: string): Promise<any>;
|
|
11
|
+
createAnonymousClient(): import("@commercetools/platform-sdk").ApiRoot;
|
|
12
|
+
protected createClientWithToken(token: string): import("@commercetools/platform-sdk").ApiRoot;
|
|
13
|
+
protected createBaseClientBuilder(): ClientBuilder;
|
|
14
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Client } from "@reactionary/core";
|
|
2
|
+
import { CommercetoolsCapabilities } from "../schema/capabilities.schema";
|
|
3
|
+
import { CommercetoolsConfiguration } from "../schema/configuration.schema";
|
|
4
|
+
export declare function withCommercetoolsCapabilities(configuration: CommercetoolsConfiguration, capabilities: CommercetoolsCapabilities): Partial<Client>;
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './core/client';
|
|
2
|
+
export * from './core/initialize';
|
|
3
|
+
export * from './providers/cart.provider';
|
|
4
|
+
export * from './providers/identity.provider';
|
|
5
|
+
export * from './providers/inventory.provider';
|
|
6
|
+
export * from './providers/price.provider';
|
|
7
|
+
export * from './providers/product.provider';
|
|
8
|
+
export * from './providers/search.provider';
|
|
9
|
+
export * from './schema/capabilities.schema';
|
|
10
|
+
export * from './schema/configuration.schema';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Cart, CartMutation, CartMutationItemAdd, CartMutationItemQuantityChange, CartMutationItemRemove, CartProvider, CartQuery, Session } from '@reactionary/core';
|
|
2
|
+
import { CommercetoolsConfiguration } from '../schema/configuration.schema';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { Cart as CTCart } from '@commercetools/platform-sdk';
|
|
5
|
+
export declare class CommercetoolsCartProvider<T extends Cart = Cart, Q extends CartQuery = CartQuery, M extends CartMutation = CartMutation> extends CartProvider<T, Q, M> {
|
|
6
|
+
protected config: CommercetoolsConfiguration;
|
|
7
|
+
constructor(config: CommercetoolsConfiguration, schema: z.ZodType<T>, querySchema: z.ZodType<Q, Q>, mutationSchema: z.ZodType<M, M>);
|
|
8
|
+
protected fetch(queries: Q[], session: Session): Promise<T[]>;
|
|
9
|
+
protected process(mutations: M[], session: Session): Promise<T>;
|
|
10
|
+
protected adjust(payload: CartMutationItemQuantityChange, session: Session): Promise<T>;
|
|
11
|
+
protected add(payload: CartMutationItemAdd, session: Session): Promise<T>;
|
|
12
|
+
protected remove(payload: CartMutationItemRemove, session: Session): Promise<T>;
|
|
13
|
+
protected getClient(session: Session): import("@commercetools/platform-sdk").ByProjectKeyCartsRequestBuilder;
|
|
14
|
+
protected composeCart(remote: CTCart): T;
|
|
15
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Identity, IdentityMutation, IdentityMutationLogin, IdentityProvider, IdentityQuery, Session } from '@reactionary/core';
|
|
2
|
+
import { CommercetoolsConfiguration } from '../schema/configuration.schema';
|
|
3
|
+
import z from 'zod';
|
|
4
|
+
export declare class CommercetoolsIdentityProvider<T extends Identity = Identity, Q extends IdentityQuery = IdentityQuery, M extends IdentityMutation = IdentityMutation> extends IdentityProvider<T, Q, M> {
|
|
5
|
+
protected config: CommercetoolsConfiguration;
|
|
6
|
+
constructor(config: CommercetoolsConfiguration, schema: z.ZodType<T>, querySchema: z.ZodType<Q, Q>, mutationSchema: z.ZodType<M, M>);
|
|
7
|
+
protected fetch(queries: Q[], session: Session): Promise<T[]>;
|
|
8
|
+
protected process(mutations: M[], session: Session): Promise<T>;
|
|
9
|
+
protected get(session: Session): Promise<T>;
|
|
10
|
+
protected login(payload: IdentityMutationLogin, session: Session): Promise<T>;
|
|
11
|
+
protected logout(session: Session): Promise<T>;
|
|
12
|
+
protected extractCustomerIdFromScopes(scopes: string): string;
|
|
13
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Inventory, InventoryProvider, InventoryQuery, RedisCache, Session } from '@reactionary/core';
|
|
2
|
+
import z from 'zod';
|
|
3
|
+
import { CommercetoolsConfiguration } from '../schema/configuration.schema';
|
|
4
|
+
import { InventoryMutation } from 'core/src/schemas/mutations/inventory.mutation';
|
|
5
|
+
export declare class CommercetoolsInventoryProvider<T extends Inventory = Inventory, Q extends InventoryQuery = InventoryQuery, M extends InventoryMutation = InventoryMutation> extends InventoryProvider<T, Q, M> {
|
|
6
|
+
protected config: CommercetoolsConfiguration;
|
|
7
|
+
protected cache: RedisCache;
|
|
8
|
+
constructor(config: CommercetoolsConfiguration, schema: z.ZodType<T>, querySchema: z.ZodType<Q, Q>, mutationSchema: z.ZodType<M, M>);
|
|
9
|
+
protected fetch(queries: Q[], session: Session): Promise<T[]>;
|
|
10
|
+
protected process(mutations: M[], session: Session): Promise<T>;
|
|
11
|
+
protected get(query: Q, session: Session): Promise<T>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BaseMutation, Price, PriceProvider, PriceQuery, Session } from '@reactionary/core';
|
|
2
|
+
import z from 'zod';
|
|
3
|
+
import { CommercetoolsConfiguration } from '../schema/configuration.schema';
|
|
4
|
+
import { PriceMutation } from 'core/src/schemas/mutations/price.mutation';
|
|
5
|
+
export declare class CommercetoolsPriceProvider<T extends Price = Price, Q extends PriceQuery = PriceQuery, M extends PriceMutation = PriceMutation> extends PriceProvider<T, Q, M> {
|
|
6
|
+
protected config: CommercetoolsConfiguration;
|
|
7
|
+
constructor(config: CommercetoolsConfiguration, schema: z.ZodType<T>, querySchema: z.ZodType<Q, Q>, mutationSchema: z.ZodType<M, M>);
|
|
8
|
+
protected fetch(queries: Q[], session: Session): Promise<T[]>;
|
|
9
|
+
protected process(mutation: BaseMutation[], session: Session): Promise<T>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ProductProvider, ProductQuery, Product, Session, BaseMutation, ProductMutation } from '@reactionary/core';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { CommercetoolsConfiguration } from '../schema/configuration.schema';
|
|
4
|
+
import { ProductProjection } from '@commercetools/platform-sdk';
|
|
5
|
+
export declare class CommercetoolsProductProvider<T extends Product = Product, Q extends ProductQuery = ProductQuery, M extends ProductMutation = ProductMutation> extends ProductProvider<T, Q, M> {
|
|
6
|
+
protected config: CommercetoolsConfiguration;
|
|
7
|
+
constructor(config: CommercetoolsConfiguration, schema: z.ZodType<T>, querySchema: z.ZodType<Q, Q>, mutationSchema: z.ZodType<M, M>);
|
|
8
|
+
protected fetch(queries: ProductQuery[], session: Session): Promise<T[]>;
|
|
9
|
+
protected process(mutation: BaseMutation[], session: Session): Promise<T>;
|
|
10
|
+
protected parse(data: ProductProjection): T;
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { SearchIdentifier, SearchMutation, SearchProvider, SearchQuery, SearchResult, Session } from '@reactionary/core';
|
|
2
|
+
import z from 'zod';
|
|
3
|
+
import { CommercetoolsConfiguration } from '../schema/configuration.schema';
|
|
4
|
+
export declare class CommercetoolsSearchProvider<T extends SearchResult = SearchResult, Q extends SearchQuery = SearchQuery, M extends SearchMutation = SearchMutation> extends SearchProvider<T, Q, M> {
|
|
5
|
+
protected config: CommercetoolsConfiguration;
|
|
6
|
+
constructor(config: CommercetoolsConfiguration, schema: z.ZodType<T>, querySchema: z.ZodType<Q, Q>, mutationSchema: z.ZodType<M, M>);
|
|
7
|
+
protected fetch(queries: Q[], session: Session): Promise<T[]>;
|
|
8
|
+
protected process(mutations: M[], session: Session): Promise<T>;
|
|
9
|
+
get(identifier: SearchIdentifier): Promise<T>;
|
|
10
|
+
parse(remote: any, query: SearchIdentifier): T;
|
|
11
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare const CommercetoolsCapabilitiesSchema: z.ZodInterface<{
|
|
3
|
+
identity: z.ZodOptional<z.ZodBoolean>;
|
|
4
|
+
search: z.ZodOptional<z.ZodBoolean>;
|
|
5
|
+
product: z.ZodOptional<z.ZodBoolean>;
|
|
6
|
+
cart: z.ZodOptional<z.ZodBoolean>;
|
|
7
|
+
price: z.ZodOptional<z.ZodBoolean>;
|
|
8
|
+
inventory: z.ZodOptional<z.ZodBoolean>;
|
|
9
|
+
}, {
|
|
10
|
+
optional: "identity" | "search" | "product" | "cart" | "price" | "inventory";
|
|
11
|
+
defaulted: never;
|
|
12
|
+
extra: Record<string, unknown>;
|
|
13
|
+
}>;
|
|
14
|
+
export type CommercetoolsCapabilities = z.infer<typeof CommercetoolsCapabilitiesSchema>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare const CommercetoolsConfigurationSchema: z.ZodInterface<{
|
|
3
|
+
projectKey: z.ZodString;
|
|
4
|
+
authUrl: z.ZodString;
|
|
5
|
+
apiUrl: z.ZodString;
|
|
6
|
+
clientId: z.ZodString;
|
|
7
|
+
clientSecret: z.ZodString;
|
|
8
|
+
}, {
|
|
9
|
+
optional: never;
|
|
10
|
+
defaulted: never;
|
|
11
|
+
extra: Record<string, unknown>;
|
|
12
|
+
}>;
|
|
13
|
+
export type CommercetoolsConfiguration = z.infer<typeof CommercetoolsConfigurationSchema>;
|