@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.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // src/ctMock.ts
2
2
  import express2 from "express";
3
- import supertest from "supertest";
4
3
  import morgan from "morgan";
4
+ import inject from "light-my-request";
5
5
  import { setupServer } from "msw/node";
6
6
  import { http, HttpResponse } from "msw";
7
7
 
@@ -66,6 +66,18 @@ var queryParamsValue = (value) => {
66
66
  return void 0;
67
67
  };
68
68
  var cloneObject = (o) => JSON.parse(JSON.stringify(o));
69
+ var mapHeaderType = (outgoingHttpHeaders) => {
70
+ const headersInit = {};
71
+ for (const key in outgoingHttpHeaders) {
72
+ const value = outgoingHttpHeaders[key];
73
+ if (Array.isArray(value)) {
74
+ headersInit[key] = value.join(", ");
75
+ } else if (value !== void 0) {
76
+ headersInit[key] = value.toString();
77
+ }
78
+ }
79
+ return headersInit;
80
+ };
69
81
 
70
82
  // src/lib/expandParser.ts
71
83
  var parseExpandClause = (clause) => {
@@ -3758,20 +3770,63 @@ var ProductRepository = class extends AbstractResourceRepository {
3758
3770
  id: draft.productType.id || ""
3759
3771
  };
3760
3772
  }
3773
+ const categoryReferences = [];
3774
+ draft.categories?.forEach((category) => {
3775
+ if (category) {
3776
+ categoryReferences.push(
3777
+ getReferenceFromResourceIdentifier(
3778
+ category,
3779
+ context.projectKey,
3780
+ this._storage
3781
+ )
3782
+ );
3783
+ } else {
3784
+ throw new CommercetoolsError(
3785
+ {
3786
+ code: "InvalidJsonInput",
3787
+ message: "Request body does not contain valid JSON.",
3788
+ detailedErrorMessage: "categories: JSON object expected."
3789
+ },
3790
+ 400
3791
+ );
3792
+ }
3793
+ });
3794
+ let taxCategoryReference = void 0;
3795
+ if (draft.taxCategory) {
3796
+ taxCategoryReference = getReferenceFromResourceIdentifier(
3797
+ draft.taxCategory,
3798
+ context.projectKey,
3799
+ this._storage
3800
+ );
3801
+ }
3802
+ let productStateReference = void 0;
3803
+ if (draft.state) {
3804
+ productStateReference = getReferenceFromResourceIdentifier(
3805
+ draft.state,
3806
+ context.projectKey,
3807
+ this._storage
3808
+ );
3809
+ }
3761
3810
  const productData = {
3762
3811
  name: draft.name,
3763
3812
  slug: draft.slug,
3764
- categories: [],
3813
+ description: draft.description,
3814
+ categories: categoryReferences,
3765
3815
  masterVariant: variantFromDraft(1, draft.masterVariant),
3766
3816
  variants: draft.variants?.map(
3767
3817
  (variant, index) => variantFromDraft(index + 2, variant)
3768
3818
  ) ?? [],
3819
+ metaTitle: draft.metaTitle,
3820
+ metaDescription: draft.metaDescription,
3821
+ metaKeywords: draft.metaKeywords,
3769
3822
  searchKeywords: draft.searchKeywords ?? {}
3770
3823
  };
