@labdigital/commercetools-mock 2.1.0 → 2.3.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
@@ -37,8 +37,8 @@ module.exports = __toCommonJS(src_exports);
37
37
 
38
38
  // src/ctMock.ts
39
39
  var import_express6 = __toESM(require("express"), 1);
40
- var import_supertest = __toESM(require("supertest"), 1);
41
40
  var import_morgan = __toESM(require("morgan"), 1);
41
+ var import_light_my_request = __toESM(require("light-my-request"), 1);
42
42
  var import_node = require("msw/node");
43
43
  var import_msw = require("msw");
44
44
 
@@ -103,6 +103,18 @@ var queryParamsValue = (value) => {
103
103
  return void 0;
104
104
  };
105
105
  var cloneObject = (o) => JSON.parse(JSON.stringify(o));
106
+ var mapHeaderType = (outgoingHttpHeaders) => {
107
+ const headersInit = {};
108
+ for (const key in outgoingHttpHeaders) {
109
+ const value = outgoingHttpHeaders[key];
110
+ if (Array.isArray(value)) {
111
+ headersInit[key] = value.join(", ");
112
+ } else if (value !== void 0) {
113
+ headersInit[key] = value.toString();
114
+ }
115
+ }
116
+ return headersInit;
117
+ };
106
118
 
107
119
  // src/lib/expandParser.ts
108
120
  var parseExpandClause = (clause) => {
@@ -3795,20 +3807,63 @@ var ProductRepository = class extends AbstractResourceRepository {
3795
3807
  id: draft.productType.id || ""
3796
3808
  };
3797
3809
  }
3810
+ const categoryReferences = [];
3811
+ draft.categories?.forEach((category) => {
3812
+ if (category) {
3813
+ categoryReferences.push(
3814
+ getReferenceFromResourceIdentifier(
3815
+ category,
3816
+ context.projectKey,
3817
+ this._storage
3818
+ )
3819
+ );
3820
+ } else {
3821
+ throw new CommercetoolsError(
3822
+ {
3823
+ code: "InvalidJsonInput",
3824
+ message: "Request body does not contain valid JSON.",
3825
+ detailedErrorMessage: "categories: JSON object expected."
3826
+ },
3827
+ 400
3828
+ );
3829
+ }
3830
+ });
3831
+ let taxCategoryReference = void 0;
3832
+ if (draft.taxCategory) {
3833
+ taxCategoryReference = getReferenceFromResourceIdentifier(
3834
+ draft.taxCategory,
3835
+ context.projectKey,
3836
+ this._storage
3837
+ );
3838
+ }
3839
+ let productStateReference = void 0;
3840
+ if (draft.state) {
3841
+ productStateReference = getReferenceFromResourceIdentifier(
3842
+ draft.state,
3843
+ context.projectKey,
3844
+ this._storage
3845
+ );
3846
+ }
3798
3847
  const productData = {
3799
3848
  name: draft.name,
3800
3849
  slug: draft.slug,
3801
- categories: [],
3850
+ description: draft.description,
3851
+ categories: categoryReferences,
3802
3852
  masterVariant: variantFromDraft(1, draft.masterVariant),
3803
3853
  variants: draft.variants?.map(
3804
3854
  (variant, index) => variantFromDraft(index + 2, variant)
3805
3855
  ) ?? [],
3856
+ metaTitle: draft.metaTitle,
3857
+ metaDescription: draft.metaDescription,
3858
+ metaKeywords: draft.metaKeywords,
3806
3859
  searchKeywords: draft.searchKeywords ?? {}
3807
3860
  };
