vue-router-query-sync 2.0.0-alpha.2 → 2.0.0-alpha.3

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 CHANGED
@@ -60,6 +60,27 @@ const tab = toRef(filters, 'tab') // WritableComputedRef<'all' | 'favorite' | nu
60
60
  tab.value = 'favorite'
61
61
  ```
62
62
 
63
+ ### `prefix` — namespacing keys
64
+
65
+ When the same schema is used more than once on a page (e.g. one paginated component per
66
+ tab), pass a `prefix` so each instance owns its own URL keys and they don't collide. Field
67
+ names in `filters` stay unprefixed; only the URL key becomes `${prefix}.${key}`.
68
+
69
+ ```ts
70
+ const users = useQueryFilters({ page: param.number({ default: 1 }) }, { prefix: 'users' })
71
+ const orders = useQueryFilters({ page: param.number({ default: 1 }) }, { prefix: 'orders' })
72
+
73
+ users.filters.page = 3 // URL: ?users.page=3
74
+ orders.filters.page = 5 // URL: ?users.page=3&orders.page=5
75
+
76
+ users.filters.page // reads users.page only — independent from orders
77
+ ```
78
+
79
+ Each instance keeps its own state in the URL, so switching between tabs and back restores
80
+ their pagination; a direct link reproduces everything. If you'd rather keep the URL minimal,
81
+ render inactive tabs with `v-if` and call `reset()` in `onUnmounted` to drop the leaving
82
+ tab's keys.
83
+
63
84
  ## `param`
64
85
 
65
86
  All options are shared: `{ default?: T; history?: 'push' | 'replace' }` (`history` defaults to
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { useQueryFilters } from './useQueryFilters';
2
2
  export { param } from './param';
3
3
  export { defineCodec } from './defineCodec';
4
- export type { QueryCodec, ParamOptions, Param, FilterSchema, InferFilters, UseQueryFiltersReturn } from './types';
4
+ export type { QueryCodec, ParamOptions, Param, FilterSchema, InferFilters, UseQueryFiltersOptions, UseQueryFiltersReturn } from './types';
5
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAC/B,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,YAAY,EACV,UAAU,EACV,YAAY,EACZ,KAAK,EACL,YAAY,EACZ,YAAY,EACZ,qBAAqB,EACtB,MAAM,SAAS,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAC/B,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,YAAY,EACV,UAAU,EACV,YAAY,EACZ,KAAK,EACL,YAAY,EACZ,YAAY,EACZ,sBAAsB,EACtB,qBAAqB,EACtB,MAAM,SAAS,CAAA"}
package/dist/index.mjs CHANGED
@@ -1,44 +1,44 @@
1
- import { reactive as A, computed as N } from "vue";
2
- import { useRoute as v, useRouter as S } from "vue-router";
3
- function g(e) {
1
+ import { reactive as m, computed as v } from "vue";
2
+ import { useRoute as S, useRouter as P } from "vue-router";
3
+ function A(e) {
4
4
  if (e != null)
5
5
  return Array.isArray(e) ? e.filter((r) => r !== null) : e;
6
6
  }
7
- function o(e, r) {
7
+ function b(e, r) {
8
8
  return e === r ? !0 : Array.isArray(e) && Array.isArray(r) ? e.length !== r.length ? !1 : e.every((t, u) => t === r[u]) : !1;
9
9
  }
10
- const h = /* @__PURE__ */ new WeakMap();
11
- function m(e) {
12
- let r = h.get(e);
10
+ const g = /* @__PURE__ */ new WeakMap();
11
+ function N(e) {
12
+ let r = g.get(e);
13
13
  return r || (r = {
14
- patch: A(/* @__PURE__ */ new Map()),
14
+ patch: m(/* @__PURE__ */ new Map()),
15
15
  usePush: !1,
16
16
  scheduled: !1
17
- }, h.set(e, r)), r;
17
+ }, g.set(e, r)), r;
18
18
  }
19
- function P(e, r) {
20
- const t = m(e);
19
+ function j(e, r) {
20
+ const t = N(e);
21
21
  return t.patch.has(r) ? { pending: !0, value: t.patch.get(r) ?? null } : { pending: !1, value: null };
22
22
  }
23
- function z(e, r, t, u) {
24
- const l = m(e);
25
- l.patch.set(r, t), u === "push" && (l.usePush = !0), l.scheduled || (l.scheduled = !0, queueMicrotask(() => q(e)));
23
+ function o(e, r, t, u) {
24
+ const l = N(e);
25
+ l.patch.set(r, t), u === "push" && (l.usePush = !0), l.scheduled || (l.scheduled = !0, queueMicrotask(() => O(e)));
26
26
  }
27
- function j(e, r) {
27
+ function q(e, r) {
28
28
  const t = Object.keys(e), u = Object.keys(r);
29
29
  if (t.length !== u.length)
30
30
  return !1;
31
31
  const l = (n, c) => {
32
32
  if (Array.isArray(n) || Array.isArray(c)) {
33
- const a = Array.isArray(n) ? n : [n], s = Array.isArray(c) ? c : [c];
34
- return a.length === s.length && a.every((i, f) => String(i) === String(s[f]));
33
+ const a = Array.isArray(n) ? n : [n], d = Array.isArray(c) ? c : [c];
34
+ return a.length === d.length && a.every((f, i) => String(f) === String(d[i]));
35
35
  }
36
36
  return String(n) === String(c);
37
37
  };
38
38
  return t.every((n) => n in r && l(e[n], r[n]));
39
39
  }
40
- function q(e) {
41
- const r = h.get(e);
40
+ function O(e) {
41
+ const r = g.get(e);
42
42
  if (!r)
43
43
  return;
44
44
  if (r.scheduled = !1, r.patch.size === 0) {
@@ -48,35 +48,35 @@ function q(e) {
48
48
  const t = r.usePush;
49
49
  r.usePush = !1;
50
50
  const u = e.currentRoute.value.query, l = {};
51
- for (const [i, f] of Object.entries(u))
52
- f !== null && (l[i] = Array.isArray(f) ? f.filter((b) => b !== null) : f);
51
+ for (const [f, i] of Object.entries(u))
52
+ i !== null && (l[f] = Array.isArray(i) ? i.filter((s) => s !== null) : i);
53
53
  const n = new Map(r.patch);
54
- n.forEach((i, f) => {
55
- i === null ? delete l[f] : l[f] = i;
54
+ n.forEach((f, i) => {
55
+ f === null ? delete l[i] : l[i] = f;
56
56
  });
57
57
  const c = {};
58
- for (const i of Object.keys(l).sort())
59
- c[i] = l[i];
58
+ for (const f of Object.keys(l).sort())
59
+ c[f] = l[f];
60
60
  const a = () => {
61
- n.forEach((i, f) => {
62
- r.patch.get(f) === i && r.patch.delete(f);
61
+ n.forEach((f, i) => {
62
+ r.patch.get(i) === f && r.patch.delete(i);
63
63
  });
64
64
  };
65
- if (j(u, c)) {
65
+ if (q(u, c)) {
66
66
  a();
67
67
  return;
68
68
  }
69
69
  (t ? e.push({ query: c }) : e.replace({ query: c })).then(a, a);
70
70
  }
71
- function O(e, r, t, u) {
72
- const l = P(r, t);
71
+ function w(e, r, t, u) {
72
+ const l = j(r, t);
73
73
  let n;
74
74
  if (l.pending) {
75
75
  if (l.value === null)
76
76
  return u.default;
77
- n = g(l.value);
77
+ n = A(l.value);
78
78
  } else
79
- n = g(e.query[t]);
79
+ n = A(e.query[t]);
80
80
  if (n === void 0)
81
81
  return u.default;
82
82
  try {
@@ -86,55 +86,55 @@ function O(e, r, t, u) {
86
86
  return u.default;
87
87
  }
88
88
  }
89
- function y(e, r, t, u) {
89
+ function z(e, r, t, u) {
90
90
  const l = t.codec.serialize(u), n = t.codec.serialize(t.default);
91
- l === null || o(l, n) ? z(e, r, null, t.history) : z(e, r, l, t.history);
91
+ l === null || b(l, n) ? o(e, r, null, t.history) : o(e, r, l, t.history);
92
92
  }
93
93
  function k(e, r, t, u) {
94
94
  let l = !1, n, c;
95
- return N({
95
+ return v({
96
96
  get: () => {
97
- const a = O(e, r, t, u), s = u.codec.serialize(a);
98
- return l && o(s, c) ? n : (l = !0, n = a, c = s, a);
97
+ const a = w(e, r, t, u), d = u.codec.serialize(a);
98
+ return l && b(d, c) ? n : (l = !0, n = a, c = d, a);
99
99
  },
100
- set: (a) => y(r, t, u, a)
100
+ set: (a) => z(r, t, u, a)
101
101
  });
102
102
  }
103
- function W(e) {
104
- const r = v(), t = S(), u = Object.keys(e), l = {};
105
- for (const s of u)
106
- l[s] = k(r, t, s, e[s]);
107
- return { filters: A(l), patch: (s) => {
108
- for (const i of Object.keys(s))
109
- i in e && y(t, i, e[i], s[i]);
103
+ function $(e, r = {}) {
104
+ const t = S(), u = P(), l = Object.keys(e), { prefix: n } = r, c = (s) => n ? `${n}.${s}` : s, a = {};
105
+ for (const s of l)
106
+ a[s] = k(t, u, c(s), e[s]);
107
+ return { filters: m(a), patch: (s) => {
108
+ for (const h of Object.keys(s))
109
+ h in e && z(u, c(h), e[h], s[h]);
110
110
  }, reset: () => {
111
- for (const s of u)
112
- y(t, s, e[s], e[s].default);
111
+ for (const s of l)
112
+ z(u, c(s), e[s], e[s].default);
113
113
  } };
114
114
  }
115
- function d(e) {
115
+ function y(e) {
116
116
  return Array.isArray(e) ? e[0] ?? "" : e;
117
117
  }
118
- function w(e) {
118
+ function F(e) {
119
119
  return { codec: {
120
120
  serialize: (t) => t === null || t === "" ? null : t,
121
- deserialize: (t) => d(t)
121
+ deserialize: (t) => y(t)
122
122
  }, default: (e == null ? void 0 : e.default) ?? null, history: (e == null ? void 0 : e.history) ?? "replace" };
123
123
  }
124
- function F(e) {
124
+ function M(e) {
125
125
  return { codec: {
126
126
  serialize: (t) => t === null ? null : String(t),
127
127
  deserialize: (t) => {
128
- const u = d(t), l = Number(u);
128
+ const u = y(t), l = Number(u);
129
129
  return u === "" || Number.isNaN(l) ? void 0 : l;
130
130
  }
131
131
  }, default: (e == null ? void 0 : e.default) ?? null, history: (e == null ? void 0 : e.history) ?? "replace" };
132
132
  }
133
- function M(e) {
133
+ function R(e) {
134
134
  return { codec: {
135
135
  serialize: (t) => t === null ? null : t ? "true" : "false",
136
136
  deserialize: (t) => {
137
- const u = d(t);
137
+ const u = y(t);
138
138
  if (u === "true")
139
139
  return !0;
140
140
  if (u === "false")
@@ -142,21 +142,21 @@ function M(e) {
142
142
  }
143
143
  }, default: (e == null ? void 0 : e.default) ?? null, history: (e == null ? void 0 : e.history) ?? "replace" };
144
144
  }
145
- function R(e) {
145
+ function E(e) {
146
146
  return { codec: {
147
147
  serialize: (t) => t.length === 0 ? null : [...t],
148
148
  deserialize: (t) => Array.isArray(t) ? [...t] : [t]
149
149
  }, default: (e == null ? void 0 : e.default) ?? [], history: (e == null ? void 0 : e.history) ?? "replace" };
150
150
  }
151
- function E(e) {
151
+ function K(e) {
152
152
  return Object.entries(e).filter(([r]) => Number.isNaN(Number(r))).map(([, r]) => r);
153
153
  }
154
154
  function C(e, r) {
155
- const t = Array.isArray(e) ? [...e] : E(e), u = new Set(t);
155
+ const t = Array.isArray(e) ? [...e] : K(e), u = new Set(t);
156
156
  return { codec: {
157
157
  serialize: (n) => n === null ? null : String(n),
158
158
  deserialize: (n) => {
159
- const c = d(n);
159
+ const c = y(n);
160
160
  if (u.has(c))
161
161
  return c;
162
162
  const a = Number(c);
@@ -165,7 +165,7 @@ function C(e, r) {
165
165
  }
166
166
  }, default: (r == null ? void 0 : r.default) ?? null, history: (r == null ? void 0 : r.history) ?? "replace" };
167
167
  }
168
- function K(e, r) {
168
+ function Q(e, r) {
169
169
  return {
170
170
  codec: {
171
171
  serialize: (u) => u === null ? null : e.serialize(u),
@@ -176,12 +176,12 @@ function K(e, r) {
176
176
  };
177
177
  }
178
178
  const B = {
179
- string: w,
180
- number: F,
181
- boolean: M,
182
- stringArray: R,
179
+ string: F,
180
+ number: M,
181
+ boolean: R,
182
+ stringArray: E,
183
183
  enum: C,
184
- custom: K
184
+ custom: Q
185
185
  };
186
186
  function D(e) {
187
187
  return e;
@@ -189,5 +189,5 @@ function D(e) {
189
189
  export {
190
190
  D as defineCodec,
191
191
  B as param,
192
- W as useQueryFilters
192
+ $ as useQueryFilters
193
193
  };
package/dist/types.d.ts CHANGED
@@ -15,6 +15,9 @@ export type FilterSchema = Record<string, Param<unknown>>;
15
15
  export type InferFilters<S extends FilterSchema> = {
16
16
  [K in keyof S]: S[K] extends Param<infer O> ? O : never;
17
17
  };
18
+ export interface UseQueryFiltersOptions {
19
+ prefix?: string;
20
+ }
18
21
  export interface UseQueryFiltersReturn<S extends FilterSchema> {
19
22
  filters: InferFilters<S>;
20
23
  patch(partial: Partial<InferFilters<S>>): void;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AACA,MAAM,WAAW,UAAU,CAAC,CAAC;IAE3B,SAAS,CAAC,KAAK,EAAE,CAAC,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,CAAA;IAE7C,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,GAAG,SAAS,CAAA;CACnD;AAGD,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI;IAC5B,OAAO,CAAC,EAAE,CAAC,CAAA;IACX,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC7B,CAAA;AAGD,MAAM,WAAW,KAAK,CAAC,GAAG;IACxB,KAAK,EAAE,UAAU,CAAC,GAAG,CAAC,CAAA;IACtB,OAAO,EAAE,GAAG,CAAA;IACZ,OAAO,EAAE,MAAM,GAAG,SAAS,CAAA;CAC5B;AAGD,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAA;AAGzD,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,YAAY,IAAI;KAChD,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK;CACxD,CAAA;AAGD,MAAM,WAAW,qBAAqB,CAAC,CAAC,SAAS,YAAY;IAC3D,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,CAAA;IACxB,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAA;IAC9C,KAAK,IAAI,IAAI,CAAA;CACd"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AACA,MAAM,WAAW,UAAU,CAAC,CAAC;IAE3B,SAAS,CAAC,KAAK,EAAE,CAAC,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,CAAA;IAE7C,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,GAAG,SAAS,CAAA;CACnD;AAGD,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI;IAC5B,OAAO,CAAC,EAAE,CAAC,CAAA;IACX,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC7B,CAAA;AAGD,MAAM,WAAW,KAAK,CAAC,GAAG;IACxB,KAAK,EAAE,UAAU,CAAC,GAAG,CAAC,CAAA;IACtB,OAAO,EAAE,GAAG,CAAA;IACZ,OAAO,EAAE,MAAM,GAAG,SAAS,CAAA;CAC5B;AAGD,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAA;AAGzD,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,YAAY,IAAI;KAChD,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK;CACxD,CAAA;AAGD,MAAM,WAAW,sBAAsB;IAGrC,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAGD,MAAM,WAAW,qBAAqB,CAAC,CAAC,SAAS,YAAY;IAC3D,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,CAAA;IACxB,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAA;IAC9C,KAAK,IAAI,IAAI,CAAA;CACd"}
@@ -1,3 +1,3 @@
1
- import { FilterSchema, UseQueryFiltersReturn } from './types';
2
- export declare function useQueryFilters<S extends FilterSchema>(schema: S): UseQueryFiltersReturn<S>;
1
+ import { FilterSchema, UseQueryFiltersOptions, UseQueryFiltersReturn } from './types';
2
+ export declare function useQueryFilters<S extends FilterSchema>(schema: S, options?: UseQueryFiltersOptions): UseQueryFiltersReturn<S>;
3
3
  //# sourceMappingURL=useQueryFilters.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useQueryFilters.d.ts","sourceRoot":"","sources":["../src/useQueryFilters.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,YAAY,EAAuB,qBAAqB,EAAE,MAAM,SAAS,CAAA;AAiCvF,wBAAgB,eAAe,CAAC,CAAC,SAAS,YAAY,EAAE,MAAM,EAAE,CAAC,GAAG,qBAAqB,CAAC,CAAC,CAAC,CA0B3F"}
1
+ {"version":3,"file":"useQueryFilters.d.ts","sourceRoot":"","sources":["../src/useQueryFilters.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,YAAY,EAGZ,sBAAsB,EACtB,qBAAqB,EACtB,MAAM,SAAS,CAAA;AAiChB,wBAAgB,eAAe,CAAC,CAAC,SAAS,YAAY,EACpD,MAAM,EAAE,CAAC,EACT,OAAO,GAAE,sBAA2B,GACnC,qBAAqB,CAAC,CAAC,CAAC,CA6B1B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-router-query-sync",
3
- "version": "2.0.0-alpha.2",
3
+ "version": "2.0.0-alpha.3",
4
4
  "description": "Reactive, typed view of Vue Router query params with codecs and defaults",
5
5
  "type": "module",
6
6
  "module": "./dist/index.mjs",