UNPKG

prismic-javascript

Version:
18 lines (15 loc) 18.2 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("cross-fetch")):"function"==typeof define&&define.amd?define(["cross-fetch"],e):(t=t||self).PrismicJS=e(t.fetch)}(this,(function(t){"use strict";t=t&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ var e=function(){return(e=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)},n=function(){function t(t){this.data={},this.data=t}return t.prototype.id=function(){return this.data.id},t.prototype.ref=function(){return this.data.ref},t.prototype.label=function(){return this.data.label},t}(),r=function(){function t(t){this.data={},this.data=t,this.variations=(t.variations||[]).map((function(t){return new n(t)}))}return t.prototype.id=function(){return this.data.id},t.prototype.googleId=function(){return this.data.googleId},t.prototype.name=function(){return this.data.name},t}(),i=function(){function t(t){t&&(this.drafts=(t.drafts||[]).map((function(t){return new r(t)})),this.running=(t.running||[]).map((function(t){return new r(t)})))}return t.prototype.current=function(){return this.running.length>0?this.running[0]:null},t.prototype.refFromCookie=function(t){if(!t||""===t.trim())return null;var e=t.trim().split(" ");if(e.length<2)return null;var n=e[0],r=parseInt(e[1],10),i=this.running.filter((function(t){return t.googleId()===n&&t.variations.length>r}))[0];return i?i.variations[r].ref():null},t}(),o=function(){function t(t,e){this.id=t,this.api=e,this.fields={}}return t.prototype.set=function(t,e){return this.fields[t]=e,this},t.prototype.ref=function(t){return this.set("ref",t)},t.prototype.query=function(t){return this.set("q",t)},t.prototype.pageSize=function(t){return this.set("pageSize",t)},t.prototype.fetch=function(t){return console.warn("Warning: Using Fetch is deprecated. Use the property `graphQuery` instead."),this.set("fetch",t)},t.prototype.fetchLinks=function(t){return console.warn("Warning: Using FetchLinks is deprecated. Use the property `graphQuery` instead."),this.set("fetchLinks",t)},t.prototype.graphQuery=function(t){return this.set("graphQuery",t)},t.prototype.lang=function(t){return this.set("lang",t)},t.prototype.page=function(t){return this.set("page",t)},t.prototype.after=function(t){return this.set("after",t)},t.prototype.orderings=function(t){return this.set("orderings",t)},t.prototype.url=function(){var e=this;return this.api.get().then((function(n){return t.toSearchForm(e,n).url()}))},t.prototype.submit=function(e){var n=this;return this.api.get().then((function(r){return t.toSearchForm(n,r).submit(e)}))},t.toSearchForm=function(t,e){var n=e.form(t.id);if(n)return Object.keys(t.fields).reduce((function(e,n){var r=t.fields[n];return"q"===n?e.query(r):"pageSize"===n?e.pageSize(r):"fetch"===n?e.fetch(r):"fetchLinks"===n?e.fetchLinks(r):"graphQuery"===n?e.graphQuery(r):"lang"===n?e.lang(r):"page"===n?e.page(r):"after"===n?e.after(r):"orderings"===n?e.orderings(r):e.set(n,r)}),n);throw new Error("Unable to access to form "+t.id)},t}(),u=function(){function t(t,e){for(var n in this.httpClient=e,this.form=t,this.data={},t.fields)t.fields[n].default&&(this.data[n]=[t.fields[n].default])}return t.prototype.set=function(t,e){var n=this.form.fields[t];if(!n)throw new Error("Unknown field "+t);var r=""===e||void 0===e?null:e,i=this.data[t]||[];return i=n.multiple?r?i.concat([r]):i:r?[r]:i,this.data[t]=i,this},t.prototype.ref=function(t){return this.set("ref",t)},t.prototype.query=function(t){if("string"==typeof t)return this.query([t]);if(Array.isArray(t))return this.set("q","["+t.join("")+"]");throw new Error("Invalid query : "+t)},t.prototype.pageSize=function(t){return this.set("pageSize",t)},t.prototype.fetch=function(t){console.warn("Warning: Using Fetch is deprecated. Use the property `graphQuery` instead.");var e=Array.isArray(t)?t.join(","):t;return this.set("fetch",e)},t.prototype.fetchLinks=function(t){console.warn("Warning: Using FetchLinks is deprecated. Use the property `graphQuery` instead.");var e=Array.isArray(t)?t.join(","):t;return this.set("fetchLinks",e)},t.prototype.graphQuery=function(t){return this.set("graphQuery",t)},t.prototype.lang=function(t){return this.set("lang",t)},t.prototype.page=function(t){return this.set("page",t)},t.prototype.after=function(t){return this.set("after",t)},t.prototype.orderings=function(t){return t?this.set("orderings","["+t.join(",")+"]"):this},t.prototype.url=function(){var t=this.form.action;if(this.data){var e=t.indexOf("?")>-1?"&":"?";for(var n in this.data)if(Object.prototype.hasOwnProperty.call(this.data,n)){var r=this.data[n];if(r)for(var i=0;i<r.length;i++)t+=e+n+"="+encodeURIComponent(r[i]),e="&"}}return t},t.prototype.submit=function(t){return this.httpClient.cachedRequest(this.url()).then((function(e){return t&&t(null,e),e})).catch((function(e){throw t&&t(e),e}))},t}(),s="at",a="not",f="missing",h="has",p="any",c="in",l="fulltext",y="similar",d="number.gt",g="number.lt",m="number.inRange",v="date.before",w="date.after",k="date.between",b="date.day-of-month",A="date.day-of-month-after",q="date.day-of-month-before",O="date.day-of-week",B="date.day-of-week-after",I="date.day-of-week-before",x="date.month",D="date.month-before",S="date.month-after",U="date.year",R="date.hour",j="date.hour-before",E="date.hour-after",C="geopoint.near";function F(t){if("string"==typeof t)return'"'+t+'"';if("number"==typeof t)return t.toString();if(t instanceof Date)return t.getTime().toString();if(Array.isArray(t))return"["+t.map((function(t){return F(t)})).join(",")+"]";if("boolean"==typeof t)return t.toString();throw new Error("Unable to encode "+t+" of type "+typeof t)}var M={near:function(t,e,n,r){return"["+C+"("+t+", "+e+", "+n+", "+r+")]"}},P={before:function(t,e){return"["+v+"("+t+", "+F(e)+")]"},after:function(t,e){return"["+w+"("+t+", "+F(e)+")]"},between:function(t,e,n){return"["+k+"("+t+", "+F(e)+", "+F(n)+")]"},dayOfMonth:function(t,e){return"["+b+"("+t+", "+e+")]"},dayOfMonthAfter:function(t,e){return"["+A+"("+t+", "+e+")]"},dayOfMonthBefore:function(t,e){return"["+q+"("+t+", "+e+")]"},dayOfWeek:function(t,e){return"["+O+"("+t+", "+F(e)+")]"},dayOfWeekAfter:function(t,e){return"["+B+"("+t+", "+F(e)+")]"},dayOfWeekBefore:function(t,e){return"["+I+"("+t+", "+F(e)+")]"},month:function(t,e){return"["+x+"("+t+", "+F(e)+")]"},monthBefore:function(t,e){return"["+D+"("+t+", "+F(e)+")]"},monthAfter:function(t,e){return"["+S+"("+t+", "+F(e)+")]"},year:function(t,e){return"["+U+"("+t+", "+e+")]"},hour:function(t,e){return"["+R+"("+t+", "+e+")]"},hourBefore:function(t,e){return"["+j+"("+t+", "+e+")]"},hourAfter:function(t,e){return"["+E+"("+t+", "+e+")]"}},T={gt:function(t,e){return"["+d+"("+t+", "+e+")]"},lt:function(t,e){return"["+g+"("+t+", "+e+")]"},inRange:function(t,e,n){return"["+m+"("+t+", "+e+", "+n+")]"}},W={at:function(t,e){return"["+s+"("+t+", "+F(e)+")]"},not:function(t,e){return"["+a+"("+t+", "+F(e)+")]"},missing:function(t){return"["+f+"("+t+")]"},has:function(t){return"["+h+"("+t+")]"},any:function(t,e){return"["+p+"("+t+", "+F(e)+")]"},in:function(t,e){return"["+c+"("+t+", "+F(e)+")]"},fulltext:function(t,e){return"["+l+"("+t+", "+F(e)+")]"},similar:function(t,e){return"["+y+'("'+t+'", '+e+")]"},date:P,dateBefore:P.before,dateAfter:P.after,dateBetween:P.between,dayOfMonth:P.dayOfMonth,dayOfMonthAfter:P.dayOfMonthAfter,dayOfMonthBefore:P.dayOfMonthBefore,dayOfWeek:P.dayOfWeek,dayOfWeekAfter:P.dayOfWeekAfter,dayOfWeekBefore:P.dayOfWeekBefore,month:P.month,monthBefore:P.monthBefore,monthAfter:P.monthAfter,year:P.year,hour:P.hour,hourBefore:P.hourBefore,hourAfter:P.hourAfter,number:T,gt:T.gt,lt:T.lt,inRange:T.inRange,near:M.near,geopoint:M},z=decodeURIComponent;var L={parse:function(t,e){if("string"!=typeof t)throw new TypeError("argument str must be a string");var n={},r=e||{},i=t.split(/; */),o=r.decode||z;return i.forEach((function(t){var e=t.indexOf("=");if(!(e<0)){var r=t.substr(0,e).trim(),i=t.substr(++e,t.length).trim();'"'==i[0]&&(i=i.slice(1,-1)),null==n[r]&&(n[r]=function(t,e){try{return e(t)}catch(e){return t}}(i,o))}})),n}};function _(t,e,n){return{token:t,documentId:e,resolve:function(r,i,o){return e&&n?n(e,{ref:t}).then((function(t){if(t){var e=r&&r(t)||t.url||i;return o&&o(null,e),e}return o&&o(null,i),i})):Promise.resolve(i)}}}var Q=function(){function t(t,e,n){this.data=t,this.masterRef=t.refs.filter((function(t){return t.isMasterRef}))[0],this.experiments=new i(t.experiments),this.bookmarks=t.bookmarks,this.httpClient=e,this.options=n,this.refs=t.refs,this.tags=t.tags,this.types=t.types,this.languages=t.languages}return t.prototype.form=function(t){var e=this.data.forms[t];return e?new u(e,this.httpClient):null},t.prototype.everything=function(){var t=this.form("everything");if(!t)throw new Error("Missing everything form");return t},t.prototype.master=function(){return this.masterRef.ref},t.prototype.ref=function(t){var e=this.data.refs.filter((function(e){return e.label===t}))[0];return e?e.ref:null},t.prototype.currentExperiment=function(){return this.experiments.current()},t.prototype.query=function(t,e,n){void 0===n&&(n=function(){});var r="function"==typeof e?{options:{},callback:e}:{options:e||{},callback:n},i=r.options,o=r.callback,u=this.everything();for(var s in i)u=u.set(s,i[s]);if(!i.ref){var a="";this.options.req?a=this.options.req.headers.cookie||"":"undefined"!=typeof window&&window.document&&(a=window.document.cookie||"");var f=L.parse(a),h=f["io.prismic.preview"],p=this.experiments.refFromCookie(f["io.prismic.experiment"]);u=u.ref(h||p||this.masterRef.ref)}return t&&u.query(t),u.submit(o)},t.prototype.queryFirst=function(t,e,n){var r="function"==typeof e?{options:{},callback:e}:{options:e||{},callback:n||function(){}},i=r.options,o=r.callback;return i.page=1,i.pageSize=1,this.query(t,i).then((function(t){var e=t&&t.results&&t.results[0];return o(null,e),e})).catch((function(t){throw o(t),t}))},t.prototype.getByID=function(t,n,r){var i=n?e({},n):{};return i.lang||(i.lang="*"),this.queryFirst(W.at("document.id",t),i,r)},t.prototype.getByIDs=function(t,n,r){var i=n?e({},n):{};return i.lang||(i.lang="*"),this.query(W.in("document.id",t),i,r)},t.prototype.getByUID=function(t,n,r,i){var o=r?e({},r):{};if("*"===o.lang)throw new Error("FORBIDDEN. You can't use getByUID with *, use the predicates instead.");return o.page||(o.page=1),this.queryFirst(W.at("my."+t+".uid",n),o,i)},t.prototype.getSingle=function(t,n,r){var i=n?e({},n):{};return this.queryFirst(W.at("document.type",t),i,r)},t.prototype.getBookmark=function(t,e,n){var r=this.data.bookmarks[t];return r?this.getByID(r,e,n):Promise.reject("Error retrieving bookmarked id")},t.prototype.getPreviewResolver=function(t,e){return _(t,e,this.getByID.bind(this))},t.prototype.previewSession=function(t,e,n,r){var i=this;return console.warn("previewSession function is deprecated in favor of getPreviewResolver function."),new Promise((function(o,u){i.httpClient.request(t,(function(s,a){if(s)r&&r(s),u(s);else if(a){if(a.mainDocument)return i.getByID(a.mainDocument,{ref:t}).then((function(t){if(t){var i=e&&e(t)||t.url||n;r&&r(null,i),o(i)}else r&&r(null,n),o(n)})).catch(u);r&&r(null,n),o(n)}}))}))},t}();function H(t){this.size=0,this.limit=t,this._keymap={}}H.prototype.put=function(t,e){var n={key:t,value:e};if(this._keymap[t]=n,this.tail?(this.tail.newer=n,n.older=this.tail):this.head=n,this.tail=n,this.size===this.limit)return this.shift();this.size++},H.prototype.shift=function(){var t=this.head;return t&&(this.head.newer?(this.head=this.head.newer,this.head.older=void 0):this.head=void 0,t.newer=t.older=void 0,delete this._keymap[t.key]),console.log("purging ",t.key),t},H.prototype.get=function(t,e){var n=this._keymap[t];if(void 0!==n)return n===this.tail||(n.newer&&(n===this.head&&(this.head=n.newer),n.newer.older=n.older),n.older&&(n.older.newer=n.newer),n.newer=void 0,n.older=this.tail,this.tail&&(this.tail.newer=n),this.tail=n),e?n:n.value},H.prototype.find=function(t){return this._keymap[t]},H.prototype.set=function(t,e){var n,r=this.get(t,!0);return r?(n=r.value,r.value=e):(n=this.put(t,e))&&(n=n.value),n},H.prototype.remove=function(t){var e=this._keymap[t];if(e)return delete this._keymap[e.key],e.newer&&e.older?(e.older.newer=e.newer,e.newer.older=e.older):e.newer?(e.newer.older=void 0,this.head=e.newer):e.older?(e.older.newer=void 0,this.tail=e.older):this.head=this.tail=void 0,this.size--,e.value},H.prototype.removeAll=function(){this.head=this.tail=void 0,this.size=0,this._keymap={}},"function"==typeof Object.keys?H.prototype.keys=function(){return Object.keys(this._keymap)}:H.prototype.keys=function(){var t=[];for(var e in this._keymap)t.push(e);return t},H.prototype.forEach=function(t,e,n){var r;if(!0===e?(n=!0,e=void 0):"object"!=typeof e&&(e=this),n)for(r=this.tail;r;)t.call(e,r.key,r.value,this),r=r.older;else for(r=this.head;r;)t.call(e,r.key,r.value,this),r=r.newer},H.prototype.toString=function(){for(var t="",e=this.head;e;)t+=String(e.key)+":"+e.value,(e=e.newer)&&(t+=" < ");return t};var J=function(){function t(t){void 0===t&&(t=1e3),this.lru=function(t){return new H(t)}(t)}return t.prototype.isExpired=function(t){var e=this.lru.get(t,!1);return!!e&&(0!==e.expiredIn&&e.expiredIn<Date.now())},t.prototype.get=function(t,e){var n=this.lru.get(t,!1);n&&!this.isExpired(t)?e(null,n.data):e&&e(null)},t.prototype.set=function(t,e,n,r){this.lru.remove(t),this.lru.put(t,{data:e,expiredIn:n?Date.now()+1e3*n:0}),r&&r(null)},t.prototype.remove=function(t,e){this.lru.remove(t),e&&e(null)},t.prototype.clear=function(t){this.lru.removeAll(),t&&t(null)},t}();var N=function(){function e(t){this.options=t||{}}return e.prototype.request=function(e,n){!function(e,n,r){var i,o={headers:{Accept:"application/json"}};n&&n.proxyAgent&&(o.agent=n.proxyAgent);var u=t(e,o);(n.timeoutInMs?Promise.race([u,new Promise((function(t,r){i=setTimeout((function(){return r(new Error(e+" response timeout"))}),n.timeoutInMs)}))]):u).then((function(t){return clearTimeout(i),~~(t.status/100!=2)?t.text().then((function(){var n=new Error("Unexpected status code ["+t.status+"] on URL "+e);throw n.status=t.status,n})):t.json().then((function(e){var n=t.headers.get("cache-control"),i=n?/max-age=(\d+)/.exec(n):null,o=i?parseInt(i[1],10):void 0;r(null,e,t,o)}))})).catch((function(t){clearTimeout(i),r(t)}))}(e,this.options,n)},e}(),K=function(){function t(t,e,n,r){this.requestHandler=t||new N({proxyAgent:n,timeoutInMs:r}),this.cache=e||new J}return t.prototype.request=function(t,e){this.requestHandler.request(t,(function(t,n,r,i){t?e&&e(t,null,r,i):n&&e&&e(null,n,r,i)}))},t.prototype.cachedRequest=function(t,e){var n=this,r=e||{};return new Promise((function(e,i){var o,u;o=function(t,n){t&&i(t),n&&e(n)},u=r.cacheKey||t,n.cache.get(u,(function(e,i){e||i?o(e,i):n.request(t,(function(t,e,i,s){if(t)o(t,null);else{var a=s||r.ttl;a&&n.cache.set(u,e,a,o),o(null,e)}}))}))}))},t}();function Y(t){return t.indexOf("?")>-1?"&":"?"}var G=function(){function t(t,e){if(this.options=e||{},this.url=t,this.options.accessToken){var n="access_token="+this.options.accessToken;this.url+=Y(t)+n}this.options.routes&&(this.url+=Y(t)+"routes="+encodeURIComponent(JSON.stringify(this.options.routes))),this.apiDataTTL=this.options.apiDataTTL||5,this.httpClient=new K(this.options.requestHandler,this.options.apiCache,this.options.proxyAgent,this.options.timeoutInMs)}return t.prototype.get=function(t){var e=this;return this.httpClient.cachedRequest(this.url,{ttl:this.apiDataTTL}).then((function(n){var r=new Q(n,e.httpClient,e.options);return t&&t(null,r),r})).catch((function(e){throw t&&t(e),e}))},t}(),V=function(){function t(t,e){this.api=new G(t,e)}return t.prototype.getApi=function(){return this.api.get()},t.prototype.everything=function(){return this.form("everything")},t.prototype.form=function(t){return new o(t,this.api)},t.prototype.query=function(t,e,n){return this.getApi().then((function(r){return r.query(t,e,n)}))},t.prototype.queryFirst=function(t,e,n){return this.getApi().then((function(r){return r.queryFirst(t,e,n)}))},t.prototype.getByID=function(t,e,n){return this.getApi().then((function(r){return r.getByID(t,e,n)}))},t.prototype.getByIDs=function(t,e,n){return this.getApi().then((function(r){return r.getByIDs(t,e,n)}))},t.prototype.getByUID=function(t,e,n,r){return this.getApi().then((function(i){return i.getByUID(t,e,n,r)}))},t.prototype.getSingle=function(t,e,n){return this.getApi().then((function(r){return r.getSingle(t,e,n)}))},t.prototype.getBookmark=function(t,e,n){return this.getApi().then((function(r){return r.getBookmark(t,e,n)}))},t.prototype.previewSession=function(t,e,n,r){return this.getApi().then((function(i){return i.previewSession(t,e,n,r)}))},t.prototype.getPreviewResolver=function(t,e){var n=this;return _(t,e,(function(t,e){return n.getApi().then((function(n){return n.getByID(t,e)}))}))},t.getApi=function(t,e){return new G(t,e).get()},t}();function X(t,e){return V.getApi(t,e)}return{experimentCookie:"io.prismic.experiment",previewCookie:"io.prismic.preview",Predicates:W,Experiments:i,Api:G,client:function(t,e){return new V(t,e)},getApi:X,api:function(t,e){return X(t,e)}}})); //# sourceMappingURL=prismic-javascript.min.js.map