@labdigital/commercetools-mock 1.10.0 → 2.0.0

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/dist/index.cjs CHANGED
@@ -36,10 +36,11 @@ __export(src_exports, {
36
36
  module.exports = __toCommonJS(src_exports);
37
37
 
38
38
  // src/ctMock.ts
39
- var import_nock = __toESM(require("nock"), 1);
40
39
  var import_express6 = __toESM(require("express"), 1);
41
40
  var import_supertest = __toESM(require("supertest"), 1);
42
41
  var import_morgan = __toESM(require("morgan"), 1);
42
+ var import_node = require("msw/node");
43
+ var import_msw = require("msw");
43
44
 
44
45
  // src/storage/abstract.ts
45
46
  var AbstractStorage = class {
@@ -1415,19 +1416,19 @@ var ProjectAPI = class {
1415
1416
 
1416
1417
  // src/lib/proxy.ts
1417
1418
  var copyHeaders = (headers) => {
1418
- const validHeaders = ["accept", "host", "authorization"];
1419
+ const validHeaders = ["accept", "host", "authorization", "content-type"];
1419
1420
  const result = {};
1420
- Object.entries(headers).forEach(([key, value]) => {
1421
+ for (const [key, value] of headers.entries()) {
1421
1422
  if (validHeaders.includes(key.toLowerCase())) {
1422
1423
  result[key] = value;
1423
1424
  }
1424
- });
1425
+ }
1425
1426
  return result;
1426
1427
  };
1427
1428
 
1428
1429
  // src/constants.ts
1429
- var DEFAULT_API_HOSTNAME = /^https:\/\/api\..*?\.commercetools.com:443$/;
1430
- var DEFAULT_AUTH_HOSTNAME = /^https:\/\/auth\..*?\.commercetools.com:443$/;
1430
+ var DEFAULT_API_HOSTNAME = "https://api.*.commercetools.com";
1431
+ var DEFAULT_AUTH_HOSTNAME = "https://auth.*.commercetools.com";
1431
1432
 
1432
1433
  // src/repositories/helpers.ts
1433
1434
  var import_uuid2 = require("uuid");
@@ -2189,6 +2190,16 @@ var CartRepository = class extends AbstractResourceRepository {
2189
2190
  }
2190
2191
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
2191
2192
  },
2193
+ addItemShippingAddress: (context, resource, { action, address }) => {
2194
+ const newAddress = createAddress(
2195
+ address,
2196
+ context.projectKey,
2197
+ this._storage
2198
+ );
2199
+ if (newAddress) {
2200
+ resource.itemShippingAddresses.push(newAddress);
2201
+ }
2202
+ },
2192
2203
  changeLineItemQuantity: (context, resource, { lineItemId, lineItemKey, quantity }) => {
2193
2204
  let lineItem;
2194
2205
  if (lineItemId) {
@@ -2312,6 +2323,26 @@ var CartRepository = class extends AbstractResourceRepository {
2312
2323
  setLocale: (context, resource, { locale }) => {
2313
2324
  resource.locale = locale;
2314
2325
  },
2326
+ setLineItemShippingDetails: (context, resource, {
2327
+ action,
2328
+ shippingDetails,
2329
+ lineItemId,
2330
+ lineItemKey
2331
+ }) => {
2332
+ const lineItem = resource.lineItems.find(
2333
+ (x) => lineItemId && x.id === lineItemId || lineItemKey && x.key === lineItemKey
2334
+ );
2335
+ if (!lineItem) {
2336
+ throw new CommercetoolsError({
2337
+ code: "General",
2338
+ message: lineItemKey ? `A line item with key '${lineItemKey}' not found.` : `A line item with ID '${lineItemId}' not found.`
2339
+ });
2340
+ }
2341
+ lineItem.shippingDetails = {
2342
+ ...shippingDetails,
2343
+ valid: true
2344
+ };
2345
+ },
2315
2346
  setShippingAddress: (context, resource, { address }) => {
2316
2347
  if (!address) {
2317
2348
  resource.shippingAddress = void 0;
@@ -4679,6 +4710,7 @@ var ProductSelectionRepository = class extends AbstractResourceRepository {
4679
4710
  const resource = {
4680
4711
  ...getBaseResourceProperties(),
4681
4712
  productCount: 0,
4713
+ key: draft.key,
4682
4714
  name: draft.name,
4683
4715
  type: "individual",
4684
4716
  mode: "Individual"
@@ -4686,7 +4718,11 @@ var ProductSelectionRepository = class extends AbstractResourceRepository {
4686
4718
  this.saveNew(context, resource);
4687
4719
  return resource;
4688
4720
  }
4689
- actions = {};
4721
+ actions = {
4722
+ changeName: (context, resource, { name }) => {
4723
+ resource.name = name;
4724
+ }
4725
+ };
4690
4726
  };
4691
4727
 
4692
4728
  // src/repositories/product-type.ts
@@ -5686,6 +5722,18 @@ var AssociateRoleServices = class extends AbstractService {
5686
5722
  }
5687
5723
  };
5688
5724
 
5725
+ // src/services/attribute-group.ts
5726
+ var AttributeGroupService = class extends AbstractService {
5727
+ repository;
5728
+ constructor(parent, repository) {
5729
+ super(parent);
5730
+ this.repository = repository;
5731
+ }
5732
+ getBasePath() {
5733
+ return "attribute-groups";
5734
+ }
5735
+ };
5736
+
5689
5737
  // src/services/business-units.ts
5690
5738
  var BusinessUnitServices = class extends AbstractService {
5691
5739
  repository;
@@ -5698,6 +5746,18 @@ var BusinessUnitServices = class extends AbstractService {
5698
5746
  }
5699
5747
  };
5700
5748
 
5749
+ // src/services/cart-discount.ts
5750
+ var CartDiscountService = class extends AbstractService {
5751
+ repository;
5752
+ constructor(parent, repository) {
5753
+ super(parent);
5754
+ this.repository = repository;
5755
+ }
5756
+ getBasePath() {
5757
+ return "cart-discounts";
5758
+ }
5759
+ };
5760
+
5701
5761
  // src/services/cart.ts
5702
5762
  var CartService = class extends AbstractService {
5703
5763
  repository;
@@ -5736,18 +5796,6 @@ var CartService = class extends AbstractService {
5736
5796
  }
5737
5797
  };
5738
5798
 
5739
- // src/services/cart-discount.ts
5740
- var CartDiscountService = class extends AbstractService {
5741
- repository;
5742
- constructor(parent, repository) {
5743
- super(parent);
5744
- this.repository = repository;
5745
- }
5746
- getBasePath() {
5747
- return "cart-discounts";
5748
- }
5749
- };
5750
-
5751
5799
  // src/services/category.ts
5752
5800
  var CategoryServices = class extends AbstractService {
5753
5801
  repository;
@@ -5824,6 +5872,18 @@ var CustomObjectService = class extends AbstractService {
5824
5872
  }
5825
5873
  };
5826
5874
 
5875
+ // src/services/customer-group.ts
5876
+ var CustomerGroupService = class extends AbstractService {
5877
+ repository;
5878
+ constructor(parent, repository) {
5879
+ super(parent);
5880
+ this.repository = repository;
5881
+ }
5882
+ getBasePath() {
5883
+ return "customer-groups";
5884
+ }
5885
+ };
5886
+
5827
5887
  // src/services/customer.ts
5828
5888
  var import_uuid8 = require("uuid");
5829
5889
  var CustomerService = class extends AbstractService {
@@ -5855,18 +5915,6 @@ var CustomerService = class extends AbstractService {
5855
5915
  }
5856
5916
  };
5857
5917
 
5858
- // src/services/customer-group.ts
5859
- var CustomerGroupService = class extends AbstractService {
5860
- repository;
5861
- constructor(parent, repository) {
5862
- super(parent);
5863
- this.repository = repository;
5864
- }
5865
- getBasePath() {
5866
- return "customer-groups";
5867
- }
5868
- };
5869
-
5870
5918
  // src/services/discount-code.ts
5871
5919
  var DiscountCodeService = class extends AbstractService {
5872
5920
  repository;
@@ -6075,18 +6123,6 @@ var PaymentService = class extends AbstractService {
6075
6123
  }
6076
6124
  };
6077
6125
 
6078
- // src/services/product.ts
6079
- var ProductService = class extends AbstractService {
6080
- repository;
6081
- constructor(parent, repository) {
6082
- super(parent);
6083
- this.repository = repository;
6084
- }
6085
- getBasePath() {
6086
- return "products";
6087
- }
6088
- };
6089
-
6090
6126
  // src/services/product-discount.ts
6091
6127
  var ProductDiscountService = class extends AbstractService {
6092
6128
  repository;
@@ -6149,6 +6185,18 @@ var ProductProjectionService = class extends AbstractService {
6149
6185
  }
6150
6186
  };
6151
6187
 
6188
+ // src/services/product-selection.ts
6189
+ var ProductSelectionService = class extends AbstractService {
6190
+ repository;
6191
+ constructor(parent, repository) {
6192
+ super(parent);
6193
+ this.repository = repository;
6194
+ }
6195
+ getBasePath() {
6196
+ return "product-selections";
6197
+ }
6198
+ };
6199
+
6152
6200
  // src/services/product-type.ts
6153
6201
  var ProductTypeService = class extends AbstractService {
6154
6202
  repository;
@@ -6161,6 +6209,18 @@ var ProductTypeService = class extends AbstractService {
6161
6209
  }
6162
6210
  };
6163
6211
 
6212
+ // src/services/product.ts
6213
+ var ProductService = class extends AbstractService {
6214
+ repository;
6215
+ constructor(parent, repository) {
6216
+ super(parent);
6217
+ this.repository = repository;
6218
+ }
6219
+ getBasePath() {
6220
+ return "products";
6221
+ }
6222
+ };
6223
+
6164
6224
  // src/services/shipping-method.ts
6165
6225
  var ShippingMethodService = class extends AbstractService {
6166
6226
  repository;
@@ -6273,18 +6333,6 @@ var ZoneService = class extends AbstractService {
6273
6333
  }
6274
6334
  };
6275
6335
 
6276
- // src/services/attribute-group.ts
6277
- var AttributeGroupService = class extends AbstractService {
6278
- repository;
6279
- constructor(parent, repository) {
6280
- super(parent);
6281
- this.repository = repository;
6282
- }
6283
- getBasePath() {
6284
- return "attribute-groups";
6285
- }
6286
- };
6287
-
6288
6336
  // src/services/index.ts
6289
6337
  var createServices = (router, repos) => ({
6290
6338
  "associate-role": new AssociateRoleServices(router, repos["associate-role"]),
@@ -6329,6 +6377,10 @@ var createServices = (router, repos) => ({
6329
6377
  router,
6330
6378
  repos["product-projection"]
6331
6379
  ),
6380
+ "product-selection": new ProductSelectionService(
6381
+ router,
6382
+ repos["product-selection"]
6383
+ ),
6332
6384
  "shopping-list": new ShoppingListService(router, repos["shopping-list"]),
6333
6385
  state: new StateService(router, repos["state"]),
6334
6386
  store: new StoreService(router, repos["store"]),
@@ -6351,12 +6403,13 @@ var DEFAULT_OPTIONS = {
6351
6403
  authHost: DEFAULT_AUTH_HOSTNAME,
6352
6404
  silent: false
6353
6405
  };
6406
+ var _globalListeners = [];
6354
6407
  var CommercetoolsMock = class {
6355
6408
  app;
6356
6409
  options;
6357
6410
  _storage;
6358
6411
  _oauth2;
6359
- _nockScopes = { auth: void 0, api: void 0 };
6412
+ _mswServer = void 0;
6360
6413
  _services;
6361
6414
  _repositories;
6362
6415
  _projectService;
@@ -6373,16 +6426,15 @@ var CommercetoolsMock = class {
6373
6426
  this.app = this.createApp({ silent: this.options.silent });
6374
6427
  }
6375
6428
  start() {
6376
- this.mockAuthHost();
6377
- this.mockApiHost();
6429
+ this.clear();
6430
+ this.startServer();
6378
6431
  }
6379
6432
  stop() {
6380
- this._nockScopes.auth?.persist(false);
6381
- this._nockScopes.auth = void 0;
6382
- this._nockScopes.api?.persist(false);
6383
- this._nockScopes.api = void 0;
6433
+ this._mswServer?.close();
6434
+ this._mswServer = void 0;
6384
6435
  }
6385
6436
  clear() {
6437
+ this._mswServer?.resetHandlers();
6386
6438
  this._storage.clear();
6387
6439
  }
6388
6440
  project(projectKey) {
@@ -6445,25 +6497,65 @@ var CommercetoolsMock = class {
6445
6497
  });
6446
6498
  return app;
6447
6499
  }
6448
- mockApiHost() {
6449
- const app = this.app;
6450
- this._nockScopes.api = (0, import_nock.default)(this.options.apiHost).persist().get(/.*/).reply(async function(uri) {
6451
- const response = await (0, import_supertest.default)(app).get(uri).set(copyHeaders(this.req.headers));
6452
- return [response.status, response.body];
6453
- }).post(/.*/).reply(async function(uri, body) {
6454
- const response = await (0, import_supertest.default)(app).post(uri).set(copyHeaders(this.req.headers)).send(body);
6455
- return [response.status, response.body];
6456
- }).delete(/.*/).reply(async function(uri, body) {
6457
- const response = await (0, import_supertest.default)(app).delete(uri).set(copyHeaders(this.req.headers)).send(body);
6458
- return [response.status, response.body];
6459
- });
6460
- }
6461
- mockAuthHost() {
6500
+ startServer() {
6501
+ if (_globalListeners.length > 0) {
6502
+ if (this._mswServer !== void 0) {
6503
+ throw new Error("Server already started");
6504
+ } else {
6505
+ console.warn("Server wasn't stopped properly, clearing");
6506
+ _globalListeners.forEach((listener) => listener.close());
6507
+ }
6508
+ }
6462
6509
  const app = this.app;
6463
- this._nockScopes.auth = (0, import_nock.default)(this.options.authHost).persist().post(/^\/oauth\/.*/).reply(async function(uri, body) {
6464
- const response = await (0, import_supertest.default)(app).post(uri + "?" + body).set(copyHeaders(this.req.headers)).send();
6465
- return [response.status, response.body];
6510
+ this._mswServer = (0, import_node.setupServer)(
6511
+ import_msw.http.post(`${this.options.authHost}/oauth/*`, async ({ request }) => {
6512
+ const text = await request.text();
6513
+ const url = new URL(request.url);
6514
+ const res = await (0, import_supertest.default)(app).post(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(text);
6515
+ return new import_msw.HttpResponse(res.text, {
6516
+ status: res.status,
6517
+ headers: res.headers
6518
+ });
6519
+ }),
6520
+ import_msw.http.get(`${this.options.apiHost}/*`, async ({ request }) => {
6521
+ const body = await request.text();
6522
+ const url = new URL(request.url);
6523
+ const res = await (0, import_supertest.default)(app).get(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(body);
6524
+ return new import_msw.HttpResponse(res.text, {
6525
+ status: res.status,
6526
+ headers: res.headers
6527
+ });
6528
+ }),
6529
+ import_msw.http.post(`${this.options.apiHost}/*`, async ({ request }) => {
6530
+ const body = await request.text();
6531
+ const url = new URL(request.url);
6532
+ const res = await (0, import_supertest.default)(app).post(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(body);
6533
+ return new import_msw.HttpResponse(res.text, {
6534
+ status: res.status,
6535
+ headers: res.headers
6536
+ });
6537
+ }),
6538
+ import_msw.http.delete(`${this.options.apiHost}/*`, async ({ request }) => {
6539
+ const body = await request.text();
6540
+ const url = new URL(request.url);
6541
+ const res = await (0, import_supertest.default)(app).delete(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(body);
6542
+ return new import_msw.HttpResponse(res.text, {
6543
+ status: res.status,
6544
+ headers: res.headers
6545
+ });
6546
+ })
6547
+ );
6548
+ this._mswServer.listen({
6549
+ // We need to allow requests done by supertest
6550
+ onUnhandledRequest: (request, print) => {
6551
+ const url = new URL(request.url);
6552
+ if (url.hostname === "127.0.0.1") {
6553
+ return;
6554
+ }
6555
+ print.error();
6556
+ }
6466
6557
  });
6558
+ _globalListeners.push(this._mswServer);
6467
6559
  }
6468
6560
  };
6469
6561
  // Annotate the CommonJS export names for ESM import in node: