@labdigital/commercetools-mock 2.5.0 → 2.7.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.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  import express from 'express';
2
+ import { SetupServer } from 'msw/node';
2
3
  import * as ctp from '@commercetools/platform-sdk';
3
4
  import { Project, ResourceIdentifier, QueryParam, AssociateRoleDraft, AssociateRole, AssociateRoleSetNameAction, AssociateRoleSetPermissionsAction, AssociateRoleChangeBuyerAssignableAction, AssociateRoleSetCustomFieldAction, AssociateRoleAddPermissionAction, AssociateRoleRemovePermissionAction, AttributeGroupDraft, AttributeGroup, AttributeGroupSetAttributesAction, AttributeGroupChangeNameAction, AttributeGroupSetDescriptionAction, AttributeGroupSetKeyAction, BusinessUnitDraft, BusinessUnit, BusinessUnitAddAddressAction, BusinessUnitAddAssociateAction, BusinessUnitSetAssociatesAction, BusinessUnitSetContactEmailAction, BusinessUnitSetStoreModeAction, BusinessUnitChangeNameAction, BusinessUnitChangeAddressAction, BusinessUnitAddStoreAction, BusinessUnitChangeParentUnitAction, BusinessUnitChangeStatusAction, CartDraft, Cart, CartAddLineItemAction, CartAddItemShippingAddressAction, CartChangeLineItemQuantityAction, CartRemoveLineItemAction, CartSetBillingAddressAction, CartSetShippingMethodAction, CartSetCountryAction, CartSetCustomerEmailAction, CartSetCustomFieldAction, CartSetCustomTypeAction, CartSetLocaleAction, CartSetLineItemShippingDetailsAction, CartSetShippingAddressAction, CartRemoveDiscountCodeAction, LineItemDraft, LineItem, CartDiscountDraft, CartDiscount, CartDiscountUpdateAction, AssetDraft, Asset, CategoryDraft, Category, CategoryChangeAssetNameAction, CategoryChangeSlugAction, CategoryChangeNameAction, CategoryChangeParentAction, CategorySetKeyAction, CategorySetAssetDescriptionAction, CategorySetAssetSourcesAction, CategorySetDescriptionAction, CategorySetMetaDescriptionAction, CategorySetMetaKeywordsAction, CategorySetMetaTitleAction, CategorySetCustomTypeAction, CategorySetCustomFieldAction, CategoryRemoveAssetAction, CategoryAddAssetAction, ChannelDraft, Channel, ChannelUpdateAction, CustomObjectDraft, CustomObject, CustomerDraft, Customer, CustomerChangeEmailAction, CustomerSetAuthenticationModeAction, CustomerSetCustomFieldAction, CustomerGroupDraft, CustomerGroup, CustomerGroupSetKeyAction, CustomerGroupChangeNameAction, CustomerGroupSetCustomTypeAction, CustomerGroupSetCustomFieldAction, DiscountCodeDraft, DiscountCode, DiscountCodeUpdateAction, Extension, ExtensionDraft, ExtensionUpdateAction, InventoryEntryDraft, InventoryEntry, InventoryEntryChangeQuantityAction, InventoryEntrySetExpectedDeliveryAction, InventoryEntrySetCustomFieldAction, InventoryEntrySetCustomTypeAction, InventoryEntrySetRestockableInDaysAction, OrderFromCartDraft, Order, CartReference, OrderImportDraft, OrderAddPaymentAction, OrderChangeOrderStateAction, OrderChangePaymentStateAction, OrderTransitionStateAction, OrderSetBillingAddressAction, OrderSetCustomerEmailAction, OrderSetCustomFieldAction, OrderSetCustomTypeAction, OrderSetLocaleAction, OrderSetOrderNumberAction, OrderSetShippingAddressAction, OrderSetStoreAction, MyOrderFromCartDraft, OrderEditDraft, OrderEdit, OrderEditUpdateAction, PaymentDraft, Payment, TransactionDraft, Transaction, PaymentSetCustomFieldAction, PaymentSetCustomTypeAction, PaymentAddTransactionAction, PaymentChangeTransactionStateAction, PaymentTransitionStateAction, ProductDraft, Product, ProductUpdateAction, ProductDiscountDraft, ProductDiscount, ProductDiscountUpdateAction, ProductProjectionPagedSearchResponse, ProductProjection, FacetResults, TermFacetResult, FilteredFacetResult, RangeFacetResult, ProductSelectionDraft, ProductSelection, ProductSelectionUpdateAction, ProductTypeDraft, ProductType, AttributeDefinitionDraft, AttributeDefinition, ProductTypeUpdateAction, ProjectUpdateAction, QuoteDraft, Quote, QuoteUpdateAction, QuoteRequestDraft, QuoteRequest, QuoteRequestUpdateAction, ReviewDraft, Review, ReviewUpdateAction, ShippingMethodDraft, ShippingMethod, ShippingMethodUpdateAction, ShoppingListDraft, ShoppingList, StagedQuoteDraft, StagedQuote, StagedQuoteUpdateAction, StandalonePriceDraft, StandalonePrice, ChannelResourceIdentifier, ChannelReference, DiscountedPriceDraft, StandalonePriceChangeActiveAction, StandalonePriceChangeValueAction, StandalonePriceSetDiscountedPriceAction, StateDraft, State, StateUpdateAction, StoreDraft, Store, StoreUpdateAction, SubscriptionDraft, Subscription, TaxCategoryDraft, TaxCategory, TaxCategoryUpdateAction, TypeDraft, Type, TypeUpdateAction, ZoneDraft, Zone, ZoneUpdateAction, BaseResource, UpdateAction } from '@commercetools/platform-sdk';
