Created
May 1, 2017 18:37
-
-
Save eob/0226a5d4fb6011ed1977d730c8a6695c to your computer and use it in GitHub Desktop.
jwt-ajax.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.JwtAjax = window.JwtAjax || { | |
/* | |
* Utils.. | |
*/ | |
getURLParameter: function(name) { | |
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || | |
[null, ''])[1].replace(/\+/g, '%20')) || null; | |
}, | |
/* | |
* These are useful even as inputs to a plain iron-ajax | |
* ======================================================== | |
*/ | |
/* | |
* | |
*/ | |
getHeaders: function() { | |
// First see if there's a token in the query string (for override) | |
var token = window.JwtAjax.getURLParameter('token'); | |
// Next see if there's one in an iron-meta tag | |
if (!token) { | |
token = document.createElement('iron-meta').byKey('jwt-token'); | |
} | |
if (!token) { | |
// TODO: Check session? Localstore? | |
return {}; | |
} | |
return { | |
"Authorization": "Bearer: " + token | |
} | |
}, | |
getApiDomain: function() { | |
// First see if there's a token in the query string (for override) | |
var api = window.JwtAjax.getURLParameter('api'); | |
// Next see if there's one in an iron-meta tag | |
if (!api) { | |
api = document.createElement('iron-meta').byKey('api-domain'); | |
} | |
// Finally return the default | |
api = "https://hdvh2pvekj.execute-api.us-west-2.amazonaws.com/production/" | |
return api; | |
}, | |
/* | |
* These are useful when creating ajax requests from js | |
*/ | |
post: function(path, body, params, cb) { | |
return window.JwtAjax._req('POST', path, body, params, cb); | |
}, | |
put: function(path, body, params, cb) { | |
return window.JwtAjax._req('PUT', path, body, params, cb); | |
}, | |
get: function(path, params, cb) { | |
return window.JwtAjax._req('GET', path, undefined, params, cb); | |
}, | |
_req: function(method, urlPath, body, params, cb) { | |
var headers = window.JwtAjax.getHeaders(); | |
var apiDomain = window.JwtAjax.getHeaders(); | |
// Guarantee a trailing slash on the prefix | |
if (apiDomain[apiDomain.length - 1] != '/') apiDomain = apiDomain + '/'; | |
// Guarantee no starting slash on the sufix | |
if (urlPath.length && (urlPath[0] == '/')) urlPath = urlPath.slice(1); | |
var ia = document.createElement('iron-ajax'); | |
ia.url = apiDomain + urlPath; | |
ia.method = method; | |
ia.headers = headers; | |
var self = this; | |
if (body && (method == 'post')) { | |
ia.handleAs = 'json'; | |
ia.headers['Content-Type'] = 'application/json' | |
ia.body = JSON.stringify(body); | |
} | |
ia.addEventListener('response', function (resp) { | |
if (resp && resp.detail && resp.detail.response) { | |
var theResponse; | |
if (typeof resp.detail.response == 'string') { | |
try { | |
var r = JSON.parse(resp.detail.response); | |
theResponse = r; | |
} | |
catch (e) { | |
theResponse = resp.detail.response; | |
} | |
} | |
else { | |
theResponse = resp.detail.response; | |
} | |
if (theResponse.error) { | |
self.showError(theResponse.msg || theResponse.message, theResponse.upgrade); | |
cb(resp.detail.response); | |
} | |
else { | |
cb(null, resp.detail.response); | |
} | |
} | |
else { | |
var res = { error: true, msg: "Could not post data" }; | |
self.showError(res.msg); | |
cb(res); | |
} | |
}); | |
ia.addEventListener('error', function (resp) { | |
var res = { error: true, msg: "Could not post data -- got error response." }; | |
self.showError(res.msg); | |
cb(res); | |
}); | |
ia.generateRequest(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment