ziggy-js 2.0.5 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.esm.js +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/src/js/index.d.ts +25 -9
package/dist/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{parse as r,stringify as t}from"qs";function n(){try{var r=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(r){}return(n=function(){return!!r})()}function e(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 i(r,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(r,e(i.key),i)}}function
|
|
1
|
+
import{parse as r,stringify as t}from"qs";function n(){try{var r=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(r){}return(n=function(){return!!r})()}function e(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 i(r,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(r,e(i.key),i)}}function o(r,t,n){return t&&i(r.prototype,t),n&&i(r,n),Object.defineProperty(r,"prototype",{writable:!1}),r}function u(){return u=Object.assign?Object.assign.bind():function(r){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var e in n)Object.prototype.hasOwnProperty.call(n,e)&&(r[e]=n[e])}return r},u.apply(this,arguments)}function f(r){return f=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(r){return r.__proto__||Object.getPrototypeOf(r)},f(r)}function c(r,t){return c=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(r,t){return r.__proto__=t,r},c(r,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,e)}function e(){return function(r,t,e){if(n())return Reflect.construct.apply(null,arguments);var i=[null];i.push.apply(i,t);var o=new(r.bind.apply(r,i));return e&&c(o,e.prototype),o}(r,arguments,f(this).constructor)}return e.prototype=Object.create(r.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),c(e,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=this;if(!this.definition.methods.includes("GET"))return!1;var e=this.template.replace(/(\/?){([^}?]*)(\??)}/g,function(r,t,e,i){var o,u="(?<"+e+">"+((null==(o=n.wheres[e])?void 0:o.replace(/(^\^)|(\$$)/g,""))||"[^/?]+")+")";return i?"("+t+u+")?":""+t+u}).replace(/^\w+:\/\//,""),i=t.replace(/^\w+:\/\//,"").split("?"),o=i[0],u=i[1],f=new RegExp("^"+e+"/?$").exec(decodeURI(o));if(f){for(var c in f.groups)f.groups[c]="string"==typeof f.groups[c]?decodeURIComponent(f.groups[c]):f.groups[c];return{params:f.groups,query:r(u)}}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 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.origin+"//",this.origin+"/").replace(/\/+$/,""):this.template},o(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:[]}}]),t}(),l=/*#__PURE__*/function(r){var n,e;function i(t,n,e,i){var o;if(void 0===e&&(e=!0),(o=r.call(this)||this).t=null!=i?i:"undefined"!=typeof Ziggy?Ziggy:null==globalThis?void 0:globalThis.Ziggy,o.t=u({},o.t,{absolute:e}),t){if(!o.t.routes[t])throw new Error("Ziggy error: route '"+t+"' is not in the route list.");o.i=new s(t,o.t.routes[t],o.t),o.o=o.u(n)}return o}e=r,(n=i).prototype=Object.create(e.prototype),n.prototype.constructor=n,c(n,e);var f=i.prototype;return f.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 u({},t,((e={})[n]=r.o[n],e))},{});return this.i.compile(this.o)+t(u({},n,this.o._query),{addQueryPrefix:!0,arrayFormat:"indices",encodeValuesOnly:!0,skipNulls:!0,encoder:function(r,t){return"boolean"==typeof r?Number(r):t(r)}})},f.l=function(r){var t=this;r?this.t.absolute&&r.startsWith("/")&&(r=this.v().host+r):r=this.h();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 u({name:e[0]},n,{route:e[1]})},f.h=function(){var r=this.v(),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.l(),e=n.name,i=n.params,o=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=u({},i,o);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],i=t[1];return Array.isArray(i)&&Array.isArray(n[e])?i.every(function(r){return n[e].includes(r)}):"object"==typeof i&&"object"==typeof n[e]&&null!==i&&null!==n[e]?r(i,n[e]):n[e]==i})}(t,l)},f.v=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}},f.has=function(r){return Object.keys(this.t.routes).includes(r)},f.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 i,o;return u({},r,e[n]?((i={})[e[n].name]=t,i):"object"==typeof t?t:((o={})[t]="",o))},{});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 u({},this.p(t),this.m(r,t))},f.p=function(r){var t=this;return r.parameterSegments.filter(function(r){return t.t.defaults[r.name]}).reduce(function(r,n,e){var i,o=n.name;return u({},r,((i={})[o]=t.t.defaults[o],i))},{})},f.m=function(r,t){var n=t.bindings,e=t.parameterSegments;return Object.entries(r).reduce(function(r,t){var i,o,f=t[0],c=t[1];if(!c||"object"!=typeof c||Array.isArray(c)||!e.some(function(r){return r.name===f}))return u({},r,((o={})[f]=c,o));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 u({},r,((i={})[f]=c[n[f]],i))},{})},f.valueOf=function(){return this.toString()},o(i,[{key:"params",get:function(){var r=this.l();return u({},r.params,r.query)}}]),i}(/*#__PURE__*/a(String));function v(r,t,n,e){var i=new l(r,t,n,e);return r?i.toString():i}var h={install:function(r,t){var n=function(r,n,e,i){return void 0===i&&(i=t),v(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)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),v(t,n,e,i)}}export{h as ZiggyVue,v as route,d as useRoute};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{parse as t,stringify as r}from"qs";function e(){return e=Object.assign?Object.assign.bind():function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i])}return t},e.apply(this,arguments)}class i{constructor(t,r,e){var i,n;this.name=t,this.definition=r,this.bindings=null!=(i=r.bindings)?i:{},this.wheres=null!=(n=r.wheres)?n:{},this.config=e}get template(){const t=`${this.origin}/${this.definition.uri}`.replace(/\/+$/,"");return""===t?"/":t}get origin(){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:""}get parameterSegments(){var t,r;return null!=(t=null==(r=this.template.match(/{[^}?]+\??}/g))?void 0:r.map(t=>({name:t.replace(/{|\??}/g,""),required:!/\?}$/.test(t)})))?t:[]}matchesUrl(r){if(!this.definition.methods.includes("GET"))return!1;const e=this.template.replace(/(\/?){([^}?]*)(\??)}/g,(t,r,e,i)=>{var n;const s=`(?<${e}>${(null==(n=this.wheres[e])?void 0:n.replace(/(^\^)|(\$$)/g,""))||"[^/?]+"})`;return i?`(${r}${s})?`:`${r}${s}`}).replace(/^\w+:\/\//,""),[i,n]=r.replace(/^\w+:\/\//,"").split("?"),s=new RegExp(`^${e}/?$`).exec(decodeURI(i));if(s){for(const t in s.groups)s.groups[t]="string"==typeof s.groups[t]?decodeURIComponent(s.groups[t]):s.groups[t];return{params:s.groups,query:t(n)}}return!1}compile(t){return this.parameterSegments.length?this.template.replace(/{([^}?]+)(\??)}/g,(r,e,i)=>{var n,s;if(!i&&[null,void 0].includes(t[e]))throw new Error(`Ziggy error: '${e}' parameter is required for route '${this.name}'.`);if(this.wheres[e]&&!new RegExp(`^${i?`(${this.wheres[e]})?`:this.wheres[e]}$`).test(null!=(s=t[e])?s:""))throw new Error(`Ziggy error: '${e}' parameter does not match required format '${this.wheres[e]}' for route '${this.name}'.`);return encodeURI(null!=(n=t[e])?n:"").replace(/%7C/g,"|").replace(/%25/g,"%").replace(/\$/g,"%24")}).replace(`${this.origin}//`,`${this.origin}/`).replace(/\/+$/,""):this.template}}class n extends String{constructor(t,r,n=!0,s){if(super(),this.t=null!=s?s:"undefined"!=typeof Ziggy?Ziggy:null==globalThis?void 0:globalThis.Ziggy,this.t=e({},this.t,{absolute:n}),t){if(!this.t.routes[t])throw new Error(`Ziggy error: route '${t}' is not in the route list.`);this.i=new i(t,this.t.routes[t],this.t),this.o=this.h(r)}}toString(){const t=Object.keys(this.o).filter(t=>!this.i.parameterSegments.some(({name:r})=>r===t)).filter(t=>"_query"!==t).reduce((t,r)=>e({},t,{[r]:this.o[r]}),{});return this.i.compile(this.o)+r(e({},t,this.o._query),{addQueryPrefix:!0,arrayFormat:"indices",encodeValuesOnly:!0,skipNulls:!0,encoder:(t,r)=>"boolean"==typeof t?Number(t):r(t)})}u(t){t?this.t.absolute&&t.startsWith("/")&&(t=this.l().host+t):t=this.m();let r={};const[n,s]=Object.entries(this.t.routes).find(([e,n])=>r=new i(e,n,this.t).matchesUrl(t))||[void 0,void 0];return e({name:n},r,{route:s})}m(){const{host:t,pathname:r,search:e}=this.l();return(this.t.absolute?t+r:r.replace(this.t.url.replace(/^\w*:\/\/[^/]+/,""),"").replace(/^\/+/,"/"))+e}current(t,r){const{name:n,params:s,query:o,route:h}=this.u();if(!t)return n;const u=new RegExp(`^${t.replace(/\./g,"\\.").replace(/\*/g,".*")}$`).test(n);if([null,void 0].includes(r)||!u)return u;const a=new i(n,h,this.t);r=this.h(r,a);const l=e({},s,o);if(Object.values(r).every(t=>!t)&&!Object.values(l).some(t=>void 0!==t))return!0;const c=(t,r)=>Object.entries(t).every(([t,e])=>Array.isArray(e)&&Array.isArray(r[t])?e.every(e=>r[t].includes(e)):"object"==typeof e&&"object"==typeof r[t]&&null!==e&&null!==r[t]?c(e,r[t]):r[t]==e);return c(r,l)}l(){var t,r,e,i,n,s;const{host:o="",pathname:h="",search:u=""}="undefined"!=typeof window?window.location:{};return{host:null!=(t=null==(r=this.t.location)?void 0:r.host)?t:o,pathname:null!=(e=null==(i=this.t.location)?void 0:i.pathname)?e:h,search:null!=(n=null==(s=this.t.location)?void 0:s.search)?n:u}}get params(){const{params:t,query:r}=this.u();return e({},t,r)}has(t){return Object.keys(this.t.routes).includes(t)}h(t={},r=this.i){null!=t||(t={}),t=["string","number"].includes(typeof t)?[t]:t;const i=r.parameterSegments.filter(({name:t})=>!this.t.defaults[t]);return Array.isArray(t)?t=t.reduce((t,r,n)=>e({},t,i[n]?{[i[n].name]:r}:"object"==typeof r?r:{[r]:""}),{}):1!==i.length||t[i[0].name]||!t.hasOwnProperty(Object.values(r.bindings)[0])&&!t.hasOwnProperty("id")||(t={[i[0].name]:t}),e({},this.p(r),this.$(t,r))}p(t){return t.parameterSegments.filter(({name:t})=>this.t.defaults[t]).reduce((t,{name:r},i)=>e({},t,{[r]:this.t.defaults[r]}),{})}$(t,{bindings:r,parameterSegments:i}){return Object.entries(t).reduce((t,[n,s])=>{if(!s||"object"!=typeof s||Array.isArray(s)||!i.some(({name:t})=>t===n))return e({},t,{[n]:s});if(!s.hasOwnProperty(r[n])){if(!s.hasOwnProperty("id"))throw new Error(`Ziggy error: object passed as '${n}' parameter is missing route model binding key '${r[n]}'.`);r[n]="id"}return e({},t,{[n]:s[r[n]]})},{})}valueOf(){return this.toString()}}function s(t,r,e,i){const s=new n(t,r,e,i);return t?s.toString():s}const o={install(t,r){const e=(t,e,i,n=r)=>s(t,e,i,n);t.config.globalProperties.route=e,
|
|
1
|
+
import{parse as t,stringify as r}from"qs";function e(){return e=Object.assign?Object.assign.bind():function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i])}return t},e.apply(this,arguments)}class i{constructor(t,r,e){var i,n;this.name=t,this.definition=r,this.bindings=null!=(i=r.bindings)?i:{},this.wheres=null!=(n=r.wheres)?n:{},this.config=e}get template(){const t=`${this.origin}/${this.definition.uri}`.replace(/\/+$/,"");return""===t?"/":t}get origin(){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:""}get parameterSegments(){var t,r;return null!=(t=null==(r=this.template.match(/{[^}?]+\??}/g))?void 0:r.map(t=>({name:t.replace(/{|\??}/g,""),required:!/\?}$/.test(t)})))?t:[]}matchesUrl(r){if(!this.definition.methods.includes("GET"))return!1;const e=this.template.replace(/(\/?){([^}?]*)(\??)}/g,(t,r,e,i)=>{var n;const s=`(?<${e}>${(null==(n=this.wheres[e])?void 0:n.replace(/(^\^)|(\$$)/g,""))||"[^/?]+"})`;return i?`(${r}${s})?`:`${r}${s}`}).replace(/^\w+:\/\//,""),[i,n]=r.replace(/^\w+:\/\//,"").split("?"),s=new RegExp(`^${e}/?$`).exec(decodeURI(i));if(s){for(const t in s.groups)s.groups[t]="string"==typeof s.groups[t]?decodeURIComponent(s.groups[t]):s.groups[t];return{params:s.groups,query:t(n)}}return!1}compile(t){return this.parameterSegments.length?this.template.replace(/{([^}?]+)(\??)}/g,(r,e,i)=>{var n,s;if(!i&&[null,void 0].includes(t[e]))throw new Error(`Ziggy error: '${e}' parameter is required for route '${this.name}'.`);if(this.wheres[e]&&!new RegExp(`^${i?`(${this.wheres[e]})?`:this.wheres[e]}$`).test(null!=(s=t[e])?s:""))throw new Error(`Ziggy error: '${e}' parameter does not match required format '${this.wheres[e]}' for route '${this.name}'.`);return encodeURI(null!=(n=t[e])?n:"").replace(/%7C/g,"|").replace(/%25/g,"%").replace(/\$/g,"%24")}).replace(`${this.origin}//`,`${this.origin}/`).replace(/\/+$/,""):this.template}}class n extends String{constructor(t,r,n=!0,s){if(super(),this.t=null!=s?s:"undefined"!=typeof Ziggy?Ziggy:null==globalThis?void 0:globalThis.Ziggy,this.t=e({},this.t,{absolute:n}),t){if(!this.t.routes[t])throw new Error(`Ziggy error: route '${t}' is not in the route list.`);this.i=new i(t,this.t.routes[t],this.t),this.o=this.h(r)}}toString(){const t=Object.keys(this.o).filter(t=>!this.i.parameterSegments.some(({name:r})=>r===t)).filter(t=>"_query"!==t).reduce((t,r)=>e({},t,{[r]:this.o[r]}),{});return this.i.compile(this.o)+r(e({},t,this.o._query),{addQueryPrefix:!0,arrayFormat:"indices",encodeValuesOnly:!0,skipNulls:!0,encoder:(t,r)=>"boolean"==typeof t?Number(t):r(t)})}u(t){t?this.t.absolute&&t.startsWith("/")&&(t=this.l().host+t):t=this.m();let r={};const[n,s]=Object.entries(this.t.routes).find(([e,n])=>r=new i(e,n,this.t).matchesUrl(t))||[void 0,void 0];return e({name:n},r,{route:s})}m(){const{host:t,pathname:r,search:e}=this.l();return(this.t.absolute?t+r:r.replace(this.t.url.replace(/^\w*:\/\/[^/]+/,""),"").replace(/^\/+/,"/"))+e}current(t,r){const{name:n,params:s,query:o,route:h}=this.u();if(!t)return n;const u=new RegExp(`^${t.replace(/\./g,"\\.").replace(/\*/g,".*")}$`).test(n);if([null,void 0].includes(r)||!u)return u;const a=new i(n,h,this.t);r=this.h(r,a);const l=e({},s,o);if(Object.values(r).every(t=>!t)&&!Object.values(l).some(t=>void 0!==t))return!0;const c=(t,r)=>Object.entries(t).every(([t,e])=>Array.isArray(e)&&Array.isArray(r[t])?e.every(e=>r[t].includes(e)):"object"==typeof e&&"object"==typeof r[t]&&null!==e&&null!==r[t]?c(e,r[t]):r[t]==e);return c(r,l)}l(){var t,r,e,i,n,s;const{host:o="",pathname:h="",search:u=""}="undefined"!=typeof window?window.location:{};return{host:null!=(t=null==(r=this.t.location)?void 0:r.host)?t:o,pathname:null!=(e=null==(i=this.t.location)?void 0:i.pathname)?e:h,search:null!=(n=null==(s=this.t.location)?void 0:s.search)?n:u}}get params(){const{params:t,query:r}=this.u();return e({},t,r)}has(t){return Object.keys(this.t.routes).includes(t)}h(t={},r=this.i){null!=t||(t={}),t=["string","number"].includes(typeof t)?[t]:t;const i=r.parameterSegments.filter(({name:t})=>!this.t.defaults[t]);return Array.isArray(t)?t=t.reduce((t,r,n)=>e({},t,i[n]?{[i[n].name]:r}:"object"==typeof r?r:{[r]:""}),{}):1!==i.length||t[i[0].name]||!t.hasOwnProperty(Object.values(r.bindings)[0])&&!t.hasOwnProperty("id")||(t={[i[0].name]:t}),e({},this.p(r),this.$(t,r))}p(t){return t.parameterSegments.filter(({name:t})=>this.t.defaults[t]).reduce((t,{name:r},i)=>e({},t,{[r]:this.t.defaults[r]}),{})}$(t,{bindings:r,parameterSegments:i}){return Object.entries(t).reduce((t,[n,s])=>{if(!s||"object"!=typeof s||Array.isArray(s)||!i.some(({name:t})=>t===n))return e({},t,{[n]:s});if(!s.hasOwnProperty(r[n])){if(!s.hasOwnProperty("id"))throw new Error(`Ziggy error: object passed as '${n}' parameter is missing route model binding key '${r[n]}'.`);r[n]="id"}return e({},t,{[n]:s[r[n]]})},{})}valueOf(){return this.toString()}}function s(t,r,e,i){const s=new n(t,r,e,i);return t?s.toString():s}const o={install(t,r){const e=(t,e,i,n=r)=>s(t,e,i,n);parseInt(t.version)>2?(t.config.globalProperties.route=e,t.provide("route",e)):t.mixin({methods:{route:e}})}};function h(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(r,e,i,n=t)=>s(r,e,i,n)}export{o as ZiggyVue,s as route,h as useRoute};
|
package/package.json
CHANGED
package/src/js/index.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ type RouteName = KnownRouteName | (string & {});
|
|
|
21
21
|
/**
|
|
22
22
|
* Information about a single route parameter.
|
|
23
23
|
*/
|
|
24
|
-
type ParameterInfo = { name: string; binding?: string };
|
|
24
|
+
type ParameterInfo = { name: string; required: boolean; binding?: string };
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
27
|
* A primitive route parameter value, as it would appear in a URL.
|
|
@@ -43,15 +43,19 @@ type ParameterValue = RawParameterValue | DefaultRoutable;
|
|
|
43
43
|
* A parseable route parameter, either plain or nested inside an object under its binding key.
|
|
44
44
|
*/
|
|
45
45
|
type Routable<I extends ParameterInfo> = I extends { binding: string }
|
|
46
|
-
? { [K in I['binding']]: RawParameterValue } | RawParameterValue
|
|
46
|
+
? ({ [K in I['binding']]: RawParameterValue } & Record<keyof any, unknown>) | RawParameterValue
|
|
47
47
|
: ParameterValue;
|
|
48
48
|
|
|
49
49
|
// Uncomment to test:
|
|
50
|
-
// type A = Routable<{ name: 'foo', binding: 'bar' }>;
|
|
50
|
+
// type A = Routable<{ name: 'foo', required: true, binding: 'bar' }>;
|
|
51
51
|
// = RawParameterValue | { bar: RawParameterValue }
|
|
52
|
-
// type B = Routable<{ name: 'foo' }>;
|
|
52
|
+
// type B = Routable<{ name: 'foo', required: true, }>;
|
|
53
53
|
// = RawParameterValue | DefaultRoutable
|
|
54
54
|
|
|
55
|
+
// Utility types for KnownRouteParamsObject
|
|
56
|
+
type RequiredParams<I extends readonly ParameterInfo[]> = Extract<I[number], { required: true }>;
|
|
57
|
+
type OptionalParams<I extends readonly ParameterInfo[]> = Extract<I[number], { required: false }>;
|
|
58
|
+
|
|
55
59
|
/**
|
|
56
60
|
* An object containing a special '_query' key to target the query string of a URL.
|
|
57
61
|
*/
|
|
@@ -64,13 +68,19 @@ type GenericRouteParamsObject = Record<keyof any, unknown> & HasQueryParam;
|
|
|
64
68
|
/**
|
|
65
69
|
* An object of parameters for a specific named route.
|
|
66
70
|
*/
|
|
67
|
-
// TODO: The keys here could be non-optional (or more detailed) if we can determine which params are required/not.
|
|
68
71
|
type KnownRouteParamsObject<I extends readonly ParameterInfo[]> = {
|
|
69
|
-
[T in I
|
|
72
|
+
[T in RequiredParams<I> as T['name']]: Routable<T>;
|
|
73
|
+
} & {
|
|
74
|
+
[T in OptionalParams<I> as T['name']]?: Routable<T>;
|
|
70
75
|
} & GenericRouteParamsObject;
|
|
71
76
|
// `readonly` allows TypeScript to determine the actual values of all the
|
|
72
77
|
// parameter names inside the array, instead of just seeing `string`.
|
|
73
78
|
// See https://github.com/tighten/ziggy/pull/664#discussion_r1329978447.
|
|
79
|
+
|
|
80
|
+
// Uncomment to test:
|
|
81
|
+
// type A = KnownRouteParamsObject<[{ name: 'foo'; required: true }, { name: 'bar'; required: false }]>;
|
|
82
|
+
// = { foo: ... } & { bar?: ... }
|
|
83
|
+
|
|
74
84
|
/**
|
|
75
85
|
* An object of route parameters.
|
|
76
86
|
*/
|
|
@@ -98,7 +108,7 @@ type KnownRouteParamsArray<I extends readonly ParameterInfo[]> = [
|
|
|
98
108
|
// See https://github.com/tighten/ziggy/pull/664#discussion_r1330002370.
|
|
99
109
|
|
|
100
110
|
// Uncomment to test:
|
|
101
|
-
// type B = KnownRouteParamsArray<[{ name: 'post'
|
|
111
|
+
// type B = KnownRouteParamsArray<[{ name: 'post'; required: true; binding: 'uuid' }]>;
|
|
102
112
|
// = [RawParameterValue | { uuid: RawParameterValue }, ...unknown[]]
|
|
103
113
|
|
|
104
114
|
/**
|
|
@@ -111,7 +121,7 @@ type RouteParamsArray<N extends RouteName> = N extends KnownRouteName
|
|
|
111
121
|
/**
|
|
112
122
|
* All possible parameter argument shapes for a route.
|
|
113
123
|
*/
|
|
114
|
-
type RouteParams<N extends RouteName> =
|
|
124
|
+
type RouteParams<N extends RouteName> = RouteParamsObject<N> | RouteParamsArray<N>;
|
|
115
125
|
|
|
116
126
|
/**
|
|
117
127
|
* A route.
|
|
@@ -146,7 +156,7 @@ interface Config {
|
|
|
146
156
|
*/
|
|
147
157
|
interface Router {
|
|
148
158
|
current(): RouteName | undefined;
|
|
149
|
-
current<T extends RouteName>(name: T, params?: RouteParams<T>): boolean;
|
|
159
|
+
current<T extends RouteName>(name: T, params?: ParameterValue | RouteParams<T>): boolean;
|
|
150
160
|
get params(): Record<string, unknown>;
|
|
151
161
|
has<T extends RouteName>(name: T): boolean;
|
|
152
162
|
}
|
|
@@ -163,6 +173,12 @@ export function route<T extends RouteName>(
|
|
|
163
173
|
absolute?: boolean,
|
|
164
174
|
config?: Config,
|
|
165
175
|
): string;
|
|
176
|
+
export function route<T extends RouteName>(
|
|
177
|
+
name: T,
|
|
178
|
+
params?: ParameterValue | undefined,
|
|
179
|
+
absolute?: boolean,
|
|
180
|
+
config?: Config,
|
|
181
|
+
): string;
|
|
166
182
|
// Called with configuration arguments only - returns a configured Router instance
|
|
167
183
|
export function route(
|
|
168
184
|
name: undefined,
|