ziggy-js
Version:
Use your Laravel named routes in JavaScript.
2 lines (1 loc) • 8.76 kB
JavaScript
import{parse as r,stringify as t}from"qs-esm";function n(r,t){for(var n=0;n<t.length;n++){var e=t[n];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"in e&&(e.writable=!0),Object.defineProperty(r,c(e.key),e)}}function e(r,t,e){return t&&n(r.prototype,t),e&&n(r,e),Object.defineProperty(r,"prototype",{writable:!1}),r}function i(){return i=Object.assign?Object.assign.bind():function(r){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var e in n)({}).hasOwnProperty.call(n,e)&&(r[e]=n[e])}return r},i.apply(null,arguments)}function o(r){return o=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(r){return r.__proto__||Object.getPrototypeOf(r)},o(r)}function u(){try{var r=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(r){}return(u=function(){return!!r})()}function f(r,t){return f=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(r,t){return r.__proto__=t,r},f(r,t)}function c(r){var t=function(r){if("object"!=typeof r||!r)return r;var t=r[Symbol.toPrimitive];if(void 0!==t){var n=t.call(r,"string");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(r)}(r);return"symbol"==typeof t?t:t+""}function a(r){var t="function"==typeof Map?new Map:void 0;return a=function(r){if(null===r||!function(r){try{return-1!==Function.toString.call(r).indexOf("[native code]")}catch(t){return"function"==typeof r}}(r))return r;if("function"!=typeof r)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(r))return t.get(r);t.set(r,n)}function n(){return function(r,t,n){if(u())return Reflect.construct.apply(null,arguments);var e=[null];e.push.apply(e,t);var i=new(r.bind.apply(r,e));return n&&f(i,n.prototype),i}(r,arguments,o(this).constructor)}return n.prototype=Object.create(r.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),f(n,r)},a(r)}var s=/*#__PURE__*/function(){function t(r,t,n){var e,i;this.name=r,this.definition=t,this.bindings=null!=(e=t.bindings)?e:{},this.wheres=null!=(i=t.wheres)?i:{},this.config=n}var n=t.prototype;return n.matchesUrl=function(t){var n,e=this;if(!this.definition.methods.includes("GET"))return!1;var i=this.template.replace(/[.*+$()[\]]/g,"\\$&").replace(/(\/?){([^}?]*)(\??)}/g,function(r,t,n,i){var o,u="(?<"+n+">"+((null==(o=e.wheres[n])?void 0:o.replace(/(^\^)|(\$$)/g,""))||"[^/?]+")+")";return i?"("+t+u+")?":""+t+u}).replace(/^\w+:\/\//,""),o=t.replace(/^\w+:\/\//,"").split("?"),u=o[0],f=o[1],c=null!=(n=new RegExp("^"+i+"/?$").exec(u))?n:new RegExp("^"+i+"/?$").exec(decodeURI(u));if(c){for(var a in c.groups)c.groups[a]="string"==typeof c.groups[a]?decodeURIComponent(c.groups[a]):c.groups[a];return{params:c.groups,query:r(f)}}return!1},n.compile=function(r){var t=this;return this.parameterSegments.length?this.template.replace(/{([^}?]+)(\??)}/g,function(n,e,i){var o,u;if(!i&&[null,void 0].includes(r[e]))throw new Error("Ziggy error: '"+e+"' parameter is required for route '"+t.name+"'.");if(t.wheres[e]&&!new RegExp("^"+(i?"("+t.wheres[e]+")?":t.wheres[e])+"$").test(null!=(u=r[e])?u:""))throw new Error("Ziggy error: '"+e+"' parameter '"+r[e]+"' does not match required format '"+t.wheres[e]+"' for route '"+t.name+"'.");return encodeURI(null!=(o=r[e])?o:"").replace(/%7C/g,"|").replace(/%25/g,"%").replace(/\$/g,"%24")}).replace(this.config.absolute?/(\.[^/]+?)(\/\/)/:/(^)(\/\/)/,"$1/").replace(/\/+$/,""):this.template},e(t,[{key:"template",get:function(){var r=(this.origin+"/"+this.definition.uri).replace(/\/+$/,"");return""===r?"/":r}},{key:"origin",get:function(){return this.config.absolute?this.definition.domain?""+this.config.url.match(/^\w+:\/\//)[0]+this.definition.domain+(this.config.port?":"+this.config.port:""):this.config.url:""}},{key:"parameterSegments",get:function(){var r,t;return null!=(r=null==(t=this.template.match(/{[^}?]+\??}/g))?void 0:t.map(function(r){return{name:r.replace(/{|\??}/g,""),required:!/\?}$/.test(r)}}))?r:[]}}])}(),l=/*#__PURE__*/function(r){function n(t,n,e,o){var u;if(void 0===e&&(e=!0),(u=r.call(this)||this).t=null!=o?o:"undefined"!=typeof Ziggy?Ziggy:null==globalThis?void 0:globalThis.Ziggy,!u.t&&"undefined"!=typeof document&&document.getElementById("ziggy-routes-json")&&(globalThis.Ziggy=JSON.parse(document.getElementById("ziggy-routes-json").textContent),u.t=globalThis.Ziggy),u.t=i({},u.t,{absolute:e}),t){if(!u.t.routes[t])throw new Error("Ziggy error: route '"+t+"' is not in the route list.");u.i=new s(t,u.t.routes[t],u.t),u.o=u.u(n)}return u}var o,u;u=r,(o=n).prototype=Object.create(u.prototype),o.prototype.constructor=o,f(o,u);var c=n.prototype;return c.toString=function(){var r=this,n=Object.keys(this.o).filter(function(t){return!r.i.parameterSegments.some(function(r){return r.name===t})}).filter(function(r){return"_query"!==r}).reduce(function(t,n){var e;return i({},t,((e={})[n]=r.o[n],e))},{});return this.i.compile(this.o)+t(i({},n,this.o._query),{addQueryPrefix:!0,arrayFormat:"indices",encodeValuesOnly:!0,skipNulls:!0,encoder:function(r,t){return"boolean"==typeof r?Number(r):t(r)}})},c.l=function(r){var t=this;r?this.t.absolute&&r.startsWith("/")&&(r=this.h().host+r):r=this.v();var n={},e=Object.entries(this.t.routes).find(function(e){return n=new s(e[0],e[1],t.t).matchesUrl(r)})||[void 0,void 0];return i({name:e[0]},n,{route:e[1]})},c.v=function(){var r=this.h(),t=r.pathname,n=r.search;return(this.t.absolute?r.host+t:t.replace(this.t.url.replace(/^\w*:\/\/[^/]+/,""),"").replace(/^\/+/,"/"))+n},c.current=function(r,t){var n=this.l(),e=n.name,o=n.params,u=n.query,f=n.route;if(!r)return e;var c=new RegExp("^"+r.replace(/\./g,"\\.").replace(/\*/g,".*")+"$").test(e);if([null,void 0].includes(t)||!c)return c;var a=new s(e,f,this.t);t=this.u(t,a);var l=i({},o,u);if(Object.values(t).every(function(r){return!r})&&!Object.values(l).some(function(r){return void 0!==r}))return!0;var h=function(r,t){return Object.entries(r).every(function(r){var n=r[0],e=r[1];return Array.isArray(e)&&Array.isArray(t[n])?e.every(function(r){return t[n].includes(r)||t[n].includes(decodeURIComponent(r))}):"object"==typeof e&&"object"==typeof t[n]&&null!==e&&null!==t[n]?h(e,t[n]):t[n]==e||t[n]==decodeURIComponent(e)})};return h(t,l)},c.h=function(){var r,t,n,e,i,o,u="undefined"!=typeof window?window.location:{},f=u.host,c=u.pathname,a=u.search;return{host:null!=(r=null==(t=this.t.location)?void 0:t.host)?r:void 0===f?"":f,pathname:null!=(n=null==(e=this.t.location)?void 0:e.pathname)?n:void 0===c?"":c,search:null!=(i=null==(o=this.t.location)?void 0:o.search)?i:void 0===a?"":a}},c.has=function(r){return this.t.routes.hasOwnProperty(r)},c.u=function(r,t){var n=this;void 0===r&&(r={}),void 0===t&&(t=this.i),null!=r||(r={}),r=["string","number"].includes(typeof r)?[r]:r;var e=t.parameterSegments.filter(function(r){return!n.t.defaults[r.name]});if(Array.isArray(r))r=r.reduce(function(r,t,n){var o,u;return i({},r,e[n]?((o={})[e[n].name]=t,o):"object"==typeof t?t:((u={})[t]="",u))},{});else if(1===e.length&&!r[e[0].name]&&(r.hasOwnProperty(Object.values(t.bindings)[0])||r.hasOwnProperty("id"))){var o;(o={})[e[0].name]=r,r=o}return i({},this.p(t),this.m(r,t))},c.p=function(r){var t=this;return r.parameterSegments.filter(function(r){return t.t.defaults[r.name]}).reduce(function(r,n,e){var o,u=n.name;return i({},r,((o={})[u]=t.t.defaults[u],o))},{})},c.m=function(r,t){var n=t.bindings,e=t.parameterSegments;return Object.entries(r).reduce(function(r,t){var o,u,f=t[0],c=t[1];if(!c||"object"!=typeof c||Array.isArray(c)||!e.some(function(r){return r.name===f}))return i({},r,((u={})[f]=c,u));if(!c.hasOwnProperty(n[f])){if(!c.hasOwnProperty("id"))throw new Error("Ziggy error: object passed as '"+f+"' parameter is missing route model binding key '"+n[f]+"'.");n[f]="id"}return i({},r,((o={})[f]=c[n[f]],o))},{})},c.valueOf=function(){return this.toString()},e(n,[{key:"params",get:function(){var r=this.l();return i({},r.params,r.query)}},{key:"routeParams",get:function(){return this.l().params}},{key:"queryParams",get:function(){return this.l().query}}])}(/*#__PURE__*/a(String));function h(r,t,n,e){var i=new l(r,t,n,e);return r?i.toString():i}var v={install:function(r,t){var n=function(r,n,e,i){return void 0===i&&(i=t),h(r,n,e,i)};parseInt(r.version)>2?(r.config.globalProperties.route=n,r.provide("route",n)):r.mixin({methods:{route:n}})}};function d(r){if(!r&&!globalThis.Ziggy&&"undefined"==typeof Ziggy&&!document.getElementById("ziggy-routes-json"))throw new Error("Ziggy error: missing configuration. Ensure that a `Ziggy` variable is defined globally or pass a config object into the useRoute hook.");return function(t,n,e,i){return void 0===i&&(i=r),h(t,n,e,i)}}export{v as ZiggyVue,h as route,d as useRoute};