3771
3824
  const resource = {
3772
3825
  ...getBaseResourceProperties(),
3773
3826
  key: draft.key,
3774
3827
  productType,
3828
+ taxCategory: taxCategoryReference,
3829
+ state: productStateReference,
3775
3830
  masterData: {
3776
3831
  current: productData,
3777
3832
  staged: productData,
@@ -3963,7 +4018,7 @@ var ProductRepository = class extends AbstractResourceRepository {
3963
4018
  return resource;
3964
4019
  },
3965
4020
  addPrice: (context, resource, { variantId, sku, price, staged }) => {
3966
- const addVariantPrice = (data) => {
4021
+ const addVariantPrice = (data, priceToAdd2) => {
3967
4022
  const { variant, isMasterVariant, variantIndex } = getVariant(
3968
4023
  data,
3969
4024
  variantId,
@@ -3975,9 +4030,9 @@ var ProductRepository = class extends AbstractResourceRepository {
3975
4030
  );
3976
4031
  }
3977
4032
  if (variant.prices === void 0) {
3978
- variant.prices = [priceFromDraft(price)];
4033
+ variant.prices = [priceToAdd2];
3979
4034
  } else {
3980
- variant.prices.push(priceFromDraft(price));
4035
+ variant.prices.push(priceToAdd2);
3981
4036
  }
3982
4037
  if (isMasterVariant) {
3983
4038
  data.masterVariant = variant;
@@ -3985,10 +4040,11 @@ var ProductRepository = class extends AbstractResourceRepository {
3985
4040
  data.variants[variantIndex] = variant;
3986
4041
  }
3987
4042
  };
4043
+ const priceToAdd = priceFromDraft(price);
3988
4044
  const onlyStaged = staged !== void 0 ? staged : true;
3989
- addVariantPrice(resource.masterData.staged);
4045
+ addVariantPrice(resource.masterData.staged, priceToAdd);
3990
4046
  if (!onlyStaged) {
3991
- addVariantPrice(resource.masterData.current);
4047
+ addVariantPrice(resource.masterData.current, priceToAdd);
3992
4048
  }
3993
4049
  checkForStagedChanges(resource);
3994
4050
  return resource;
@@ -4069,21 +4125,270 @@ var ProductRepository = class extends AbstractResourceRepository {
4069
4125
  }
4070
4126
  checkForStagedChanges(resource);
4071
4127
  return resource;
4128
+ },
4129
+ changeName: (context, resource, { name, staged }) => {
4130
+ const onlyStaged = staged !== void 0 ? staged : true;
4131
+ resource.masterData.staged.name = name;
4132
+ if (!onlyStaged) {
4133
+ resource.masterData.current.name = name;
4134
+ }
4135
+ checkForStagedChanges(resource);
4136
+ return resource;
4137
+ },
4138
+ changeSlug: (context, resource, { slug, staged }) => {
4139
+ const onlyStaged = staged !== void 0 ? staged : true;
4140
+ resource.masterData.staged.slug = slug;
4141
+ if (!onlyStaged) {
4142
+ resource.masterData.current.slug = slug;
4143
+ }
4144
+ checkForStagedChanges(resource);
4145
+ return resource;
4146
+ },
4147
+ setMetaTitle: (context, resource, { metaTitle, staged }) => {
4148
+ const onlyStaged = staged !== void 0 ? staged : true;
4149
+ resource.masterData.staged.metaTitle = metaTitle;
4150
+ if (!onlyStaged) {
4151
+ resource.masterData.current.metaTitle = metaTitle;
4152
+ }
4153
+ checkForStagedChanges(resource);
4154
+ return resource;
4155
+ },
4156
+ setMetaDescription: (context, resource, { metaDescription, staged }) => {
4157
+ const onlyStaged = staged !== void 0 ? staged : true;
4158
+ resource.masterData.staged.metaDescription = metaDescription;
4159
+ if (!onlyStaged) {
4160
+ resource.masterData.current.metaDescription = metaDescription;
4161
+ }
4162
+ checkForStagedChanges(resource);
4163
+ return resource;
4164
+ },
4165
+ setMetaKeywords: (context, resource, { metaKeywords, staged }) => {
4166
+ const onlyStaged = staged !== void 0 ? staged : true;
4167
+ resource.masterData.staged.metaKeywords = metaKeywords;
4168
+ if (!onlyStaged) {
4169
+ resource.masterData.current.metaKeywords = metaKeywords;
4170
+ }
4171
+ checkForStagedChanges(resource);
4172
+ return resource;
4173
+ },
4174
+ addVariant: (context, resource, {
4175
+ sku,
4176
+ key,
4177
+ prices,
4178
+ images,
4179
+ attributes,
4180
+ staged,
4181
+ assets
4182
+ }) => {
4183
+ const variantDraft = {
4184
+ sku,
4185
+ key,
4186
+ prices,
4187
+ images,
4188
+ attributes,
4189
+ assets
4190
+ };
4191
+ const dataStaged = resource.masterData.staged;
4192
+ const allVariants = [
4193
+ dataStaged.masterVariant,
4194
+ ...dataStaged.variants ?? []
4195
+ ];
4196
+ const maxId = allVariants.reduce(
4197
+ (max, element) => element.id > max ? element.id : max,
4198
+ 0
4199
+ );
4200
+ const variant = variantFromDraft(maxId + 1, variantDraft);
4201
+ dataStaged.variants.push(variant);
4202
+ const onlyStaged = staged !== void 0 ? staged : true;
4203
+ if (!onlyStaged) {
4204
+ resource.masterData.current.variants.push(variant);
4205
+ }
4206
+ checkForStagedChanges(resource);
4207
+ return resource;
4208
+ },
4209
+ removeVariant: (context, resource, { id, sku, staged }) => {
4210
+ const removeVariant = (data) => {
4211
+ const { variant, isMasterVariant, variantIndex } = getVariant(
4212
+ data,
4213
+ id,
4214
+ sku
4215
+ );
4216
+ if (!variant) {
4217
+ throw new Error(
4218
+ `Variant with id ${id} or sku ${sku} not found on product ${resource.id}`
4219
+ );
4220
+ }
4221
+ if (isMasterVariant) {
4222
+ throw new Error(
4223
+ `Can not remove the variant [ID:${id}] for [Product:${resource.id}] since it's the master variant`
4224
+ );
4225
+ }
4226
+ data.variants.splice(variantIndex, 1);
4227
+ };
4228
+ const onlyStaged = staged !== void 0 ? staged : true;
4229
+ removeVariant(resource.masterData.staged);
4230
+ if (!onlyStaged) {
4231
+ removeVariant(resource.masterData.current);
4232
+ }
4233
+ checkForStagedChanges(resource);
4234
+ return resource;
4235
+ },
4236
+ changeMasterVariant: (context, resource, { variantId, sku, staged }) => {
4237
+ const setMaster = (data) => {
4238
+ const { variant, isMasterVariant, variantIndex } = getVariant(
4239
+ data,
4240
+ variantId,
4241
+ sku
4242
+ );
4243
+ if (!variant) {
4244
+ throw new Error(
4245
+ `Variant with id ${variantId} or sku ${sku} not found on product ${resource.id}`
4246
+ );
4247
+ }
4248
+ if (!isMasterVariant) {
4249
+ const masterVariantPrev = data.masterVariant;
4250
+ data.masterVariant = variant;
4251
+ data.variants.splice(variantIndex, 1);
4252
+ data.variants.push(masterVariantPrev);
4253
+ }
4254
+ };
4255
+ const onlyStaged = staged !== void 0 ? staged : true;
4256
+ setMaster(resource.masterData.staged);
4257
+ if (!onlyStaged) {
4258
+ setMaster(resource.masterData.current);
4259
+ }
4260
+ checkForStagedChanges(resource);
4261
+ return resource;
4262
+ },
4263
+ setTaxCategory: (context, resource, { taxCategory }) => {
4264
+ let taxCategoryReference = void 0;
4265
+ if (taxCategory) {
4266
+ taxCategoryReference = getReferenceFromResourceIdentifier(
4267
+ taxCategory,
4268
+ context.projectKey,
4269
+ this._storage
4270
+ );
4271
+ } else {
4272
+ throw new CommercetoolsError(
4273
+ {
4274
+ code: "InvalidJsonInput",
4275
+ message: "Request body does not contain valid JSON.",
4276
+ detailedErrorMessage: "actions -> taxCategory: Missing required value"
4277
+ },
4278
+ 400
4279
+ );
4280
+ }
4281
+ resource.taxCategory = taxCategoryReference;
4282
+ return resource;
4283
+ },
4284
+ addToCategory: (context, resource, { category, staged, orderHint }) => {
4285
+ const addCategory = (data) => {
4286
+ if (category) {
4287
+ data.categories.push(
4288
+ getReferenceFromResourceIdentifier(
4289
+ category,
4290
+ context.projectKey,
4291
+ this._storage
4292
+ )
4293
+ );
4294
+ } else {
4295
+ throw new CommercetoolsError(
4296
+ {
4297
+ code: "InvalidJsonInput",
4298
+ message: "Request body does not contain valid JSON.",
4299
+ detailedErrorMessage: "actions -> category: Missing required value"
4300
+ },
4301
+ 400
4302
+ );
4303
+ }
4304
+ };
4305
+ const onlyStaged = staged !== void 0 ? staged : true;
4306
+ addCategory(resource.masterData.staged);
4307
+ if (!onlyStaged) {
4308
+ addCategory(resource.masterData.current);
4309
+ }
4310
+ checkForStagedChanges(resource);
4311
+ return resource;
4312
+ },
4313
+ removeFromCategory: (context, resource, { category, staged }) => {
4314
+ const removeCategory = (data) => {
4315
+ if (category) {
4316
+ const resolvedCategory = getReferenceFromResourceIdentifier(
4317
+ category,
4318
+ context.projectKey,
4319
+ this._storage
4320
+ );
4321
+ const foundCategory = data.categories.find(
4322
+ (productCategory) => {
4323
+ if (productCategory.id == resolvedCategory.id) {
4324
+ return productCategory;
4325
+ }
4326
+ return false;
4327
+ }
4328
+ );
4329
+ if (!foundCategory) {
4330
+ throw new CommercetoolsError(
4331
+ {
4332
+ code: "InvalidOperation",
4333
+ message: `Cannot remove from category '${resolvedCategory.id}' because product '${resource.masterData.current.name}' is not in that category.`
4334
+ },
4335
+ 400
4336
+ );
4337
+ }
4338
+ data.categories = data.categories.filter(
4339
+ (productCategory) => {
4340
+ if (productCategory.id == resolvedCategory.id) {
4341
+ return false;
4342
+ }
4343
+ return true;
4344
+ }
4345
+ );
4346
+ } else {
4347
+ throw new CommercetoolsError(
4348
+ {
4349
+ code: "InvalidJsonInput",
4350
+ message: "Request body does not contain valid JSON.",
4351
+ detailedErrorMessage: "actions -> category: Missing required value"
4352
+ },
4353
+ 400
4354
+ );
4355
+ }
4356
+ };
4357
+ const onlyStaged = staged !== void 0 ? staged : true;
4358
+ removeCategory(resource.masterData.staged);
4359
+ if (!onlyStaged) {
4360
+ removeCategory(resource.masterData.current);
4361
+ }
4362
+ checkForStagedChanges(resource);
4363
+ return resource;
4364
+ },
4365
+ transitionState: (context, resource, { state, force }) => {
4366
+ let productStateReference = void 0;
4367
+ if (state) {
4368
+ productStateReference = getReferenceFromResourceIdentifier(
4369
+ state,
4370
+ context.projectKey,
4371
+ this._storage
4372
+ );
4373
+ resource.state = productStateReference;
4374
+ } else {
4375
+ throw new CommercetoolsError(
4376
+ {
4377
+ code: "InvalidJsonInput",
4378
+ message: "Request body does not contain valid JSON.",
4379
+ detailedErrorMessage: "actions -> state: Missing required value"
4380
+ },
4381
+ 400
4382
+ );
4383
+ }
4384
+ return resource;
4072
4385
  }
4073
- // 'changeName': () => {},
4074
- // 'changeSlug': () => {},
4075
- // 'addVariant': () => {},
4076
- // 'removeVariant': () => {},
4077
- // 'changeMasterVariant': () => {},
4078
4386
  // 'setPrices': () => {},
4079
4387
  // 'setProductPriceCustomType': () => {},
4080
4388
  // 'setProductPriceCustomField': () => {},
4081
4389
  // 'setDiscountedPrice': () => {},
4082
4390
  // 'setAttributeInAllVariants': () => {},
4083
- // 'addToCategory': () => {},
4084
4391
  // 'setCategoryOrderHint': () => {},
4085
- // 'removeFromCategory': () => {},
4086
- // 'setTaxCategory': () => {},
4087
4392
  // 'setSku': () => {},
4088
4393
  // 'setProductVariantKey': () => {},
4089
4394
  // 'setImageLabel': () => {},
@@ -4098,12 +4403,8 @@ var ProductRepository = class extends AbstractResourceRepository {
4098
4403
  // 'setAssetCustomType': () => {},
4099
4404
  // 'setAssetCustomField': () => {},
4100
4405
  // 'setSearchKeywords': () => {},
4101
- // 'setMetaTitle': () => {},
4102
- // 'setMetaDescription': () => {},
4103
- // 'setMetaKeywords': () => {},
4104
4406
  // 'revertStagedChanges': () => {},
4105
4407
  // 'revertStagedVariantChanges': () => {},
4106
- // 'transitionState': () => {},
4107
4408
  };
4108
4409
  };
4109
4410
  var checkForStagedChanges = (product) => {
@@ -4137,6 +4438,7 @@ var getVariant = (productData, variantId, sku) => {
4137
4438
  var variantFromDraft = (variantId, variant) => ({
4138
4439
  id: variantId,
4139
4440
  sku: variant?.sku,
4441
+ key: variant?.key,
4140
4442
  attributes: variant?.attributes ?? [],
4141
4443
  prices: variant?.prices?.map(priceFromDraft),
4142
4444
  assets: [],
@@ -4144,6 +4446,7 @@ var variantFromDraft = (variantId, variant) => ({
4144
4446
  });
4145
4447
  var priceFromDraft = (draft) => ({
4146
4448
  id: uuidv46(),
4449
+ key: draft.key,
4147
4450
  country: draft.country,
4148
4451
  value: createTypedMoney(draft.value)
4149
4452
  });
@@ -6604,42 +6907,46 @@ var CommercetoolsMock = class {
6604
6907
  _globalListeners.forEach((listener) => listener.close());
6605
6908
  }
6606
6909
  }
6607
- const app = this.app;
6910
+ const server = this.app;
6608
6911
  this._mswServer = setupServer(
6609
6912
  http.post(`${this.options.authHost}/oauth/*`, async ({ request }) => {
6610
- const text = await request.text();
6913
+ const body = await request.text();
6611
6914
  const url = new URL(request.url);
6612
- const res = await supertest(app).post(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(text);
6613
- return new HttpResponse(res.text, {
6614
- status: res.status,
6615
- headers: res.headers
6915
+ const headers = copyHeaders(request.headers);
6916
+ const res = await inject(server).post(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
6917
+ return new HttpResponse(res.body, {
6918
+ status: res.statusCode,
6919
+ headers: mapHeaderType(res.headers)
6616
6920
  });
6617
6921
  }),
6618
6922
  http.get(`${this.options.apiHost}/*`, async ({ request }) => {
6619
6923
  const body = await request.text();
6620
6924
  const url = new URL(request.url);
6621
- const res = await supertest(app).get(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(body);
6622
- return new HttpResponse(res.text, {
6623
- status: res.status,
6624
- headers: res.headers
6925
+ const headers = copyHeaders(request.headers);
6926
+ const res = await inject(server).get(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
6927
+ return new HttpResponse(res.body, {
6928
+ status: res.statusCode,
6929
+ headers: mapHeaderType(res.headers)
6625
6930
  });
6626
6931
  }),
6627
6932
  http.post(`${this.options.apiHost}/*`, async ({ request }) => {
6628
6933
  const body = await request.text();
6629
6934
  const url = new URL(request.url);
6630
- const res = await supertest(app).post(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(body);
6631
- return new HttpResponse(res.text, {
6632
- status: res.status,
6633
- headers: res.headers
6935
+ const headers = copyHeaders(request.headers);
6936
+ const res = await inject(server).post(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
6937
+ return new HttpResponse(res.body, {
6938
+ status: res.statusCode,
6939
+ headers: mapHeaderType(res.headers)
6634
6940
  });
6635
6941
  }),
6636
6942
  http.delete(`${this.options.apiHost}/*`, async ({ request }) => {
6637
6943
  const body = await request.text();
6638
6944
  const url = new URL(request.url);
6639
- const res = await supertest(app).delete(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(body);
6640
- return new HttpResponse(res.text, {
6641
- status: res.status,
6642
- headers: res.headers
6945
+ const headers = copyHeaders(request.headers);
6946
+ const res = await inject(server).delete(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
6947
+ return new HttpResponse(res.body, {
6948
+ status: res.statusCode,
6949
+ headers: mapHeaderType(res.headers)
6643
6950
  });
6644
6951
  })
6645
6952
  );