Skip to content

Instantly share code, notes, and snippets.

@koohq
Created January 8, 2018 18:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koohq/a37e23656a341ad2d95a5405981c72f9 to your computer and use it in GitHub Desktop.
Save koohq/a37e23656a341ad2d95a5405981c72f9 to your computer and use it in GitHub Desktop.
Provides method for executing SharePoint REST API (based on SP.RequestExecutor.js)
/**
* sp-request.js
*
* (c) 2018 koohq. Licensed under CC0.
* https://creativecommons.org/publicdomain/zero/1.0/legalcode
*/
var SPRequest = (function() {
// constructor
function SPRequest(webUrl) {
this.webUrl = webUrl;
}
// HTTP GET Method
SPRequest.prototype.get = function get(url, options) {
var options = mergeOptions(url, options);
var methodInfo = { method: 'GET' };
return send.call(this, options, methodInfo);
}
// HTTP POST Method
SPRequest.prototype.post = function(url, options) {
return postInternal.call(this, url, options);
}
// HTTP DELETE Method
SPRequest.prototype.delete = function(url, options) {
return postInternal.call(this, url, options, 'DELETE');
}
// HTTP MERGE Method
SPRequest.prototype.merge = function(url, options) {
return postInternal.call(this, url, options, 'MERGE');
};
// HTTP PUT Method
SPRequest.prototype.put = function(url, options) {
return postInternal.call(this, url, options, 'PUT');
};
// Common method for HTTP POST/DELETE/MERGE/PUT
function postInternal(url, options, xHttpMethod) {
var options = mergeOptions(url, options);
var methodInfo = { method: 'POST', body: '' };
if (typeof xHttpMethod === 'string') {
ensureNestedObject(methodInfo, 'headers');
methodInfo.headers['X-HTTP-METHOD'] = xHttpMethod;
}
if (Boolean(options.binaryStringRequestBody)) {
methodInfo.body = options.data;
methodInfo.binaryStringRequestBody = true;
} else {
methodInfo.body = (typeof options.data === 'string') ? options.data : JSON.stringify(options.data);
ensureNestedObject(methodInfo, 'headers');
methodInfo.headers['content-type'] = 'application/json;odata=verbose';
}
if (typeof options.ifMatch === 'string') {
ensureNestedObject(methodInfo, 'headers');
methodInfo.headers['IF-MATCH'] = options.ifMatch;
}
return send.call(this, options, methodInfo);
}
// Common method for HTTP GET/POST/DELETE/MERGE/PUT
function send(options, methodInfo) {
var requestInfo = cloneJSONObject(methodInfo);
if (typeof options.url !== 'string') {
throw new TypeError('url or options.url is required.');
} else {
requestInfo.url = options.url;
}
var headers = options.headers;
if (typeof headers === 'object') {
var keys = Object.keys(headers);
if (keys.length > 0) {
ensureNestedObject(requestInfo, 'headers');
keys.forEach(function(key) {
requestInfo.headers[key] = headers[key];
});
}
}
if (typeof options.timeout === 'number') {
requestInfo.timeout = options.timeout;
}
var deserialize;
if (Boolean(options.binaryStringResponseBody)) {
requestInfo.binaryStringResponseBody = true;
deserialize = getBody;
} else {
ensureNestedObject(requestInfo, 'headers');
requestInfo.headers['accept'] = 'application/json;odata=verbose';
deserialize = getParsedBody;
}
var webUrl = this.webUrl;
return this.setup()
.then(function() {
return new Promise(function(resolve, reject) {
var executor = new SP.RequestExecutor(webUrl);
requestInfo.success = resolve;
requestInfo.error = reject;
executor.executeAsync(requestInfo);
})
.then(deserialize, deserialize);
});
}
// Ensure SP.RequestExecutor.js loaded
SPRequest.prototype.setup = function() {
var thenable;
if (typeof this.webUrl === 'undefined') {
throw new TypeError('SPRequest.prototype.webUrl is required.')
}
var webUrl = this.webUrl;
var callback;
if (typeof SP === 'undefined' || typeof SP.RequestExecutor === 'undefined') {
callback = function(resolve, reject) { appendScript(webUrl + '/_layouts/15/SP.RequestExecutor.js', resolve, reject); };
thenable = new Promise(callback);;
} else {
callback = function(resolve) { resolve(); }
thenable = new Promise(callback);
}
return thenable;
};
function mergeOptions(urlOrOptions, options) {
var o;
switch (typeof urlOrOptions) {
case 'string':
var u = urlOrOptions;
if (typeof options === 'object') {
o = cloneJSONObject(urlOrOptions);
if (typeof o.url === 'undefined') {
o.url = urlOrOptions;
}
} else {
o = { url: u };
}
break;
case 'object':
o = cloneJSONObject(urlOrOptions);
break;
default:
break;
}
return o;
}
function getBody(data) {
return (typeof data.body === 'undefined') ? '' : data.body;
}
function getParsedBody(data) {
var u;
return (typeof data.body === 'undefined') ? u : JSON.parse(data.body);
}
function appendScript(src, onLoad, onError) {
var script = document.createElement('script');
script.src = src;
(typeof onLoad === 'function') && (script.onload = onLoad);
(typeof onError === 'function') && (script.onerror = onError);
document.head.appendChild(script);
}
function ensureNestedObject(obj, key) {
if (typeof obj[key] === 'undefined') {
obj[key] = {};
}
return obj;
}
function cloneJSONObject(obj) {
return JSON.parse(JSON.stringify(obj));
}
return SPRequest;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment