ziggy-js 1.7.2 → 1.8.1
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/README.md +202 -236
- package/dist/index.es.js +1 -1
- package/dist/index.js +1 -1
- package/dist/index.m.js +1 -1
- package/dist/react.es.js +1 -1
- package/dist/react.js +1 -1
- package/dist/react.m.js +1 -1
- package/dist/vue.es.js +1 -1
- package/dist/vue.js +1 -1
- package/dist/vue.m.js +1 -1
- package/package.json +21 -14
- package/src/js/index.d.ts +172 -0
- package/src/js/Route.js +0 -123
- package/src/js/Router.js +0 -275
- package/src/js/index.js +0 -7
- package/src/js/react.js +0 -9
- package/src/js/vue.js +0 -17
package/src/js/Route.js
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { parse } from 'qs';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A Laravel route. This class represents one route and its configuration and metadata.
|
|
5
|
-
*/
|
|
6
|
-
export default class Route {
|
|
7
|
-
/**
|
|
8
|
-
* @param {String} name - Route name.
|
|
9
|
-
* @param {Object} definition - Route definition.
|
|
10
|
-
* @param {Object} config - Ziggy configuration.
|
|
11
|
-
*/
|
|
12
|
-
constructor(name, definition, config) {
|
|
13
|
-
this.name = name;
|
|
14
|
-
this.definition = definition;
|
|
15
|
-
this.bindings = definition.bindings ?? {};
|
|
16
|
-
this.wheres = definition.wheres ?? {};
|
|
17
|
-
this.config = config;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Get a 'template' of the complete URL for this route.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* https://{team}.ziggy.dev/user/{user}
|
|
25
|
-
*
|
|
26
|
-
* @return {String} Route template.
|
|
27
|
-
*/
|
|
28
|
-
get template() {
|
|
29
|
-
const template = `${this.origin}/${this.definition.uri}`.replace(/\/+$/, '');
|
|
30
|
-
|
|
31
|
-
return template === '' ? '/' : template;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Get a template of the origin for this route.
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* https://{team}.ziggy.dev/
|
|
39
|
-
*
|
|
40
|
-
* @return {String} Route origin template.
|
|
41
|
-
*/
|
|
42
|
-
get origin() {
|
|
43
|
-
// If we're building just a path there's no origin, otherwise: if this route has a
|
|
44
|
-
// domain configured we construct the origin with that, if not we use the app URL
|
|
45
|
-
return !this.config.absolute ? '' : this.definition.domain
|
|
46
|
-
? `${this.config.url.match(/^\w+:\/\//)[0]}${this.definition.domain}${this.config.port ? `:${this.config.port}` : ''}`
|
|
47
|
-
: this.config.url;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Get an array of objects representing the parameters that this route accepts.
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* [{ name: 'team', required: true }, { name: 'user', required: false }]
|
|
55
|
-
*
|
|
56
|
-
* @return {Array} Parameter segments.
|
|
57
|
-
*/
|
|
58
|
-
get parameterSegments() {
|
|
59
|
-
return this.template.match(/{[^}?]+\??}/g)?.map((segment) => ({
|
|
60
|
-
name: segment.replace(/{|\??}/g, ''),
|
|
61
|
-
required: !/\?}$/.test(segment),
|
|
62
|
-
})) ?? [];
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Get whether this route's template matches the given URL.
|
|
67
|
-
*
|
|
68
|
-
* @param {String} url - URL to check.
|
|
69
|
-
* @return {Object|false} - If this route matches, returns the matched parameters.
|
|
70
|
-
*/
|
|
71
|
-
matchesUrl(url) {
|
|
72
|
-
if (!this.definition.methods.includes('GET')) return false;
|
|
73
|
-
|
|
74
|
-
// Transform the route's template into a regex that will match a hydrated URL,
|
|
75
|
-
// by replacing its parameter segments with matchers for parameter values
|
|
76
|
-
const pattern = this.template
|
|
77
|
-
.replace(/(\/?){([^}?]*)(\??)}/g, (_, slash, segment, optional) => {
|
|
78
|
-
const regex = `(?<${segment}>${this.wheres[segment]?.replace(/(^\^)|(\$$)/g, '') || '[^/?]+'})`;
|
|
79
|
-
return optional ? `(${slash}${regex})?` : `${slash}${regex}`;
|
|
80
|
-
})
|
|
81
|
-
.replace(/^\w+:\/\//, '');
|
|
82
|
-
|
|
83
|
-
const [location, query] = url.replace(/^\w+:\/\//, '').split('?');
|
|
84
|
-
|
|
85
|
-
const matches = new RegExp(`^${pattern}/?$`).exec(location);
|
|
86
|
-
|
|
87
|
-
if (matches) {
|
|
88
|
-
for (const k in matches.groups) {
|
|
89
|
-
matches.groups[k] = typeof matches.groups[k] === 'string' ? decodeURIComponent(matches.groups[k]) : matches.groups[k];
|
|
90
|
-
}
|
|
91
|
-
return { params: matches.groups, query: parse(query) };
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return false;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Hydrate and return a complete URL for this route with the given parameters.
|
|
99
|
-
*
|
|
100
|
-
* @param {Object} params
|
|
101
|
-
* @return {String}
|
|
102
|
-
*/
|
|
103
|
-
compile(params) {
|
|
104
|
-
const segments = this.parameterSegments;
|
|
105
|
-
|
|
106
|
-
if (!segments.length) return this.template;
|
|
107
|
-
|
|
108
|
-
return this.template.replace(/{([^}?]+)(\??)}/g, (_, segment, optional) => {
|
|
109
|
-
// If the parameter is missing but is not optional, throw an error
|
|
110
|
-
if (!optional && [null, undefined].includes(params[segment])) {
|
|
111
|
-
throw new Error(`Ziggy error: '${segment}' parameter is required for route '${this.name}'.`)
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (this.wheres[segment]) {
|
|
115
|
-
if (!new RegExp(`^${optional ? `(${this.wheres[segment]})?` : this.wheres[segment]}$`).test(params[segment] ?? '')) {
|
|
116
|
-
throw new Error(`Ziggy error: '${segment}' parameter does not match required format '${this.wheres[segment]}' for route '${this.name}'.`)
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return encodeURI(params[segment] ?? '').replace(/%7C/g, '|').replace(/%25/g, '%').replace(/\$/g, '%24');
|
|
121
|
-
}).replace(`${this.origin}//`, `${this.origin}/`).replace(/\/+$/, '');
|
|
122
|
-
}
|
|
123
|
-
}
|
package/src/js/Router.js
DELETED
|
@@ -1,275 +0,0 @@
|
|
|
1
|
-
import { stringify } from 'qs';
|
|
2
|
-
import Route from './Route';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* A collection of Laravel routes. This class constitutes Ziggy's main API.
|
|
6
|
-
*/
|
|
7
|
-
export default class Router extends String {
|
|
8
|
-
/**
|
|
9
|
-
* @param {String} [name] - Route name.
|
|
10
|
-
* @param {(String|Number|Array|Object)} [params] - Route parameters.
|
|
11
|
-
* @param {Boolean} [absolute] - Whether to include the URL origin.
|
|
12
|
-
* @param {Object} [config] - Ziggy configuration.
|
|
13
|
-
*/
|
|
14
|
-
constructor(name, params, absolute = true, config) {
|
|
15
|
-
super();
|
|
16
|
-
|
|
17
|
-
this._config = config ?? (typeof Ziggy !== 'undefined' ? Ziggy : globalThis?.Ziggy);
|
|
18
|
-
this._config = { ...this._config, absolute };
|
|
19
|
-
|
|
20
|
-
if (name) {
|
|
21
|
-
if (!this._config.routes[name]) {
|
|
22
|
-
throw new Error(`Ziggy error: route '${name}' is not in the route list.`);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
this._route = new Route(name, this._config.routes[name], this._config);
|
|
26
|
-
this._params = this._parse(params);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Get the compiled URL string for the current route and parameters.
|
|
32
|
-
*
|
|
33
|
-
* @example
|
|
34
|
-
* // with 'posts.show' route 'posts/{post}'
|
|
35
|
-
* (new Router('posts.show', 1)).toString(); // 'https://ziggy.dev/posts/1'
|
|
36
|
-
*
|
|
37
|
-
* @return {String}
|
|
38
|
-
*/
|
|
39
|
-
toString() {
|
|
40
|
-
// Get parameters that don't correspond to any route segments to append them to the query
|
|
41
|
-
const unhandled = Object.keys(this._params)
|
|
42
|
-
.filter((key) => !this._route.parameterSegments.some(({ name }) => name === key))
|
|
43
|
-
.filter((key) => key !== '_query')
|
|
44
|
-
.reduce((result, current) => ({ ...result, [current]: this._params[current] }), {});
|
|
45
|
-
|
|
46
|
-
return this._route.compile(this._params) + stringify({ ...unhandled, ...this._params['_query'] }, {
|
|
47
|
-
addQueryPrefix: true,
|
|
48
|
-
arrayFormat: 'indices',
|
|
49
|
-
encodeValuesOnly: true,
|
|
50
|
-
skipNulls: true,
|
|
51
|
-
encoder: (value, encoder) => typeof value === 'boolean' ? Number(value) : encoder(value),
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Get the parameters, values, and metadata from the given URL.
|
|
57
|
-
*
|
|
58
|
-
* @param {String} [url] - The URL to inspect, defaults to the current window URL.
|
|
59
|
-
* @return {{ name: string, params: Object, query: Object, route: Route }}
|
|
60
|
-
*/
|
|
61
|
-
_unresolve(url) {
|
|
62
|
-
if (!url) {
|
|
63
|
-
url = this._currentUrl();
|
|
64
|
-
} else if (this._config.absolute && url.startsWith('/')) {
|
|
65
|
-
// If we are using absolute URLs and a relative URL
|
|
66
|
-
// is passed, prefix the host to make it absolute
|
|
67
|
-
url = this._location().host + url;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
let matchedParams = {};
|
|
71
|
-
const [name, route] = Object.entries(this._config.routes).find(
|
|
72
|
-
([name, route]) => (matchedParams = new Route(name, route, this._config).matchesUrl(url))
|
|
73
|
-
) || [undefined, undefined];
|
|
74
|
-
|
|
75
|
-
return { name, ...matchedParams, route };
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
_currentUrl() {
|
|
79
|
-
const { host, pathname, search } = this._location();
|
|
80
|
-
|
|
81
|
-
return (
|
|
82
|
-
this._config.absolute
|
|
83
|
-
? host + pathname
|
|
84
|
-
: pathname.replace(this._config.url.replace(/^\w*:\/\/[^/]+/, ''), '').replace(/^\/+/, '/')
|
|
85
|
-
) + search;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Get the name of the route matching the current window URL, or, given a route name
|
|
90
|
-
* and parameters, check if the current window URL and parameters match that route.
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* // at URL https://ziggy.dev/posts/4 with 'posts.show' route 'posts/{post}'
|
|
94
|
-
* route().current(); // 'posts.show'
|
|
95
|
-
* route().current('posts.index'); // false
|
|
96
|
-
* route().current('posts.show'); // true
|
|
97
|
-
* route().current('posts.show', { post: 1 }); // false
|
|
98
|
-
* route().current('posts.show', { post: 4 }); // true
|
|
99
|
-
*
|
|
100
|
-
* @param {String} [name] - Route name to check.
|
|
101
|
-
* @param {(String|Number|Array|Object)} [params] - Route parameters.
|
|
102
|
-
* @return {(Boolean|String|undefined)}
|
|
103
|
-
*/
|
|
104
|
-
current(name, params) {
|
|
105
|
-
const { name: current, params: currentParams, query, route } = this._unresolve();
|
|
106
|
-
|
|
107
|
-
// If a name wasn't passed, return the name of the current route
|
|
108
|
-
if (!name) return current;
|
|
109
|
-
|
|
110
|
-
// Test the passed name against the current route, matching some
|
|
111
|
-
// basic wildcards, e.g. passing `events.*` matches `events.show`
|
|
112
|
-
const match = new RegExp(`^${name.replace(/\./g, '\\.').replace(/\*/g, '.*')}$`).test(current);
|
|
113
|
-
|
|
114
|
-
if ([null, undefined].includes(params) || !match) return match;
|
|
115
|
-
|
|
116
|
-
const routeObject = new Route(current, route, this._config);
|
|
117
|
-
|
|
118
|
-
params = this._parse(params, routeObject);
|
|
119
|
-
const routeParams = { ...currentParams, ...query };
|
|
120
|
-
|
|
121
|
-
// If the current window URL has no route parameters, and the passed parameters are empty, return true
|
|
122
|
-
if (Object.values(params).every(p => !p) && !Object.values(routeParams).some(v => v !== undefined)) return true;
|
|
123
|
-
|
|
124
|
-
// Check that all passed parameters match their values in the current window URL
|
|
125
|
-
// Use weak equality because all values in the current window URL will be strings
|
|
126
|
-
return Object.entries(params).every(([key, value]) => routeParams[key] == value);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Get an object representing the current location (by default this will be
|
|
131
|
-
* the JavaScript `window` global if it's available).
|
|
132
|
-
*
|
|
133
|
-
* @return {Object}
|
|
134
|
-
*/
|
|
135
|
-
_location() {
|
|
136
|
-
const { host = '', pathname = '', search = '' } = typeof window !== 'undefined' ? window.location : {};
|
|
137
|
-
|
|
138
|
-
return {
|
|
139
|
-
host: this._config.location?.host ?? host,
|
|
140
|
-
pathname: this._config.location?.pathname ?? pathname,
|
|
141
|
-
search: this._config.location?.search ?? search,
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Get all parameter values from the current window URL.
|
|
147
|
-
*
|
|
148
|
-
* @example
|
|
149
|
-
* // at URL https://tighten.ziggy.dev/posts/4?lang=en with 'posts.show' route 'posts/{post}' and domain '{team}.ziggy.dev'
|
|
150
|
-
* route().params; // { team: 'tighten', post: 4, lang: 'en' }
|
|
151
|
-
*
|
|
152
|
-
* @return {Object}
|
|
153
|
-
*/
|
|
154
|
-
get params() {
|
|
155
|
-
const { params, query } = this._unresolve();
|
|
156
|
-
|
|
157
|
-
return { ...params, ...query };
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Check whether the given route exists.
|
|
162
|
-
*
|
|
163
|
-
* @param {String} name
|
|
164
|
-
* @return {Boolean}
|
|
165
|
-
*/
|
|
166
|
-
has(name) {
|
|
167
|
-
return Object.keys(this._config.routes).includes(name);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Parse Laravel-style route parameters of any type into a normalized object.
|
|
172
|
-
*
|
|
173
|
-
* @example
|
|
174
|
-
* // with route parameter names 'event' and 'venue'
|
|
175
|
-
* _parse(1); // { event: 1 }
|
|
176
|
-
* _parse({ event: 2, venue: 3 }); // { event: 2, venue: 3 }
|
|
177
|
-
* _parse(['Taylor', 'Matt']); // { event: 'Taylor', venue: 'Matt' }
|
|
178
|
-
* _parse([4, { uuid: 56789, name: 'Grand Canyon' }]); // { event: 4, venue: 56789 }
|
|
179
|
-
*
|
|
180
|
-
* @param {(String|Number|Array|Object)} params - Route parameters.
|
|
181
|
-
* @param {Route} route - Route instance.
|
|
182
|
-
* @return {Object} Normalized complete route parameters.
|
|
183
|
-
*/
|
|
184
|
-
_parse(params = {}, route = this._route) {
|
|
185
|
-
params ??= {}
|
|
186
|
-
|
|
187
|
-
// If `params` is a string or integer, wrap it in an array
|
|
188
|
-
params = ['string', 'number'].includes(typeof params) ? [params] : params;
|
|
189
|
-
|
|
190
|
-
// Separate segments with and without defaults, and fill in the default values
|
|
191
|
-
const segments = route.parameterSegments.filter(({ name }) => !this._config.defaults[name]);
|
|
192
|
-
|
|
193
|
-
if (Array.isArray(params)) {
|
|
194
|
-
// If the parameters are an array they have to be in order, so we can transform them into
|
|
195
|
-
// an object by keying them with the template segment names in the order they appear
|
|
196
|
-
params = params.reduce((result, current, i) => segments[i]
|
|
197
|
-
? ({ ...result, [segments[i].name]: current })
|
|
198
|
-
: typeof current === 'object'
|
|
199
|
-
? ({ ...result, ...current })
|
|
200
|
-
: ({ ...result, [current]: '' }), {});
|
|
201
|
-
} else if (
|
|
202
|
-
segments.length === 1
|
|
203
|
-
&& !params[segments[0].name]
|
|
204
|
-
&& (params.hasOwnProperty(Object.values(route.bindings)[0]) || params.hasOwnProperty('id'))
|
|
205
|
-
) {
|
|
206
|
-
// If there is only one template segment and `params` is an object, that object is
|
|
207
|
-
// ambiguous—it could contain the parameter key and value, or it could be an object
|
|
208
|
-
// representing just the value (e.g. a model); we can inspect it to find out, and
|
|
209
|
-
// if it's just the parameter value, we can wrap it in an object with its key
|
|
210
|
-
params = { [segments[0].name]: params };
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
return {
|
|
214
|
-
...this._defaults(route),
|
|
215
|
-
...this._substituteBindings(params, route),
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Populate default parameters for the given route.
|
|
221
|
-
*
|
|
222
|
-
* @example
|
|
223
|
-
* // with default parameters { locale: 'en', country: 'US' } and 'posts.show' route '{locale}/posts/{post}'
|
|
224
|
-
* defaults(...); // { locale: 'en' }
|
|
225
|
-
*
|
|
226
|
-
* @param {Route} route
|
|
227
|
-
* @return {Object} Default route parameters.
|
|
228
|
-
*/
|
|
229
|
-
_defaults(route) {
|
|
230
|
-
return route.parameterSegments.filter(({ name }) => this._config.defaults[name])
|
|
231
|
-
.reduce((result, { name }, i) => ({ ...result, [name]: this._config.defaults[name] }), {});
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Substitute Laravel route model bindings in the given parameters.
|
|
236
|
-
*
|
|
237
|
-
* @example
|
|
238
|
-
* _substituteBindings({ post: { id: 4, slug: 'hello-world', title: 'Hello, world!' } }, { bindings: { post: 'slug' } }); // { post: 'hello-world' }
|
|
239
|
-
*
|
|
240
|
-
* @param {Object} params - Route parameters.
|
|
241
|
-
* @param {Object} route - Route definition.
|
|
242
|
-
* @return {Object} Normalized route parameters.
|
|
243
|
-
*/
|
|
244
|
-
_substituteBindings(params, { bindings, parameterSegments }) {
|
|
245
|
-
return Object.entries(params).reduce((result, [key, value]) => {
|
|
246
|
-
// If the value isn't an object, or if the key isn't a named route parameter,
|
|
247
|
-
// there's nothing to substitute so we return it as-is
|
|
248
|
-
if (!value || typeof value !== 'object' || Array.isArray(value) || !parameterSegments.some(({ name }) => name === key)) {
|
|
249
|
-
return { ...result, [key]: value };
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
if (!value.hasOwnProperty(bindings[key])) {
|
|
253
|
-
if (value.hasOwnProperty('id')) {
|
|
254
|
-
// As a fallback, we still accept an 'id' key not explicitly registered as a binding
|
|
255
|
-
bindings[key] = 'id';
|
|
256
|
-
} else {
|
|
257
|
-
throw new Error(`Ziggy error: object passed as '${key}' parameter is missing route model binding key '${bindings[key]}'.`)
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
return { ...result, [key]: value[bindings[key]] };
|
|
262
|
-
}, {});
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
valueOf() {
|
|
266
|
-
return this.toString();
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* @deprecated since v1.0, use `has()` instead.
|
|
271
|
-
*/
|
|
272
|
-
check(name) {
|
|
273
|
-
return this.has(name);
|
|
274
|
-
}
|
|
275
|
-
}
|
package/src/js/index.js
DELETED
package/src/js/react.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import route from './index.js';
|
|
2
|
-
|
|
3
|
-
export function useRoute(defaultConfig) {
|
|
4
|
-
if (!defaultConfig && !globalThis.Ziggy && typeof Ziggy === 'undefined') {
|
|
5
|
-
throw new Error('Ziggy error: missing configuration. Ensure that a `Ziggy` variable is defined globally or pass a config object into `useRoute()`.');
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
return (name, params, absolute, config = defaultConfig) => route(name, params, absolute, config);
|
|
9
|
-
};
|
package/src/js/vue.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import route from './index.js';
|
|
2
|
-
|
|
3
|
-
export const ZiggyVue = {
|
|
4
|
-
install(app, options) {
|
|
5
|
-
const r = (name, params, absolute, config = options) => route(name, params, absolute, config);
|
|
6
|
-
|
|
7
|
-
app.mixin({
|
|
8
|
-
methods: {
|
|
9
|
-
route: r,
|
|
10
|
-
},
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
if (parseInt(app.version) > 2) {
|
|
14
|
-
app.provide('route', r);
|
|
15
|
-
}
|
|
16
|
-
},
|
|
17
|
-
};
|