@labdigital/commercetools-mock 2.11.0 → 2.12.1

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.d.cts CHANGED
@@ -3,6 +3,30 @@ import { SetupServer } from 'msw/node';
3
3
  import * as ctp from '@commercetools/platform-sdk';
4
4
  import { Project, ResourceIdentifier, QueryParam, AssociateRoleDraft, AssociateRole, AssociateRoleSetNameAction, AssociateRoleSetPermissionsAction, AssociateRoleChangeBuyerAssignableAction, AssociateRoleSetCustomFieldAction, AssociateRoleAddPermissionAction, AssociateRoleRemovePermissionAction, AttributeGroupDraft, AttributeGroup, AttributeGroupSetAttributesAction, AttributeGroupChangeNameAction, AttributeGroupSetDescriptionAction, AttributeGroupSetKeyAction, BusinessUnitDraft, BusinessUnit, BusinessUnitAddAddressAction, BusinessUnitAddAssociateAction, BusinessUnitSetAssociatesAction, BusinessUnitSetContactEmailAction, BusinessUnitSetStoreModeAction, BusinessUnitChangeNameAction, BusinessUnitChangeAddressAction, BusinessUnitAddStoreAction, BusinessUnitChangeParentUnitAction, BusinessUnitChangeStatusAction, CartDraft, Cart, CartAddLineItemAction, CartAddItemShippingAddressAction, CartChangeLineItemQuantityAction, CartRemoveLineItemAction, CartSetBillingAddressAction, CartSetShippingMethodAction, CartSetCountryAction, CartSetCustomerEmailAction, CartSetCustomFieldAction, CartSetCustomShippingMethodAction, CartSetCustomTypeAction, CartSetLocaleAction, CartSetLineItemShippingDetailsAction, CartSetShippingAddressAction, CartRemoveDiscountCodeAction, LineItemDraft, LineItem, CartDiscountDraft, CartDiscount, CartDiscountUpdateAction, AssetDraft, Asset, CategoryDraft, Category, CategoryChangeAssetNameAction, CategoryChangeSlugAction, CategoryChangeNameAction, CategoryChangeParentAction, CategorySetKeyAction, CategorySetAssetDescriptionAction, CategorySetAssetSourcesAction, CategorySetDescriptionAction, CategorySetMetaDescriptionAction, CategorySetMetaKeywordsAction, CategorySetMetaTitleAction, CategorySetCustomTypeAction, CategorySetCustomFieldAction, CategoryRemoveAssetAction, CategoryAddAssetAction, ChannelDraft, Channel, ChannelUpdateAction, CustomObjectDraft, CustomObject, CustomerDraft, Customer, CustomerChangeEmailAction, CustomerSetAuthenticationModeAction, CustomerSetCustomFieldAction, CustomerGroupDraft, CustomerGroup, CustomerGroupSetKeyAction, CustomerGroupChangeNameAction, CustomerGroupSetCustomTypeAction, CustomerGroupSetCustomFieldAction, DiscountCodeDraft, DiscountCode, DiscountCodeUpdateAction, Extension, ExtensionDraft, ExtensionUpdateAction, InventoryEntryDraft, InventoryEntry, InventoryEntryChangeQuantityAction, InventoryEntrySetExpectedDeliveryAction, InventoryEntrySetCustomFieldAction, InventoryEntrySetCustomTypeAction, InventoryEntrySetRestockableInDaysAction, OrderFromCartDraft, Order, CartReference, OrderImportDraft, OrderAddPaymentAction, OrderAddReturnInfoAction, OrderChangeOrderStateAction, OrderChangePaymentStateAction, OrderTransitionStateAction, OrderSetBillingAddressAction, OrderSetCustomerEmailAction, OrderSetCustomFieldAction, OrderSetCustomTypeAction, OrderSetLocaleAction, OrderSetOrderNumberAction, OrderSetShippingAddressAction, OrderSetStoreAction, MyOrderFromCartDraft, OrderEditDraft, OrderEdit, OrderEditUpdateAction, PaymentDraft, Payment, TransactionDraft, Transaction, PaymentSetCustomFieldAction, PaymentSetCustomTypeAction, PaymentAddTransactionAction, PaymentChangeTransactionStateAction, PaymentTransitionStateAction, ProductDraft, Product, ProductUpdateAction, ProductDiscountDraft, ProductDiscount, ProductDiscountUpdateAction, ProductProjectionPagedSearchResponse, ProductProjection, FacetResults, TermFacetResult, FilteredFacetResult, RangeFacetResult, ProductSelectionDraft, ProductSelection, ProductSelectionUpdateAction, ProductTypeDraft, ProductType, AttributeDefinitionDraft, AttributeDefinition, ProductTypeUpdateAction, ProjectUpdateAction, QuoteDraft, Quote, QuoteUpdateAction, QuoteRequestDraft, QuoteRequest, QuoteRequestUpdateAction, ReviewDraft, Review, ReviewUpdateAction, ShippingMethodDraft, ShippingMethod, ZoneReference, ShippingRate, ShippingMethodUpdateAction, ShoppingListDraft, ShoppingList, StagedQuoteDraft, StagedQuote, StagedQuoteUpdateAction, StandalonePriceDraft, StandalonePrice, ChannelResourceIdentifier, ChannelReference, DiscountedPriceDraft, StandalonePriceChangeActiveAction, StandalonePriceChangeValueAction, StandalonePriceSetDiscountedPriceAction, StateDraft, State, StateUpdateAction, StoreDraft, Store, StoreUpdateAction, SubscriptionDraft, Subscription, TaxCategoryDraft, TaxCategory, TaxCategoryUpdateAction, TypeDraft, Type, TypeUpdateAction, ZoneDraft, Zone, ZoneUpdateAction, BaseResource, UpdateAction } from '@commercetools/platform-sdk';
