@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.cjs +97 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +97 -29
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/exceptions.ts +7 -0
- package/src/oauth/server.test.ts +89 -0
- package/src/oauth/server.ts +54 -11
- package/src/oauth/store.ts +43 -2
package/dist/index.cjs
CHANGED
|
@@ -1287,6 +1287,7 @@ var import_express = __toESM(require("express"), 1);
|
|
|
1287
1287
|
|
|
1288
1288
|
// src/oauth/store.ts
|
|
1289
1289
|
var import_crypto = require("crypto");
|
|
1290
|
+
var import_uuid2 = require("uuid");
|
|
1290
1291
|
var OAuth2Store = class {
|
|
1291
1292
|
tokens = [];
|
|
1292
1293
|
validate = true;
|
|
@@ -1298,7 +1299,22 @@ var OAuth2Store = class {
|
|
|
1298
1299
|
access_token: (0, import_crypto.randomBytes)(16).toString("base64"),
|
|
1299
1300
|
token_type: "Bearer",
|
|
1300
1301
|
expires_in: 172800,
|
|
1301
|
-
scope: scope || "todo"
|
|
1302
|
+
scope: scope || "todo",
|
|
1303
|
+
refresh_token: `my-project-${(0, import_crypto.randomBytes)(16).toString("base64")}`
|
|
1304
|
+
};
|
|
1305
|
+
this.tokens.push(token);
|
|
1306
|
+
return token;
|
|
1307
|
+
}
|
|
1308
|
+
getAnonymousToken(scope, anonymousId) {
|
|
1309
|
+
if (!anonymousId) {
|
|
1310
|
+
anonymousId = (0, import_uuid2.v4)();
|
|
1311
|
+
}
|
|
1312
|
+
const token = {
|
|
1313
|
+
access_token: (0, import_crypto.randomBytes)(16).toString("base64"),
|
|
1314
|
+
token_type: "Bearer",
|
|
1315
|
+
expires_in: 172800,
|
|
1316
|
+
scope: scope ? `${scope} anonymous_id:${anonymousId}` : `anonymous_id:${anonymousId}`,
|
|
1317
|
+
refresh_token: `my-project-${(0, import_crypto.randomBytes)(16).toString("base64")}`
|
|
1302
1318
|
};
|
|
1303
1319
|
this.tokens.push(token);
|
|
1304
1320
|
return token;
|
|
@@ -1308,11 +1324,29 @@ var OAuth2Store = class {
|
|
|
1308
1324
|
access_token: (0, import_crypto.randomBytes)(16).toString("base64"),
|
|
1309
1325
|
token_type: "Bearer",
|
|
1310
1326
|
expires_in: 172800,
|
|
1311
|
-
scope: scope ? `${scope}
|
|
1327
|
+
scope: scope ? `${scope} customer_id:${customerId}` : `customer_id:${customerId}`,
|
|
1328
|
+
refresh_token: `my-project-${(0, import_crypto.randomBytes)(16).toString("base64")}`
|
|
1312
1329
|
};
|
|
1313
1330
|
this.tokens.push(token);
|
|
1314
1331
|
return token;
|
|
1315
1332
|
}
|
|
1333
|
+
refreshToken(clientId, clientSecret, refreshToken) {
|
|
1334
|
+
const existing = this.tokens.find((t) => t.refresh_token === refreshToken);
|
|
1335
|
+
if (!existing) {
|
|
1336
|
+
return void 0;
|
|
1337
|
+
}
|
|
1338
|
+
const token = {
|
|
1339
|
+
...existing,
|
|
1340
|
+
access_token: (0, import_crypto.randomBytes)(16).toString("base64")
|
|
1341
|
+
};
|
|
1342
|
+
this.tokens.push(token);
|
|
1343
|
+
return {
|
|
1344
|
+
access_token: token.access_token,
|
|
1345
|
+
token_type: token.token_type,
|
|
1346
|
+
expires_in: token.expires_in,
|
|
1347
|
+
scope: token.scope
|
|
1348
|
+
};
|
|
1349
|
+
}
|
|
1316
1350
|
validateToken(token) {
|
|
1317
1351
|
if (!this.validate)
|
|
1318
1352
|
return true;
|
|
@@ -1452,11 +1486,36 @@ var OAuth2Server = class {
|
|
|
1452
1486
|
);
|
|
1453
1487
|
return response.status(200).send(token);
|
|
1454
1488
|
} else if (grantType === "refresh_token") {
|
|
1455
|
-
const
|
|
1489
|
+
const refreshToken = request.query.refresh_token?.toString();
|
|
1490
|
+
if (!refreshToken) {
|
|
1491
|
+
return next(
|
|
1492
|
+
new CommercetoolsError(
|
|
1493
|
+
{
|
|
1494
|
+
code: "invalid_request",
|
|
1495
|
+
message: "Missing required parameter: refresh_token."
|
|
1496
|
+
},
|
|
1497
|
+
400
|
|
1498
|
+
)
|
|
1499
|
+
);
|
|
1500
|
+
}
|
|
1501
|
+
const token = this.store.refreshToken(
|
|
1456
1502
|
request.credentials.clientId,
|
|
1457
1503
|
request.credentials.clientSecret,
|
|
1458
|
-
|
|
1504
|
+
refreshToken
|
|
1459
1505
|
);
|
|
1506
|
+
if (!token) {
|
|
1507
|
+
return next(
|
|
1508
|
+
new CommercetoolsError(
|
|
1509
|
+
{
|
|
1510
|
+
statusCode: 400,
|
|
1511
|
+
message: "The refresh token was not found. It may have expired.",
|
|
1512
|
+
error: "invalid_grant",
|
|
1513
|
+
error_description: "The refresh token was not found. It may have expired."
|
|
1514
|
+
},
|
|
1515
|
+
400
|
|
1516
|
+
)
|
|
1517
|
+
);
|
|
1518
|
+
}
|
|
1460
1519
|
return response.status(200).send(token);
|
|
1461
1520
|
} else {
|
|
1462
1521
|
return next(
|
|
@@ -1523,15 +1582,24 @@ var OAuth2Server = class {
|
|
|
1523
1582
|
);
|
|
1524
1583
|
}
|
|
1525
1584
|
async anonymousTokenHandler(request, response, next) {
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1585
|
+
const grantType = request.query.grant_type || request.body.grant_type;
|
|
1586
|
+
if (!grantType) {
|
|
1587
|
+
return next(
|
|
1588
|
+
new CommercetoolsError(
|
|
1589
|
+
{
|
|
1590
|
+
code: "invalid_request",
|
|
1591
|
+
message: "Missing required parameter: grant_type."
|
|
1592
|
+
},
|
|
1593
|
+
400
|
|
1594
|
+
)
|
|
1595
|
+
);
|
|
1596
|
+
}
|
|
1597
|
+
if (grantType === "client_credentials") {
|
|
1598
|
+
const scope = request.query.scope?.toString() || request.body.scope?.toString();
|
|
1599
|
+
const anonymous_id = void 0;
|
|
1600
|
+
const token = this.store.getAnonymousToken(scope, anonymous_id);
|
|
1601
|
+
return response.status(200).send(token);
|
|
1602
|
+
}
|
|
1535
1603
|
}
|
|
1536
1604
|
};
|
|
1537
1605
|
|
|
@@ -1591,7 +1659,7 @@ var DEFAULT_API_HOSTNAME = "https://api.*.commercetools.com";
|
|
|
1591
1659
|
var DEFAULT_AUTH_HOSTNAME = "https://auth.*.commercetools.com";
|
|
1592
1660
|
|
|
1593
1661
|
// src/repositories/helpers.ts
|
|
1594
|
-
var
|
|
1662
|
+
var import_uuid3 = require("uuid");
|
|
1595
1663
|
var createAddress = (base, projectKey, storage) => {
|
|
1596
1664
|
if (!base)
|
|
1597
1665
|
return void 0;
|
|
@@ -1629,7 +1697,7 @@ var createCustomFields = (draft, projectKey, storage) => {
|
|
|
1629
1697
|
};
|
|
1630
1698
|
};
|
|
1631
1699
|
var createPrice = (draft) => ({
|
|
1632
|
-
id: (0,
|
|
1700
|
+
id: (0, import_uuid3.v4)(),
|
|
1633
1701
|
value: createTypedMoney(draft.value)
|
|
1634
1702
|
});
|
|
1635
1703
|
var createCentPrecisionMoney = (value) => {
|
|
@@ -2195,7 +2263,7 @@ var BusinessUnitRepository = class extends AbstractResourceRepository {
|
|
|
2195
2263
|
};
|
|
2196
2264
|
|
|
2197
2265
|
// src/repositories/cart.ts
|
|
2198
|
-
var
|
|
2266
|
+
var import_uuid4 = require("uuid");
|
|
2199
2267
|
var CartRepository = class extends AbstractResourceRepository {
|
|
2200
2268
|
getTypeId() {
|
|
2201
2269
|
return "cart";
|
|
@@ -2328,7 +2396,7 @@ var CartRepository = class extends AbstractResourceRepository {
|
|
|
2328
2396
|
);
|
|
2329
2397
|
}
|
|
2330
2398
|
resource.lineItems.push({
|
|
2331
|
-
id: (0,
|
|
2399
|
+
id: (0, import_uuid4.v4)(),
|
|
2332
2400
|
productId: product.id,
|
|
2333
2401
|
productKey: product.key,
|
|
2334
2402
|
productSlug: product.masterData.current.slug,
|
|
@@ -2599,7 +2667,7 @@ var CartRepository = class extends AbstractResourceRepository {
|
|
|
2599
2667
|
);
|
|
2600
2668
|
}
|
|
2601
2669
|
return {
|
|
2602
|
-
id: (0,
|
|
2670
|
+
id: (0, import_uuid4.v4)(),
|
|
2603
2671
|
productId: product.id,
|
|
2604
2672
|
productKey: product.key,
|
|
2605
2673
|
productSlug: product.masterData.current.slug,
|
|
@@ -2770,14 +2838,14 @@ var CartDiscountRepository = class extends AbstractResourceRepository {
|
|
|
2770
2838
|
};
|
|
2771
2839
|
|
|
2772
2840
|
// src/repositories/category.ts
|
|
2773
|
-
var
|
|
2841
|
+
var import_uuid5 = require("uuid");
|
|
2774
2842
|
var CategoryRepository = class extends AbstractResourceRepository {
|
|
2775
2843
|
getTypeId() {
|
|
2776
2844
|
return "category";
|
|
2777
2845
|
}
|
|
2778
2846
|
assetFromAssetDraft = (draft, context) => ({
|
|
2779
2847
|
...draft,
|
|
2780
|
-
id: (0,
|
|
2848
|
+
id: (0, import_uuid5.v4)(),
|
|
2781
2849
|
custom: createCustomFields(draft.custom, context.projectKey, this._storage)
|
|
2782
2850
|
});
|
|
2783
2851
|
create(context, draft) {
|
|
@@ -2792,7 +2860,7 @@ var CategoryRepository = class extends AbstractResourceRepository {
|
|
|
2792
2860
|
ancestors: [],
|
|
2793
2861
|
// TODO
|
|
2794
2862
|
assets: draft.assets?.map((d) => ({
|
|
2795
|
-
id: (0,
|
|
2863
|
+
id: (0, import_uuid5.v4)(),
|
|
2796
2864
|
name: d.name,
|
|
2797
2865
|
description: d.description,
|
|
2798
2866
|
sources: d.sources,
|
|
@@ -3788,7 +3856,7 @@ var OrderEditRepository = class extends AbstractResourceRepository {
|
|
|
3788
3856
|
};
|
|
3789
3857
|
|
|
3790
3858
|
// src/repositories/payment.ts
|
|
3791
|
-
var
|
|
3859
|
+
var import_uuid6 = require("uuid");
|
|
3792
3860
|
var PaymentRepository = class extends AbstractResourceRepository {
|
|
3793
3861
|
getTypeId() {
|
|
3794
3862
|
return "payment";
|
|
@@ -3823,7 +3891,7 @@ var PaymentRepository = class extends AbstractResourceRepository {
|
|
|
3823
3891
|
}
|
|
3824
3892
|
transactionFromTransactionDraft = (draft, context) => ({
|
|
3825
3893
|
...draft,
|
|
3826
|
-
id: (0,
|
|
3894
|
+
id: (0, import_uuid6.v4)(),
|
|
3827
3895
|
amount: createCentPrecisionMoney(draft.amount),
|
|
3828
3896
|
custom: createCustomFields(draft.custom, context.projectKey, this._storage),
|
|
3829
3897
|
state: draft.state ?? "Initial"
|
|
@@ -3907,7 +3975,7 @@ var PaymentRepository = class extends AbstractResourceRepository {
|
|
|
3907
3975
|
};
|
|
3908
3976
|
|
|
3909
3977
|
// src/repositories/product.ts
|
|
3910
|
-
var
|
|
3978
|
+
var import_uuid7 = require("uuid");
|
|
3911
3979
|
var import_deep_equal2 = __toESM(require("deep-equal"), 1);
|
|
3912
3980
|
var ProductRepository = class extends AbstractResourceRepository {
|
|
3913
3981
|
getTypeId() {
|
|
@@ -4013,7 +4081,7 @@ var ProductRepository = class extends AbstractResourceRepository {
|
|
|
4013
4081
|
}
|
|
4014
4082
|
priceFromDraft(context, draft) {
|
|
4015
4083
|
return {
|
|
4016
|
-
id: (0,
|
|
4084
|
+
id: (0, import_uuid7.v4)(),
|
|
4017
4085
|
key: draft.key,
|
|
4018
4086
|
country: draft.country,
|
|
4019
4087
|
value: createTypedMoney(draft.value),
|
|
@@ -6055,7 +6123,7 @@ var SubscriptionRepository = class extends AbstractResourceRepository {
|
|
|
6055
6123
|
};
|
|
6056
6124
|
|
|
6057
6125
|
// src/repositories/tax-category.ts
|
|
6058
|
-
var
|
|
6126
|
+
var import_uuid8 = require("uuid");
|
|
6059
6127
|
var TaxCategoryRepository = class extends AbstractResourceRepository {
|
|
6060
6128
|
getTypeId() {
|
|
6061
6129
|
return "tax-category";
|
|
@@ -6071,7 +6139,7 @@ var TaxCategoryRepository = class extends AbstractResourceRepository {
|
|
|
6071
6139
|
}
|
|
6072
6140
|
taxRateFromTaxRateDraft = (draft) => ({
|
|
6073
6141
|
...draft,
|
|
6074
|
-
id: (0,
|
|
6142
|
+
id: (0, import_uuid8.v4)(),
|
|
6075
6143
|
amount: draft.amount || 0
|
|
6076
6144
|
});
|
|
6077
6145
|
actions = {
|
|
@@ -6630,7 +6698,7 @@ var CustomerGroupService = class extends AbstractService {
|
|
|
6630
6698
|
};
|
|
6631
6699
|
|
|
6632
6700
|
// src/services/customer.ts
|
|
6633
|
-
var
|
|
6701
|
+
var import_uuid9 = require("uuid");
|
|
6634
6702
|
var CustomerService = class extends AbstractService {
|
|
6635
6703
|
repository;
|
|
6636
6704
|
constructor(parent, repository) {
|
|
@@ -6654,7 +6722,7 @@ var CustomerService = class extends AbstractService {
|
|
|
6654
6722
|
...rest,
|
|
6655
6723
|
customerId: customer.results[0].id,
|
|
6656
6724
|
expiresAt: new Date(Date.now() + ttlMinutes * 60).toISOString(),
|
|
6657
|
-
value: (0,
|
|
6725
|
+
value: (0, import_uuid9.v4)()
|
|
6658
6726
|
});
|
|
6659
6727
|
});
|
|
6660
6728
|
}
|