ziggy-js 1.7.1 → 1.8.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/README.md +36 -4
- package/package.json +9 -8
- package/src/js/index.d.ts +163 -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/README.md
CHANGED
|
@@ -17,7 +17,7 @@ Ziggy supports all versions of Laravel from `5.4` onward, and all modern browser
|
|
|
17
17
|
- [The `route()` helper](#the-route-helper)
|
|
18
18
|
- [The `Router` class](#the-router-class)
|
|
19
19
|
- [Route-model binding](#route-model-binding)
|
|
20
|
-
- [TypeScript
|
|
20
|
+
- [TypeScript](#typescript)
|
|
21
21
|
- [**Advanced Setup**](#advanced-setup)
|
|
22
22
|
- [JavaScript frameworks](#javascript-frameworks)
|
|
23
23
|
- [Vue](#vue)
|
|
@@ -31,7 +31,11 @@ Ziggy supports all versions of Laravel from `5.4` onward, and all modern browser
|
|
|
31
31
|
|
|
32
32
|
## Installation
|
|
33
33
|
|
|
34
|
-
Install Ziggy
|
|
34
|
+
Install Ziggy in your Laravel app:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
composer require tightenco/ziggy
|
|
38
|
+
```
|
|
35
39
|
|
|
36
40
|
Add the `@routes` Blade directive to your main layout (_before_ your application's JavaScript), and the `route()` helper function will now be available globally!
|
|
37
41
|
|
|
@@ -281,9 +285,37 @@ route('authors.photos.show', [{ id: 1, name: 'Jacob' }, photo]);
|
|
|
281
285
|
// 'https://ziggy.test/authors/1/photos/714b19e8-ac5e-4dab-99ba-34dc6fdd24a5'
|
|
282
286
|
```
|
|
283
287
|
|
|
284
|
-
#### TypeScript
|
|
288
|
+
#### TypeScript
|
|
289
|
+
|
|
290
|
+
Ziggy includes TypeScript type definitions, and a helper command that can generate additional type definitions to enable route name and parameter autocompletion.
|
|
291
|
+
|
|
292
|
+
To generate the route types, run Ziggy's Artisan command with the `--types` or `--types-only` option:
|
|
293
|
+
|
|
294
|
+
```bash
|
|
295
|
+
php artisan ziggy:generate --types
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
To make your IDE aware that Ziggy's `route()` helper is available globally, and to type it correctly, add a declaration like this in a `.d.ts` file somewhere in your project:
|
|
299
|
+
|
|
300
|
+
```ts
|
|
301
|
+
import routeFn from 'ziggy-js';
|
|
302
|
+
|
|
303
|
+
declare global {
|
|
304
|
+
var route: typeof routeFn;
|
|
305
|
+
}
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
If you don't have Ziggy's NPM package installed, add the following to your `jsconfig.json` or `tsconfig.json` to load Ziggy's types from the Composer vendor directory:
|
|
285
309
|
|
|
286
|
-
|
|
310
|
+
```json
|
|
311
|
+
{
|
|
312
|
+
"compilerOptions": {
|
|
313
|
+
"paths": {
|
|
314
|
+
"ziggy-js": ["./vendor/tightenco/ziggy"]
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
```
|
|
287
319
|
|
|
288
320
|
## Advanced Setup
|
|
289
321
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ziggy-js",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "Use your Laravel named routes in JavaScript.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"laravel",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
}
|
|
26
26
|
],
|
|
27
27
|
"files": [
|
|
28
|
-
"src/js",
|
|
28
|
+
"src/js/index.d.ts",
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"source": "src/js/index.js",
|
|
@@ -35,18 +35,19 @@
|
|
|
35
35
|
"browser": "dist/index.js",
|
|
36
36
|
"module": "dist/index.m.js",
|
|
37
37
|
"esmodule": "dist/index.es.js",
|
|
38
|
+
"types": "src/js/index.d.ts",
|
|
38
39
|
"repository": {
|
|
39
40
|
"type": "git",
|
|
40
41
|
"url": "https://github.com/tighten/ziggy.git"
|
|
41
42
|
},
|
|
42
43
|
"scripts": {
|
|
43
|
-
"build": "microbundle --name route --format modern,es,umd --external none --no-sourcemap",
|
|
44
|
-
"build:vue": "microbundle --entry src/js/vue.js --output dist/vue.js --name ZiggyVue --format modern,es,umd --external none --no-sourcemap",
|
|
45
|
-
"build:react": "microbundle --entry src/js/react.js --output dist/react.js --name ZiggyReact --format modern,es,umd --external none --no-sourcemap",
|
|
44
|
+
"build": "microbundle --name route --format modern,es,umd --external none --no-sourcemap --no-generateTypes",
|
|
45
|
+
"build:vue": "microbundle --entry src/js/vue.js --output dist/vue.js --name ZiggyVue --format modern,es,umd --external none --no-sourcemap --no-generateTypes",
|
|
46
|
+
"build:react": "microbundle --entry src/js/react.js --output dist/react.js --name ZiggyReact --format modern,es,umd --external none --no-sourcemap --no-generateTypes",
|
|
46
47
|
"watch": "npm run build watch",
|
|
47
|
-
"build:npm": "microbundle --name route --format modern,es,umd --no-sourcemap",
|
|
48
|
-
"build:npm:vue": "microbundle --entry src/js/vue.js --output dist/vue.js --name ZiggyVue --format modern,es,umd --no-sourcemap",
|
|
49
|
-
"build:npm:react": "microbundle --entry src/js/react.js --output dist/react.js --name ZiggyReact --format modern,es,umd --no-sourcemap",
|
|
48
|
+
"build:npm": "microbundle --name route --format modern,es,umd --no-sourcemap --no-generateTypes",
|
|
49
|
+
"build:npm:vue": "microbundle --entry src/js/vue.js --output dist/vue.js --name ZiggyVue --format modern,es,umd --no-sourcemap --no-generateTypes",
|
|
50
|
+
"build:npm:react": "microbundle --entry src/js/react.js --output dist/react.js --name ZiggyReact --format modern,es,umd --no-sourcemap --no-generateTypes",
|
|
50
51
|
"test": "jest --verbose",
|
|
51
52
|
"prepublishOnly": "npm run build:npm && npm run build:npm:vue && npm run build:npm:react"
|
|
52
53
|
},
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A list of routes and their parameters and bindings.
|
|
3
|
+
*
|
|
4
|
+
* Extended and filled by the route list generated with `php artisan ziggy:generate --types`.
|
|
5
|
+
*/
|
|
6
|
+
export interface RouteList {}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A route name registered with Ziggy.
|
|
10
|
+
*/
|
|
11
|
+
type KnownRouteName = keyof RouteList;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A route name, or any string.
|
|
15
|
+
*/
|
|
16
|
+
type RouteName = KnownRouteName | (string & {});
|
|
17
|
+
// `(string & {})` prevents TypeScript from reducing this type to just `string`,
|
|
18
|
+
// which would prevent intellisense from autocompleting known route names.
|
|
19
|
+
// See https://stackoverflow.com/a/61048124/6484459.
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Information about a single route parameter.
|
|
23
|
+
*/
|
|
24
|
+
type ParameterInfo = { name: string, binding?: string };
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A primitive route parameter value, as it would appear in a URL.
|
|
28
|
+
*/
|
|
29
|
+
type RawParameterValue = string | number;
|
|
30
|
+
// TODO: Technically booleans work too, does it make sense to add them? Here? What would that look like?
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* An object parameter value containing the 'default' binding key `id`, e.g. representing an Eloquent model.
|
|
34
|
+
*/
|
|
35
|
+
type DefaultRoutable = { id: RawParameterValue } & Record<keyof any, unknown>;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A route parameter value.
|
|
39
|
+
*/
|
|
40
|
+
type ParameterValue = RawParameterValue | DefaultRoutable;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A parseable route parameter, either plain or nested inside an object under its binding key.
|
|
44
|
+
*/
|
|
45
|
+
type Routable<I extends ParameterInfo> = I extends { binding: string }
|
|
46
|
+
? { [K in I['binding']]: RawParameterValue } | RawParameterValue
|
|
47
|
+
: ParameterValue;
|
|
48
|
+
|
|
49
|
+
// Uncomment to test:
|
|
50
|
+
// type A = Routable<{ name: 'foo', binding: 'bar' }>;
|
|
51
|
+
// = RawParameterValue | { bar: RawParameterValue }
|
|
52
|
+
// type B = Routable<{ name: 'foo' }>;
|
|
53
|
+
// = RawParameterValue | DefaultRoutable
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* An object containing a special '_query' key to target the query string of a URL.
|
|
57
|
+
*/
|
|
58
|
+
type HasQueryParam = { _query?: Record<string, unknown> };
|
|
59
|
+
/**
|
|
60
|
+
* An object of parameters for an unspecified route.
|
|
61
|
+
*/
|
|
62
|
+
type GenericRouteParamsObject = Record<keyof any, unknown> & HasQueryParam;
|
|
63
|
+
// `keyof any` essentially makes it function as a plain `Record`
|
|
64
|
+
/**
|
|
65
|
+
* An object of parameters for a specific named route.
|
|
66
|
+
*/
|
|
67
|
+
// TODO: The keys here could be non-optional (or more detailed) if we can determine which params are required/not.
|
|
68
|
+
type KnownRouteParamsObject<I extends readonly ParameterInfo[]> = { [T in I[number] as T['name']]?: Routable<T> } & GenericRouteParamsObject;
|
|
69
|
+
// `readonly` allows TypeScript to determine the actual values of all the
|
|
70
|
+
// parameter names inside the array, instead of just seeing `string`.
|
|
71
|
+
// See https://github.com/tighten/ziggy/pull/664#discussion_r1329978447.
|
|
72
|
+
/**
|
|
73
|
+
* An object of route parameters.
|
|
74
|
+
*/
|
|
75
|
+
type RouteParamsObject<N extends RouteName> = N extends KnownRouteName ? KnownRouteParamsObject<RouteList[N]> : GenericRouteParamsObject;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* An array of parameters for an unspecified route.
|
|
79
|
+
*/
|
|
80
|
+
// TODO: this may be able to be more specific, like `Routable<ParameterInfo>[]`,
|
|
81
|
+
// depending how we want to handle nested objects inside parameter arrays
|
|
82
|
+
type GenericRouteParamsArray = unknown[];
|
|
83
|
+
/**
|
|
84
|
+
* An array of parameters for a specific named route.
|
|
85
|
+
*/
|
|
86
|
+
type KnownRouteParamsArray<I extends readonly ParameterInfo[]> = [...{ [K in keyof I]: Routable<I[K]> }, ...unknown[]];
|
|
87
|
+
// Because `K in keyof I` for a `readonly` array is always a number, even though this
|
|
88
|
+
// looks like `{ 0: T, 1: U, 2: V }` TypeScript generates `[T, U, V]`. The nested
|
|
89
|
+
// array destructing lets us type the first n items in the array, which are known
|
|
90
|
+
// route parameters, and then allow arbitrary additional items.
|
|
91
|
+
// See https://github.com/tighten/ziggy/pull/664#discussion_r1330002370.
|
|
92
|
+
|
|
93
|
+
// Uncomment to test:
|
|
94
|
+
// type B = KnownRouteParamsArray<[{ name: 'post', binding: 'uuid' }]>;
|
|
95
|
+
// = [RawParameterValue | { uuid: RawParameterValue }, ...unknown[]]
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* An array of route parameters.
|
|
99
|
+
*/
|
|
100
|
+
type RouteParamsArray<N extends RouteName> = N extends KnownRouteName ? KnownRouteParamsArray<RouteList[N]> : GenericRouteParamsArray;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* All possible parameter argument shapes for a route.
|
|
104
|
+
*/
|
|
105
|
+
type RouteParams<N extends RouteName> = ParameterValue | RouteParamsObject<N> | RouteParamsArray<N>;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* A route.
|
|
109
|
+
*/
|
|
110
|
+
interface Route {
|
|
111
|
+
uri: string,
|
|
112
|
+
methods: ('GET' | 'HEAD' | 'POST' | 'PATCH' | 'PUT' | 'OPTIONS' | 'DELETE')[],
|
|
113
|
+
domain?: string,
|
|
114
|
+
parameters?: string[],
|
|
115
|
+
bindings?: Record<string, string>,
|
|
116
|
+
wheres?: Record<string, unknown>,
|
|
117
|
+
middleware?: string[],
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Ziggy's config object.
|
|
122
|
+
*/
|
|
123
|
+
interface Config {
|
|
124
|
+
url: string,
|
|
125
|
+
port: number | null,
|
|
126
|
+
defaults: Record<string, RawParameterValue>,
|
|
127
|
+
routes: Record<string, Route>,
|
|
128
|
+
location?: {
|
|
129
|
+
host?: string,
|
|
130
|
+
pathname?: string,
|
|
131
|
+
search?: string,
|
|
132
|
+
},
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Ziggy's Router class.
|
|
137
|
+
*/
|
|
138
|
+
interface Router {
|
|
139
|
+
current(): RouteName | undefined,
|
|
140
|
+
current<T extends RouteName>(name: T, params?: RouteParams<T>): boolean,
|
|
141
|
+
get params(): Record<string, unknown>,
|
|
142
|
+
has<T extends RouteName>(name: T): boolean,
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Ziggy's route helper.
|
|
147
|
+
*/
|
|
148
|
+
// Called with no arguments - returns a Router instance
|
|
149
|
+
export default function route(): Router;
|
|
150
|
+
// Called with a route name and optional additional arguments - returns a URL string
|
|
151
|
+
export default function route<T extends RouteName>(
|
|
152
|
+
name: T,
|
|
153
|
+
params?: RouteParams<T> | undefined,
|
|
154
|
+
absolute?: boolean,
|
|
155
|
+
config?: Config,
|
|
156
|
+
): string;
|
|
157
|
+
// Called with configuration arguments only - returns a configured Router instance
|
|
158
|
+
export default function route(
|
|
159
|
+
name: undefined,
|
|
160
|
+
params: undefined,
|
|
161
|
+
absolute?: boolean,
|
|
162
|
+
config?: Config,
|
|
163
|
+
): Router;
|
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
|
-
};
|