ideal-postcodes-core
Version:
Ideal Postcodes core frontend javascript library
8 lines (7 loc) • 12.6 kB
JavaScript
/**
* ideal-postcodes-core - Ideal Postcodes core frontend javascript library
* @version v1.0.0
* @link https://ideal-postcodes.co.uk/
* @license MIT
*/
var __extends=this&&this.__extends||function(e,t){function r(){this.constructor=e}for(var o in t)t.hasOwnProperty(o)&&(e[o]=t[o]);e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)},IdealPostcodes;!function(e){e.API_URL="api.ideal-postcodes.co.uk",e.TLS=!0,e.VERSION="v1",e.DEFAULT_TIMEOUT=1e4,e.STRICT_AUTHORISATION=!1,e.removeGlobalReference=function(){root&&root.IdealPostcodes&&(root.IdealPostcodes=void 0)}}(IdealPostcodes||(IdealPostcodes={}));var freeGlobal="object"==typeof global&&global&&global.Object===Object&&global,root=freeGlobal||Function("return this")(),freeExports="object"==typeof exports&&exports&&!exports.nodeType&&exports,freeModule=freeExports&&"object"==typeof module&&module&&!module.nodeType&&module;"function"==typeof define&&"object"==typeof define.amd&&define.amd?define(function(){return IdealPostcodes}):freeModule&&(freeModule.exports=IdealPostcodes,freeExports.IdealPostcodes=IdealPostcodes),root.IdealPostcodes=IdealPostcodes;var IdealPostcodes;!function(e){var t;!function(e){e.now=function(){return Date.now()},e.debounce=function(t,r){function o(){var c=e.now()-a;c>0&&c<r?n=setTimeout(o,r-c):(n=null,u=t.apply(i,s),n||(i=s=null))}void 0===r&&(r=100);var n,s,i,a,u;return function(){return i=this,s=arguments,a=e.now(),n||(n=setTimeout(o,r)),u}},e.extend=function(e){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];for(var o=t.length,n=0;n<o;n++){var s=t[n];for(var i in s)void 0!==s[i]&&(e[i]=s[i])}return e}}(t=e.Utils||(e.Utils={}))}(IdealPostcodes||(IdealPostcodes={}));var IdealPostcodes;!function(e){var t=["id","postcode","query","limit","page","post_town","postcode_outward","filter"],r=function(e){return t.map(function(t){return[t,e[t]]}).filter(function(e){return void 0!==e[1]}).map(function(e){return e.join("=")}).join("|")},o=function(){function e(){this.initialiseStore(),this.active=!0}return e.prototype.disable=function(){this.active=!1},e.prototype.enable=function(){this.active=!0},e.prototype.initialiseStore=function(){this.store={postcodeStore:{},addressStore:{},autocompleteStore:{},udprnStore:{},umprnStore:{}}},e.prototype.cacheAddressQuery=function(e,t){if(this.active){var o=r(e);this.store.addressStore[o]=t}},e.prototype.getAddressQuery=function(e){if(this.active){var t=r(e);return this.store.addressStore[t]}},e.prototype.cachePostcodeQuery=function(e,t){if(this.active){var o=r(e);this.store.postcodeStore[o]=t}},e.prototype.getPostcodeQuery=function(e){if(this.active){var t=r(e);return this.store.postcodeStore[t]}},e.prototype.cacheAutocompleteQuery=function(e,t){if(this.active){var o=r(e);this.store.autocompleteStore[o]=t}},e.prototype.getAutocompleteQuery=function(e){if(this.active){var t=r(e);return this.store.autocompleteStore[t]}},e.prototype.cacheUdprnQuery=function(e,t){if(this.active){var o=r(e);this.store.udprnStore[o]=t}},e.prototype.getUdprnQuery=function(e){if(this.active){var t=r(e);return this.store.udprnStore[t]}},e.prototype.cacheUmprnQuery=function(e,t){if(this.active){var o=r(e);this.store.umprnStore[o]=t}},e.prototype.getUmprnQuery=function(e){if(this.active){var t=r(e);return this.store.umprnStore[t]}},e}();e.Cache=o}(IdealPostcodes||(IdealPostcodes={}));var IdealPostcodes;!function(e){var t;!function(e){e.blankRe=/^\s*$/,e.AllowedAuthorizationParameters=["api_key"],e.detectTls=function(e){try{return"http:"!==e.location.protocol}catch(e){return!0}},e.legacyBrowser=function(t){var r=e.isIE(t),o=e.isOpera(t);return!!(r&&r<=9)||!!(o&&o<=12)},e.isIE=function(e){var t=e?e.navigator:window.navigator;try{var r=t.userAgent.toLowerCase();return r.indexOf("msie")!==-1&&parseInt(r.split("msie")[1])}catch(e){return!1}},e.isOpera=function(e){var t=e?e.opera:window.opera;if(!t)return!1;if("[object Opera]"!==Object.prototype.toString.call(t))return!1;try{var r=parseInt(t.version(),10);return!isNaN(r)&&r}catch(e){return!1}},e.generateQueryString=function(e){var t=[];for(var r in e)t.push(encodeURIComponent(r)+"="+encodeURIComponent(e[r]));return t.join("&")},e.constructHeaders=function(t){var r={};return r.Authorization=e.constructAuthenticationHeader(t),r},e.deconstructAuthenticationHeader=function(e){var t={};return e?(e.replace("IDEALPOSTCODES ","").trim().split(" ").forEach(function(e){var r=e.split("=");"string"==typeof r[0]&&"string"==typeof r[1]&&(t[r[0]]=r[1].replace(/(^"|"$)/g,""))}),t):t},e.constructAuthenticationHeader=function(t){for(var r=[],o=0;o<e.AllowedAuthorizationParameters.length;o++){var n=e.AllowedAuthorizationParameters[o];void 0!==t[n]&&r.push(n+'="'+t[n]+'"')}return 0===r.length?"":"IDEALPOSTCODES "+r.join(" ")},e.constructQueryString=function(e){var t={};return e.filter&&(t.filter=e.filter.join(",")),e.licensee&&(t.licensee=e.licensee),e.tags&&(t.tags=e.tags.join(",")),t},e.constructAutocompleteQueryString=function(e){var t={};return t.query=e.query,e.limit&&(t.limit=e.limit),e.postcode_outward&&(t.postcode_outward=e.postcode_outward.join(",")),e.post_town&&(t.post_town=e.post_town.join(",")),t},e.constructAddressQueryString=function(e){var t={};return t.query=e.query,t.page=e.page||0,t.limit=e.limit||10,e.postcode_outward&&(t.postcode_outward=e.postcode_outward.join(",")),e.post_town&&(t.post_town=e.post_town.join(",")),t}}(t=e.Transport||(e.Transport={}))}(IdealPostcodes||(IdealPostcodes={}));var IdealPostcodes;!function(e){var t;!function(e){var t=function(e){function t(t){var r=e.call(this)||this;return r.message="Ideal Postcodes Error: "+t.message,r}return __extends(t,e),t}(Error);e.IdealPostcodesError=t;var r=function(e){function t(){return e.call(this,{message:"Unable to parse JSON response"})||this}return __extends(t,e),t}(t);e.JsonParseError=r}(t=e.Errors||(e.Errors={}))}(IdealPostcodes||(IdealPostcodes={}));var IdealPostcodes;!function(e){var t;!function(e){e.parse=function(t){var o=t.status;if(200!==o){switch(o){case 503:return new r}try{return e.parseErrorResponse(JSON.parse(t.responseText),o)}catch(t){return new e.JsonParseError}}},e.parseErrorResponse=function(e,r){var o=e.code,s=e.message;return void 0===o||void 0===s?new n:new t({responseCode:o,status:r,message:s})};var t=function(e){function t(t){var r=e.call(this,t)||this;return t.status&&(r.status=t.status),t.responseCode&&(r.responseCode=t.responseCode),r}return __extends(t,e),t}(e.IdealPostcodesError);e.IdealPostcodesApiError=t;var r=function(e){function t(){return e.call(this,{status:503,message:"Rate Limit Reached. Please wait a while before you retry your request"})||this}return __extends(t,e),t}(t);e.RateLimitError=r;var o=function(e){function t(){return e.call(this,{message:"Request timed out"})||this}return __extends(t,e),t}(t);e.RequestTimeoutError=o;var n=function(e){function t(){return e.call(this,{message:"Unknown AJAX error occurred when accessing API"})||this}return __extends(t,e),t}(t);e.GenericApiError=n}(t=e.Errors||(e.Errors={}))}(IdealPostcodes||(IdealPostcodes={}));var IdealPostcodes;!function(e){var t;!function(t){t.getXhr=function(){try{return new(XMLHttpRequest||ActiveXObject)("MSXML2.XMLHTTP.3.0")}catch(e){return null}},t.xhrRequest=function(r,o){var n=r.url,s=t.generateQueryString(r.queryString);s.length>0&&(n+="?"+s);var i=t.getXhr();i.open(r.method,n,!0);try{for(var a in r.headers)i.setRequestHeader(a,r.headers[a])}catch(e){}var u=setTimeout(function(){i.onreadystatechange=function(){},i.abort(),o(new Error("Request timeout"),null,i)},r.timeout);return i.onreadystatechange=function(){var r;if(4===i.readyState){if(clearTimeout(u),200!==i.status)return o(e.Errors.parse(i),{},i);try{r=t.blankRe.test(i.responseText)?{}:JSON.parse(i.responseText)}catch(e){return o(new Error("parsererror"),null,i)}return o(null,r,i)}},i.send(r.data),i}}(t=e.Transport||(e.Transport={}))}(IdealPostcodes||(IdealPostcodes={}));var IdealPostcodes;!function(e){var t;!function(t){var r=0,o=function(){},n=function(r,o){r.queryString.callback=o;var n=r.headers,s=t.deconstructAuthenticationHeader(n.Authorization);return e.Utils.extend(r.queryString,s),t.generateQueryString(r.queryString)},s=function(e){var t=e.code;return t&&"number"==typeof t?parseInt(String(t).slice(0,3)):500};t.jsonpRequest=function(t,i){r+=1;var a=t.url;if(t.method&&"get"!==t.method.toLowerCase())return i(new Error("Browser is unable to perform non-GET requests"),null,null),null;var u="idpc_"+e.Utils.now()+"_"+r,c=n(t,u);c.length>0&&(a+="?"+c);var l=document.getElementsByTagName("script")[0]||document.head,d=setTimeout(function(){p(),i(new Error("Request timeout"),null,null)},t.timeout),p=function(){h.parentNode&&h.parentNode.removeChild(h),window[u]=o,d&&clearTimeout(d)};window[u]=function(t){p();var r=s(t),o={responseText:t,status:r};return 200!==o.status?i(e.Errors.parseErrorResponse(t,r),null,o):i(null,t,o)};var h=document.createElement("script");return h.src=a,h.type="text/javascript",l.parentNode.insertBefore(h,l),null}}(t=e.Transport||(e.Transport={}))}(IdealPostcodes||(IdealPostcodes={}));var IdealPostcodes;!function(e){var t;!function(t){t.defaultHeaders={Accept:"text/javascript, application/javascript"},t.request=function(r,o){var n={url:r.url,method:r.method||"GET",headers:r.headers||{},queryString:r.queryString||{},timeout:r.timeout||e.DEFAULT_TIMEOUT,data:r.data||null};return e.Utils.extend(n.headers,t.defaultHeaders),t.legacyBrowser()?t.jsonpRequest(n,o):t.xhrRequest(n,o)}}(t=e.Transport||(e.Transport={}))}(IdealPostcodes||(IdealPostcodes={}));var IdealPostcodes;!function(e){var t=e.Utils.extend,r=e.Transport,o=r.constructHeaders,n=r.constructQueryString,s=r.constructAddressQueryString,i=r.constructAutocompleteQueryString,a=function(){function r(t){void 0===t&&(t={});var r=this;this.api_key=t.api_key,this.tls=void 0===t.tls?e.TLS:t.tls,this.version=void 0===t.version?e.VERSION:t.version,this.baseUrl=void 0===t.baseUrl?e.API_URL:t.baseUrl,this.strictAuthorisation=void 0===t.strictAuthorisation?e.STRICT_AUTHORISATION:t.strictAuthorisation,this.cache=new e.Cache;var o=this;this.autocompleteCallback=function(){},this.debouncedAutocomplete=e.Utils.debounce(function(e){r.lookupAutocomplete(e,o.autocompleteCallback)})}return r.prototype.apiUrl=function(){return"http"+(this.tls?"s":"")+"://"+this.baseUrl+"/"+this.version},r.prototype.ping=function(t){e.Transport.request({url:"http"+(this.tls?"s":"")+"://"+this.baseUrl},t)},r.prototype.lookupPostcode=function(t,r){var s=this;t.api_key=this.api_key;var i=o(t),a=n(t),u=this.cache.getPostcodeQuery(t);return u?r(null,u):void e.Transport.request({url:this.apiUrl()+"/postcodes/"+encodeURIComponent(t.postcode),headers:i,queryString:a},function(e,o,n){return e&&4040===e.responseCode?r(null,[],n):e?r(e,null,n):(s.cache.cachePostcodeQuery(t,o.result),r(null,o.result,n))})},r.prototype.lookupAddress=function(r,i){var a=this;r.api_key=this.api_key;var u=o(r),c=n(r);t(c,s(r));var l=this.cache.getAddressQuery(r);return l?i(null,l):void e.Transport.request({url:this.apiUrl()+"/addresses",headers:u,queryString:c},function(e,t,o){return e?i(e,null,o):(a.cache.cacheAddressQuery(r,t.result),i(null,t.result,o))})},r.prototype.lookupAutocomplete=function(r,s){var a=this;r.api_key=this.api_key;var u=o(r),c=n(r);t(c,i(r));var l=this.cache.getAutocompleteQuery(r);return l?s(null,l,null,r):(this.strictAuthorisation||(c.api_key=this.api_key,delete u.Authorization),void e.Transport.request({url:this.apiUrl()+"/autocomplete/addresses",headers:u,queryString:c},function(e,t,o){return e?s(e,null,o,r):(a.cache.cacheAutocompleteQuery(r,t.result),s(null,t.result,o,r))}))},r.prototype.lookupUdprn=function(t,r){var s=this;t.api_key=this.api_key;var i=o(t),a=n(t),u=this.cache.getUdprnQuery(t);return u?r(null,u):void e.Transport.request({url:this.apiUrl()+"/udprn/"+t.id,headers:i,queryString:a},function(e,o,n){return e?r(e,null,n):(s.cache.cacheUdprnQuery(t,o.result),r(null,o.result,n))})},r.prototype.lookupUmprn=function(t,r){var s=this;t.api_key=this.api_key;var i=o(t),a=n(t),u=this.cache.getUmprnQuery(t);return u?r(null,u):void e.Transport.request({url:this.apiUrl()+"/umprn/"+t.id,headers:i,queryString:a},function(e,o,n){return e?r(e,null,n):(s.cache.cacheUmprnQuery(t,o.result),r(null,o.result,n))})},r.prototype.checkKeyUsability=function(t,r){e.Transport.request({url:this.apiUrl()+"/keys/"+this.api_key,queryString:n(t)},function(e,t,o){return e?r(e,null,o):r(null,t.result,o)})},r.prototype.autocompleteAddress=function(e){this.debouncedAutocomplete(e)},r.prototype.registerAutocompleteCallback=function(e){this.autocompleteCallback=e},r}();e.Client=a}(IdealPostcodes||(IdealPostcodes={}));