# BF Utility Functions

| Function                                           | Description                                                                                                                                                                                                                                                                                                                                                                                                         |
| -------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Actions**                                        |                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `BF.actionsClear()`                                | Clears the action script buffer halting all subsequent actions (current action completes)                                                                                                                                                                                                                                                                                                                           |
|                                                    |                                                                                                                                                                                                                                                                                                                                                                                                                     |
| **Errors**                                         |                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `BF.errorClearAll()`                               | Clears the error cache                                                                                                                                                                                                                                                                                                                                                                                              |
| `BF.errorGetAll()`                                 | Returns all current errors                                                                                                                                                                                                                                                                                                                                                                                          |
| `BF.errorThrow(errorCode, errorDescription, info)` | Creates an error                                                                                                                                                                                                                                                                                                                                                                                                    |
| `BF.authGetLastFeedback()`                         | Returns the latest authentication feedback object from `store.state.auth.lastFeedback`. Useful for reading the latest auth `code`, `message`, `type`, and `numericCode` (for error flows).                                                                                                                                                                                                                          |
|                                                    |                                                                                                                                                                                                                                                                                                                                                                                                                     |
| **Actions**                                        |                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `BF.namedAction(name, options)`                    | Runs a named action, options are propagated to all actions within the named action.                                                                                                                                                                                                                                                                                                                                 |
|                                                    |                                                                                                                                                                                                                                                                                                                                                                                                                     |
| **Misc**                                           |                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `BF.getPaths(object, JPquery)`                     | Returns JSONPath query on passed object                                                                                                                                                                                                                                                                                                                                                                             |
| `BF.getQueryParam(key)`                            | Returns the URL query key value                                                                                                                                                                                                                                                                                                                                                                                     |
| `BF.i18n(key)`                                     | Internationalization translations. Returns the key's value in its correct language.                                                                                                                                                                                                                                                                                                                                 |
| `BF.getUUID()`                                     | Returns a UUID                                                                                                                                                                                                                                                                                                                                                                                                      |
| `BF.pwaSetManifest(manifest)`                      | Sets the manifest for PWA apps. `manifest` is a JSON object that confirms to a standard PWA manifest object.                                                                                                                                                                                                                                                                                                        |
| **`BF.getVersion()`**                              | <p>Returns an object with version info: <code>{ basecode, app }</code>.<br><strong>basecode</strong> is the BF operating system version. <strong>app</strong> is the published app version (from the evironments card if present, otherwise falls back to <code>site.hash</code>).<br><em>Available in basecode v3.2.19+</em>.</p>                                                                                  |
| `BF.rebuildReactivity(object)`                     | <p>This function is normally run upon the completion of a <code>function</code> internally by the BF actions processor.<br>It recursively scans the object passed and adds new reactive keys to keys that are new or do not have reactivity. You will not need to use this within a function unless that function has a callback that later mutates a reactive object. Eg: setTimeout() which takes a callback.</p> |
| `BF.socketConnect()`                               | Connects the client to the BF server via a socket                                                                                                                                                                                                                                                                                                                                                                   |
| `BF.socketDisconnect()`                            | Disconnects the client from the BF servers (Goes off line)                                                                                                                                                                                                                                                                                                                                                          |
|                                                    |                                                                                                                                                                                                                                                                                                                                                                                                                     |
| **Dynamic Library Loading**                        |                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `BF.libraryLoadOnce(url, options)`                 | <p>Load external JavaScript libraries, ESM modules, and CSS files from CDN dynamically. Libraries are loaded once and cached automatically (idempotent).<br><em>Available in basecode v3.2.31+</em>.</p>                                                                                                                                                                                                            |
| `BF.libraryIsLoaded(url)`                          | <p>Check if a library is already loaded. Returns boolean.<br><em>Available in basecode v3.2.31+</em>.</p>                                                                                                                                                                                                                                                                                                           |
| `BF.libraryGetLoaded()`                            | <p>Get array of all loaded library URLs.<br><em>Available in basecode v3.2.31+</em>.</p>                                                                                                                                                                                                                                                                                                                            |

## Dynamic Library Loading

For detailed documentation on the dynamic library loading functions (`BF.libraryLoadOnce`, `BF.libraryIsLoaded`, `BF.libraryGetLoaded`), see:

{% content-ref url="bf-utility-function-ver-0.9.20+/bf-dynamic-library-loading" %}
[bf-dynamic-library-loading](https://docs.klai.studio/reference/bf-utility-function-ver-0.9.20+/bf-dynamic-library-loading)
{% endcontent-ref %}

## Auth Feedback Helper

### `BF.authGetLastFeedback()`

Returns the latest auth feedback object written by BetterForms authentication flows.

Example return values:

```javascript
{
  code: "auth.magic_link_sent",
  message: "If that email can be used to sign in, a magic link has been sent.",
  type: "info"
}
```

```javascript
{
  code: "auth.invalid_login",
  message: "We couldn't sign you in. Check your details and try again.",
  type: "error",
  numericCode: 10150
}
```

Typical usage:

```javascript
const feedback = BF.authGetLastFeedback();

if (feedback && feedback.code === "auth.invalid_login") {
  console.log(feedback.message);
}
```

Notes:

* This is a read helper only.
* The value mirrors `store.state.auth.lastFeedback`.
* For page UI, you can also read `model.authMessage`, `model.authMessageCode`, and `model.authMessageType`.
