diff --git a/CHANGELOG.md b/CHANGELOG.md index 83dba3e3..b37c7002 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,16 @@ Breaking changes are marked with ⚠️. ## [Unreleased] +## [v2.1.0] - 2024-03-26 + +**Added** + +- Improve generated types to include optional types by @alexmccabe in https://github.com/tighten/ziggy/pull/697 + +**Fixed** + +* Fix Vue plugin on Vue 2 by @bakerkretzmar in https://github.com/tighten/ziggy/pull/738 + ## [v2.0.5] - 2024-03-19 **Fixed** @@ -380,7 +390,8 @@ See [UPGRADING](UPGRADING.md#upgrading-from-1x-to-2x) for detailed upgrade instr For previous changes see the [Releases](https://github.com/tighten/ziggy/releases) page. -[Unreleased]: https://github.com/tighten/ziggy/compare/v2.0.5...HEAD +[Unreleased]: https://github.com/tighten/ziggy/compare/v2.1.0...HEAD +[v2.1.0]: https://github.com/tighten/ziggy/compare/v2.0.5...v2.1.0 [v2.0.5]: https://github.com/tighten/ziggy/compare/v2.0.4...v2.0.5 [v2.0.4]: https://github.com/tighten/ziggy/compare/v2.0.3...v2.0.4 [v2.0.3]: https://github.com/tighten/ziggy/compare/v2.0.2...v2.0.3 diff --git a/dist/index.esm.js b/dist/index.esm.js index a1cf66d4..e09b9759 100644 --- a/dist/index.esm.js +++ b/dist/index.esm.js @@ -1 +1 @@ -function r(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(r=function(){return!!t})()}function t(r){var t=function(r,t){if("object"!=typeof r||!r)return r;var n=r[Symbol.toPrimitive];if(void 0!==n){var e=n.call(r,"string");if("object"!=typeof e)return e;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(r)}(r);return"symbol"==typeof t?t:String(t)}function n(r,n){for(var e=0;e1;){var t=r.pop(),n=t.obj[t.prop];if(p(n)){for(var e=[],o=0;o=48&&a<=57||a>=65&&a<=90||a>=97&&a<=122||o===s.RFC1738&&(40===a||41===a)?u+=i.charAt(f):a<128?u+=y[a]:a<2048?u+=y[192|a>>6]+y[128|63&a]:a<55296||a>=57344?u+=y[224|a>>12]+y[128|a>>6&63]+y[128|63&a]:(a=65536+((1023&a)<<10|1023&i.charCodeAt(f+=1)),u+=y[240|a>>18]+y[128|a>>12&63]+y[128|a>>6&63]+y[128|63&a])}return u},isBuffer:function(r){return!(!r||"object"!=typeof r||!(r.constructor&&r.constructor.isBuffer&&r.constructor.isBuffer(r)))},isRegExp:function(r){return"[object RegExp]"===Object.prototype.toString.call(r)},maybeMap:function(r,t){if(p(r)){for(var n=[],e=0;e0?h.join(",")||null:void 0}];else if(m(f))k=f;else{var x=Object.keys(h);k=a?x.sort(a):x}for(var N=0;N-1?r.split(","):r},D=function(r,t,n,e){if(r){var o=n.allowDots?r.replace(/\.([^.[]+)/g,"[$1]"):r,i=/(\[[^[\]]*])/g,u=n.depth>0&&/(\[[^[\]]*])/.exec(o),f=u?o.slice(0,u.index):o,a=[];if(f){if(!n.plainObjects&&T.call(Object.prototype,f)&&!n.allowPrototypes)return;a.push(f)}for(var c=0;n.depth>0&&null!==(u=i.exec(o))&&c=0;--i){var u,f=r[i];if("[]"===f&&n.parseArrays)u=[].concat(o);else{u=n.plainObjects?Object.create(null):{};var a="["===f.charAt(0)&&"]"===f.charAt(f.length-1)?f.slice(1,-1):f,c=parseInt(a,10);n.parseArrays||""!==a?!isNaN(c)&&f!==a&&String(c)===a&&c>=0&&n.parseArrays&&c<=n.arrayLimit?(u=[])[c]=o:"__proto__"!==a&&(u[a]=o):u={0:o}}o=u}return o}(a,t,n,e)}},$=function(r,t){var n=function(r){if(!r)return N;if(null!=r.decoder&&"function"!=typeof r.decoder)throw new TypeError("Decoder has to be a function.");if(void 0!==r.charset&&"utf-8"!==r.charset&&"iso-8859-1"!==r.charset)throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");return{allowDots:void 0===r.allowDots?N.allowDots:!!r.allowDots,allowPrototypes:"boolean"==typeof r.allowPrototypes?r.allowPrototypes:N.allowPrototypes,arrayLimit:"number"==typeof r.arrayLimit?r.arrayLimit:N.arrayLimit,charset:void 0===r.charset?N.charset:r.charset,charsetSentinel:"boolean"==typeof r.charsetSentinel?r.charsetSentinel:N.charsetSentinel,comma:"boolean"==typeof r.comma?r.comma:N.comma,decoder:"function"==typeof r.decoder?r.decoder:N.decoder,delimiter:"string"==typeof r.delimiter||b.isRegExp(r.delimiter)?r.delimiter:N.delimiter,depth:"number"==typeof r.depth||!1===r.depth?+r.depth:N.depth,ignoreQueryPrefix:!0===r.ignoreQueryPrefix,interpretNumericEntities:"boolean"==typeof r.interpretNumericEntities?r.interpretNumericEntities:N.interpretNumericEntities,parameterLimit:"number"==typeof r.parameterLimit?r.parameterLimit:N.parameterLimit,parseArrays:!1!==r.parseArrays,plainObjects:"boolean"==typeof r.plainObjects?r.plainObjects:N.plainObjects,strictNullHandling:"boolean"==typeof r.strictNullHandling?r.strictNullHandling:N.strictNullHandling}}(t);if(""===r||null==r)return n.plainObjects?Object.create(null):{};for(var e="string"==typeof r?function(r,t){var n,e={},o=(t.ignoreQueryPrefix?r.replace(/^\?/,""):r).split(t.delimiter,Infinity===t.parameterLimit?void 0:t.parameterLimit),i=-1,u=t.charset;if(t.charsetSentinel)for(n=0;n-1&&(a=x(a)?[a]:a),e[f]=T.call(e,f)?b.combine(e[f],a):a}return e}(r,n):r,o=n.plainObjects?Object.create(null):{},i=Object.keys(e),u=0;u"+((null==(i=t.wheres[e])?void 0:i.replace(/(^\^)|(\$$)/g,""))||"[^/?]+")+")";return o?"("+n+u+")?":""+n+u}).replace(/^\w+:\/\//,""),e=r.replace(/^\w+:\/\//,"").split("?"),o=e[0],i=e[1],u=new RegExp("^"+n+"/?$").exec(decodeURI(o));if(u){for(var f in u.groups)u.groups[f]="string"==typeof u.groups[f]?decodeURIComponent(u.groups[f]):u.groups[f];return{params:u.groups,query:$(i)}}return!1},t.compile=function(r){var t=this;return this.parameterSegments.length?this.template.replace(/{([^}?]+)(\??)}/g,function(n,e,o){var i,u;if(!o&&[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("^"+(o?"("+t.wheres[e]+")?":t.wheres[e])+"$").test(null!=(u=r[e])?u:""))throw new Error("Ziggy error: '"+e+"' parameter does not match required format '"+t.wheres[e]+"' for route '"+t.name+"'.");return encodeURI(null!=(i=r[e])?i:"").replace(/%7C/g,"|").replace(/%25/g,"%").replace(/\$/g,"%24")}).replace(this.origin+"//",this.origin+"/").replace(/\/+$/,""):this.template},e(r,[{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:[]}}]),r}(),Z=/*#__PURE__*/function(r){var t,n;function i(t,n,e,i){var u;if(void 0===e&&(e=!0),(u=r.call(this)||this).t=null!=i?i:"undefined"!=typeof Ziggy?Ziggy:null==globalThis?void 0:globalThis.Ziggy,u.t=o({},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 I(t,u.t.routes[t],u.t),u.u=u.l(n)}return u}n=r,(t=i).prototype=Object.create(n.prototype),t.prototype.constructor=t,u(t,n);var f=i.prototype;return f.toString=function(){var r=this,t=Object.keys(this.u).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 o({},t,((e={})[n]=r.u[n],e))},{});return this.i.compile(this.u)+function(r,t){var n,e=r,o=function(r){if(!r)return R;if(null!=r.encoder&&"function"!=typeof r.encoder)throw new TypeError("Encoder has to be a function.");var t=r.charset||R.charset;if(void 0!==r.charset&&"utf-8"!==r.charset&&"iso-8859-1"!==r.charset)throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");var n=s.default;if(void 0!==r.format){if(!h.call(s.formatters,r.format))throw new TypeError("Unknown format option provided.");n=r.format}var e=s.formatters[n],o=R.filter;return("function"==typeof r.filter||m(r.filter))&&(o=r.filter),{addQueryPrefix:"boolean"==typeof r.addQueryPrefix?r.addQueryPrefix:R.addQueryPrefix,allowDots:void 0===r.allowDots?R.allowDots:!!r.allowDots,charset:t,charsetSentinel:"boolean"==typeof r.charsetSentinel?r.charsetSentinel:R.charsetSentinel,delimiter:void 0===r.delimiter?R.delimiter:r.delimiter,encode:"boolean"==typeof r.encode?r.encode:R.encode,encoder:"function"==typeof r.encoder?r.encoder:R.encoder,encodeValuesOnly:"boolean"==typeof r.encodeValuesOnly?r.encodeValuesOnly:R.encodeValuesOnly,filter:o,format:n,formatter:e,serializeDate:"function"==typeof r.serializeDate?r.serializeDate:R.serializeDate,skipNulls:"boolean"==typeof r.skipNulls?r.skipNulls:R.skipNulls,sort:"function"==typeof r.sort?r.sort:null,strictNullHandling:"boolean"==typeof r.strictNullHandling?r.strictNullHandling:R.strictNullHandling}}(t);"function"==typeof o.filter?e=(0,o.filter)("",e):m(o.filter)&&(n=o.filter);var i=[];if("object"!=typeof e||null===e)return"";var u=g[t&&t.arrayFormat in g?t.arrayFormat:t&&"indices"in t?t.indices?"indices":"repeat":"indices"];n||(n=Object.keys(e)),o.sort&&n.sort(o.sort);for(var f=0;f0?l+c:""}(o({},t,this.u._query),{addQueryPrefix:!0,arrayFormat:"indices",encodeValuesOnly:!0,skipNulls:!0,encoder:function(r,t){return"boolean"==typeof r?Number(r):t(r)}})},f.v=function(r){var t=this;r?this.t.absolute&&r.startsWith("/")&&(r=this.p().host+r):r=this.h();var n={},e=Object.entries(this.t.routes).find(function(e){return n=new I(e[0],e[1],t.t).matchesUrl(r)})||[void 0,void 0];return o({name:e[0]},n,{route:e[1]})},f.h=function(){var r=this.p(),t=r.pathname,n=r.search;return(this.t.absolute?r.host+t:t.replace(this.t.url.replace(/^\w*:\/\/[^/]+/,""),"").replace(/^\/+/,"/"))+n},f.current=function(r,t){var n=this.v(),e=n.name,i=n.params,u=n.query,f=n.route;if(!r)return e;var a=new RegExp("^"+r.replace(/\./g,"\\.").replace(/\*/g,".*")+"$").test(e);if([null,void 0].includes(t)||!a)return a;var c=new I(e,f,this.t);t=this.l(t,c);var l=o({},i,u);return!(!Object.values(t).every(function(r){return!r})||Object.values(l).some(function(r){return void 0!==r}))||function r(t,n){return Object.entries(t).every(function(t){var e=t[0],o=t[1];return Array.isArray(o)&&Array.isArray(n[e])?o.every(function(r){return n[e].includes(r)}):"object"==typeof o&&"object"==typeof n[e]&&null!==o&&null!==n[e]?r(o,n[e]):n[e]==o})}(t,l)},f.p=function(){var r,t,n,e,o,i,u="undefined"!=typeof window?window.location:{},f=u.host,a=u.pathname,c=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===a?"":a,search:null!=(o=null==(i=this.t.location)?void 0:i.search)?o:void 0===c?"":c}},f.has=function(r){return Object.keys(this.t.routes).includes(r)},f.l=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 i,u;return o({},r,e[n]?((i={})[e[n].name]=t,i):"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 i;(i={})[e[0].name]=r,r=i}return o({},this.m(t),this.j(r,t))},f.m=function(r){var t=this;return r.parameterSegments.filter(function(r){return t.t.defaults[r.name]}).reduce(function(r,n,e){var i,u=n.name;return o({},r,((i={})[u]=t.t.defaults[u],i))},{})},f.j=function(r,t){var n=t.bindings,e=t.parameterSegments;return Object.entries(r).reduce(function(r,t){var i,u,f=t[0],a=t[1];if(!a||"object"!=typeof a||Array.isArray(a)||!e.some(function(r){return r.name===f}))return o({},r,((u={})[f]=a,u));if(!a.hasOwnProperty(n[f])){if(!a.hasOwnProperty("id"))throw new Error("Ziggy error: object passed as '"+f+"' parameter is missing route model binding key '"+n[f]+"'.");n[f]="id"}return o({},r,((i={})[f]=a[n[f]],i))},{})},f.valueOf=function(){return this.toString()},e(i,[{key:"params",get:function(){var r=this.v();return o({},r.params,r.query)}}]),i}(/*#__PURE__*/f(String));function F(r,t,n,e){var o=new Z(r,t,n,e);return r?o.toString():o}var P={install:function(r,t){var n=function(r,n,e,o){return void 0===o&&(o=t),F(r,n,e,o)};r.config.globalProperties.route=n,parseInt(r.version)>2&&r.provide("route",n)}};function B(r){if(!r&&!globalThis.Ziggy&&"undefined"==typeof Ziggy)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,o){return void 0===o&&(o=r),F(t,n,e,o)}}export{P as ZiggyVue,F as route,B as useRoute}; +function r(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(r=function(){return!!t})()}function t(r){var t=function(r,t){if("object"!=typeof r||!r)return r;var n=r[Symbol.toPrimitive];if(void 0!==n){var e=n.call(r,"string");if("object"!=typeof e)return e;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(r)}(r);return"symbol"==typeof t?t:String(t)}function n(r,n){for(var e=0;e1;){var t=r.pop(),n=t.obj[t.prop];if(p(n)){for(var e=[],o=0;o=48&&a<=57||a>=65&&a<=90||a>=97&&a<=122||o===s.RFC1738&&(40===a||41===a)?u+=i.charAt(f):a<128?u+=y[a]:a<2048?u+=y[192|a>>6]+y[128|63&a]:a<55296||a>=57344?u+=y[224|a>>12]+y[128|a>>6&63]+y[128|63&a]:(a=65536+((1023&a)<<10|1023&i.charCodeAt(f+=1)),u+=y[240|a>>18]+y[128|a>>12&63]+y[128|a>>6&63]+y[128|63&a])}return u},isBuffer:function(r){return!(!r||"object"!=typeof r||!(r.constructor&&r.constructor.isBuffer&&r.constructor.isBuffer(r)))},isRegExp:function(r){return"[object RegExp]"===Object.prototype.toString.call(r)},maybeMap:function(r,t){if(p(r)){for(var n=[],e=0;e0?h.join(",")||null:void 0}];else if(m(f))k=f;else{var x=Object.keys(h);k=a?x.sort(a):x}for(var N=0;N-1?r.split(","):r},D=function(r,t,n,e){if(r){var o=n.allowDots?r.replace(/\.([^.[]+)/g,"[$1]"):r,i=/(\[[^[\]]*])/g,u=n.depth>0&&/(\[[^[\]]*])/.exec(o),f=u?o.slice(0,u.index):o,a=[];if(f){if(!n.plainObjects&&T.call(Object.prototype,f)&&!n.allowPrototypes)return;a.push(f)}for(var c=0;n.depth>0&&null!==(u=i.exec(o))&&c=0;--i){var u,f=r[i];if("[]"===f&&n.parseArrays)u=[].concat(o);else{u=n.plainObjects?Object.create(null):{};var a="["===f.charAt(0)&&"]"===f.charAt(f.length-1)?f.slice(1,-1):f,c=parseInt(a,10);n.parseArrays||""!==a?!isNaN(c)&&f!==a&&String(c)===a&&c>=0&&n.parseArrays&&c<=n.arrayLimit?(u=[])[c]=o:"__proto__"!==a&&(u[a]=o):u={0:o}}o=u}return o}(a,t,n,e)}},$=function(r,t){var n=function(r){if(!r)return N;if(null!=r.decoder&&"function"!=typeof r.decoder)throw new TypeError("Decoder has to be a function.");if(void 0!==r.charset&&"utf-8"!==r.charset&&"iso-8859-1"!==r.charset)throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");return{allowDots:void 0===r.allowDots?N.allowDots:!!r.allowDots,allowPrototypes:"boolean"==typeof r.allowPrototypes?r.allowPrototypes:N.allowPrototypes,arrayLimit:"number"==typeof r.arrayLimit?r.arrayLimit:N.arrayLimit,charset:void 0===r.charset?N.charset:r.charset,charsetSentinel:"boolean"==typeof r.charsetSentinel?r.charsetSentinel:N.charsetSentinel,comma:"boolean"==typeof r.comma?r.comma:N.comma,decoder:"function"==typeof r.decoder?r.decoder:N.decoder,delimiter:"string"==typeof r.delimiter||b.isRegExp(r.delimiter)?r.delimiter:N.delimiter,depth:"number"==typeof r.depth||!1===r.depth?+r.depth:N.depth,ignoreQueryPrefix:!0===r.ignoreQueryPrefix,interpretNumericEntities:"boolean"==typeof r.interpretNumericEntities?r.interpretNumericEntities:N.interpretNumericEntities,parameterLimit:"number"==typeof r.parameterLimit?r.parameterLimit:N.parameterLimit,parseArrays:!1!==r.parseArrays,plainObjects:"boolean"==typeof r.plainObjects?r.plainObjects:N.plainObjects,strictNullHandling:"boolean"==typeof r.strictNullHandling?r.strictNullHandling:N.strictNullHandling}}(t);if(""===r||null==r)return n.plainObjects?Object.create(null):{};for(var e="string"==typeof r?function(r,t){var n,e={},o=(t.ignoreQueryPrefix?r.replace(/^\?/,""):r).split(t.delimiter,Infinity===t.parameterLimit?void 0:t.parameterLimit),i=-1,u=t.charset;if(t.charsetSentinel)for(n=0;n-1&&(a=x(a)?[a]:a),e[f]=T.call(e,f)?b.combine(e[f],a):a}return e}(r,n):r,o=n.plainObjects?Object.create(null):{},i=Object.keys(e),u=0;u"+((null==(i=t.wheres[e])?void 0:i.replace(/(^\^)|(\$$)/g,""))||"[^/?]+")+")";return o?"("+n+u+")?":""+n+u}).replace(/^\w+:\/\//,""),e=r.replace(/^\w+:\/\//,"").split("?"),o=e[0],i=e[1],u=new RegExp("^"+n+"/?$").exec(decodeURI(o));if(u){for(var f in u.groups)u.groups[f]="string"==typeof u.groups[f]?decodeURIComponent(u.groups[f]):u.groups[f];return{params:u.groups,query:$(i)}}return!1},t.compile=function(r){var t=this;return this.parameterSegments.length?this.template.replace(/{([^}?]+)(\??)}/g,function(n,e,o){var i,u;if(!o&&[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("^"+(o?"("+t.wheres[e]+")?":t.wheres[e])+"$").test(null!=(u=r[e])?u:""))throw new Error("Ziggy error: '"+e+"' parameter does not match required format '"+t.wheres[e]+"' for route '"+t.name+"'.");return encodeURI(null!=(i=r[e])?i:"").replace(/%7C/g,"|").replace(/%25/g,"%").replace(/\$/g,"%24")}).replace(this.origin+"//",this.origin+"/").replace(/\/+$/,""):this.template},e(r,[{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:[]}}]),r}(),Z=/*#__PURE__*/function(r){var t,n;function i(t,n,e,i){var u;if(void 0===e&&(e=!0),(u=r.call(this)||this).t=null!=i?i:"undefined"!=typeof Ziggy?Ziggy:null==globalThis?void 0:globalThis.Ziggy,u.t=o({},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 I(t,u.t.routes[t],u.t),u.u=u.l(n)}return u}n=r,(t=i).prototype=Object.create(n.prototype),t.prototype.constructor=t,u(t,n);var f=i.prototype;return f.toString=function(){var r=this,t=Object.keys(this.u).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 o({},t,((e={})[n]=r.u[n],e))},{});return this.i.compile(this.u)+function(r,t){var n,e=r,o=function(r){if(!r)return R;if(null!=r.encoder&&"function"!=typeof r.encoder)throw new TypeError("Encoder has to be a function.");var t=r.charset||R.charset;if(void 0!==r.charset&&"utf-8"!==r.charset&&"iso-8859-1"!==r.charset)throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");var n=s.default;if(void 0!==r.format){if(!h.call(s.formatters,r.format))throw new TypeError("Unknown format option provided.");n=r.format}var e=s.formatters[n],o=R.filter;return("function"==typeof r.filter||m(r.filter))&&(o=r.filter),{addQueryPrefix:"boolean"==typeof r.addQueryPrefix?r.addQueryPrefix:R.addQueryPrefix,allowDots:void 0===r.allowDots?R.allowDots:!!r.allowDots,charset:t,charsetSentinel:"boolean"==typeof r.charsetSentinel?r.charsetSentinel:R.charsetSentinel,delimiter:void 0===r.delimiter?R.delimiter:r.delimiter,encode:"boolean"==typeof r.encode?r.encode:R.encode,encoder:"function"==typeof r.encoder?r.encoder:R.encoder,encodeValuesOnly:"boolean"==typeof r.encodeValuesOnly?r.encodeValuesOnly:R.encodeValuesOnly,filter:o,format:n,formatter:e,serializeDate:"function"==typeof r.serializeDate?r.serializeDate:R.serializeDate,skipNulls:"boolean"==typeof r.skipNulls?r.skipNulls:R.skipNulls,sort:"function"==typeof r.sort?r.sort:null,strictNullHandling:"boolean"==typeof r.strictNullHandling?r.strictNullHandling:R.strictNullHandling}}(t);"function"==typeof o.filter?e=(0,o.filter)("",e):m(o.filter)&&(n=o.filter);var i=[];if("object"!=typeof e||null===e)return"";var u=g[t&&t.arrayFormat in g?t.arrayFormat:t&&"indices"in t?t.indices?"indices":"repeat":"indices"];n||(n=Object.keys(e)),o.sort&&n.sort(o.sort);for(var f=0;f0?l+c:""}(o({},t,this.u._query),{addQueryPrefix:!0,arrayFormat:"indices",encodeValuesOnly:!0,skipNulls:!0,encoder:function(r,t){return"boolean"==typeof r?Number(r):t(r)}})},f.v=function(r){var t=this;r?this.t.absolute&&r.startsWith("/")&&(r=this.p().host+r):r=this.h();var n={},e=Object.entries(this.t.routes).find(function(e){return n=new I(e[0],e[1],t.t).matchesUrl(r)})||[void 0,void 0];return o({name:e[0]},n,{route:e[1]})},f.h=function(){var r=this.p(),t=r.pathname,n=r.search;return(this.t.absolute?r.host+t:t.replace(this.t.url.replace(/^\w*:\/\/[^/]+/,""),"").replace(/^\/+/,"/"))+n},f.current=function(r,t){var n=this.v(),e=n.name,i=n.params,u=n.query,f=n.route;if(!r)return e;var a=new RegExp("^"+r.replace(/\./g,"\\.").replace(/\*/g,".*")+"$").test(e);if([null,void 0].includes(t)||!a)return a;var c=new I(e,f,this.t);t=this.l(t,c);var l=o({},i,u);return!(!Object.values(t).every(function(r){return!r})||Object.values(l).some(function(r){return void 0!==r}))||function r(t,n){return Object.entries(t).every(function(t){var e=t[0],o=t[1];return Array.isArray(o)&&Array.isArray(n[e])?o.every(function(r){return n[e].includes(r)}):"object"==typeof o&&"object"==typeof n[e]&&null!==o&&null!==n[e]?r(o,n[e]):n[e]==o})}(t,l)},f.p=function(){var r,t,n,e,o,i,u="undefined"!=typeof window?window.location:{},f=u.host,a=u.pathname,c=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===a?"":a,search:null!=(o=null==(i=this.t.location)?void 0:i.search)?o:void 0===c?"":c}},f.has=function(r){return Object.keys(this.t.routes).includes(r)},f.l=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 i,u;return o({},r,e[n]?((i={})[e[n].name]=t,i):"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 i;(i={})[e[0].name]=r,r=i}return o({},this.m(t),this.j(r,t))},f.m=function(r){var t=this;return r.parameterSegments.filter(function(r){return t.t.defaults[r.name]}).reduce(function(r,n,e){var i,u=n.name;return o({},r,((i={})[u]=t.t.defaults[u],i))},{})},f.j=function(r,t){var n=t.bindings,e=t.parameterSegments;return Object.entries(r).reduce(function(r,t){var i,u,f=t[0],a=t[1];if(!a||"object"!=typeof a||Array.isArray(a)||!e.some(function(r){return r.name===f}))return o({},r,((u={})[f]=a,u));if(!a.hasOwnProperty(n[f])){if(!a.hasOwnProperty("id"))throw new Error("Ziggy error: object passed as '"+f+"' parameter is missing route model binding key '"+n[f]+"'.");n[f]="id"}return o({},r,((i={})[f]=a[n[f]],i))},{})},f.valueOf=function(){return this.toString()},e(i,[{key:"params",get:function(){var r=this.v();return o({},r.params,r.query)}}]),i}(/*#__PURE__*/f(String));function F(r,t,n,e){var o=new Z(r,t,n,e);return r?o.toString():o}var P={install:function(r,t){var n=function(r,n,e,o){return void 0===o&&(o=t),F(r,n,e,o)};parseInt(r.version)>2?(r.config.globalProperties.route=n,r.provide("route",n)):r.mixin({methods:{route:n}})}};function B(r){if(!r&&!globalThis.Ziggy&&"undefined"==typeof Ziggy)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,o){return void 0===o&&(o=r),F(t,n,e,o)}}export{P as ZiggyVue,F as route,B as useRoute}; diff --git a/dist/index.js b/dist/index.js index 539c37d3..26824f4f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -function t(){return t=Object.assign?Object.assign.bind():function(t){for(var e=1;e1;){var e=t.pop(),r=e.obj[e.prop];if(u(r)){for(var n=[],o=0;o=48&&c<=57||c>=65&&c<=90||c>=97&&c<=122||i===o.RFC1738&&(40===c||41===c)?s+=u.charAt(f):c<128?s+=a[c]:c<2048?s+=a[192|c>>6]+a[128|63&c]:c<55296||c>=57344?s+=a[224|c>>12]+a[128|c>>6&63]+a[128|63&c]:(c=65536+((1023&c)<<10|1023&u.charCodeAt(f+=1)),s+=a[240|c>>18]+a[128|c>>12&63]+a[128|c>>6&63]+a[128|63&c])}return s},isBuffer:function(t){return!(!t||"object"!=typeof t||!(t.constructor&&t.constructor.isBuffer&&t.constructor.isBuffer(t)))},isRegExp:function(t){return"[object RegExp]"===Object.prototype.toString.call(t)},maybeMap:function(t,e){if(u(t)){for(var r=[],n=0;n0?w.join(",")||null:void 0}];else if(p(a))R=a;else{var N=Object.keys(w);R=s?N.sort(s):N}for(var T=0;T-1?t.split(","):t},S=function(t,e,r,n){if(t){var o=r.allowDots?t.replace(/\.([^.[]+)/g,"[$1]"):t,i=/(\[[^[\]]*])/g,u=r.depth>0&&/(\[[^[\]]*])/.exec(o),a=u?o.slice(0,u.index):o,s=[];if(a){if(!r.plainObjects&&j.call(Object.prototype,a)&&!r.allowPrototypes)return;s.push(a)}for(var f=0;r.depth>0&&null!==(u=i.exec(o))&&f=0;--i){var u,a=t[i];if("[]"===a&&r.parseArrays)u=[].concat(o);else{u=r.plainObjects?Object.create(null):{};var s="["===a.charAt(0)&&"]"===a.charAt(a.length-1)?a.slice(1,-1):a,f=parseInt(s,10);r.parseArrays||""!==s?!isNaN(f)&&a!==s&&String(f)===s&&f>=0&&r.parseArrays&&f<=r.arrayLimit?(u=[])[f]=o:"__proto__"!==s&&(u[s]=o):u={0:o}}o=u}return o}(s,e,r,n)}},R=function(t,e){var r=function(t){if(!t)return $;if(null!=t.decoder&&"function"!=typeof t.decoder)throw new TypeError("Decoder has to be a function.");if(void 0!==t.charset&&"utf-8"!==t.charset&&"iso-8859-1"!==t.charset)throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");return{allowDots:void 0===t.allowDots?$.allowDots:!!t.allowDots,allowPrototypes:"boolean"==typeof t.allowPrototypes?t.allowPrototypes:$.allowPrototypes,arrayLimit:"number"==typeof t.arrayLimit?t.arrayLimit:$.arrayLimit,charset:void 0===t.charset?$.charset:t.charset,charsetSentinel:"boolean"==typeof t.charsetSentinel?t.charsetSentinel:$.charsetSentinel,comma:"boolean"==typeof t.comma?t.comma:$.comma,decoder:"function"==typeof t.decoder?t.decoder:$.decoder,delimiter:"string"==typeof t.delimiter||f.isRegExp(t.delimiter)?t.delimiter:$.delimiter,depth:"number"==typeof t.depth||!1===t.depth?+t.depth:$.depth,ignoreQueryPrefix:!0===t.ignoreQueryPrefix,interpretNumericEntities:"boolean"==typeof t.interpretNumericEntities?t.interpretNumericEntities:$.interpretNumericEntities,parameterLimit:"number"==typeof t.parameterLimit?t.parameterLimit:$.parameterLimit,parseArrays:!1!==t.parseArrays,plainObjects:"boolean"==typeof t.plainObjects?t.plainObjects:$.plainObjects,strictNullHandling:"boolean"==typeof t.strictNullHandling?t.strictNullHandling:$.strictNullHandling}}(e);if(""===t||null==t)return r.plainObjects?Object.create(null):{};for(var n="string"==typeof t?function(t,e){var r,n={},o=(e.ignoreQueryPrefix?t.replace(/^\?/,""):t).split(e.delimiter,Infinity===e.parameterLimit?void 0:e.parameterLimit),i=-1,u=e.charset;if(e.charsetSentinel)for(r=0;r-1&&(s=w(s)?[s]:s),n[a]=j.call(n,a)?f.combine(n[a],s):s}return n}(t,r):t,o=r.plainObjects?Object.create(null):{},i=Object.keys(n),u=0;u({name:t.replace(/{|\??}/g,""),required:!/\?}$/.test(t)})))?t:[]}matchesUrl(t){if(!this.definition.methods.includes("GET"))return!1;const e=this.template.replace(/(\/?){([^}?]*)(\??)}/g,(t,e,r,n)=>{var o;const i=`(?<${r}>${(null==(o=this.wheres[r])?void 0:o.replace(/(^\^)|(\$$)/g,""))||"[^/?]+"})`;return n?`(${e}${i})?`:`${e}${i}`}).replace(/^\w+:\/\//,""),[r,n]=t.replace(/^\w+:\/\//,"").split("?"),o=new RegExp(`^${e}/?$`).exec(decodeURI(r));if(o){for(const t in o.groups)o.groups[t]="string"==typeof o.groups[t]?decodeURIComponent(o.groups[t]):o.groups[t];return{params:o.groups,query:R(n)}}return!1}compile(t){return this.parameterSegments.length?this.template.replace(/{([^}?]+)(\??)}/g,(e,r,n)=>{var o,i;if(!n&&[null,void 0].includes(t[r]))throw new Error(`Ziggy error: '${r}' parameter is required for route '${this.name}'.`);if(this.wheres[r]&&!new RegExp(`^${n?`(${this.wheres[r]})?`:this.wheres[r]}$`).test(null!=(i=t[r])?i:""))throw new Error(`Ziggy error: '${r}' parameter does not match required format '${this.wheres[r]}' for route '${this.name}'.`);return encodeURI(null!=(o=t[r])?o:"").replace(/%7C/g,"|").replace(/%25/g,"%").replace(/\$/g,"%24")}).replace(`${this.origin}//`,`${this.origin}/`).replace(/\/+$/,""):this.template}}class N extends String{constructor(e,r,n=!0,o){if(super(),this.t=null!=o?o:"undefined"!=typeof Ziggy?Ziggy:null==globalThis?void 0:globalThis.Ziggy,this.t=t({},this.t,{absolute:n}),e){if(!this.t.routes[e])throw new Error(`Ziggy error: route '${e}' is not in the route list.`);this.i=new x(e,this.t.routes[e],this.t),this.u=this.l(r)}}toString(){const e=Object.keys(this.u).filter(t=>!this.i.parameterSegments.some(({name:e})=>e===t)).filter(t=>"_query"!==t).reduce((e,r)=>t({},e,{[r]:this.u[r]}),{});return this.i.compile(this.u)+function(t,e){var r,n=t,i=function(t){if(!t)return v;if(null!=t.encoder&&"function"!=typeof t.encoder)throw new TypeError("Encoder has to be a function.");var e=t.charset||v.charset;if(void 0!==t.charset&&"utf-8"!==t.charset&&"iso-8859-1"!==t.charset)throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");var r=o.default;if(void 0!==t.format){if(!c.call(o.formatters,t.format))throw new TypeError("Unknown format option provided.");r=t.format}var n=o.formatters[r],i=v.filter;return("function"==typeof t.filter||p(t.filter))&&(i=t.filter),{addQueryPrefix:"boolean"==typeof t.addQueryPrefix?t.addQueryPrefix:v.addQueryPrefix,allowDots:void 0===t.allowDots?v.allowDots:!!t.allowDots,charset:e,charsetSentinel:"boolean"==typeof t.charsetSentinel?t.charsetSentinel:v.charsetSentinel,delimiter:void 0===t.delimiter?v.delimiter:t.delimiter,encode:"boolean"==typeof t.encode?t.encode:v.encode,encoder:"function"==typeof t.encoder?t.encoder:v.encoder,encodeValuesOnly:"boolean"==typeof t.encodeValuesOnly?t.encodeValuesOnly:v.encodeValuesOnly,filter:i,format:r,formatter:n,serializeDate:"function"==typeof t.serializeDate?t.serializeDate:v.serializeDate,skipNulls:"boolean"==typeof t.skipNulls?t.skipNulls:v.skipNulls,sort:"function"==typeof t.sort?t.sort:null,strictNullHandling:"boolean"==typeof t.strictNullHandling?t.strictNullHandling:v.strictNullHandling}}(e);"function"==typeof i.filter?n=(0,i.filter)("",n):p(i.filter)&&(r=i.filter);var u=[];if("object"!=typeof n||null===n)return"";var a=l[e&&e.arrayFormat in l?e.arrayFormat:e&&"indices"in e?e.indices?"indices":"repeat":"indices"];r||(r=Object.keys(n)),i.sort&&r.sort(i.sort);for(var s=0;s0?y+h:""}(t({},e,this.u._query),{addQueryPrefix:!0,arrayFormat:"indices",encodeValuesOnly:!0,skipNulls:!0,encoder:(t,e)=>"boolean"==typeof t?Number(t):e(t)})}p(e){e?this.t.absolute&&e.startsWith("/")&&(e=this.h().host+e):e=this.v();let r={};const[n,o]=Object.entries(this.t.routes).find(([t,n])=>r=new x(t,n,this.t).matchesUrl(e))||[void 0,void 0];return t({name:n},r,{route:o})}v(){const{host:t,pathname:e,search:r}=this.h();return(this.t.absolute?t+e:e.replace(this.t.url.replace(/^\w*:\/\/[^/]+/,""),"").replace(/^\/+/,"/"))+r}current(e,r){const{name:n,params:o,query:i,route:u}=this.p();if(!e)return n;const a=new RegExp(`^${e.replace(/\./g,"\\.").replace(/\*/g,".*")}$`).test(n);if([null,void 0].includes(r)||!a)return a;const s=new x(n,u,this.t);r=this.l(r,s);const f=t({},o,i);if(Object.values(r).every(t=>!t)&&!Object.values(f).some(t=>void 0!==t))return!0;const c=(t,e)=>Object.entries(t).every(([t,r])=>Array.isArray(r)&&Array.isArray(e[t])?r.every(r=>e[t].includes(r)):"object"==typeof r&&"object"==typeof e[t]&&null!==r&&null!==e[t]?c(r,e[t]):e[t]==r);return c(r,f)}h(){var t,e,r,n,o,i;const{host:u="",pathname:a="",search:s=""}="undefined"!=typeof window?window.location:{};return{host:null!=(t=null==(e=this.t.location)?void 0:e.host)?t:u,pathname:null!=(r=null==(n=this.t.location)?void 0:n.pathname)?r:a,search:null!=(o=null==(i=this.t.location)?void 0:i.search)?o:s}}get params(){const{params:e,query:r}=this.p();return t({},e,r)}has(t){return Object.keys(this.t.routes).includes(t)}l(e={},r=this.i){null!=e||(e={}),e=["string","number"].includes(typeof e)?[e]:e;const n=r.parameterSegments.filter(({name:t})=>!this.t.defaults[t]);return Array.isArray(e)?e=e.reduce((e,r,o)=>t({},e,n[o]?{[n[o].name]:r}:"object"==typeof r?r:{[r]:""}),{}):1!==n.length||e[n[0].name]||!e.hasOwnProperty(Object.values(r.bindings)[0])&&!e.hasOwnProperty("id")||(e={[n[0].name]:e}),t({},this.m(r),this.j(e,r))}m(e){return e.parameterSegments.filter(({name:t})=>this.t.defaults[t]).reduce((e,{name:r},n)=>t({},e,{[r]:this.t.defaults[r]}),{})}j(e,{bindings:r,parameterSegments:n}){return Object.entries(e).reduce((e,[o,i])=>{if(!i||"object"!=typeof i||Array.isArray(i)||!n.some(({name:t})=>t===o))return t({},e,{[o]:i});if(!i.hasOwnProperty(r[o])){if(!i.hasOwnProperty("id"))throw new Error(`Ziggy error: object passed as '${o}' parameter is missing route model binding key '${r[o]}'.`);r[o]="id"}return t({},e,{[o]:i[r[o]]})},{})}valueOf(){return this.toString()}}function T(t,e,r,n){const o=new N(t,e,r,n);return t?o.toString():o}const k={install(t,e){const r=(t,r,n,o=e)=>T(t,r,n,o);t.config.globalProperties.route=r,parseInt(t.version)>2&&t.provide("route",r)}};function C(t){if(!t&&!globalThis.Ziggy&&"undefined"==typeof Ziggy)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(e,r,n,o=t)=>T(e,r,n,o)}export{k as ZiggyVue,T as route,C as useRoute}; +function t(){return t=Object.assign?Object.assign.bind():function(t){for(var e=1;e1;){var e=t.pop(),r=e.obj[e.prop];if(u(r)){for(var n=[],o=0;o=48&&c<=57||c>=65&&c<=90||c>=97&&c<=122||i===o.RFC1738&&(40===c||41===c)?s+=u.charAt(f):c<128?s+=a[c]:c<2048?s+=a[192|c>>6]+a[128|63&c]:c<55296||c>=57344?s+=a[224|c>>12]+a[128|c>>6&63]+a[128|63&c]:(c=65536+((1023&c)<<10|1023&u.charCodeAt(f+=1)),s+=a[240|c>>18]+a[128|c>>12&63]+a[128|c>>6&63]+a[128|63&c])}return s},isBuffer:function(t){return!(!t||"object"!=typeof t||!(t.constructor&&t.constructor.isBuffer&&t.constructor.isBuffer(t)))},isRegExp:function(t){return"[object RegExp]"===Object.prototype.toString.call(t)},maybeMap:function(t,e){if(u(t)){for(var r=[],n=0;n0?w.join(",")||null:void 0}];else if(p(a))R=a;else{var N=Object.keys(w);R=s?N.sort(s):N}for(var T=0;T-1?t.split(","):t},S=function(t,e,r,n){if(t){var o=r.allowDots?t.replace(/\.([^.[]+)/g,"[$1]"):t,i=/(\[[^[\]]*])/g,u=r.depth>0&&/(\[[^[\]]*])/.exec(o),a=u?o.slice(0,u.index):o,s=[];if(a){if(!r.plainObjects&&j.call(Object.prototype,a)&&!r.allowPrototypes)return;s.push(a)}for(var f=0;r.depth>0&&null!==(u=i.exec(o))&&f=0;--i){var u,a=t[i];if("[]"===a&&r.parseArrays)u=[].concat(o);else{u=r.plainObjects?Object.create(null):{};var s="["===a.charAt(0)&&"]"===a.charAt(a.length-1)?a.slice(1,-1):a,f=parseInt(s,10);r.parseArrays||""!==s?!isNaN(f)&&a!==s&&String(f)===s&&f>=0&&r.parseArrays&&f<=r.arrayLimit?(u=[])[f]=o:"__proto__"!==s&&(u[s]=o):u={0:o}}o=u}return o}(s,e,r,n)}},R=function(t,e){var r=function(t){if(!t)return $;if(null!=t.decoder&&"function"!=typeof t.decoder)throw new TypeError("Decoder has to be a function.");if(void 0!==t.charset&&"utf-8"!==t.charset&&"iso-8859-1"!==t.charset)throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");return{allowDots:void 0===t.allowDots?$.allowDots:!!t.allowDots,allowPrototypes:"boolean"==typeof t.allowPrototypes?t.allowPrototypes:$.allowPrototypes,arrayLimit:"number"==typeof t.arrayLimit?t.arrayLimit:$.arrayLimit,charset:void 0===t.charset?$.charset:t.charset,charsetSentinel:"boolean"==typeof t.charsetSentinel?t.charsetSentinel:$.charsetSentinel,comma:"boolean"==typeof t.comma?t.comma:$.comma,decoder:"function"==typeof t.decoder?t.decoder:$.decoder,delimiter:"string"==typeof t.delimiter||f.isRegExp(t.delimiter)?t.delimiter:$.delimiter,depth:"number"==typeof t.depth||!1===t.depth?+t.depth:$.depth,ignoreQueryPrefix:!0===t.ignoreQueryPrefix,interpretNumericEntities:"boolean"==typeof t.interpretNumericEntities?t.interpretNumericEntities:$.interpretNumericEntities,parameterLimit:"number"==typeof t.parameterLimit?t.parameterLimit:$.parameterLimit,parseArrays:!1!==t.parseArrays,plainObjects:"boolean"==typeof t.plainObjects?t.plainObjects:$.plainObjects,strictNullHandling:"boolean"==typeof t.strictNullHandling?t.strictNullHandling:$.strictNullHandling}}(e);if(""===t||null==t)return r.plainObjects?Object.create(null):{};for(var n="string"==typeof t?function(t,e){var r,n={},o=(e.ignoreQueryPrefix?t.replace(/^\?/,""):t).split(e.delimiter,Infinity===e.parameterLimit?void 0:e.parameterLimit),i=-1,u=e.charset;if(e.charsetSentinel)for(r=0;r-1&&(s=w(s)?[s]:s),n[a]=j.call(n,a)?f.combine(n[a],s):s}return n}(t,r):t,o=r.plainObjects?Object.create(null):{},i=Object.keys(n),u=0;u({name:t.replace(/{|\??}/g,""),required:!/\?}$/.test(t)})))?t:[]}matchesUrl(t){if(!this.definition.methods.includes("GET"))return!1;const e=this.template.replace(/(\/?){([^}?]*)(\??)}/g,(t,e,r,n)=>{var o;const i=`(?<${r}>${(null==(o=this.wheres[r])?void 0:o.replace(/(^\^)|(\$$)/g,""))||"[^/?]+"})`;return n?`(${e}${i})?`:`${e}${i}`}).replace(/^\w+:\/\//,""),[r,n]=t.replace(/^\w+:\/\//,"").split("?"),o=new RegExp(`^${e}/?$`).exec(decodeURI(r));if(o){for(const t in o.groups)o.groups[t]="string"==typeof o.groups[t]?decodeURIComponent(o.groups[t]):o.groups[t];return{params:o.groups,query:R(n)}}return!1}compile(t){return this.parameterSegments.length?this.template.replace(/{([^}?]+)(\??)}/g,(e,r,n)=>{var o,i;if(!n&&[null,void 0].includes(t[r]))throw new Error(`Ziggy error: '${r}' parameter is required for route '${this.name}'.`);if(this.wheres[r]&&!new RegExp(`^${n?`(${this.wheres[r]})?`:this.wheres[r]}$`).test(null!=(i=t[r])?i:""))throw new Error(`Ziggy error: '${r}' parameter does not match required format '${this.wheres[r]}' for route '${this.name}'.`);return encodeURI(null!=(o=t[r])?o:"").replace(/%7C/g,"|").replace(/%25/g,"%").replace(/\$/g,"%24")}).replace(`${this.origin}//`,`${this.origin}/`).replace(/\/+$/,""):this.template}}class N extends String{constructor(e,r,n=!0,o){if(super(),this.t=null!=o?o:"undefined"!=typeof Ziggy?Ziggy:null==globalThis?void 0:globalThis.Ziggy,this.t=t({},this.t,{absolute:n}),e){if(!this.t.routes[e])throw new Error(`Ziggy error: route '${e}' is not in the route list.`);this.i=new x(e,this.t.routes[e],this.t),this.u=this.l(r)}}toString(){const e=Object.keys(this.u).filter(t=>!this.i.parameterSegments.some(({name:e})=>e===t)).filter(t=>"_query"!==t).reduce((e,r)=>t({},e,{[r]:this.u[r]}),{});return this.i.compile(this.u)+function(t,e){var r,n=t,i=function(t){if(!t)return v;if(null!=t.encoder&&"function"!=typeof t.encoder)throw new TypeError("Encoder has to be a function.");var e=t.charset||v.charset;if(void 0!==t.charset&&"utf-8"!==t.charset&&"iso-8859-1"!==t.charset)throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");var r=o.default;if(void 0!==t.format){if(!c.call(o.formatters,t.format))throw new TypeError("Unknown format option provided.");r=t.format}var n=o.formatters[r],i=v.filter;return("function"==typeof t.filter||p(t.filter))&&(i=t.filter),{addQueryPrefix:"boolean"==typeof t.addQueryPrefix?t.addQueryPrefix:v.addQueryPrefix,allowDots:void 0===t.allowDots?v.allowDots:!!t.allowDots,charset:e,charsetSentinel:"boolean"==typeof t.charsetSentinel?t.charsetSentinel:v.charsetSentinel,delimiter:void 0===t.delimiter?v.delimiter:t.delimiter,encode:"boolean"==typeof t.encode?t.encode:v.encode,encoder:"function"==typeof t.encoder?t.encoder:v.encoder,encodeValuesOnly:"boolean"==typeof t.encodeValuesOnly?t.encodeValuesOnly:v.encodeValuesOnly,filter:i,format:r,formatter:n,serializeDate:"function"==typeof t.serializeDate?t.serializeDate:v.serializeDate,skipNulls:"boolean"==typeof t.skipNulls?t.skipNulls:v.skipNulls,sort:"function"==typeof t.sort?t.sort:null,strictNullHandling:"boolean"==typeof t.strictNullHandling?t.strictNullHandling:v.strictNullHandling}}(e);"function"==typeof i.filter?n=(0,i.filter)("",n):p(i.filter)&&(r=i.filter);var u=[];if("object"!=typeof n||null===n)return"";var a=l[e&&e.arrayFormat in l?e.arrayFormat:e&&"indices"in e?e.indices?"indices":"repeat":"indices"];r||(r=Object.keys(n)),i.sort&&r.sort(i.sort);for(var s=0;s0?y+h:""}(t({},e,this.u._query),{addQueryPrefix:!0,arrayFormat:"indices",encodeValuesOnly:!0,skipNulls:!0,encoder:(t,e)=>"boolean"==typeof t?Number(t):e(t)})}p(e){e?this.t.absolute&&e.startsWith("/")&&(e=this.h().host+e):e=this.v();let r={};const[n,o]=Object.entries(this.t.routes).find(([t,n])=>r=new x(t,n,this.t).matchesUrl(e))||[void 0,void 0];return t({name:n},r,{route:o})}v(){const{host:t,pathname:e,search:r}=this.h();return(this.t.absolute?t+e:e.replace(this.t.url.replace(/^\w*:\/\/[^/]+/,""),"").replace(/^\/+/,"/"))+r}current(e,r){const{name:n,params:o,query:i,route:u}=this.p();if(!e)return n;const a=new RegExp(`^${e.replace(/\./g,"\\.").replace(/\*/g,".*")}$`).test(n);if([null,void 0].includes(r)||!a)return a;const s=new x(n,u,this.t);r=this.l(r,s);const f=t({},o,i);if(Object.values(r).every(t=>!t)&&!Object.values(f).some(t=>void 0!==t))return!0;const c=(t,e)=>Object.entries(t).every(([t,r])=>Array.isArray(r)&&Array.isArray(e[t])?r.every(r=>e[t].includes(r)):"object"==typeof r&&"object"==typeof e[t]&&null!==r&&null!==e[t]?c(r,e[t]):e[t]==r);return c(r,f)}h(){var t,e,r,n,o,i;const{host:u="",pathname:a="",search:s=""}="undefined"!=typeof window?window.location:{};return{host:null!=(t=null==(e=this.t.location)?void 0:e.host)?t:u,pathname:null!=(r=null==(n=this.t.location)?void 0:n.pathname)?r:a,search:null!=(o=null==(i=this.t.location)?void 0:i.search)?o:s}}get params(){const{params:e,query:r}=this.p();return t({},e,r)}has(t){return Object.keys(this.t.routes).includes(t)}l(e={},r=this.i){null!=e||(e={}),e=["string","number"].includes(typeof e)?[e]:e;const n=r.parameterSegments.filter(({name:t})=>!this.t.defaults[t]);return Array.isArray(e)?e=e.reduce((e,r,o)=>t({},e,n[o]?{[n[o].name]:r}:"object"==typeof r?r:{[r]:""}),{}):1!==n.length||e[n[0].name]||!e.hasOwnProperty(Object.values(r.bindings)[0])&&!e.hasOwnProperty("id")||(e={[n[0].name]:e}),t({},this.m(r),this.j(e,r))}m(e){return e.parameterSegments.filter(({name:t})=>this.t.defaults[t]).reduce((e,{name:r},n)=>t({},e,{[r]:this.t.defaults[r]}),{})}j(e,{bindings:r,parameterSegments:n}){return Object.entries(e).reduce((e,[o,i])=>{if(!i||"object"!=typeof i||Array.isArray(i)||!n.some(({name:t})=>t===o))return t({},e,{[o]:i});if(!i.hasOwnProperty(r[o])){if(!i.hasOwnProperty("id"))throw new Error(`Ziggy error: object passed as '${o}' parameter is missing route model binding key '${r[o]}'.`);r[o]="id"}return t({},e,{[o]:i[r[o]]})},{})}valueOf(){return this.toString()}}function T(t,e,r,n){const o=new N(t,e,r,n);return t?o.toString():o}const k={install(t,e){const r=(t,r,n,o=e)=>T(t,r,n,o);parseInt(t.version)>2?(t.config.globalProperties.route=r,t.provide("route",r)):t.mixin({methods:{route:r}})}};function C(t){if(!t&&!globalThis.Ziggy&&"undefined"==typeof Ziggy)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(e,r,n,o=t)=>T(e,r,n,o)}export{k as ZiggyVue,T as route,C as useRoute}; diff --git a/package-lock.json b/package-lock.json index 08469bd7..113d9e40 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ziggy-js", - "version": "2.0.5", + "version": "2.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ziggy-js", - "version": "2.0.5", + "version": "2.1.0", "license": "MIT", "dependencies": { "qs": "~6.9.7" diff --git a/package.json b/package.json index f4d0c9f4..92d75290 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ziggy-js", - "version": "2.0.5", + "version": "2.1.0", "description": "Use your Laravel named routes in JavaScript.", "keywords": [ "laravel",