3808
3861
  const resource = {
3809
3862
  ...getBaseResourceProperties(),
3810
3863
  key: draft.key,
3811
3864
  productType,
3865
+ taxCategory: taxCategoryReference,
3866
+ state: productStateReference,
3812
3867
  masterData: {
3813
3868
  current: productData,
3814
3869
  staged: productData,
@@ -4000,7 +4055,7 @@ var ProductRepository = class extends AbstractResourceRepository {
4000
4055
  return resource;
4001
4056
  },
4002
4057
  addPrice: (context, resource, { variantId, sku, price, staged }) => {
4003
- const addVariantPrice = (data) => {
4058
+ const addVariantPrice = (data, priceToAdd2) => {
4004
4059
  const { variant, isMasterVariant, variantIndex } = getVariant(
4005
4060
  data,
4006
4061
  variantId,
@@ -4012,9 +4067,9 @@ var ProductRepository = class extends AbstractResourceRepository {
4012
4067
  );
4013
4068
  }
4014
4069
  if (variant.prices === void 0) {
4015
- variant.prices = [priceFromDraft(price)];
4070
+ variant.prices = [priceToAdd2];
4016
4071
  } else {
4017
- variant.prices.push(priceFromDraft(price));
4072
+ variant.prices.push(priceToAdd2);
4018
4073
  }
4019
4074
  if (isMasterVariant) {
4020
4075
  data.masterVariant = variant;
@@ -4022,10 +4077,11 @@ var ProductRepository = class extends AbstractResourceRepository {
4022
4077
  data.variants[variantIndex] = variant;
4023
4078
  }
4024
4079
  };
4080
+ const priceToAdd = priceFromDraft(price);
4025
4081
  const onlyStaged = staged !== void 0 ? staged : true;
4026
- addVariantPrice(resource.masterData.staged);
4082
+ addVariantPrice(resource.masterData.staged, priceToAdd);
4027
4083
  if (!onlyStaged) {
4028
- addVariantPrice(resource.masterData.current);
4084
+ addVariantPrice(resource.masterData.current, priceToAdd);
4029
4085
  }
4030
4086
  checkForStagedChanges(resource);
4031
4087
  return resource;
@@ -4106,21 +4162,270 @@ var ProductRepository = class extends AbstractResourceRepository {
4106
4162
  }
4107
4163
  checkForStagedChanges(resource);
4108
4164
  return resource;
4165
+ },
4166
+ changeName: (context, resource, { name, staged }) => {
4167
+ const onlyStaged = staged !== void 0 ? staged : true;
4168
+ resource.masterData.staged.name = name;
4169
+ if (!onlyStaged) {
4170
+ resource.masterData.current.name = name;
4171
+ }
4172
+ checkForStagedChanges(resource);
4173
+ return resource;
4174
+ },
4175
+ changeSlug: (context, resource, { slug, staged }) => {
4176
+ const onlyStaged = staged !== void 0 ? staged : true;
4177
+ resource.masterData.staged.slug = slug;
4178
+ if (!onlyStaged) {
4179
+ resource.masterData.current.slug = slug;
4180
+ }
4181
+ checkForStagedChanges(resource);
4182
+ return resource;
4183
+ },
4184
+ setMetaTitle: (context, resource, { metaTitle, staged }) => {
4185
+ const onlyStaged = staged !== void 0 ? staged : true;
4186
+ resource.masterData.staged.metaTitle = metaTitle;
4187
+ if (!onlyStaged) {
4188
+ resource.masterData.current.metaTitle = metaTitle;
4189
+ }
4190
+ checkForStagedChanges(resource);
4191
+ return resource;
4192
+ },
4193
+ setMetaDescription: (context, resource, { metaDescription, staged }) => {
4194
+ const onlyStaged = staged !== void 0 ? staged : true;
4195
+ resource.masterData.staged.metaDescription = metaDescription;
4196
+ if (!onlyStaged) {
4197
+ resource.masterData.current.metaDescription = metaDescription;
4198
+ }
4199
+ checkForStagedChanges(resource);
4200
+ return resource;
4201
+ },
4202
+ setMetaKeywords: (context, resource, { metaKeywords, staged }) => {
4203
+ const onlyStaged = staged !== void 0 ? staged : true;
4204
+ resource.masterData.staged.metaKeywords = metaKeywords;
4205
+ if (!onlyStaged) {
4206
+ resource.masterData.current.metaKeywords = metaKeywords;
4207
+ }
4208
+ checkForStagedChanges(resource);
4209
+ return resource;
4210
+ },
4211
+ addVariant: (context, resource, {
4212
+ sku,
4213
+ key,
4214
+ prices,
4215
+ images,
4216
+ attributes,
4217
+ staged,
4218
+ assets
4219
+ }) => {
4220
+ const variantDraft = {
4221
+ sku,
4222
+ key,
4223
+ prices,
4224
+ images,
4225
+ attributes,
4226
+ assets
4227
+ };
4228
+ const dataStaged = resource.masterData.staged;
4229
+ const allVariants = [
4230
+ dataStaged.masterVariant,
4231
+ ...dataStaged.variants ?? []
4232
+ ];
4233
+ const maxId = allVariants.reduce(
4234
+ (max, element) => element.id > max ? element.id : max,
4235
+ 0
4236
+ );
4237
+ const variant = variantFromDraft(maxId + 1, variantDraft);
4238
+ dataStaged.variants.push(variant);
4239
+ const onlyStaged = staged !== void 0 ? staged : true;
4240
+ if (!onlyStaged) {
4241
+ resource.masterData.current.variants.push(variant);
4242
+ }
4243
+ checkForStagedChanges(resource);
4244
+ return resource;
4245
+ },
4246
+ removeVariant: (context, resource, { id, sku, staged }) => {
4247
+ const removeVariant = (data) => {
4248
+ const { variant, isMasterVariant, variantIndex } = getVariant(
4249
+ data,
4250
+ id,
4251
+ sku
4252
+ );
4253
+ if (!variant) {
4254
+ throw new Error(
4255
+ `Variant with id ${id} or sku ${sku} not found on product ${resource.id}`
4256
+ );
4257
+ }
4258
+ if (isMasterVariant) {
4259
+ throw new Error(
4260
+ `Can not remove the variant [ID:${id}] for [Product:${resource.id}] since it's the master variant`
4261
+ );
4262
+ }
4263
+ data.variants.splice(variantIndex, 1);
4264
+ };
4265
+ const onlyStaged = staged !== void 0 ? staged : true;
4266
+ removeVariant(resource.masterData.staged);
4267
+ if (!onlyStaged) {
4268
+ removeVariant(resource.masterData.current);
4269
+ }
4270
+ checkForStagedChanges(resource);
4271
+ return resource;
4272
+ },
4273
+ changeMasterVariant: (context, resource, { variantId, sku, staged }) => {
4274
+ const setMaster = (data) => {
4275
+ const { variant, isMasterVariant, variantIndex } = getVariant(
4276
+ data,
4277
+ variantId,
4278
+ sku
4279
+ );
4280
+ if (!variant) {
4281
+ throw new Error(
4282
+ `Variant with id ${variantId} or sku ${sku} not found on product ${resource.id}`
4283
+ );
4284
+ }
4285
+ if (!isMasterVariant) {
4286
+ const masterVariantPrev = data.masterVariant;
4287
+ data.masterVariant = variant;
4288
+ data.variants.splice(variantIndex, 1);
4289
+ data.variants.push(masterVariantPrev);
4290
+ }
4291
+ };
4292
+ const onlyStaged = staged !== void 0 ? staged : true;
4293
+ setMaster(resource.masterData.staged);
4294
+ if (!onlyStaged) {
4295
+ setMaster(resource.masterData.current);
4296
+ }
4297
+ checkForStagedChanges(resource);
4298
+ return resource;
4299
+ },
4300
+ setTaxCategory: (context, resource, { taxCategory }) => {
4301
+ let taxCategoryReference = void 0;
4302
+ if (taxCategory) {
4303
+ taxCategoryReference = getReferenceFromResourceIdentifier(
4304
+ taxCategory,
4305
+ context.projectKey,
4306
+ this._storage
4307
+ );
4308
+ } else {
4309
+ throw new CommercetoolsError(
4310
+ {
4311
+ code: "InvalidJsonInput",
4312
+ message: "Request body does not contain valid JSON.",
4313
+ detailedErrorMessage: "actions -> taxCategory: Missing required value"
4314
+ },
4315
+ 400
4316
+ );
4317
+ }
4318
+ resource.taxCategory = taxCategoryReference;
4319
+ return resource;
4320
+ },
4321
+ addToCategory: (context, resource, { category, staged, orderHint }) => {
4322
+ const addCategory = (data) => {
4323
+ if (category) {
4324
+ data.categories.push(
4325
+ getReferenceFromResourceIdentifier(
4326
+ category,
4327
+ context.projectKey,
4328
+ this._storage
4329
+ )
4330
+ );
4331
+ } else {
4332
+ throw new CommercetoolsError(
4333
+ {
4334
+ code: "InvalidJsonInput",
4335
+ message: "Request body does not contain valid JSON.",
4336
+ detailedErrorMessage: "actions -> category: Missing required value"
4337
+ },
4338
+ 400
4339
+ );
4340
+ }
4341
+ };
4342
+ const onlyStaged = staged !== void 0 ? staged : true;
4343
+ addCategory(resource.masterData.staged);
4344
+ if (!onlyStaged) {
4345
+ addCategory(resource.masterData.current);
4346
+ }
4347
+ checkForStagedChanges(resource);
4348
+ return resource;
4349
+ },
4350
+ removeFromCategory: (context, resource, { category, staged }) => {
4351
+ const removeCategory = (data) => {
4352
+ if (category) {
4353
+ const resolvedCategory = getReferenceFromResourceIdentifier(
4354
+ category,
4355
+ context.projectKey,
4356
+ this._storage
4357
+ );
4358
+ const foundCategory = data.categories.find(
4359
+ (productCategory) => {
4360
+ if (productCategory.id == resolvedCategory.id) {
4361
+ return productCategory;
4362
+ }
4363
+ return false;
4364
+ }
4365
+ );
4366
+ if (!foundCategory) {
4367
+ throw new CommercetoolsError(
4368
+ {
4369
+ code: "InvalidOperation",
4370
+ message: `Cannot remove from category '${resolvedCategory.id}' because product '${resource.masterData.current.name}' is not in that category.`
4371
+ },
4372
+ 400
4373
+ );
4374
+ }
4375
+ data.categories = data.categories.filter(
4376
+ (productCategory) => {
4377
+ if (productCategory.id == resolvedCategory.id) {
4378
+ return false;
4379
+ }
4380
+ return true;
4381
+ }
4382
+ );
4383
+ } else {
4384
+ throw new CommercetoolsError(
4385
+ {
4386
+ code: "InvalidJsonInput",
4387
+ message: "Request body does not contain valid JSON.",
4388
+ detailedErrorMessage: "actions -> category: Missing required value"
4389
+ },
4390
+ 400
4391
+ );
4392
+ }
4393
+ };
4394
+ const onlyStaged = staged !== void 0 ? staged : true;
4395
+ removeCategory(resource.masterData.staged);
4396
+ if (!onlyStaged) {
4397
+ removeCategory(resource.masterData.current);
4398
+ }
4399
+ checkForStagedChanges(resource);
4400
+ return resource;
4401
+ },
4402
+ transitionState: (context, resource, { state, force }) => {
4403
+ let productStateReference = void 0;
4404
+ if (state) {
4405
+ productStateReference = getReferenceFromResourceIdentifier(
4406
+ state,
4407
+ context.projectKey,
4408
+ this._storage
4409
+ );
4410
+ resource.state = productStateReference;
4411
+ } else {
4412
+ throw new CommercetoolsError(
4413
+ {
4414
+ code: "InvalidJsonInput",
4415
+ message: "Request body does not contain valid JSON.",
4416
+ detailedErrorMessage: "actions -> state: Missing required value"
4417
+ },
4418
+ 400
4419
+ );
4420
+ }
4421
+ return resource;
4109
4422
  }
4110
- // 'changeName': () => {},
4111
- // 'changeSlug': () => {},
4112
- // 'addVariant': () => {},
4113
- // 'removeVariant': () => {},
4114
- // 'changeMasterVariant': () => {},
4115
4423
  // 'setPrices': () => {},
4116
4424
  // 'setProductPriceCustomType': () => {},
4117
4425
  // 'setProductPriceCustomField': () => {},
4118
4426
  // 'setDiscountedPrice': () => {},
4119
4427
  // 'setAttributeInAllVariants': () => {},
4120
- // 'addToCategory': () => {},
4121
4428
  // 'setCategoryOrderHint': () => {},
4122
- // 'removeFromCategory': () => {},
4123
- // 'setTaxCategory': () => {},
4124
4429
  // 'setSku': () => {},
4125
4430
  // 'setProductVariantKey': () => {},
4126
4431
  // 'setImageLabel': () => {},
@@ -4135,12 +4440,8 @@ var ProductRepository = class extends AbstractResourceRepository {
4135
4440
  // 'setAssetCustomType': () => {},
4136
4441
  // 'setAssetCustomField': () => {},
4137
4442
  // 'setSearchKeywords': () => {},
4138
- // 'setMetaTitle': () => {},
4139
- // 'setMetaDescription': () => {},
4140
- // 'setMetaKeywords': () => {},
4141
4443
  // 'revertStagedChanges': () => {},
4142
4444
  // 'revertStagedVariantChanges': () => {},
4143
- // 'transitionState': () => {},
4144
4445
  };
4145
4446
  };
4146
4447
  var checkForStagedChanges = (product) => {
@@ -4174,6 +4475,7 @@ var getVariant = (productData, variantId, sku) => {
4174
4475
  var variantFromDraft = (variantId, variant) => ({
4175
4476
  id: variantId,
4176
4477
  sku: variant?.sku,
4478
+ key: variant?.key,
4177
4479
  attributes: variant?.attributes ?? [],
4178
4480
  prices: variant?.prices?.map(priceFromDraft),
4179
4481
  assets: [],
@@ -4181,6 +4483,7 @@ var variantFromDraft = (variantId, variant) => ({
4181
4483
  });
4182
4484
  var priceFromDraft = (draft) => ({
4183
4485
  id: (0, import_uuid6.v4)(),
4486
+ key: draft.key,
4184
4487
  country: draft.country,
4185
4488
  value: createTypedMoney(draft.value)
4186
4489
  });
@@ -6641,42 +6944,46 @@ var CommercetoolsMock = class {
6641
6944
  _globalListeners.forEach((listener) => listener.close());
6642
6945
  }
6643
6946
  }
6644
- const app = this.app;
6947
+ const server = this.app;
6645
6948
  this._mswServer = (0, import_node.setupServer)(
6646
6949
  import_msw.http.post(`${this.options.authHost}/oauth/*`, async ({ request }) => {
6647
- const text = await request.text();
6950
+ const body = await request.text();
6648
6951
  const url = new URL(request.url);
6649
- const res = await (0, import_supertest.default)(app).post(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(text);
6650
- return new import_msw.HttpResponse(res.text, {
6651
- status: res.status,
6652
- headers: res.headers
6952
+ const headers = copyHeaders(request.headers);
6953
+ const res = await (0, import_light_my_request.default)(server).post(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
6954
+ return new import_msw.HttpResponse(res.body, {
6955
+ status: res.statusCode,
6956
+ headers: mapHeaderType(res.headers)
6653
6957
  });
6654
6958
  }),
6655
6959
  import_msw.http.get(`${this.options.apiHost}/*`, async ({ request }) => {
6656
6960
  const body = await request.text();
6657
6961
  const url = new URL(request.url);
6658
- const res = await (0, import_supertest.default)(app).get(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(body);
6659
- return new import_msw.HttpResponse(res.text, {
6660
- status: res.status,
6661
- headers: res.headers
6962
+ const headers = copyHeaders(request.headers);
6963
+ const res = await (0, import_light_my_request.default)(server).get(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
6964
+ return new import_msw.HttpResponse(res.body, {
6965
+ status: res.statusCode,
6966
+ headers: mapHeaderType(res.headers)
6662
6967
  });
6663
6968
  }),
6664
6969
  import_msw.http.post(`${this.options.apiHost}/*`, async ({ request }) => {
6665
6970
  const body = await request.text();
6666
6971
  const url = new URL(request.url);
6667
- const res = await (0, import_supertest.default)(app).post(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(body);
6668
- return new import_msw.HttpResponse(res.text, {
6669
- status: res.status,
6670
- headers: res.headers
6972
+ const headers = copyHeaders(request.headers);
6973
+ const res = await (0, import_light_my_request.default)(server).post(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
6974
+ return new import_msw.HttpResponse(res.body, {
6975
+ status: res.statusCode,
6976
+ headers: mapHeaderType(res.headers)
6671
6977
  });
6672
6978
  }),
6673
6979
  import_msw.http.delete(`${this.options.apiHost}/*`, async ({ request }) => {
6674
6980
  const body = await request.text();
6675
6981
  const url = new URL(request.url);
6676
- const res = await (0, import_supertest.default)(app).delete(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(body);
6677
- return new import_msw.HttpResponse(res.text, {
6678
- status: res.status,
6679
- headers: res.headers
6982
+ const headers = copyHeaders(request.headers);
6983
+ const res = await (0, import_light_my_request.default)(server).delete(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
6984
+ return new import_msw.HttpResponse(res.body, {
6985
+ status: res.statusCode,
6986
+ headers: mapHeaderType(res.headers)
6680
6987
  });
6681
6988
  })
6682
6989
  );