Skip to content

Commit

Permalink
feat: Allow xAPI config for version and adapter to be overriden when …
Browse files Browse the repository at this point in the history
…instantiating CMI5 class
  • Loading branch information
CookieCookson committed Dec 5, 2024
1 parent 5b75a24 commit 93a5c4a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/AbstractCmi5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
Cmi5TerminateStatement,
} from "./Cmi5Statements";
import { AdapterPromise } from "@xapi/xapi/dist/types/adapters";
import { XAPIConfig } from "@xapi/xapi/dist/types/XAPIConfig";

export * from "./interfaces";

Expand All @@ -66,9 +67,14 @@ export default class AbstractCmi5 {
private _initializedDate!: Date;
private _authToken: string | null = null;
private _xapi: XAPI;

constructor(launchParameters: LaunchParameters) {
this._launchParameters = launchParameters;
private _xapiConfig: Pick<XAPIConfig, "adapter" | "version">;

constructor(config: {
launchParameters: LaunchParameters;
xapiConfig: Pick<XAPIConfig, "adapter" | "version">;
}) {
this._launchParameters = config.launchParameters;
this._xapiConfig = config.xapiConfig;
if (!this._launchParameters.fetch) {
throw Error("Unable to construct, no `fetch` parameter found in URL.");
} else if (!this._launchParameters.endpoint) {
Expand Down Expand Up @@ -139,6 +145,7 @@ export default class AbstractCmi5 {
: await this.getAuthTokenFromLMS(this._launchParameters.fetch);
this._authToken = authToken;
this._xapi = new XAPI({
...this._xapiConfig,
endpoint: this._launchParameters.endpoint,
auth: `Basic ${authToken}`,
});
Expand Down
13 changes: 9 additions & 4 deletions src/Cmi5.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import XAPI from "@xapi/xapi";
import { LaunchParameters } from "./interfaces";
import AbstractCmi5 from "./AbstractCmi5";
import { XAPIConfig } from "@xapi/xapi/dist/types/XAPIConfig";

export * from "./interfaces";

export default class Cmi5 extends AbstractCmi5 {
private static _instance: Cmi5 | null = null;

constructor(
launchParameters: LaunchParameters = Cmi5.getLaunchParametersFromLMS()
) {
super(launchParameters);
constructor(config?: {
launchParameters?: LaunchParameters;
xapiConfig?: Pick<XAPIConfig, "adapter" | "version">;
}) {
const launchParameters =
config?.launchParameters ?? Cmi5.getLaunchParametersFromLMS();
const xapiConfig = config?.xapiConfig ?? {};
super({ launchParameters, xapiConfig });
}

static get instance(): Cmi5 {
Expand Down

0 comments on commit 93a5c4a

Please sign in to comment.