Skip to content

Instantly share code, notes, and snippets.

@k98kurz
Last active November 11, 2015 15:04
Show Gist options
  • Save k98kurz/f00a5565f2b5659b21cc to your computer and use it in GitHub Desktop.
Save k98kurz/f00a5565f2b5659b21cc to your computer and use it in GitHub Desktop.
Another layer of abstraction for ajax requests to restful api endpoints (unfinished)
// Copyleft Jon Tyler, 2015+
// MIT license
// requires jQuery
// requires an api endpoint; array of headers is optional
// probably pointless without modification/extension
function Resource(endpoint, options) {
var settings = {};
settings.endpoint = endpoint;
settings.headers = (typeof options == "object") ? options.headers || [] : [];
settings.getinputs = (typeof options == "object") ? options.getinputs || [] : [];
settings.defaults = (typeof options == "object") ? options.defaults || {} : {};
if (typeof settings.headers !== 'object' || !(settings.headers instanceof Array))
settings.headers = [];
if (typeof settings.getinputs !== 'object' || !(settings.getinputs instanceof Array))
settings.getinputs = [];
// remove excess trailing slashes in the endpoint
while (settings.endpoint[settings.endpoint.length - 1] == "/") {
settings.endpoint = settings.endpoint.substr(0, settings.endpoint.length - 1);
}
settings.endpoint;
// general request logic (i.e. headers + any custom stuff)
function request (options, callback, next) {
if (typeof options !== 'object') options = {};
if (typeof options.headers !== 'object') options.headers = {};
if (typeof options.getinputs !== 'object') options.getinputs = {};
var headers = {}, hd, gd;
// parse headers
hd = typeof settings.defaults.headers == 'object' && JSON.stringify(settings.defaults.headers).length > 2;
for (var h = 0; h < settings.headers.length; ++h) {
if (hd)
headers[settings.headers[h]] = settings.defaults.headers[settings.headers[h]] || '';
if (options.headers[settings.headers[h]])
headers[settings.headers[h]] = options.headers[settings.headers[h]];
}
next(headers, options, callback);
}
function get (url, headers, options, callback) {
// parse GET url input options
gd = typeof settings.defaults.getinputs == 'object' && JSON.stringify(settings.defaults.getinputs).length > 2;
if (options.getinputs.length > 0 || gd) {
url += "?";
for (var h = 0, hl = settings.getinputs.length; h < hl; ++h) {
if (options.getinputs[settings.getinputs[h]])
url += settings.getinputs[h] + "=" + escape(options.getinputs[settings.getinputs[h]]) + (h < hl ? "&" : '');
else if (gd && settings.defaults.getinputs[settings.getinputs[h]])
url += settings.getinputs[h] + "=" + escape(settings.defaults.getinputs[settings.getinputs[h]]) + (h < hl ? "&" : '');
}
}
$.ajax({
datatype: 'text/html',
url : url,
type : 'GET',
complete : function (xhr, statustext) {
if (statustext == 'success') {
callback(xhr.responseJSON);
} else {
callback({"error" : statustext, 'xhr' : xhr});
}
},
headers : headers
});
}
// get a single item
this.show = function(options, callback) {
request(options, callback, function (headers, options) {
var id = options.id, url = settings.endpoint + (typeof id == 'number' && Number.isInteger(id) ? '/' + id : '');
get(url, headers, options, callback);
});
};
// list all items
this.list = function(options, callback) {
request(options, callback, function (headers, options, callback) {
var url = settings.endpoint;
get(url, headers, options, callback);
});
};
// create an item
this.store = function(options, callback) {
request(options, callback, function (headers, options) {
$.ajax({
'url' : settings.endpoint,
'type' : 'POST',
'complete' : function (xhr, statustext) {
if (statustext == 'success') {
callback(xhr.responseJSON);
} else {
callback({"error" : statustext, 'xhr' : xhr});
}
},
'headers' : headers,
'data' : options.data
});
});
};
// update an item
this.update = function(options, callback) {
request(options, callback, function (headers, options) {
options.id = (typeof options.id == 'number') ? options.id | 0 : 0;
$.ajax({
'url' : settings.endpoint + '/' + options.id,
'type' : 'PATCH',
'complete' : function (xhr, statustext) {
if (statustext == 'success') {
callback(xhr.responseJSON);
} else {
callback({"error" : statustext, 'xhr' : xhr});
}
},
'headers' : headers,
'data' : options.data
});
});
};
// delete an item
this.delete = function(options, callback) {
request(options, callback, function (headers, options) {
options.id = (typeof options.id == 'number') ? options.id | 0 : 0;
$.ajax({
'url' : settings.endpoint + '/' + options.id,
'type' : 'DELETE',
'complete' : function (xhr, statustext) {
if (statustext == 'success') {
callback(xhr.responseJSON);
} else {
callback({"error" : statustext, 'xhr' : xhr});
}
},
'headers' : headers
});
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment