@labdigital/commercetools-mock 1.5.0 → 1.6.1

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.
Files changed (129) hide show
  1. package/README.md +5 -4
  2. package/dist/index.cjs +117 -17
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +30 -7
  5. package/dist/index.d.ts +30 -7
  6. package/dist/index.js +117 -17
  7. package/dist/index.js.map +1 -1
  8. package/package.json +4 -3
  9. package/src/constants.ts +2 -2
  10. package/src/ctMock.ts +176 -176
  11. package/src/exceptions.ts +10 -10
  12. package/src/helpers.ts +26 -26
  13. package/src/index.test.ts +173 -173
  14. package/src/index.ts +3 -3
  15. package/src/lib/expandParser.ts +19 -19
  16. package/src/lib/haversine.test.ts +13 -13
  17. package/src/lib/haversine.ts +14 -14
  18. package/src/lib/masking.ts +15 -15
  19. package/src/lib/parser.ts +2 -2
  20. package/src/lib/predicateParser.test.ts +204 -204
  21. package/src/lib/predicateParser.ts +398 -398
  22. package/src/lib/projectionSearchFilter.test.ts +168 -168
  23. package/src/lib/projectionSearchFilter.ts +272 -269
  24. package/src/lib/proxy.ts +8 -8
  25. package/src/oauth/errors.ts +4 -4
  26. package/src/oauth/helpers.ts +6 -6
  27. package/src/oauth/server.ts +103 -101
  28. package/src/oauth/store.ts +27 -27
  29. package/src/priceSelector.test.ts +68 -68
  30. package/src/priceSelector.ts +70 -70
  31. package/src/product-projection-search.ts +296 -296
  32. package/src/projectAPI.test.ts +3 -3
  33. package/src/projectAPI.ts +46 -46
  34. package/src/repositories/abstract.ts +190 -190
  35. package/src/repositories/associate-role.ts +10 -7
  36. package/src/repositories/attribute-group.ts +63 -8
  37. package/src/repositories/business-unit.ts +10 -7
  38. package/src/repositories/cart-discount.ts +134 -134
  39. package/src/repositories/cart.ts +517 -514
  40. package/src/repositories/category.ts +170 -167
  41. package/src/repositories/channel.ts +114 -111
  42. package/src/repositories/custom-object.ts +66 -63
  43. package/src/repositories/customer-group.ts +72 -69
  44. package/src/repositories/customer.ts +90 -90
  45. package/src/repositories/discount-code.ts +171 -168
  46. package/src/repositories/errors.ts +15 -15
  47. package/src/repositories/extension.ts +79 -76
  48. package/src/repositories/helpers.ts +180 -180
  49. package/src/repositories/index.ts +39 -39
  50. package/src/repositories/inventory-entry.ts +98 -95
  51. package/src/repositories/my-order.ts +11 -11
  52. package/src/repositories/order-edit.ts +29 -29
  53. package/src/repositories/order.test.ts +191 -191
  54. package/src/repositories/order.ts +393 -393
  55. package/src/repositories/payment.ts +155 -155
  56. package/src/repositories/product-discount.ts +149 -149
  57. package/src/repositories/product-projection.ts +135 -50
  58. package/src/repositories/product-selection.ts +31 -31
  59. package/src/repositories/product-type.ts +156 -156
  60. package/src/repositories/product.ts +600 -597
  61. package/src/repositories/project.ts +136 -135
  62. package/src/repositories/quote-request.ts +19 -19
  63. package/src/repositories/quote.ts +19 -19
  64. package/src/repositories/review.ts +24 -24
  65. package/src/repositories/shipping-method.ts +217 -217
  66. package/src/repositories/shopping-list.ts +49 -49
  67. package/src/repositories/staged-quote.ts +20 -20
  68. package/src/repositories/standalone-price.ts +72 -61
  69. package/src/repositories/state.ts +84 -84
  70. package/src/repositories/store.ts +114 -114
  71. package/src/repositories/subscription.ts +40 -40
  72. package/src/repositories/tax-category.ts +98 -98
  73. package/src/repositories/type.ts +157 -157
  74. package/src/repositories/zone.ts +71 -71
  75. package/src/server.ts +2 -2
  76. package/src/services/abstract.ts +173 -173
  77. package/src/services/attribute-group.ts +16 -0
  78. package/src/services/cart-discount.ts +8 -8
  79. package/src/services/cart.test.ts +409 -409
  80. package/src/services/cart.ts +50 -50
  81. package/src/services/category.test.ts +25 -25
  82. package/src/services/category.ts +8 -8
  83. package/src/services/channel.ts +8 -8
  84. package/src/services/custom-object.test.ts +184 -184
  85. package/src/services/custom-object.ts +48 -48
  86. package/src/services/customer-group.ts +8 -8
  87. package/src/services/customer.test.ts +151 -151
  88. package/src/services/customer.ts +27 -27
  89. package/src/services/discount-code.ts +8 -8
  90. package/src/services/extension.ts +8 -8
  91. package/src/services/index.ts +52 -44
  92. package/src/services/inventory-entry.test.ts +162 -162
  93. package/src/services/inventory-entry.ts +8 -8
  94. package/src/services/my-cart.test.ts +78 -78
  95. package/src/services/my-cart.ts +28 -28
  96. package/src/services/my-customer.test.ts +44 -44
  97. package/src/services/my-customer.ts +53 -53
  98. package/src/services/my-order.ts +20 -20
  99. package/src/services/my-payment.test.ts +65 -65
  100. package/src/services/my-payment.ts +8 -8
  101. package/src/services/order.test.ts +527 -527
  102. package/src/services/order.ts +31 -31
  103. package/src/services/payment.test.ts +65 -65
  104. package/src/services/payment.ts +8 -8
  105. package/src/services/product-discount.ts +8 -8
  106. package/src/services/product-projection.test.ts +502 -428
  107. package/src/services/product-projection.ts +32 -18
  108. package/src/services/product-type.test.ts +56 -56
  109. package/src/services/product-type.ts +8 -8
  110. package/src/services/product.test.ts +510 -510
  111. package/src/services/product.ts +8 -8
  112. package/src/services/project.ts +34 -34
  113. package/src/services/shipping-method.test.ts +81 -81
  114. package/src/services/shipping-method.ts +12 -12
  115. package/src/services/shopping-list.ts +8 -8
  116. package/src/services/standalone-price.test.ts +256 -256
  117. package/src/services/standalone-price.ts +8 -8
  118. package/src/services/state.test.ts +42 -42
  119. package/src/services/state.ts +8 -8
  120. package/src/services/store.test.ts +57 -57
  121. package/src/services/store.ts +8 -8
  122. package/src/services/subscription.ts +8 -8
  123. package/src/services/tax-category.test.ts +61 -61
  124. package/src/services/tax-category.ts +8 -8
  125. package/src/services/type.ts +8 -8
  126. package/src/services/zone.ts +8 -8
  127. package/src/storage/abstract.ts +58 -58
  128. package/src/storage/in-memory.ts +419 -419
  129. package/src/types.ts +82 -82
package/README.md CHANGED
@@ -14,7 +14,8 @@ yarn add --dev @labdigital/commercetools-mock
14
14
  ```
15
15
 
16
16
  ## Docker image
17
- This codebase is also available as a docker image where it provides a runnable http server exposing the mocked endpoints. See
17
+
18
+ This codebase is also available as a docker image where it provides a runnable http server exposing the mocked endpoints. See
18
19
  https://github.com/labd/commercetools-mock-server
19
20
 
20
21
  ## Example
@@ -70,10 +71,10 @@ Implement the following:
70
71
  - Add new service to src/storage.ts InMemoryStorage
71
72
  - Adjust src/types.ts RepositoryMap and possibly serviceTypes
72
73
 
73
-
74
74
  ## Releasing
75
+
75
76
  This codebases use [@changesets](https://github.com/changesets/changesets) for release and version management
77
+
76
78
  - Create a new changeset using `pnpm changeset`
77
- - Push the changes to the `main` branch.
79
+ - Push the changes to the `main` branch.
78
80
  - GitHub actions will create a release PR. When the release is ready merge the release branch
79
-
package/dist/index.cjs CHANGED
@@ -231,10 +231,13 @@ var TokenTypes = class {
231
231
  }
232
232
  peek(source, position) {
233
233
  const s = source.substr(position);
234
- return first(this.tokenTypes.filter((tt) => tt.enabled), (tt) => {
235
- tt.regex.lastIndex = 0;
236
- return tt.regex.exec(s);
237
- });
234
+ return first(
235
+ this.tokenTypes.filter((tt) => tt.enabled),
236
+ (tt) => {
237
+ tt.regex.lastIndex = 0;
238
+ return tt.regex.exec(s);
239
+ }
240
+ );
238
241
  }
239
242
  token(type, pattern, skip = false) {
240
243
  this.tokenTypes.push({
@@ -679,7 +682,7 @@ var validateSymbol = (val) => {
679
682
  };
680
683
  var resolveSymbol = (val, vars) => {
681
684
  if (val.type === "var") {
682
- if (!(val.value in vars)) {
685
+ if (!(val.value in (vars ?? {}))) {
683
686
  throw new PredicateError(`Missing parameter value for ${val.value}`);
684
687
  }
685
688
  return vars[val.value];
@@ -776,7 +779,7 @@ var generateMatchFunc = (predicate) => {
776
779
  return (obj, vars) => {
777
780
  const value = resolveValue(obj, left);
778
781
  if (value) {
779
- return expr(value);
782
+ return expr(value, vars);
780
783
  }
781
784
  return false;
782
785
  };
@@ -1753,8 +1756,30 @@ var AttributeGroupRepository = class extends AbstractResourceRepository {
1753
1756
  return "attribute-group";
1754
1757
  }
1755
1758
  create(context, draft) {
1756
- throw new Error("Method not implemented.");
1759
+ const resource = {
1760
+ ...getBaseResourceProperties(),
1761
+ name: draft.name,
1762
+ description: draft.description,
1763
+ key: draft.key,
1764
+ attributes: draft.attributes
1765
+ };
1766
+ this.saveNew(context, resource);
1767
+ return resource;
1757
1768
  }
1769
+ actions = {
1770
+ setAttributes: (_context, resource, { attributes }) => {
1771
+ resource.attributes = attributes;
1772
+ },
1773
+ changeName: (_context, resource, { name }) => {
1774
+ resource.name = name;
1775
+ },
1776
+ setDescription: (_context, resource, { description }) => {
1777
+ resource.description = description;
1778
+ },
1779
+ setKey: (_context, resource, { key }) => {
1780
+ resource.key = key;
1781
+ }
1782
+ };
1758
1783
  };
1759
1784
 
1760
1785
  // src/repositories/business-unit.ts
@@ -4244,18 +4269,62 @@ var ProductProjectionRepository = class extends AbstractResourceRepository {
4244
4269
  create(context, draft) {
4245
4270
  throw new Error("No valid action");
4246
4271
  }
4272
+ get(context, id, params = {}) {
4273
+ const resource = this._storage.get(
4274
+ context.projectKey,
4275
+ "product",
4276
+ id,
4277
+ params
4278
+ );
4279
+ if (resource) {
4280
+ return this._searchService.transform(resource, false);
4281
+ }
4282
+ return null;
4283
+ }
4247
4284
  query(context, params = {}) {
4248
- const response = this._storage.query(context.projectKey, "product", {
4249
- expand: params.expand,
4250
- where: params.where,
4251
- offset: params.offset,
4252
- limit: params.limit
4285
+ let resources = this._storage.all(context.projectKey, "product").map((r) => this._searchService.transform(r, params.staged ?? false)).filter((p) => {
4286
+ if (!params.staged) {
4287
+ return p.published;
4288
+ }
4289
+ return true;
4253
4290
  });
4291
+ if (params.where) {
4292
+ const variableMap = {};
4293
+ for (const [k, v] of Object.entries(params)) {
4294
+ if (k.startsWith("var.")) {
4295
+ variableMap[k.substring(4)] = v;
4296
+ }
4297
+ }
4298
+ try {
4299
+ const filterFunc = parseQueryExpression(params.where);
4300
+ resources = resources.filter(
4301
+ (resource) => filterFunc(resource, variableMap)
4302
+ );
4303
+ } catch (err) {
4304
+ throw new CommercetoolsError(
4305
+ {
4306
+ code: "InvalidInput",
4307
+ message: err.message
4308
+ },
4309
+ 400
4310
+ );
4311
+ }
4312
+ }
4313
+ if (params.expand !== void 0) {
4314
+ resources = resources.map(
4315
+ (resource) => this._storage.expand(context.projectKey, resource, params.expand)
4316
+ );
4317
+ }
4318
+ const totalResults = resources.length;
4319
+ const offset = params.offset || 0;
4320
+ const limit = params.limit || 20;
4321
+ const results = resources.slice(offset, offset + limit);
4254
4322
  return {
4255
- ...response,
4256
- results: response.results.map(
4257
- (r) => this._searchService.transform(r, false)
4258
- )
4323
+ count: totalResults,
4324
+ total: results.length,
4325
+ offset,
4326
+ limit,
4327
+ results
4259
4328
  };
4260
4329
  }
4261
4330
  search(context, query) {
@@ -5693,6 +5762,18 @@ var ProductProjectionService = class extends AbstractService {
5693
5762
  extraRoutes(router) {
5694
5763
  router.get("/search", this.search.bind(this));
5695
5764
  }
5765
+ get(request, response) {
5766
+ const limit = this._parseParam(request.query.limit);
5767
+ const offset = this._parseParam(request.query.offset);
5768
+ const result = this.repository.query(getRepositoryContext(request), {
5769
+ ...request.query,
5770
+ expand: this._parseParam(request.query.expand),
5771
+ where: this._parseParam(request.query.where),
5772
+ limit: limit !== void 0 ? Number(limit) : void 0,
5773
+ offset: offset !== void 0 ? Number(offset) : void 0
5774
+ });
5775
+ return response.status(200).send(result);
5776
+ }
5696
5777
  search(request, response) {
5697
5778
  const resource = this.repository.search(
5698
5779
  getRepositoryContext(request),
@@ -5826,6 +5907,18 @@ var ZoneService = class extends AbstractService {
5826
5907
  }
5827
5908
  };
5828
5909
 
5910
+ // src/services/attribute-group.ts
5911
+ var AttributeGroupService = class extends AbstractService {
5912
+ repository;
5913
+ constructor(parent, repository) {
5914
+ super(parent);
5915
+ this.repository = repository;
5916
+ }
5917
+ getBasePath() {
5918
+ return "attribute-groups";
5919
+ }
5920
+ };
5921
+
5829
5922
  // src/services/index.ts
5830
5923
  var createServices = (router, repos) => ({
5831
5924
  category: new CategoryServices(router, repos["category"]),
@@ -5846,7 +5939,10 @@ var createServices = (router, repos) => ({
5846
5939
  ),
5847
5940
  order: new OrderService(router, repos["order"]),
5848
5941
  payment: new PaymentService(router, repos["payment"]),
5849
- "standalone-price": new StandAlonePriceService(router, repos["standalone-price"]),
5942
+ "standalone-price": new StandAlonePriceService(
5943
+ router,
5944
+ repos["standalone-price"]
5945
+ ),
5850
5946
  "my-cart": new MyCartService(router, repos["my-cart"]),
5851
5947
  "my-order": new MyOrderService(router, repos["my-order"]),
5852
5948
  "my-customer": new MyCustomerService(router, repos["my-customer"]),
@@ -5870,6 +5966,10 @@ var createServices = (router, repos) => ({
5870
5966
  store: new StoreService(router, repos["store"]),
5871
5967
  subscription: new SubscriptionService(router, repos["subscription"]),
5872
5968
  "tax-category": new TaxCategoryService(router, repos["tax-category"]),
5969
+ "attribute-group": new AttributeGroupService(
5970
+ router,
5971
+ repos["attribute-group"]
5972
+ ),
5873
5973
  type: new TypeService(router, repos["type"]),
5874
5974
  zone: new ZoneService(router, repos["zone"])
5875
5975
  });