# Helpers

## toBasicAuth

Converts a username and password string combination into a `Basic` Authorization token. `toBasicAuth` is a static method of the `XAPI` Class.

```typescript
const username = "username";
const password = "password";
const auth = XAPI.toBasicAuth(username, password);
console.log(auth); // Basic dXNlcm5hbWU6cGFzc3dvcmQ=
```

## calculateISO8601Duration

Useful for calculating ISO8601 durations when submitting statements that require durations. `calculateISO8601Duration` is a static method of the `XAPI` Class.

```typescript
const startDate: Date = new Date();
const endDate: Date = new Date(startDate);
endDate.setDate(endDate.getDate() + 1);
const duration = XAPI.calculateISO8601Duration(startDate, endDate));
console.log(duration); // P1DT
```

## getXAPILaunchData

Gets the launch data if the module was launched using [xAPI Launch](http://adlnet.github.io/xapi-launch/). `getXAPILaunchData` is a static method of the `XAPI` Class.

```typescript
XAPI.getXAPILaunchData().then((result) => {
  const launchData: XAPILaunchData = result.data;
  this.xapi = new XAPI({
    endpoint: launchData.endpoint
  });
  const actor = launchData.actor;
});
```

## getTinCanLaunchData

Gets the launch data if the module was launched as a TinCan module. `getTinCanLaunchData` is a static method of the `XAPI` Class.

```typescript
const tinCanLaunchData: TinCanLaunchParameters = XAPI.getTinCanLaunchData();
const xapi = new XAPI({
  endpoint: tinCanLaunchData.endpoint,
  auth: tinCanLaunchData.auth
});
const actor = tinCanLaunchData.actor;
```

## getSearchQueryParamsAsObject

Converts search query parameters from a string into an object. `getSearchQueryParamsAsObject` is a static method of the `XAPI` Object. Used in `getTinCanLaunchData` and `getXAPILaunchData`. If any JSON is detected an attempt is made to parse it, else properties are returned as decoded strings.

```
// Location: index.html?endpoint=https%3A%2F%2Fcloud.scorm.com%2Flrs%2FABCDEFGHIJ%2F

const queryParamsString = location.search;
const queryParamsObject = XAPI.getSearchQueryParamsAsObject(queryParamsString);

console.log(queryParamsObject);
// { endpoint: "https://cloud.scorm.com/lrs/ABCDEFGHIJ/" }
```

For backwards compatibility with some LMS/LRS (SCORM Cloud), if an `actor` property is found containing string arrays for `name`, `mbox` and `account` these are coerced into objects containing the first array entry only. Also if the property is `account` the first array object is converted from:&#x20;

`[{ accountServiceHomePage: "...", accountName: "..." }]`

into:

`{ homePage: "...", name: "..." }`

## getAxios

Returns the internal Axios instance. Useful for configuring interceptors or providing other advanced configuration.

```
const xapi = new XAPI({
  ...
});

const axios = xapi.getAbout();

// Do stuff with `axios`
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.xapijs.dev/xapi-wrapper-library/helpers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
