This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsndapi.min.js
executable file
·7 lines (7 loc) · 4.07 KB
/
sndapi.min.js
1
2
3
4
5
6
7
/**
* sndapi-js - SND news API ajax call wrapper for the browser
* @version v1.0.2
* @link https://bitbucket.org/schibstednorge/snd-api-js
* @license BSD-2-Clause
*/
!function(e){"use strict";function t(){return this instanceof t?(this._onSuccess=[],this._onError=[],this._state=0,void(this._resolution=null)):new t}t.prototype={success:function(e){return this._onSuccess.push(e),this._refire(),this},fail:function(e){return this._onError.push(e),this._refire(),this},resolve:function(){var e,t=this._resolution||arguments;for(this._state=1,this._resolution=t;e=this._onSuccess.shift();)e.apply(this,t);return this},reject:function(){var e,t=this._resolution||arguments;for(this._state=2,this._resolution=t;e=this._onError.shift();)e.apply(this,t);return this},handleWith:function(e){return this._onSuccess.forEach(function(t){e._onSuccess.push(t)}),this._onError.forEach(function(t){e._onError.push(t)}),this._onSuccess=e._onSuccess,this._onError=e._onError,this._refire(),this},_refire:function(){this._state>0&&(1===this._state?this.resolve():this.reject())}},e.SNDAPI=e.SNDAPI||function(n){function s(){return{tokenIsSet:!!h.token,tokenTimerIsSet:!!h.tokenTimer}}function r(e){if(!f.key)throw new Error("API key is required for SND API initialization");return i(e)}function o(){var e;return e=new Date,e.setHours(e.getHours()+1),e.setMinutes(0),e.setSeconds(0),e.setMilliseconds(0),e}function i(e){var t=u({async:!0},e);return t=u(t,{sign:!1,url:f.signatureServiceUrl+"?api-key="+f.key}),h.tokenTimer&&clearTimeout(h.tokenTimer),h.tokenTimer=setTimeout(i,o()-new Date),c(t).success(function(e,t){var n;if(h.token=e.token,p.length)for(;n=p.shift();)c(n.options).handleWith(n.result)}).fail(function(e){})}function u(e,t){var n,s={},r=Object.hasOwnProperty;for(n in e)r.call(e,n)&&(s[n]=e[n]);if("object"==typeof t)for(n in t)r.call(t,n)&&void 0!==t[n]&&(s[n]=t[n]);return s}function c(n){function s(){return clearTimeout(o._timeouter),"success"===l?y.resolve(d.response,d):y.reject(d)}var r=u({async:!0,url:null,postData:null,contentType:null,preferJSON:!0,sign:!0,timeout:3e4,retries:2},n),o=a(),i=r.postData?"POST":"GET",l=null,d={response:null},y=new t;if(/^([a-z]+:)?\/\//.test(r.url)||(r.url=f.prefixUrl+r.url),!o)return l="fail",y;if(y.req=o,o._timeouter=setTimeout(function(){l="fail",d={statusCode:-1,statusText:"Request timed out",response:null},y.reject(d)},r.timeout),!h.token&&r.sign)return p.push({options:r,result:y}),y;if(o.open(i,r.url,r.async),h.token&&r.sign&&(o.setRequestHeader("X-Snd-Apisignature",h.token),o.setRequestHeader("X-Snd-Apikey",f.key)),r.postData&&(r.contentType&&null!=r.contentType?o.setRequestHeader("Content-type",r.contentType):o.setRequestHeader("Content-type","application/x-www-form-urlencoded")),r.preferJSON&&o.setRequestHeader("Accept","application/json"),o.onreadystatechange=function(){if(4===o.readyState){if(200!==o.status&&304!==o.status){if(403===o.status&&(h.token=null,r.retries))return r.retries--,c(r);l="fail",d={statusCode:o.status,statusText:o.statusText,response:o.responseText}}else if(l="success",d={statusCode:o.status,statusText:o.statusText,response:o.responseText},o.getResponseHeader("Content-Type").match(/^application\/json/)&&JSON)try{d.response=JSON.parse(d.response)}catch(e){}s()}},4===o.readyState)return e.console&&e.console.warn("I did not expect to get here"),null;try{o.send(r.postData)}catch(T){l="fail",d={statusCode:T.code||0,statusText:T.message||"Error caught when trying to send request",response:T},s()}return y}function a(){for(var e=!1,t=0;t<d.length;t++){try{e=d[t]()}catch(n){continue}break}return e}var l,f=u({signatureServiceUrl:"//api.schibsted.tech/proxy/sts/v3/signature",prefixUrl:"//api.schibsted.tech/content/v3/",key:null},n),h={token:null,tokenTimer:null},p=[],d=[function(){return new XMLHttpRequest},function(){return new ActiveXObject("Msxml2.XMLHTTP")},function(){return new ActiveXObject("Msxml3.XMLHTTP")},function(){return new ActiveXObject("Microsoft.XMLHTTP")}];return l={init:r,getState:s,ajax:c}},e.SNDAPI.prototype.SchoenfinkelizedResult=t}("object"==typeof global?global:this),"undefined"!=typeof module&&module.exports&&(module.exports=SNDAPI);