@went.tf/laravel-rust-ziggy 0.0.0-stage → 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 WentTheFox
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,44 @@
1
+ /** What the server shares: `Routes` of laravel-rust-ziggy, serialized. */
2
+ export interface RoutesSource {
3
+ /** The application's URL, put in front of the path by `route(name, params, true)`. */
4
+ url?: string;
5
+ routes: Record<string, {
6
+ uris: string[];
7
+ methods: string[];
8
+ }>;
9
+ }
10
+ /**
11
+ * Opt in to checked names and parameters by including the declaration the server generates (`Routes::declaration()`):
12
+ *
13
+ * declare module '@went.tf/laravel-rust-ziggy' {
14
+ * interface Register {
15
+ * routes: { 'users.show': { user: string | number } };
16
+ * }
17
+ * }
18
+ *
19
+ * `route()` then only accepts those names, with the parameters of their URIs.
20
+ */
21
+ export interface Register {
22
+ }
23
+ type Registered = Register extends {
24
+ routes: infer R;
25
+ } ? R : never;
26
+ /** Any string until the app registers its routes, then only their names. */
27
+ export type RouteName = [Registered] extends [never] ? string : keyof Registered & string;
28
+ export type ParamValue = string | number | boolean | null | undefined;
29
+ /** Parameters that are not placeholders of the URI become the query string, so any extra key is allowed. */
30
+ export type Query = Record<string, ParamValue | ParamValue[]>;
31
+ /** The parameters of a route, loosely typed until the app registers its routes. */
32
+ export type RouteParams<N extends string> = [Registered] extends [never] ? Query : N extends keyof Registered ? Registered[N] & Query : never;
33
+ export interface RouteFunction {
34
+ <N extends RouteName>(name: N, ...rest: {} extends RouteParams<N> ? [params?: RouteParams<N>, absolute?: boolean] : [params: RouteParams<N>, absolute?: boolean]): string;
35
+ }
36
+ /**
37
+ * Builds `route(name, params?, absolute?)` around a source of the shared routes. `useSource` runs when `route` is called, so it
38
+ * follows whatever the page currently shares.
39
+ *
40
+ * Parameters that fill a placeholder go in the path (`{user}`, `{year?}`, `{post:slug}`, `{*path}`); the rest become the query
41
+ * string. A name that several URIs share (`/legal` and `/{language}/legal`) uses the one the parameters satisfy.
42
+ */
43
+ export declare function createRoute(useSource: () => RoutesSource): RouteFunction;
44
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,77 @@
1
+ const placeholders = /\{(\*?)([^}?:]+)(?::[^}?]+)?(\?)?\}/g;
2
+ function parse(uri) {
3
+ return [...uri.matchAll(placeholders)].map((match) => ({
4
+ name: match[2],
5
+ optional: match[3] === '?',
6
+ wildcard: match[1] === '*',
7
+ }));
8
+ }
9
+ const present = (value) => value !== undefined && value !== null && value !== '';
10
+ /** A URI whose required placeholders all have a value, preferring the one that uses the most of the given parameters. */
11
+ function pick(name, uris, params) {
12
+ let best;
13
+ for (const uri of uris) {
14
+ const wanted = parse(uri);
15
+ if (wanted.some((placeholder) => !placeholder.optional && !present(params[placeholder.name]))) {
16
+ continue;
17
+ }
18
+ const used = wanted.filter((placeholder) => present(params[placeholder.name])).length;
19
+ if (!best || used > best.used) {
20
+ best = { uri, used };
21
+ }
22
+ }
23
+ if (!best) {
24
+ const missing = uris.map((uri) => `${uri} needs ${parse(uri).filter((p) => !p.optional).map((p) => p.name).join(', ')}`);
25
+ throw new Error(`route '${name}' is missing parameters: ${missing.join('; ')}`);
26
+ }
27
+ return best.uri;
28
+ }
29
+ function encode(value, wildcard) {
30
+ const text = String(value);
31
+ return wildcard ? text.split('/').map(encodeURIComponent).join('/') : encodeURIComponent(text);
32
+ }
33
+ function queryString(query) {
34
+ const search = new URLSearchParams();
35
+ for (const [key, value] of Object.entries(query)) {
36
+ for (const item of Array.isArray(value) ? value : [value]) {
37
+ if (present(item)) {
38
+ search.append(Array.isArray(value) ? `${key}[]` : key, String(item));
39
+ }
40
+ }
41
+ }
42
+ const text = search.toString();
43
+ return text ? `?${text}` : '';
44
+ }
45
+ /**
46
+ * Builds `route(name, params?, absolute?)` around a source of the shared routes. `useSource` runs when `route` is called, so it
47
+ * follows whatever the page currently shares.
48
+ *
49
+ * Parameters that fill a placeholder go in the path (`{user}`, `{year?}`, `{post:slug}`, `{*path}`); the rest become the query
50
+ * string. A name that several URIs share (`/legal` and `/{language}/legal`) uses the one the parameters satisfy.
51
+ */
52
+ export function createRoute(useSource) {
53
+ return ((name, params = {}, absolute = false) => {
54
+ const source = useSource();
55
+ const entry = source.routes[name];
56
+ if (!entry) {
57
+ throw new Error(`route '${name}' does not exist`);
58
+ }
59
+ const uri = pick(name, entry.uris, params);
60
+ const used = new Set();
61
+ let path = uri.replace(placeholders, (_whole, star, key, _optional, _offset) => {
62
+ const value = params[key];
63
+ used.add(key);
64
+ return present(value) ? encode(value, star === '*') : '';
65
+ });
66
+ // An unfilled optional placeholder leaves its slash behind
67
+ path = path.replace(/\/{2,}/g, '/').replace(/(.)\/$/, '$1');
68
+ const query = {};
69
+ for (const [key, value] of Object.entries(params)) {
70
+ if (!used.has(key)) {
71
+ query[key] = value;
72
+ }
73
+ }
74
+ const base = absolute ? (source.url ?? '') : '';
75
+ return `${base}${path}${queryString(query)}`;
76
+ });
77
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * `route()` bound to the Inertia page prop `ziggy`, which the server shares from `Routes`:
3
+ *
4
+ * props! { ziggy => Routes::from_table(app.routes()).with_url(app_url) }
5
+ */
6
+ export declare const route: import("./index.js").RouteFunction;
@@ -0,0 +1,8 @@
1
+ import { usePage } from '@inertiajs/vue3';
2
+ import { createRoute } from './index.js';
3
+ /**
4
+ * `route()` bound to the Inertia page prop `ziggy`, which the server shares from `Routes`:
5
+ *
6
+ * props! { ziggy => Routes::from_table(app.routes()).with_url(app_url) }
7
+ */
8
+ export const route = createRoute(() => usePage().props.ziggy);
package/package.json CHANGED
@@ -1,6 +1,63 @@
1
1
  {
2
- "name": "@went.tf/laravel-rust-ziggy",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@went.tf/laravel-rust-ziggy",
3
+ "version": "0.1.0",
4
+ "description": "Frontend half of laravel-rust-ziggy: a route() helper that builds URLs from the named routes a Laravel for Rust app shares, with typed names and parameters, for Vue and React through Inertia",
5
+ "license": "MIT",
6
+ "homepage": "https://github.com/WentTheFox/laravel-rust-ziggy#readme",
7
+ "bugs": "https://github.com/WentTheFox/laravel-rust-ziggy/issues",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/WentTheFox/laravel-rust-ziggy.git",
11
+ "directory": "js"
12
+ },
13
+ "keywords": [
14
+ "ziggy",
15
+ "routes",
16
+ "inertia",
17
+ "vue",
18
+ "laravel",
19
+ "typescript"
20
+ ],
21
+ "type": "module",
22
+ "sideEffects": false,
23
+ "engines": {
24
+ "node": ">=24"
25
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "default": "./dist/index.js"
33
+ },
34
+ "./inertia/vue": {
35
+ "types": "./dist/inertia-vue.d.ts",
36
+ "default": "./dist/inertia-vue.js"
37
+ }
38
+ },
39
+ "scripts": {
40
+ "build": "tsc -p tsconfig.build.json",
41
+ "types:check": "tsc --noEmit",
42
+ "test": "vitest run",
43
+ "types:smoke": "tsc -p types-smoke/tsconfig.json && tsc -p types-smoke/typed/tsconfig.json",
44
+ "test:coverage": "vitest run --coverage",
45
+ "prepublishOnly": "pnpm run types:check && pnpm run test:coverage && pnpm run build && pnpm run types:smoke"
46
+ },
47
+ "peerDependencies": {
48
+ "@inertiajs/vue3": ">=2"
49
+ },
50
+ "peerDependenciesMeta": {
51
+ "@inertiajs/vue3": {
52
+ "optional": true
53
+ }
54
+ },
55
+ "devDependencies": {
56
+ "@inertiajs/vue3": "^3.11.0",
57
+ "@types/node": "^22.0.0",
58
+ "@vitest/coverage-v8": "3.2.7",
59
+ "typescript": "^5.9.3",
60
+ "vitest": "^3.2.0",
61
+ "vue": "^3.5.45"
62
+ }
63
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.