@labdigital/commercetools-mock 2.10.0 → 2.12.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
@@ -1250,6 +1250,7 @@ import express from "express";
1250
1250
 
1251
1251
  // src/oauth/store.ts
1252
1252
  import { randomBytes } from "crypto";
1253
+ import { v4 as uuidv42 } from "uuid";
1253
1254
  var OAuth2Store = class {
1254
1255
  tokens = [];
1255
1256
  validate = true;
@@ -1261,7 +1262,22 @@ var OAuth2Store = class {
1261
1262
  access_token: randomBytes(16).toString("base64"),
1262
1263
  token_type: "Bearer",
1263
1264
  expires_in: 172800,
1264
- scope: scope || "todo"
1265
+ scope: scope || "todo",
1266
+ refresh_token: `my-project-${randomBytes(16).toString("base64")}`
1267
+ };
1268
+ this.tokens.push(token);
1269
+ return token;
1270
+ }
1271
+ getAnonymousToken(scope, anonymousId) {
1272
+ if (!anonymousId) {
1273
+ anonymousId = uuidv42();
1274
+ }
1275
+ const token = {
1276
+ access_token: randomBytes(16).toString("base64"),
1277
+ token_type: "Bearer",
1278
+ expires_in: 172800,
1279
+ scope: scope ? `${scope} anonymous_id:${anonymousId}` : `anonymous_id:${anonymousId}`,
1280
+ refresh_token: `my-project-${randomBytes(16).toString("base64")}`
1265
1281
  };
1266
1282
  this.tokens.push(token);
1267
1283
  return token;
@@ -1271,11 +1287,29 @@ var OAuth2Store = class {
1271
1287
  access_token: randomBytes(16).toString("base64"),
1272
1288
  token_type: "Bearer",
1273
1289
  expires_in: 172800,
1274
- scope: scope ? `${scope} custome_id:${customerId}` : `customer_id: ${customerId}`
1290
+ scope: scope ? `${scope} customer_id:${customerId}` : `customer_id:${customerId}`,
1291
+ refresh_token: `my-project-${randomBytes(16).toString("base64")}`
1275
1292
  };
1276
1293
  this.tokens.push(token);
1277
1294
  return token;
1278
1295
  }
