Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom headers to the XHR Request #338

Merged
merged 3 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ The APIs include several settings to customize the functionality of each API:
| `mastery_override` | false | true/false | (SCORM 1.2) Used to override a module's `cmi.core.lesson_status` so that a pass/fail is determined based on a mastery score and the user's raw score, rather than using whatever status is provided by the module. An example of this would be if a module is published using a `Complete/Incomplete` final status, but the LMS always wants to receive a `Passed/Failed` for quizzes, then we can use this setting to override the given final status. |
| `selfReportSessionTime` | false | true/false | Should the API override the default `session_time` reported by the module? Useful when modules don't properly report time. |
| `alwaysSendTotalTime` | false | true/false | Should the API always send `total_time` when committing to the LMS |
| `xhrWithCredentials` | false | true/false | Sets the withCredentials flag on the request to the LMS |
| `xhrHeaders` | {} | Object | This allows setting of additional headers on the request to the LMS where the key should be the header name and the value is the value of the header you want to send |
| `responseHandler` | function | | A function to properly tranform the response from the LMS to the correct format. The APIs expect the result from the LMS to be in the following format (errorCode is optional): `{ "result": true, "errorCode": 0 }` |

## Initial Values
Expand Down
12 changes: 11 additions & 1 deletion dist/aicc.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions dist/aicc.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,8 @@ var BaseAPI = /*#__PURE__*/function () {
selfReportSessionTime: false,
alwaysSendTotalTime: false,
strict_errors: true,
xhrHeaders: {},
xhrWithCredentials: false,
responseHandler: function responseHandler(xhr) {
var result;

Expand Down Expand Up @@ -1711,6 +1713,14 @@ var BaseAPI = /*#__PURE__*/function () {
var httpReq = new XMLHttpRequest();
httpReq.open('POST', url, settings.asyncCommit);

if (Object.keys(settings.xhrHeaders).length) {
Object.keys(settings.xhrHeaders).forEach(function (header) {
httpReq.setRequestHeader(header, settings.xhrHeaders[header]);
});
}

httpReq.withCredentials = settings.xhrWithCredentials;

if (settings.asyncCommit) {
httpReq.onload = function (e) {
if (typeof settings.responseHandler === 'function') {
Expand Down
12 changes: 11 additions & 1 deletion dist/scorm-again.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions dist/scorm-again.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,8 @@ var BaseAPI = /*#__PURE__*/function () {
selfReportSessionTime: false,
alwaysSendTotalTime: false,
strict_errors: true,
xhrHeaders: {},
xhrWithCredentials: false,
responseHandler: function responseHandler(xhr) {
var result;

Expand Down Expand Up @@ -1711,6 +1713,14 @@ var BaseAPI = /*#__PURE__*/function () {
var httpReq = new XMLHttpRequest();
httpReq.open('POST', url, settings.asyncCommit);

if (Object.keys(settings.xhrHeaders).length) {
Object.keys(settings.xhrHeaders).forEach(function (header) {
httpReq.setRequestHeader(header, settings.xhrHeaders[header]);
});
}

httpReq.withCredentials = settings.xhrWithCredentials;

if (settings.asyncCommit) {
httpReq.onload = function (e) {
if (typeof settings.responseHandler === 'function') {
Expand Down
12 changes: 11 additions & 1 deletion dist/scorm12.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions dist/scorm12.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ var BaseAPI = /*#__PURE__*/function () {
selfReportSessionTime: false,
alwaysSendTotalTime: false,
strict_errors: true,
xhrHeaders: {},
xhrWithCredentials: false,
responseHandler: function responseHandler(xhr) {
var result;

Expand Down Expand Up @@ -1590,6 +1592,14 @@ var BaseAPI = /*#__PURE__*/function () {
var httpReq = new XMLHttpRequest();
httpReq.open('POST', url, settings.asyncCommit);

if (Object.keys(settings.xhrHeaders).length) {
Object.keys(settings.xhrHeaders).forEach(function (header) {
httpReq.setRequestHeader(header, settings.xhrHeaders[header]);
});
}

httpReq.withCredentials = settings.xhrWithCredentials;

if (settings.asyncCommit) {
httpReq.onload = function (e) {
if (typeof settings.responseHandler === 'function') {
Expand Down
12 changes: 11 additions & 1 deletion dist/scorm2004.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions dist/scorm2004.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ var BaseAPI = /*#__PURE__*/function () {
selfReportSessionTime: false,
alwaysSendTotalTime: false,
strict_errors: true,
xhrHeaders: {},
xhrWithCredentials: false,
responseHandler: function responseHandler(xhr) {
var result;

Expand Down Expand Up @@ -1590,6 +1592,14 @@ var BaseAPI = /*#__PURE__*/function () {
var httpReq = new XMLHttpRequest();
httpReq.open('POST', url, settings.asyncCommit);

if (Object.keys(settings.xhrHeaders).length) {
Object.keys(settings.xhrHeaders).forEach(function (header) {
httpReq.setRequestHeader(header, settings.xhrHeaders[header]);
});
}

httpReq.withCredentials = settings.xhrWithCredentials;

if (settings.asyncCommit) {
httpReq.onload = function (e) {
if (typeof settings.responseHandler === 'function') {
Expand Down
11 changes: 11 additions & 0 deletions src/BaseAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export default class BaseAPI {
selfReportSessionTime: false,
alwaysSendTotalTime: false,
strict_errors: true,
xhrHeaders: {},
xhrWithCredentials: false,
responseHandler: function(xhr) {
let result;
if (typeof xhr !== 'undefined') {
Expand Down Expand Up @@ -1093,6 +1095,15 @@ export default class BaseAPI {
if (!settings.sendBeaconCommit) {
const httpReq = new XMLHttpRequest();
httpReq.open('POST', url, settings.asyncCommit);

if(Object.keys(settings.xhrHeaders).length) {
Object.keys(settings.xhrHeaders).forEach((header) => {
httpReq.setRequestHeader(header, settings.xhrHeaders[header])
})
}

httpReq.withCredentials = settings.xhrWithCredentials

if (settings.asyncCommit) {
httpReq.onload = function(e) {
if (typeof settings.responseHandler === 'function') {
Expand Down