> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mastermindcms.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Utils

> Unified calls and common helpers exported by msm-components

`Utils` provides small helper functions used by many components and services.

## Unified WebSocket calls

These helpers use the built-in client instance (exported as `MSM2App`) to call the unified WebSocket JSON API:

* `callService(beanId, scope, functionName, args, eventType?, endpoint?)`
* `callRepository(repositoryId, query, eventType?, endpoint?)`
* `updateBean(beanId, scope, payload, eventType?, endpoint?)`

### Calling a backend service

`callService` returns a `Promise` containing the full client response. The backend value is in `response.result`:

```js theme={null}
import { callService } from "msm-components/dist/services.js";

const response = await callService(
  "sellerRegistrationServiceImpl",
  "PROTOTYPE",
  "getLoggedProfile",
  []
);

const profile = response.result;
```

Arguments are passed in the same order as the backend method parameters. Search requests commonly use a query object:

```js theme={null}
const request = {
  query: { isPublishedForSale: true },
  ignoreRegexWrap: ["isPublishedForSale"],
  offset: 0,
  limit: 10,
  sortName: "createdDate",
  sortDirection: "DESC",
};

const response = await callService(
  "searchManagerServiceImpl",
  "PROTOTYPE",
  "search",
  [request]
);

const items = response.result?.data?.content ?? [];
```

If the backend method expects a string, serialize the request with `JSON.stringify(request)`. `scope` is usually `PROTOTYPE`; `eventType` can be `USER`, `SHARED`, or `GLOBAL`.

### Calling a repository

```js theme={null}
import { callRepository } from "msm-components/dist/services.js";

const response = await callRepository("blogCategoryRepository", {
  query: { _id: categoryId },
  sortName: "_id",
  sortDirection: "ASC",
  skip: 0,
  limit: 1,
});

const category = response.result?.[0];
```

### Updating a bean

`updateBean` updates values on a bean registered with `@Component` or `@Service`.

```js theme={null}
import { updateBean } from "msm-components/dist/services.js";

const response = await updateBean(
  "dummyBean",
  "PROTOTYPE",
  { description: "Updated description" }
);
```

All three helpers use the shared built-in `MSM2App` instance, which must be available before they are called.

## Context and settings helpers

* `getLangCode()`
* `getActiveLanguages()`
* `getLanguageDetection()`
* `getWebSiteSettings()`
* `getCdnFolder()`
* `getSecuredFolder()`
* `getSeoUrl(entityId, repositoryName)`
* `getSuperUserLogin()`
* `getAttributeTypes()`
* `getAttributeAreaTypes()`
* `getFormattedDate(date, pattern)`
* `isLogged()`
* `getLoggedProfile()`
* `isOpenAiActive()`

```js theme={null}
import { getWebSiteSettings, getSeoUrl } from "msm-components/dist/services.js";

const settings = await getWebSiteSettings();
const url = await getSeoUrl(entityId, "sellerSkuRepository");
```

## Cookies and URL helpers

* `getCookie(name)`
* `setCookie(name, value, expires?, domain?, path?)`
* `deleteAllCookies(ignore)`
* `getDomainName()`
* `languageAwareUrl(url)`
* `setPlatformInfo()`
* `escapeRegExp(string)`
* `getPayloadOfForm(element)`

```js theme={null}
import { languageAwareUrl, getCookie, setCookie } from "msm-components/dist/services.js";

setCookie("promo", "spring", null, null, "/");
console.log(getCookie("i18next"));
const url = await languageAwareUrl("/catalog");
```
