spws
Version:
SharePoint Web Services Wrapper
91 lines • 4.49 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// Classes
var spwsError_1 = __importDefault(require("./spwsError"));
// Utils
var escapeXml_1 = __importDefault(require("../utils/escapeXml"));
var SpwsRequest = /** @class */ (function () {
function SpwsRequest(_a) {
var webURL = _a.webURL, webService = _a.webService, soapAction = _a.soapAction;
var _this = this;
this.escapeXml = escapeXml_1.default;
this.envelope = "";
/**
* Adds the body XML between the header and footer.
*
* @param body The body XML
* @example
* // Creates an envelope for a getList request
* createEnvelope(`<GetList xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>Announcements</listName></GetList>`)
*/
this.createEnvelope = function (body) {
_this.envelope = "\n <soap:Envelope\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n <soap:Body>\n ".concat(body, "\n </soap:Body>\n </soap:Envelope>");
return _this;
};
this.send = function () {
return new Promise(function (resolve, reject) {
// Create callback
_this.xhr.onreadystatechange = function () {
if (_this.xhr.readyState === XMLHttpRequest.DONE) {
// Create responseXML object as sometimes the xml may be empty
var responseXML = _this.xhr.responseXML;
// If responseXML is null
if (!responseXML) {
// Create parser
var parser = new DOMParser();
try {
// Assign parsed response text to response XML
responseXML = parser.parseFromString(_this.xhr.responseText || "", "text/xml");
}
catch (error) {
// If an error occurs, assign responseXML as an empty document
responseXML = parser.parseFromString("", "text/xml");
}
}
var xData = {
responseText: _this.xhr.responseText,
responseXML: responseXML,
status: _this.xhr.status,
statusText: _this.xhr.statusText,
envelope: _this.envelope,
message: "",
};
if (_this.xhr.status === 200) {
resolve(xData);
}
else {
switch (_this.xhr.status) {
case 0:
xData.responseText = xData.responseText || "Unknown error with xhr status of 0";
break;
default:
break;
}
// Create message string
xData.message = xData.statusText || xData.responseText;
// Create response error
reject(new spwsError_1.default(xData));
}
}
};
// Send Request
_this.xhr.send(_this.envelope);
});
};
// Create XHR
var xhr = new XMLHttpRequest();
xhr.open("POST", "".concat(webURL, "/_vti_bin/").concat(webService, ".asmx"));
xhr.setRequestHeader("Content-Type", "text/xml; charset=\"utf-8\"");
// If soap action needed, set header
if (soapAction)
xhr.setRequestHeader("SOAPAction", soapAction);
// Assign XHR to class
this.xhr = xhr;
}
return SpwsRequest;
}());
exports.default = SpwsRequest;
//# sourceMappingURL=spwsRequest.js.map