npm package: @plasma-platform/service-carts
Description: Carts service SDK
Author: Anton Nemtsev
Latest version: 5.0.3

![version](https://badgen.net/npm/v/@plasma-platform/service-carts)
![nextVersion](https://badgen.net/npm/v/@plasma-platform/service-carts/next)
![downloads](https://badgen.net/npm/dt/@plasma-platform/service-carts)
![license](https://badgen.net/npm/license/@plasma-platform/service-carts)
![dependency](https://badgen.net/npm/dependents/@plasma-platform/service-carts)  

* * *

[Full documentation](https://templatemonster.gitlab.io/PlasmaPlatform/Frontend/plasma-platform/?path=/story/plasma-platform-sdk--service-carts)  
install:  
`npm i @plasma-platform/service-carts -S`  

# Short documentation

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

-   [CartsService](#cartsservice)
    -   [Parameters](#parameters)
    -   [addNewCart](#addnewcart)
        -   [Parameters](#parameters-1)
        -   [Examples](#examples)
    -   [cloneCart](#clonecart)
        -   [Parameters](#parameters-2)
        -   [Examples](#examples-1)
    -   [getCartById](#getcartbyid)
        -   [Parameters](#parameters-3)
        -   [Examples](#examples-2)
    -   [getCartCalculatedById](#getcartcalculatedbyid)
        -   [Parameters](#parameters-4)
        -   [Examples](#examples-3)
    -   [getMyCarts](#getmycarts)
        -   [Parameters](#parameters-5)
        -   [Examples](#examples-4)
    -   [linkItemToCart](#linkitemtocart)
        -   [Parameters](#parameters-6)
        -   [Examples](#examples-5)
    -   [linkItemToItem](#linkitemtoitem)
        -   [Parameters](#parameters-7)
        -   [Examples](#examples-6)
    -   [linkServiceToBundle](#linkservicetobundle)
        -   [Parameters](#parameters-8)
        -   [Examples](#examples-7)
    -   [unlinkServiceFromBundle](#unlinkservicefrombundle)
        -   [Parameters](#parameters-9)
        -   [Examples](#examples-8)
    -   [mergeCarts](#mergecarts)
        -   [Parameters](#parameters-10)
        -   [Examples](#examples-9)
    -   [unLinkItemFromCart](#unlinkitemfromcart)
        -   [Parameters](#parameters-11)
        -   [Examples](#examples-10)
    -   [unLinkItemFromItem](#unlinkitemfromitem)
        -   [Parameters](#parameters-12)
        -   [Examples](#examples-11)
    -   [updateCart](#updatecart)
        -   [Parameters](#parameters-13)
    -   [reassignCart](#reassigncart)
        -   [Parameters](#parameters-14)
        -   [Examples](#examples-12)
    -   [getToken](#gettoken)
        -   [Parameters](#parameters-15)
        -   [Examples](#examples-13)
-   [defaultAutoTrim](#defaultautotrim)

## CartsService

-   **See: <https://jsonplaceholder.typicode.com/>**

Carts Micro Service API SDK

### Parameters

-   `url` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** service url
-   `token` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | null)** user access tocken if available (optional, default `null`)

### addNewCart

Add new cart

#### Parameters

-   `currency` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) Currency code iso3
-   `locale` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** cart locale
-   `attributes` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** List of predefined attributes to be stored with cart
    -   `attributes.affiliate` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Affiliate name
    -   `attributes.affiliateAuthor` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** AffiliateAuthor name
    -   `attributes.operator` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Operator name
    -   `attributes.description` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Description for cart
    -   `attributes.channel` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Channel name (create_cart)
    -   `attributes.trackers` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Analytics trackers
    -   `attributes.trackers` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** [ga] - Google Analytics tracker
    -   `attributes.trackers` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** [ad] - Google Adwords tracker
    -   `attributes.trackers` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** [sc] - Soft Cube tracker

#### Examples

```javascript
(async () => {
  const cartsService = new CartsService('https://jsonplaceholder.typicode.com/', 'dsugsahfdkljhafljkdfhajkldshfad');
  const cart = await cartsService.addNewCart('USD', { affiliate: 'colorlib' });
})();
```

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** object with new cart properties

### cloneCart

Clone cart

#### Parameters

-   `id` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) cart id for cone

#### Examples

```javascript
(async () => {
  const cartsService = new CartsService('https://jsonplaceholder.typicode.com/', 'hfkjasdhfljkadhflkajdshflkadjhald');
  const cart = await cartsService.cloneCart(111);
})();
```

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** object with new cart properties

### getCartById

Get cart by id

#### Parameters

-   `id` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) Cart id

#### Examples

```javascript
(async () => {
  const cartsService = new CartsService('https://jsonplaceholder.typicode.com/');
  const cart = await cartsService.getCartById(111);
})();
```

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** object with cart properties

### getCartCalculatedById

Get cart calculated by id

#### Parameters

-   `id` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) Cart id

#### Examples

```javascript
(async () => {
  const cartsService = new CartsService('https://jsonplaceholder.typicode.com/');
  const cart = await cartsService.getCartCalculatedById(111);
})();
```

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** object with cart properties

### getMyCarts

Get my carts

#### Parameters

-   `typeSort` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** type of sorting products in cart

#### Examples

```javascript
(async () => {
  const cartsService = new CartsService('https://jsonplaceholder.typicode.com/');
  const cart = await cartsService.getMyCarts();
})();
```

Returns **[array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)** array with objects cart properties

### linkItemToCart

Link item to cart

#### Parameters

-   `productServiceLink` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) link to product in service products
-   `cartId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) cart id
-   `itemType` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) type of product (products | services | discounts | externalProducts)
-   `data` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (required) params for link product to cart
    -   `data.license` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** license id
    -   `data.channel` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** channel for service (required if itemType is services)
    -   `data.locale` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** users locale
    -   `data.position` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** position for service
    -   `data.presentation_id` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** presentation id for service

#### Examples

```javascript
(async () => {
  const cartsService = new CartsService('https://jsonplaceholder.typicode.com/');
  const cart = await cartsService.linkItemToCart('http://service-products.dev/api/v1/products/en/55555', 111, 'products', {
    license: 50,
  });
})();
```

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** object cart properties

### linkItemToItem

Link item to item

#### Parameters

-   `productServiceLink` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) link to product in service products
-   `cartId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) cart id
-   `itemId` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (required) item id
-   `data` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (required) params for link product to cart
    -   `data.channel` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) channel for service
    -   `data.locale` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** users locale
    -   `data.position` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** position for service
    -   `data.presentation_id` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** presentation id for service
-   `typeLink` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** type of link to item

#### Examples

```javascript
(async () => {
  const cartsService = new CartsService('https://jsonplaceholder.typicode.com/');
  const cart = await cartsService.linkItemToItem('http://service-services.dev/api/v1/services/en/1', 111, 222, {
    channel: 'tm-cart'
  }, 'bundles');
})();
```

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** object cart properties

### linkServiceToBundle

Link service to bundle

#### Parameters

-   `productServiceLinks` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) link to service in service products
-   `cartId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) cart id
-   `itemId` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (required) item id
-   `data` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (required) params for link product to cart
    -   `data.channel` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) channel for service
-   `bundleId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** bundle's id

#### Examples

```javascript
(async () => {
  const cartsService = new CartsService('https://jsonplaceholder.typicode.com/');
  const cart = await cartsService.linkItemToItem('http://service-services.dev/api/v1/services/en/1', 111, 222, {
    channel: 'tm-cart'
  }, 'qwerty123');
})();
```

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** object cart properties

### unlinkServiceFromBundle

Unlink service from bundle

#### Parameters

-   `productServiceLinks` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) link to service in service products
-   `cartId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) cart id
-   `itemId` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (required) item id
-   `bundleId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** bundle's id

#### Examples

```javascript
(async () => {
  const cartsService = new CartsService('https://jsonplaceholder.typicode.com/');
  const cart = await cartsService.unlinkServiceFromBundle('http://service-services.dev/api/v1/services/en/1', 111, 222, 'qwerty123');
})();
```

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** object cart properties

### mergeCarts

Merge carts

#### Parameters

-   `users_cart_id` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) users cart id
-   `anonymous_cart_id` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) anonymous cart id

#### Examples

```javascript
(async () => {
  const cartsService = new CartsService('https://jsonplaceholder.typicode.com/', 'dsugsahfdkljhafljkdfhajkldshfad');
  const cart = await cartsService.mergeCarts(111, 333);
})();
```

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** object with new cart properties

### unLinkItemFromCart

Unlink item from cart

#### Parameters

-   `productServiceLink` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) link to product in service products
-   `cartId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) cart id
-   `itemType` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) type of product (products | services | discounts | externalProducts)

#### Examples

```javascript
(async () => {
  const cartsService = new CartsService('https://jsonplaceholder.typicode.com/');
  const cart = await cartsService.unLinkItemFromCart('http://service-products.dev/api/v1/products/en/55555', 111, 'products');
})();
```

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** object cart properties

### unLinkItemFromItem

Unlink item from item

#### Parameters

-   `productServiceLink` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) link to product in service products
-   `cartId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) cart id
-   `itemTypeTarget` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) item type target
-   `itemId` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (required) item id
-   `itemTypeSource` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) item type source

#### Examples

```javascript
(async () => {
  const cartsService = new CartsService('https://jsonplaceholder.typicode.com/');
  const cart = await cartsService.unLinkItemFromItem('http://service-services.dev/api/v1/services/en/1', 111, 'services', 222, 'services');
})();
```

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** object cart properties

### updateCart

update cart

#### Parameters

-   `id` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) cart id for cone
-   `currency` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** currency for shopping cart (iso3)
-   `locale` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** cart locale
-   `attributes` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** list of predefined attributes to be stored with cart
    -   `attributes.affiliate` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Affiliate name
    -   `attributes.affiliateAuthor` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** AffiliateAuthor name
    -   `attributes.operator` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Operator name
    -   `attributes.description` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Description for cart
    -   `attributes.channel` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Channel name (create_cart)
    -   `attributes.trackers` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Analytics trackers
    -   `attributes.trackers` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** [ga] - Google Analytics tracker
    -   `attributes.trackers` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** [ad] - Google Adwords tracker
    -   `attributes.trackers` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** [sc] - Soft Cube tracker

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** object with new cart properties

### reassignCart

Reassign cart

#### Parameters

-   `cardId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) cart Id
-   `link` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) link to user profile

#### Examples

```javascript
(async () => {
  const cartsService = new CartsService('https://jsonplaceholder.typicode.com/', 'dsugsahfdkljhafljkdfhajkldshfad');
  const cart = await cartsService.reassignCart('123');
})();
```

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** object with new cart properties

### getToken

Get token for centrifuge

#### Parameters

-   `options` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 
    -   `options.id` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (required) cart Id
    -   `options.time` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (required) timestamp for centrifuge

#### Examples

```javascript
<caption>Get token for centrifuge<caption>
import Config from 'Config';
import CartsService from '@plasma-platform/tm-service-carts';
import TMCentrifuge from '@plasma-platform/tm-centrifuge';
(async () => {
  const cartsService = new CartsService('//socket.templatemonster.com/', 'ZW7NJyKVEtjyGCjPBXOZyQrjNZ5aOjIcJD7SAFNm');
  const timestamp = Math.round(new Date().getTime() / 1000);
  const [ cart ] = await cartsService.getMyCarts('-created_at&per-page=1');
  const token = await cartsService.getToken({
     id: cart.id,
     timestamp,
   });
  const centrifuge = TMCentrifuge({
     url: Config.centrifugeCartUrl,
     timestamp,
  });

  centrifuge.subscribe('update', (message)=>{…});
})();
```

-   Throws **any** ErrorBadRequest

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** object with new cart properties

## defaultAutoTrim

Object with class service messages

Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)