@labdigital/commercetools-mock 1.4.0 → 1.6.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.
Files changed (129) hide show
  1. package/README.md +5 -4
  2. package/dist/index.cjs +116 -18
  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 +116 -18
  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 +93 -79
  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 -389
  55. package/src/repositories/payment.ts +155 -155
  56. package/src/repositories/product-discount.ts +149 -149
  57. package/src/repositories/product-projection.ts +116 -52
  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 -129
  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 +492 -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
@@ -2520,6 +2545,12 @@ var CustomerRepository = class extends AbstractResourceRepository {
2520
2545
  },
2521
2546
  400
2522
2547
  );
2548
+ },
2549
+ setCustomField: (_context, resource, { name, value }) => {
2550
+ if (!resource.custom) {
2551
+ throw new Error("Resource has no custom field");
2552
+ }
2553
+ resource.custom.fields[name] = value;
2523
2554
  }
2524
2555
  };
2525
2556
  };
@@ -3028,7 +3059,11 @@ var OrderRepository = class extends AbstractResourceRepository {
3028
3059
  `No state found with key=${state.key} or id=${state.key}`
3029
3060
  );
3030
3061
  }
3031
- resource.state = { typeId: "state", id: resolvedType.id };
3062
+ resource.state = {
3063
+ typeId: "state",
3064
+ id: resolvedType.id,
3065
+ obj: { ...resolvedType, key: state.key ?? "" }
3066
+ };
3032
3067
  },
3033
3068
  setBillingAddress: (context, resource, { address }) => {
3034
3069
  resource.billingAddress = createAddress(
@@ -4235,17 +4270,49 @@ var ProductProjectionRepository = class extends AbstractResourceRepository {
4235
4270
  throw new Error("No valid action");
4236
4271
  }
4237
4272
  query(context, params = {}) {
4238
- const response = this._storage.query(context.projectKey, "product", {
4239
- expand: params.expand,
4240
- where: params.where,
4241
- offset: params.offset,
4242
- limit: params.limit
4273
+ let resources = this._storage.all(context.projectKey, "product").map((r) => this._searchService.transform(r, params.staged ?? false)).filter((p) => {
4274
+ if (!params.staged) {
4275
+ return p.published;
4276
+ }
4277
+ return true;
4243
4278
  });
4279
+ if (params.where) {
4280
+ const variableMap = {};
4281
+ for (const [k, v] of Object.entries(params)) {
4282
+ if (k.startsWith("var.")) {
4283
+ variableMap[k.substring(4)] = v;
4284
+ }
4285
+ }
4286
+ try {
4287
+ const filterFunc = parseQueryExpression(params.where);
4288
+ resources = resources.filter(
4289
+ (resource) => filterFunc(resource, variableMap)
4290
+ );
4291
+ } catch (err) {
4292
+ throw new CommercetoolsError(
4293
+ {
4294
+ code: "InvalidInput",
4295
+ message: err.message
4296
+ },
4297
+ 400
4298
+ );
4299
+ }
4300
+ }
4301
+ if (params.expand !== void 0) {
4302
+ resources = resources.map(
4303
+ (resource) => this._storage.expand(context.projectKey, resource, params.expand)
4304
+ );
4305
+ }
4306
+ const totalResults = resources.length;
4307
+ const offset = params.offset || 0;
4308
+ const limit = params.limit || 20;
4309
+ const results = resources.slice(offset, offset + limit);
4244
4310
  return {
4245
- ...response,
4246
- results: response.results.map(
4247
- (r) => this._searchService.transform(r, false)
4248
- )
4311
+ count: totalResults,
4312
+ total: results.length,
4313
+ offset,
4314
+ limit,
4315
+ results
4249
4316
  };
4250
4317
  }
4251
4318
  search(context, query) {
@@ -5683,6 +5750,18 @@ var ProductProjectionService = class extends AbstractService {
5683
5750
  extraRoutes(router) {
5684
5751
  router.get("/search", this.search.bind(this));
5685
5752
  }
5753
+ get(request, response) {
5754
+ const limit = this._parseParam(request.query.limit);
5755
+ const offset = this._parseParam(request.query.offset);
5756
+ const result = this.repository.query(getRepositoryContext(request), {
5757
+ ...request.query,
5758
+ expand: this._parseParam(request.query.expand),
5759
+ where: this._parseParam(request.query.where),
5760
+ limit: limit !== void 0 ? Number(limit) : void 0,
5761
+ offset: offset !== void 0 ? Number(offset) : void 0
5762
+ });
5763
+ return response.status(200).send(result);
5764
+ }
5686
5765
  search(request, response) {
5687
5766
  const resource = this.repository.search(
5688
5767
  getRepositoryContext(request),
@@ -5816,6 +5895,18 @@ var ZoneService = class extends AbstractService {
5816
5895
  }
5817
5896
  };
5818
5897
 
5898
+ // src/services/attribute-group.ts
5899
+ var AttributeGroupService = class extends AbstractService {
5900
+ repository;
5901
+ constructor(parent, repository) {
5902
+ super(parent);
5903
+ this.repository = repository;
5904
+ }
5905
+ getBasePath() {
5906
+ return "attribute-groups";
5907
+ }
5908
+ };
5909
+
5819
5910
  // src/services/index.ts
5820
5911
  var createServices = (router, repos) => ({
5821
5912
  category: new CategoryServices(router, repos["category"]),
@@ -5836,7 +5927,10 @@ var createServices = (router, repos) => ({
5836
5927
  ),
5837
5928
  order: new OrderService(router, repos["order"]),
5838
5929
  payment: new PaymentService(router, repos["payment"]),
5839
- "standalone-price": new StandAlonePriceService(router, repos["standalone-price"]),
5930
+ "standalone-price": new StandAlonePriceService(
5931
+ router,
5932
+ repos["standalone-price"]
5933
+ ),
5840
5934
  "my-cart": new MyCartService(router, repos["my-cart"]),
5841
5935
  "my-order": new MyOrderService(router, repos["my-order"]),
5842
5936
  "my-customer": new MyCustomerService(router, repos["my-customer"]),
@@ -5860,6 +5954,10 @@ var createServices = (router, repos) => ({
5860
5954
  store: new StoreService(router, repos["store"]),
5861
5955
  subscription: new SubscriptionService(router, repos["subscription"]),
5862
5956
  "tax-category": new TaxCategoryService(router, repos["tax-category"]),
5957
+ "attribute-group": new AttributeGroupService(
5958
+ router,
5959
+ repos["attribute-group"]
5960
+ ),
5863
5961
  type: new TypeService(router, repos["type"]),
5864
5962
  zone: new ZoneService(router, repos["zone"])
5865
5963
  });