1296
+ refreshToken(clientId, clientSecret, refreshToken) {
1297
+ const existing = this.tokens.find((t) => t.refresh_token === refreshToken);
1298
+ if (!existing) {
1299
+ return void 0;
1300
+ }
1301
+ const token = {
1302
+ ...existing,
1303
+ access_token: randomBytes(16).toString("base64")
1304
+ };
1305
+ this.tokens.push(token);
1306
+ return {
1307
+ access_token: token.access_token,
1308
+ token_type: token.token_type,
1309
+ expires_in: token.expires_in,
1310
+ scope: token.scope
1311
+ };
1312
+ }
1279
1313
  validateToken(token) {
1280
1314
  if (!this.validate)
1281
1315
  return true;
@@ -1415,11 +1449,36 @@ var OAuth2Server = class {
1415
1449
  );
1416
1450
  return response.status(200).send(token);
1417
1451
  } else if (grantType === "refresh_token") {
1418
- const token = this.store.getClientToken(
1452
+ const refreshToken = request.query.refresh_token?.toString();
1453
+ if (!refreshToken) {
1454
+ return next(
1455
+ new CommercetoolsError(
1456
+ {
1457
+ code: "invalid_request",
1458
+ message: "Missing required parameter: refresh_token."
1459
+ },
1460
+ 400
1461
+ )
1462
+ );
1463
+ }
1464
+ const token = this.store.refreshToken(
1419
1465
  request.credentials.clientId,
1420
1466
  request.credentials.clientSecret,
1421
- request.query.scope?.toString()
1467
+ refreshToken
1422
1468
  );
1469
+ if (!token) {
1470
+ return next(
1471
+ new CommercetoolsError(
1472
+ {
1473
+ statusCode: 400,
1474
+ message: "The refresh token was not found. It may have expired.",
1475
+ error: "invalid_grant",
1476
+ error_description: "The refresh token was not found. It may have expired."
1477
+ },
1478
+ 400
1479
+ )
1480
+ );
1481
+ }
1423
1482
  return response.status(200).send(token);
1424
1483
  } else {
1425
1484
  return next(
@@ -1486,15 +1545,24 @@ var OAuth2Server = class {
1486
1545
  );
1487
1546
  }
1488
1547
  async anonymousTokenHandler(request, response, next) {
1489
- return next(
1490
- new CommercetoolsError(
1491
- {
1492
- code: "invalid_client",
1493
- message: "Not implemented yet in commercetools-mock"
1494
- },
1495
- 401
1496
- )
1497
- );
1548
+ const grantType = request.query.grant_type || request.body.grant_type;
1549
+ if (!grantType) {
1550
+ return next(
1551
+ new CommercetoolsError(
1552
+ {
1553
+ code: "invalid_request",
1554
+ message: "Missing required parameter: grant_type."
1555
+ },
1556
+ 400
1557
+ )
1558
+ );
1559
+ }
1560
+ if (grantType === "client_credentials") {
1561
+ const scope = request.query.scope?.toString() || request.body.scope?.toString();
1562
+ const anonymous_id = void 0;
1563
+ const token = this.store.getAnonymousToken(scope, anonymous_id);
1564
+ return response.status(200).send(token);
1565
+ }
1498
1566
  }
1499
1567
  };
1500
1568
 
@@ -1554,7 +1622,7 @@ var DEFAULT_API_HOSTNAME = "https://api.*.commercetools.com";
1554
1622
  var DEFAULT_AUTH_HOSTNAME = "https://auth.*.commercetools.com";
1555
1623
 
1556
1624
  // src/repositories/helpers.ts
1557
- import { v4 as uuidv42 } from "uuid";
1625
+ import { v4 as uuidv43 } from "uuid";
1558
1626
  var createAddress = (base, projectKey, storage) => {
1559
1627
  if (!base)
1560
1628
  return void 0;
@@ -1592,7 +1660,7 @@ var createCustomFields = (draft, projectKey, storage) => {
1592
1660
  };
1593
1661
  };
1594
1662
  var createPrice = (draft) => ({
1595
- id: uuidv42(),
1663
+ id: uuidv43(),
1596
1664
  value: createTypedMoney(draft.value)
1597
1665
  });
1598
1666
  var createCentPrecisionMoney = (value) => {
@@ -2158,7 +2226,7 @@ var BusinessUnitRepository = class extends AbstractResourceRepository {
2158
2226
  };
2159
2227
 
2160
2228
  // src/repositories/cart.ts
2161
- import { v4 as uuidv43 } from "uuid";
2229
+ import { v4 as uuidv44 } from "uuid";
2162
2230
  var CartRepository = class extends AbstractResourceRepository {
2163
2231
  getTypeId() {
2164
2232
  return "cart";
@@ -2291,7 +2359,7 @@ var CartRepository = class extends AbstractResourceRepository {
2291
2359
  );
2292
2360
  }
2293
2361
  resource.lineItems.push({
2294
- id: uuidv43(),
2362
+ id: uuidv44(),
2295
2363
  productId: product.id,
2296
2364
  productKey: product.key,
2297
2365
  productSlug: product.masterData.current.slug,
@@ -2562,7 +2630,7 @@ var CartRepository = class extends AbstractResourceRepository {
2562
2630
  );
2563
2631
  }
2564
2632
  return {
2565
- id: uuidv43(),
2633
+ id: uuidv44(),
2566
2634
  productId: product.id,
2567
2635
  productKey: product.key,
2568
2636
  productSlug: product.masterData.current.slug,
@@ -2733,14 +2801,14 @@ var CartDiscountRepository = class extends AbstractResourceRepository {
2733
2801
  };
2734
2802
 
2735
2803
  // src/repositories/category.ts
2736
- import { v4 as uuidv44 } from "uuid";
2804
+ import { v4 as uuidv45 } from "uuid";
2737
2805
  var CategoryRepository = class extends AbstractResourceRepository {
2738
2806
  getTypeId() {
2739
2807
  return "category";
2740
2808
  }
2741
2809
  assetFromAssetDraft = (draft, context) => ({
2742
2810
  ...draft,
2743
- id: uuidv44(),
2811
+ id: uuidv45(),
2744
2812
  custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2745
2813
  });
2746
2814
  create(context, draft) {
@@ -2755,7 +2823,7 @@ var CategoryRepository = class extends AbstractResourceRepository {
2755
2823
  ancestors: [],
2756
2824
  // TODO
2757
2825
  assets: draft.assets?.map((d) => ({
2758
- id: uuidv44(),
2826
+ id: uuidv45(),
2759
2827
  name: d.name,
2760
2828
  description: d.description,
2761
2829
  sources: d.sources,
@@ -3751,7 +3819,7 @@ var OrderEditRepository = class extends AbstractResourceRepository {
3751
3819
  };
3752
3820
 
3753
3821
  // src/repositories/payment.ts
3754
- import { v4 as uuidv45 } from "uuid";
3822
+ import { v4 as uuidv46 } from "uuid";
3755
3823
  var PaymentRepository = class extends AbstractResourceRepository {
3756
3824
  getTypeId() {
3757
3825
  return "payment";
@@ -3786,7 +3854,7 @@ var PaymentRepository = class extends AbstractResourceRepository {
3786
3854
  }
3787
3855
  transactionFromTransactionDraft = (draft, context) => ({
3788
3856
  ...draft,
3789
- id: uuidv45(),
3857
+ id: uuidv46(),
3790
3858
  amount: createCentPrecisionMoney(draft.amount),
3791
3859
  custom: createCustomFields(draft.custom, context.projectKey, this._storage),
3792
3860
  state: draft.state ?? "Initial"
@@ -3870,7 +3938,7 @@ var PaymentRepository = class extends AbstractResourceRepository {
3870
3938
  };
3871
3939
 
3872
3940
  // src/repositories/product.ts
3873
- import { v4 as uuidv46 } from "uuid";
3941
+ import { v4 as uuidv47 } from "uuid";
3874
3942
  import deepEqual2 from "deep-equal";
3875
3943
  var ProductRepository = class extends AbstractResourceRepository {
3876
3944
  getTypeId() {
@@ -3976,7 +4044,7 @@ var ProductRepository = class extends AbstractResourceRepository {
3976
4044
  }
3977
4045
  priceFromDraft(context, draft) {
3978
4046
  return {
3979
- id: uuidv46(),
4047
+ id: uuidv47(),
3980
4048
  key: draft.key,
3981
4049
  country: draft.country,
3982
4050
  value: createTypedMoney(draft.value),
@@ -6018,7 +6086,7 @@ var SubscriptionRepository = class extends AbstractResourceRepository {
6018
6086
  };
6019
6087
 
6020
6088
  // src/repositories/tax-category.ts
6021
- import { v4 as uuidv47 } from "uuid";
6089
+ import { v4 as uuidv48 } from "uuid";
6022
6090
  var TaxCategoryRepository = class extends AbstractResourceRepository {
6023
6091
  getTypeId() {
6024
6092
  return "tax-category";
@@ -6034,7 +6102,7 @@ var TaxCategoryRepository = class extends AbstractResourceRepository {
6034
6102
  }
6035
6103
  taxRateFromTaxRateDraft = (draft) => ({
6036
6104
  ...draft,
6037
- id: uuidv47(),
6105
+ id: uuidv48(),
6038
6106
  amount: draft.amount || 0
6039
6107
  });
6040
6108
  actions = {
@@ -6593,7 +6661,7 @@ var CustomerGroupService = class extends AbstractService {
6593
6661
  };
6594
6662
 
6595
6663
  // src/services/customer.ts
6596
- import { v4 as uuidv48 } from "uuid";
6664
+ import { v4 as uuidv49 } from "uuid";
6597
6665
  var CustomerService = class extends AbstractService {
6598
6666
  repository;
6599
6667
  constructor(parent, repository) {
@@ -6617,7 +6685,7 @@ var CustomerService = class extends AbstractService {
6617
6685
  ...rest,
6618
6686
  customerId: customer.results[0].id,
6619
6687
  expiresAt: new Date(Date.now() + ttlMinutes * 60).toISOString(),
6620
- value: uuidv48()
6688
+ value: uuidv49()
6621
6689
  });
6622
6690
  });
6623
6691
  }