Helpers

A selection of useful helpers for converting data into the required formats.

toBasicAuth

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

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.

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. getXAPILaunchData is a static method of the XAPI Class.

XAPI.getXAPILaunchData().then((result) => {
const launchData: XAPILaunchData = result.data;
this.xapi = new XAPI(launchData.endpoint);
this.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.

const tinCanLaunchData: TinCanLaunchParameters = XAPI.getTinCanLaunchData();
const xapi = new XAPI(tinCanLaunchData.endpoint, 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, if an actor property is found containing string arrays for name, mbox and account these are converted into objects containing the first array entry only. Also if the property is account the first array object is converted from:

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

into:

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