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.
1
const username = "username";
2
const password = "password";
3
const auth = XAPI.toBasicAuth(username, password);
4
console.log(auth); // Basic dXNlcm5hbWU6cGFzc3dvcmQ=
Copied!

calculateISO8601Duration

Useful for calculating ISO8601 durations when submitting statements that require durations. calculateISO8601Duration is a static method of the XAPI Class.
1
const startDate: Date = new Date();
2
const endDate: Date = new Date(startDate);
3
endDate.setDate(endDate.getDate() + 1);
4
const duration = XAPI.calculateISO8601Duration(startDate, endDate));
5
console.log(duration); // P1DT
Copied!

getXAPILaunchData

Gets the launch data if the module was launched using xAPI Launch. getXAPILaunchData is a static method of the XAPI Class.
1
XAPI.getXAPILaunchData().then((result) => {
2
const launchData: XAPILaunchData = result.data;
3
this.xapi = new XAPI({
4
endpoint: launchData.endpoint
5
});
6
const actor = launchData.actor;
7
});
Copied!

getTinCanLaunchData

Gets the launch data if the module was launched as a TinCan module. getTinCanLaunchData is a static method of the XAPI Class.
1
const tinCanLaunchData: TinCanLaunchParameters = XAPI.getTinCanLaunchData();
2
const xapi = new XAPI({
3
endpoint: tinCanLaunchData.endpoint,
4
auth: tinCanLaunchData.auth
5
});
6
const actor = tinCanLaunchData.actor;
Copied!

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.
1
// Location: index.html?endpoint=https%3A%2F%2Fcloud.scorm.com%2Flrs%2FABCDEFGHIJ%2F
2
3
const queryParamsString = location.search;
4
const queryParamsObject = XAPI.getSearchQueryParamsAsObject(queryParamsString);
5
6
console.log(queryParamsObject);
7
// { endpoint: "https://cloud.scorm.com/lrs/ABCDEFGHIJ/" }
Copied!
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:
[{ accountServiceHomePage: "...", accountName: "..." }]
into:
{ homePage: "...", name: "..." }
Last modified 1mo ago