4
5
 
@@ -464,6 +465,7 @@ declare const createRepositories: (storage: AbstractStorage) => {
464
465
  'my-order': MyOrderRepository;
465
466
  'my-customer': CustomerRepository;
466
467
  'my-payment': PaymentRepository;
468
+ 'my-shopping-list': ShoppingListRepository;
467
469
  product: ProductRepository;
468
470
  'product-type': ProductTypeRepository;
469
471
  'product-discount': ProductDiscountRepository;
@@ -643,6 +645,7 @@ declare class CommercetoolsMock {
643
645
  project(projectKey?: string): ProjectAPI;
644
646
  runServer(port?: number, options?: AppOptions): void;
645
647
  private createApp;
648
+ registerHandlers(server: SetupServer): void;
646
649
  private startServer;
647
650
  }
648
651
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import express from 'express';
2
+ import { SetupServer } from 'msw/node';
2
3
  import * as ctp from '@commercetools/platform-sdk';
3
4
  import { Project, ResourceIdentifier, QueryParam, AssociateRoleDraft, AssociateRole, AssociateRoleSetNameAction, AssociateRoleSetPermissionsAction, AssociateRoleChangeBuyerAssignableAction, AssociateRoleSetCustomFieldAction, AssociateRoleAddPermissionAction, AssociateRoleRemovePermissionAction, AttributeGroupDraft, AttributeGroup, AttributeGroupSetAttributesAction, AttributeGroupChangeNameAction, AttributeGroupSetDescriptionAction, AttributeGroupSetKeyAction, BusinessUnitDraft, BusinessUnit, BusinessUnitAddAddressAction, BusinessUnitAddAssociateAction, BusinessUnitSetAssociatesAction, BusinessUnitSetContactEmailAction, BusinessUnitSetStoreModeAction, BusinessUnitChangeNameAction, BusinessUnitChangeAddressAction, BusinessUnitAddStoreAction, BusinessUnitChangeParentUnitAction, BusinessUnitChangeStatusAction, CartDraft, Cart, CartAddLineItemAction, CartAddItemShippingAddressAction, CartChangeLineItemQuantityAction, CartRemoveLineItemAction, CartSetBillingAddressAction, CartSetShippingMethodAction, CartSetCountryAction, CartSetCustomerEmailAction, CartSetCustomFieldAction, CartSetCustomTypeAction, CartSetLocaleAction, CartSetLineItemShippingDetailsAction, CartSetShippingAddressAction, CartRemoveDiscountCodeAction, LineItemDraft, LineItem, CartDiscountDraft, CartDiscount, CartDiscountUpdateAction, AssetDraft, Asset, CategoryDraft, Category, CategoryChangeAssetNameAction, CategoryChangeSlugAction, CategoryChangeNameAction, CategoryChangeParentAction, CategorySetKeyAction, CategorySetAssetDescriptionAction, CategorySetAssetSourcesAction, CategorySetDescriptionAction, CategorySetMetaDescriptionAction, CategorySetMetaKeywordsAction, CategorySetMetaTitleAction, CategorySetCustomTypeAction, CategorySetCustomFieldAction, CategoryRemoveAssetAction, CategoryAddAssetAction, ChannelDraft, Channel, ChannelUpdateAction, CustomObjectDraft, CustomObject, CustomerDraft, Customer, CustomerChangeEmailAction, CustomerSetAuthenticationModeAction, CustomerSetCustomFieldAction, CustomerGroupDraft, CustomerGroup, CustomerGroupSetKeyAction, CustomerGroupChangeNameAction, CustomerGroupSetCustomTypeAction, CustomerGroupSetCustomFieldAction, DiscountCodeDraft, DiscountCode, DiscountCodeUpdateAction, Extension, ExtensionDraft, ExtensionUpdateAction, InventoryEntryDraft, InventoryEntry, InventoryEntryChangeQuantityAction, InventoryEntrySetExpectedDeliveryAction, InventoryEntrySetCustomFieldAction, InventoryEntrySetCustomTypeAction, InventoryEntrySetRestockableInDaysAction, OrderFromCartDraft, Order, CartReference, OrderImportDraft, OrderAddPaymentAction, OrderChangeOrderStateAction, OrderChangePaymentStateAction, OrderTransitionStateAction, OrderSetBillingAddressAction, OrderSetCustomerEmailAction, OrderSetCustomFieldAction, OrderSetCustomTypeAction, OrderSetLocaleAction, OrderSetOrderNumberAction, OrderSetShippingAddressAction, OrderSetStoreAction, MyOrderFromCartDraft, OrderEditDraft, OrderEdit, OrderEditUpdateAction, PaymentDraft, Payment, TransactionDraft, Transaction, PaymentSetCustomFieldAction, PaymentSetCustomTypeAction, PaymentAddTransactionAction, PaymentChangeTransactionStateAction, PaymentTransitionStateAction, ProductDraft, Product, ProductUpdateAction, ProductDiscountDraft, ProductDiscount, ProductDiscountUpdateAction, ProductProjectionPagedSearchResponse, ProductProjection, FacetResults, TermFacetResult, FilteredFacetResult, RangeFacetResult, ProductSelectionDraft, ProductSelection, ProductSelectionUpdateAction, ProductTypeDraft, ProductType, AttributeDefinitionDraft, AttributeDefinition, ProductTypeUpdateAction, ProjectUpdateAction, QuoteDraft, Quote, QuoteUpdateAction, QuoteRequestDraft, QuoteRequest, QuoteRequestUpdateAction, ReviewDraft, Review, ReviewUpdateAction, ShippingMethodDraft, ShippingMethod, ShippingMethodUpdateAction, ShoppingListDraft, ShoppingList, StagedQuoteDraft, StagedQuote, StagedQuoteUpdateAction, StandalonePriceDraft, StandalonePrice, ChannelResourceIdentifier, ChannelReference, DiscountedPriceDraft, StandalonePriceChangeActiveAction, StandalonePriceChangeValueAction, StandalonePriceSetDiscountedPriceAction, StateDraft, State, StateUpdateAction, StoreDraft, Store, StoreUpdateAction, SubscriptionDraft, Subscription, TaxCategoryDraft, TaxCategory, TaxCategoryUpdateAction, TypeDraft, Type, TypeUpdateAction, ZoneDraft, Zone, ZoneUpdateAction, BaseResource, UpdateAction } from '@commercetools/platform-sdk';
4
5
 
@@ -464,6 +465,7 @@ declare const createRepositories: (storage: AbstractStorage) => {
464
465
  'my-order': MyOrderRepository;
465
466
  'my-customer': CustomerRepository;
466
467
  'my-payment': PaymentRepository;
468
+ 'my-shopping-list': ShoppingListRepository;
467
469
  product: ProductRepository;
468
470
  'product-type': ProductTypeRepository;
469
471
  'product-discount': ProductDiscountRepository;
@@ -643,6 +645,7 @@ declare class CommercetoolsMock {
643
645
  project(projectKey?: string): ProjectAPI;
644
646
  runServer(port?: number, options?: AppOptions): void;
645
647
  private createApp;
648
+ registerHandlers(server: SetupServer): void;
646
649
  private startServer;
647
650
  }
648
651
 
package/dist/index.js CHANGED
@@ -5994,6 +5994,7 @@ var createRepositories = (storage) => ({
5994
5994
  "my-order": new MyOrderRepository(storage),
5995
5995
  "my-customer": new CustomerRepository(storage),
5996
5996
  "my-payment": new PaymentRepository(storage),
5997
+ "my-shopping-list": new ShoppingListRepository(storage),
5997
5998
  product: new ProductRepository(storage),
5998
5999
  "product-type": new ProductTypeRepository(storage),
5999
6000
  "product-discount": new ProductDiscountRepository(storage),
@@ -6449,6 +6450,7 @@ var MyCustomerService = class extends AbstractService {
6449
6450
  const router = Router3({ mergeParams: true });
6450
6451
  this.extraRoutes(router);
6451
6452
  router.get("", this.getMe.bind(this));
6453
+ router.post("", this.updateMe.bind(this));
6452
6454
  router.post("/signup", this.signUp.bind(this));
6453
6455
  router.post("/login", this.signIn.bind(this));
6454
6456
  parent.use(`/${basePath}`, router);
@@ -6460,6 +6462,21 @@ var MyCustomerService = class extends AbstractService {
6460
6462
  }
6461
6463
  return response.status(200).send(resource);
6462
6464
  }
6465
+ updateMe(request, response) {
6466
+ const resource = this.repository.getMe(getRepositoryContext(request));
6467
+ if (!resource) {
6468
+ return response.status(404).send("Not found");
6469
+ }
6470
+ const updateRequest = request.body;
6471
+ const updatedResource = this.repository.processUpdateActions(
6472
+ getRepositoryContext(request),
6473
+ resource,
6474
+ updateRequest.version,
6475
+ updateRequest.actions
6476
+ );
6477
+ const result = this._expandWithId(request, updatedResource.id);
6478
+ return response.status(200).send(result);
6479
+ }
6463
6480
  signUp(request, response) {
6464
6481
  const draft = request.body;
6465
6482
  const resource = this.repository.create(
@@ -6526,6 +6543,18 @@ var MyPaymentService = class extends AbstractService {
6526
6543
  }
6527
6544
  };
6528
6545
 
6546
+ // src/services/my-shopping-list.ts
6547
+ var MyShoppingListService = class extends AbstractService {
6548
+ repository;
6549
+ constructor(parent, repository) {
6550
+ super(parent);
6551
+ this.repository = repository;
6552
+ }
6553
+ getBasePath() {
6554
+ return "me/shopping-lists";
6555
+ }
6556
+ };
6557
+
6529
6558
  // src/services/order.ts
6530
6559
  var OrderService = class extends AbstractService {
6531
6560
  repository;
@@ -6813,6 +6842,10 @@ var createServices = (router, repos) => ({
6813
6842
  "my-order": new MyOrderService(router, repos["my-order"]),
6814
6843
  "my-customer": new MyCustomerService(router, repos["my-customer"]),
6815
6844
  "my-payment": new MyPaymentService(router, repos["my-payment"]),
6845
+ "my-shopping-list": new MyShoppingListService(
6846
+ router,
6847
+ repos["my-shopping-list"]
6848
+ ),
6816
6849
  "shipping-method": new ShippingMethodService(
6817
6850
  router,
6818
6851
  repos["shipping-method"]
@@ -6884,7 +6917,6 @@ var CommercetoolsMock = class {
6884
6917
  this._mswServer = void 0;
6885
6918
  }
6886
6919
  clear() {
6887
- this._mswServer?.resetHandlers();
6888
6920
  this._storage.clear();
6889
6921
  }
6890
6922
  project(projectKey) {
@@ -6955,22 +6987,17 @@ var CommercetoolsMock = class {
6955
6987
  });
6956
6988
  return app;
6957
6989
  }
6958
- startServer() {
6959
- if (_globalListeners.length > 0) {
6960
- if (this._mswServer !== void 0) {
6961
- throw new Error("Server already started");
6962
- } else {
6963
- console.warn("Server wasn't stopped properly, clearing");
6964
- _globalListeners.forEach((listener) => listener.close());
6965
- }
6966
- }
6967
- const server = this.app;
6968
- this._mswServer = setupServer(
6990
+ // registerHandlers is an alternative way to work with commercetools-mock, it
6991
+ // allows you to manage msw server yourself and register the handlers needed
6992
+ // for commercetools-mock to work.
6993
+ registerHandlers(server) {
6994
+ const app = this.app;
6995
+ server.use(
6969
6996
  http.post(`${this.options.authHost}/oauth/*`, async ({ request }) => {
6970
6997
  const body = await request.text();
6971
6998
  const url = new URL(request.url);
6972
6999
  const headers = copyHeaders(request.headers);
6973
- const res = await inject(server).post(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7000
+ const res = await inject(app).post(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
6974
7001
  return new HttpResponse(res.body, {
6975
7002
  status: res.statusCode,
6976
7003
  headers: mapHeaderType(res.headers)
@@ -6980,7 +7007,7 @@ var CommercetoolsMock = class {
6980
7007
  const body = await request.text();
6981
7008
  const url = new URL(request.url);
6982
7009
  const headers = copyHeaders(request.headers);
6983
- const res = await inject(server).get(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7010
+ const res = await inject(app).get(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
6984
7011
  if (res.statusCode === 200) {
6985
7012
  const parsedBody = JSON.parse(res.body);
6986
7013
  const resultCount = "count" in parsedBody ? parsedBody.count : Object.keys(parsedBody).length;
@@ -6998,7 +7025,7 @@ var CommercetoolsMock = class {
6998
7025
  const body = await request.text();
6999
7026
  const url = new URL(request.url);
7000
7027
  const headers = copyHeaders(request.headers);
7001
- const res = await inject(server).get(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7028
+ const res = await inject(app).get(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7002
7029
  return new HttpResponse(res.body, {
7003
7030
  status: res.statusCode,
7004
7031
  headers: mapHeaderType(res.headers)
@@ -7008,7 +7035,7 @@ var CommercetoolsMock = class {
7008
7035
  const body = await request.text();
7009
7036
  const url = new URL(request.url);
7010
7037
  const headers = copyHeaders(request.headers);
7011
- const res = await inject(server).post(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7038
+ const res = await inject(app).post(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7012
7039
  return new HttpResponse(res.body, {
7013
7040
  status: res.statusCode,
7014
7041
  headers: mapHeaderType(res.headers)
@@ -7018,14 +7045,26 @@ var CommercetoolsMock = class {
7018
7045
  const body = await request.text();
7019
7046
  const url = new URL(request.url);
7020
7047
  const headers = copyHeaders(request.headers);
7021
- const res = await inject(server).delete(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7048
+ const res = await inject(app).delete(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7022
7049
  return new HttpResponse(res.body, {
7023
7050
  status: res.statusCode,
7024
7051
  headers: mapHeaderType(res.headers)
7025
7052
  });
7026
7053
  })
7027
7054
  );
7028
- this._mswServer.listen({
7055
+ }
7056
+ startServer() {
7057
+ if (_globalListeners.length > 0) {
7058
+ if (this._mswServer !== void 0) {
7059
+ throw new Error("Server already started");
7060
+ } else {
7061
+ console.warn("Server wasn't stopped properly, clearing");
7062
+ _globalListeners.forEach((listener) => listener.close());
7063
+ }
7064
+ }
7065
+ const server = setupServer();
7066
+ this.registerHandlers(server);
7067
+ server.listen({
7029
7068
  // We need to allow requests done by supertest
7030
7069
  onUnhandledRequest: (request, print) => {
7031
7070
  const url = new URL(request.url);
@@ -7035,7 +7074,8 @@ var CommercetoolsMock = class {
7035
7074
  print.error();
7036
7075
  }
7037
7076
  });
7038
- _globalListeners.push(this._mswServer);
7077
+ _globalListeners.push(server);
7078
+ this._mswServer = server;
7039
7079
  }
7040
7080
  };
7041
7081
  export {