5
5
 
6
+ type Token = {
7
+ access_token: string;
8
+ token_type: 'Bearer';
9
+ expires_in: number;
10
+ scope: string;
11
+ refresh_token?: string;
12
+ };
13
+ declare class OAuth2Store {
14
+ tokens: Token[];
15
+ validate: boolean;
16
+ constructor(validate?: boolean);
17
+ addToken(token: Token): void;
18
+ getClientToken(clientId: string, clientSecret: string, scope?: string): Token;
19
+ getAnonymousToken(scope: string, anonymousId: string | undefined): Token;
20
+ getCustomerToken(scope: string, customerId: string): Token;
21
+ refreshToken(clientId: string, clientSecret: string, refreshToken: string): {
22
+ access_token: string;
23
+ token_type: "Bearer";
24
+ expires_in: number;
25
+ scope: string;
26
+ } | undefined;
27
+ validateToken(token: string): boolean;
28
+ }
29
+
6
30
  type GetParams$1 = {
7
31
  expand?: string[];
8
32
  };
@@ -677,6 +701,7 @@ declare class CommercetoolsMock {
677
701
  stop(): void;
678
702
  clear(): void;
679
703
  project(projectKey?: string): ProjectAPI;
704
+ authStore(): OAuth2Store;
680
705
  runServer(port?: number, options?: AppOptions): void;
681
706
  private createApp;
682
707
  registerHandlers(server: SetupServer): void;
package/dist/index.d.ts CHANGED
@@ -3,6 +3,30 @@ import { SetupServer } from 'msw/node';
3
3
  import * as ctp from '@commercetools/platform-sdk';
4
4
  import { Project, ResourceIdentifier, QueryParam, AssociateRoleDraft, AssociateRole, AssociateRoleSetNameAction, AssociateRoleSetPermissionsAction, AssociateRoleChangeBuyerAssignableAction, AssociateRoleSetCustomFieldAction, AssociateRoleAddPermissionAction, AssociateRoleRemovePermissionAction, AttributeGroupDraft, AttributeGroup, AttributeGroupSetAttributesAction, AttributeGroupChangeNameAction, AttributeGroupSetDescriptionAction, AttributeGroupSetKeyAction, BusinessUnitDraft, BusinessUnit, BusinessUnitAddAddressAction, BusinessUnitAddAssociateAction, BusinessUnitSetAssociatesAction, BusinessUnitSetContactEmailAction, BusinessUnitSetStoreModeAction, BusinessUnitChangeNameAction, BusinessUnitChangeAddressAction, BusinessUnitAddStoreAction, BusinessUnitChangeParentUnitAction, BusinessUnitChangeStatusAction, CartDraft, Cart, CartAddLineItemAction, CartAddItemShippingAddressAction, CartChangeLineItemQuantityAction, CartRemoveLineItemAction, CartSetBillingAddressAction, CartSetShippingMethodAction, CartSetCountryAction, CartSetCustomerEmailAction, CartSetCustomFieldAction, CartSetCustomShippingMethodAction, CartSetCustomTypeAction, CartSetLocaleAction, CartSetLineItemShippingDetailsAction, CartSetShippingAddressAction, CartRemoveDiscountCodeAction, LineItemDraft, LineItem, CartDiscountDraft, CartDiscount, CartDiscountUpdateAction, AssetDraft, Asset, CategoryDraft, Category, CategoryChangeAssetNameAction, CategoryChangeSlugAction, CategoryChangeNameAction, CategoryChangeParentAction, CategorySetKeyAction, CategorySetAssetDescriptionAction, CategorySetAssetSourcesAction, CategorySetDescriptionAction, CategorySetMetaDescriptionAction, CategorySetMetaKeywordsAction, CategorySetMetaTitleAction, CategorySetCustomTypeAction, CategorySetCustomFieldAction, CategoryRemoveAssetAction, CategoryAddAssetAction, ChannelDraft, Channel, ChannelUpdateAction, CustomObjectDraft, CustomObject, CustomerDraft, Customer, CustomerChangeEmailAction, CustomerSetAuthenticationModeAction, CustomerSetCustomFieldAction, CustomerGroupDraft, CustomerGroup, CustomerGroupSetKeyAction, CustomerGroupChangeNameAction, CustomerGroupSetCustomTypeAction, CustomerGroupSetCustomFieldAction, DiscountCodeDraft, DiscountCode, DiscountCodeUpdateAction, Extension, ExtensionDraft, ExtensionUpdateAction, InventoryEntryDraft, InventoryEntry, InventoryEntryChangeQuantityAction, InventoryEntrySetExpectedDeliveryAction, InventoryEntrySetCustomFieldAction, InventoryEntrySetCustomTypeAction, InventoryEntrySetRestockableInDaysAction, OrderFromCartDraft, Order, CartReference, OrderImportDraft, OrderAddPaymentAction, OrderAddReturnInfoAction, OrderChangeOrderStateAction, OrderChangePaymentStateAction, OrderTransitionStateAction, OrderSetBillingAddressAction, OrderSetCustomerEmailAction, OrderSetCustomFieldAction, OrderSetCustomTypeAction, OrderSetLocaleAction, OrderSetOrderNumberAction, OrderSetShippingAddressAction, OrderSetStoreAction, MyOrderFromCartDraft, OrderEditDraft, OrderEdit, OrderEditUpdateAction, PaymentDraft, Payment, TransactionDraft, Transaction, PaymentSetCustomFieldAction, PaymentSetCustomTypeAction, PaymentAddTransactionAction, PaymentChangeTransactionStateAction, PaymentTransitionStateAction, ProductDraft, Product, ProductUpdateAction, ProductDiscountDraft, ProductDiscount, ProductDiscountUpdateAction, ProductProjectionPagedSearchResponse, ProductProjection, FacetResults, TermFacetResult, FilteredFacetResult, RangeFacetResult, ProductSelectionDraft, ProductSelection, ProductSelectionUpdateAction, ProductTypeDraft, ProductType, AttributeDefinitionDraft, AttributeDefinition, ProductTypeUpdateAction, ProjectUpdateAction, QuoteDraft, Quote, QuoteUpdateAction, QuoteRequestDraft, QuoteRequest, QuoteRequestUpdateAction, ReviewDraft, Review, ReviewUpdateAction, ShippingMethodDraft, ShippingMethod, ZoneReference, ShippingRate, ShippingMethodUpdateAction, ShoppingListDraft, ShoppingList, StagedQuoteDraft, StagedQuote, StagedQuoteUpdateAction, StandalonePriceDraft, StandalonePrice, ChannelResourceIdentifier, ChannelReference, DiscountedPriceDraft, StandalonePriceChangeActiveAction, StandalonePriceChangeValueAction, StandalonePriceSetDiscountedPriceAction, StateDraft, State, StateUpdateAction, StoreDraft, Store, StoreUpdateAction, SubscriptionDraft, Subscription, TaxCategoryDraft, TaxCategory, TaxCategoryUpdateAction, TypeDraft, Type, TypeUpdateAction, ZoneDraft, Zone, ZoneUpdateAction, BaseResource, UpdateAction } from '@commercetools/platform-sdk';
5
5
 
6
+ type Token = {
7
+ access_token: string;
8
+ token_type: 'Bearer';
9
+ expires_in: number;
10
+ scope: string;
11
+ refresh_token?: string;
12
+ };
13
+ declare class OAuth2Store {
14
+ tokens: Token[];
15
+ validate: boolean;
16
+ constructor(validate?: boolean);
17
+ addToken(token: Token): void;
18
+ getClientToken(clientId: string, clientSecret: string, scope?: string): Token;
19
+ getAnonymousToken(scope: string, anonymousId: string | undefined): Token;
20
+ getCustomerToken(scope: string, customerId: string): Token;
21
+ refreshToken(clientId: string, clientSecret: string, refreshToken: string): {
22
+ access_token: string;
23
+ token_type: "Bearer";
24
+ expires_in: number;
25
+ scope: string;
26
+ } | undefined;
27
+ validateToken(token: string): boolean;
28
+ }
29
+
6
30
  type GetParams$1 = {
7
31
  expand?: string[];
8
32
  };
@@ -677,6 +701,7 @@ declare class CommercetoolsMock {
677
701
  stop(): void;
678
702
  clear(): void;
679
703
  project(projectKey?: string): ProjectAPI;
704
+ authStore(): OAuth2Store;
680
705
  runServer(port?: number, options?: AppOptions): void;
681
706
  private createApp;
682
707
  registerHandlers(server: SetupServer): void;
package/dist/index.js CHANGED
@@ -1257,14 +1257,18 @@ var OAuth2Store = class {
1257
1257
  constructor(validate = true) {
1258
1258
  this.validate = validate;
1259
1259
  }
1260
+ addToken(token) {
1261
+ this.tokens.push(token);
1262
+ }
1260
1263
  getClientToken(clientId, clientSecret, scope) {
1261
1264
  const token = {
1262
1265
  access_token: randomBytes(16).toString("base64"),
1263
1266
  token_type: "Bearer",
1264
1267
  expires_in: 172800,
1265
- scope: scope || "todo"
1268
+ scope: scope || "todo",
1269
+ refresh_token: `my-project-${randomBytes(16).toString("base64")}`
1266
1270
  };
1267
- this.tokens.push(token);
1271
+ this.addToken(token);
1268
1272
  return token;
1269
1273
  }
1270
1274
  getAnonymousToken(scope, anonymousId) {
@@ -1275,9 +1279,10 @@ var OAuth2Store = class {
1275
1279
  access_token: randomBytes(16).toString("base64"),
1276
1280
  token_type: "Bearer",
1277
1281
  expires_in: 172800,
1278
- scope: scope ? `${scope} anonymous_id:${anonymousId}` : `anonymous_id:${anonymousId}`
1282
+ scope: scope ? `${scope} anonymous_id:${anonymousId}` : `anonymous_id:${anonymousId}`,
1283
+ refresh_token: `my-project-${randomBytes(16).toString("base64")}`
1279
1284
  };
1280
- this.tokens.push(token);
1285
+ this.addToken(token);
1281
1286
  return token;
1282
1287
  }
1283
1288
  getCustomerToken(scope, customerId) {
@@ -1285,11 +1290,29 @@ var OAuth2Store = class {
1285
1290
  access_token: randomBytes(16).toString("base64"),
1286
1291
  token_type: "Bearer",
1287
1292
  expires_in: 172800,
1288
- scope: scope ? `${scope} customer_id:${customerId}` : `customer_id:${customerId}`
1293
+ scope: scope ? `${scope} customer_id:${customerId}` : `customer_id:${customerId}`,
1294
+ refresh_token: `my-project-${randomBytes(16).toString("base64")}`
1289
1295
  };
1290
- this.tokens.push(token);
1296
+ this.addToken(token);
1291
1297
  return token;
1292
1298
  }
1299
+ refreshToken(clientId, clientSecret, refreshToken) {
1300
+ const existing = this.tokens.find((t) => t.refresh_token === refreshToken);
1301
+ if (!existing) {
1302
+ return void 0;
1303
+ }
1304
+ const token = {
1305
+ ...existing,
1306
+ access_token: randomBytes(16).toString("base64")
1307
+ };
1308
+ this.addToken(token);
1309
+ return {
1310
+ access_token: token.access_token,
1311
+ token_type: token.token_type,
1312
+ expires_in: token.expires_in,
1313
+ scope: token.scope
1314
+ };
1315
+ }
1293
1316
  validateToken(token) {
1294
1317
  if (!this.validate)
1295
1318
  return true;
@@ -1429,11 +1452,36 @@ var OAuth2Server = class {
1429
1452
  );
1430
1453
  return response.status(200).send(token);
1431
1454
  } else if (grantType === "refresh_token") {
1432
- const token = this.store.getClientToken(
1455
+ const refreshToken = request.query.refresh_token?.toString();
1456
+ if (!refreshToken) {
1457
+ return next(
1458
+ new CommercetoolsError(
1459
+ {
1460
+ code: "invalid_request",
1461
+ message: "Missing required parameter: refresh_token."
1462
+ },
1463
+ 400
1464
+ )
1465
+ );
1466
+ }
1467
+ const token = this.store.refreshToken(
1433
1468
  request.credentials.clientId,
1434
1469
  request.credentials.clientSecret,
1435
- request.query.scope?.toString()
1470
+ refreshToken
1436
1471
  );
1472
+ if (!token) {
1473
+ return next(
1474
+ new CommercetoolsError(
1475
+ {
1476
+ statusCode: 400,
1477
+ message: "The refresh token was not found. It may have expired.",
1478
+ error: "invalid_grant",
1479
+ error_description: "The refresh token was not found. It may have expired."
1480
+ },
1481
+ 400
1482
+ )
1483
+ );
1484
+ }
1437
1485
  return response.status(200).send(token);
1438
1486
  } else {
1439
1487
  return next(
@@ -7239,6 +7287,9 @@ var CommercetoolsMock = class {
7239
7287
  this._storage
7240
7288
  );
7241
7289
  }
7290
+ authStore() {
7291
+ return this._oauth2.store;
7292
+ }
7242
7293
  runServer(port = 3e3, options) {
7243
7294
  const server = this.app.listen(port, () => {
7244
7295
  console.info(`Mock server listening at http://localhost:${port}`);