@tolki/ts 0.0.1 → 0.2.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.js CHANGED
@@ -1,14 +1,16 @@
1
- import { cases as o, defineEnum as f, from as u, tryFrom as r } from "./enums.js";
2
- import { addRouteDefault as a, defineRoute as m, formSafeOptions as R, getRouteDefaults as d, resetRouteDefaults as l, setRouteDefaults as n } from "./routes.js";
1
+ import { cases as o, defineEnum as a, from as f, tryFrom as u } from "./enums.js";
2
+ import { addRouteDefault as s, annotatePageProps as n, annotateRequestPayload as m, defineRoute as R, formSafeOptions as d, getRouteDefaults as l, resetRouteDefaults as p, setRouteDefaults as D } from "./routes.js";
3
3
  export {
4
- a as addRouteDefault,
4
+ s as addRouteDefault,
5
+ n as annotatePageProps,
6
+ m as annotateRequestPayload,
5
7
  o as cases,
6
- f as defineEnum,
7
- m as defineRoute,
8
- R as formSafeOptions,
9
- u as from,
10
- d as getRouteDefaults,
11
- l as resetRouteDefaults,
12
- n as setRouteDefaults,
13
- r as tryFrom
8
+ a as defineEnum,
9
+ R as defineRoute,
10
+ d as formSafeOptions,
11
+ f as from,
12
+ l as getRouteDefaults,
13
+ p as resetRouteDefaults,
14
+ D as setRouteDefaults,
15
+ u as tryFrom
14
16
  };
package/dist/routes.d.ts CHANGED
@@ -1,5 +1,43 @@
1
- import { DefineRouteResult, RouteArgMeta, RouteCallResult, RouteCallResultWithComponent, RouteComponentType, RouteFormResult, RouteMetadata, RouteQueryOptions } from '../packages/types/src/index.ts';
2
- export type { DefineRouteResult, RouteArgMeta, RouteCallResult, RouteCallResultWithComponent, RouteComponentType, RouteFormResult, RouteMetadata, RouteQueryOptions, };
1
+ import { DefineRouteResult, InferPageProps, InferRequestPayload, RouteArgMeta, RouteCallResult, RouteCallResultWithComponent, RouteComponentType, RouteFormResult, RouteMetadata, RouteQueryOptions } from '../packages/types/src/index.ts';
2
+ export type { DefineRouteResult, InferPageProps, InferRequestPayload, RouteArgMeta, RouteCallResult, RouteCallResultWithComponent, RouteComponentType, RouteFormResult, RouteMetadata, RouteQueryOptions, };
3
+ /**
4
+ * Attach a phantom page props type to a `defineRoute` result without losing
5
+ * the inferred TMethods / TArgs / TComponent specificity.
6
+ *
7
+ * Use a curried call so TypeScript can infer the route generics while you
8
+ * supply `TPageProps` explicitly:
9
+ *
10
+ * ```typescript
11
+ * export const dashboard = annotatePageProps<DashboardPageProps>()(defineRoute({ ... }));
12
+ * type Props = InferPageProps<typeof dashboard>; // DashboardPageProps
13
+ * ```
14
+ *
15
+ * Already-annotated routes can be re-annotated — the existing `TPageProps` generic
16
+ * (`TExistingPageProps`) is accepted and discarded in favour of the new one.
17
+ *
18
+ * @returns A one-argument function that accepts a `defineRoute` result and returns
19
+ * the same value cast to carry `TPageProps`.
20
+ */
21
+ export declare function annotatePageProps<TPageProps>(): <TMethods extends readonly string[], TArgs extends readonly RouteArgMeta[], TComponent extends RouteComponentType | undefined, TExistingPageProps, TExistingRequestPayload>(route: DefineRouteResult<TMethods, TArgs, TComponent, TExistingPageProps, TExistingRequestPayload>) => DefineRouteResult<TMethods, TArgs, TComponent, TPageProps, TExistingRequestPayload>;
22
+ /**
23
+ * Attach a phantom request payload type to a `defineRoute` result without
24
+ * losing the inferred TMethods / TArgs / TComponent / TPageProps specificity.
25
+ *
26
+ * Use a curried call so TypeScript can infer the route generics while you
27
+ * supply `TRequestPayload` explicitly:
28
+ *
29
+ * ```typescript
30
+ * export const store = annotateRequestPayload<StorePostRequest>()(defineRoute({ ... }));
31
+ * type Payload = InferRequestPayload<typeof store>; // StorePostRequest
32
+ * ```
33
+ *
34
+ * Already-annotated routes can be re-annotated — the existing `TRequestPayload` generic
35
+ * (`TExistingRequestPayload`) is accepted and discarded in favour of the new one.
36
+ *
37
+ * @returns A one-argument function that accepts a `defineRoute` result and returns
38
+ * the same value cast to carry `TRequestPayload`.
39
+ */
40
+ export declare function annotateRequestPayload<TRequestPayload>(): <TMethods extends readonly string[], TArgs extends readonly RouteArgMeta[], TComponent extends RouteComponentType | undefined, TPageProps, TExistingRequestPayload>(route: DefineRouteResult<TMethods, TArgs, TComponent, TPageProps, TExistingRequestPayload>) => DefineRouteResult<TMethods, TArgs, TComponent, TPageProps, TRequestPayload>;
3
41
  /**
4
42
  * Set global default route parameter values.
5
43
  *
@@ -1 +1 @@
1
- {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,iBAAiB,EACjB,YAAY,EACZ,eAAe,EACf,4BAA4B,EAC5B,kBAAkB,EAClB,eAAe,EACf,aAAa,EACb,iBAAiB,EACpB,MAAM,cAAc,CAAC;AAYtB,YAAY,EACR,iBAAiB,EACjB,YAAY,EACZ,eAAe,EACf,4BAA4B,EAC5B,kBAAkB,EAClB,eAAe,EACf,aAAa,EACb,iBAAiB,GACpB,CAAC;AAiBF;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAC5B,QAAQ,EACF,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,GACzC,CAAC,MAAM,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC,GACxD,IAAI,CAEN;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC3B,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GACjC,IAAI,CAMN;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAE5E;AAED;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CAEzC;AA8YD;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAC3B,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,iBAAiB,GAC5B,iBAAiB,CAWnB;AAED;;;;;;;;;;;;;;;;;;;;;;GAs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B,CAAC,QAAQ,EAAE,KAAK,EAAE,UAAU,CAAC,CAyFhD"}
package/dist/routes.js CHANGED
@@ -1,26 +1,36 @@
1
- import { isString as U, isFunction as E, isUndefined as j, isNull as R, isObject as g, isArray as w, isNumber as K, isBoolean as Q } from "@tolki/utils";
2
- const C = () => ({});
3
- let b = C;
4
- function I(e) {
5
- b = E(e) ? e : () => ({ ...e });
1
+ import { isString as U, isFunction as D, isUndefined as $, isNull as j, isObject as a, isArray as q, isNumber as E, isBoolean as K } from "@tolki/utils";
2
+ function I() {
3
+ return function(e) {
4
+ return e;
5
+ };
6
+ }
7
+ function N() {
8
+ return function(e) {
9
+ return e;
10
+ };
11
+ }
12
+ const w = () => ({});
13
+ let y = w;
14
+ function T(e) {
15
+ y = D(e) ? e : () => ({ ...e });
6
16
  }
7
- function N(e, n) {
8
- const o = b;
9
- b = () => ({
17
+ function V(e, n) {
18
+ const o = y;
19
+ y = () => ({
10
20
  ...o(),
11
21
  [e]: n
12
22
  });
13
23
  }
14
- function T() {
15
- return { ...b() };
24
+ function v() {
25
+ return { ...y() };
16
26
  }
17
- function V() {
18
- b = C;
27
+ function L() {
28
+ y = w;
19
29
  }
20
30
  function O(e, n) {
21
- if (j(e) || R(e))
31
+ if ($(e) || j(e))
22
32
  return "";
23
- if (g(e)) {
33
+ if (a(e)) {
24
34
  if (n?._routeKey) {
25
35
  const o = n._routeKey;
26
36
  if (o in e)
@@ -43,11 +53,11 @@ function O(e, n) {
43
53
  return String(e);
44
54
  }
45
55
  function S(e, n) {
46
- if (R(n) || j(n))
56
+ if (j(n) || $(n))
47
57
  return [];
48
- if (Q(n))
58
+ if (K(n))
49
59
  return [`${encodeURIComponent(e)}=${n ? "1" : "0"}`];
50
- if (w(n)) {
60
+ if (q(n)) {
51
61
  const o = [];
52
62
  for (let r = 0; r < n.length; r++)
53
63
  o.push(
@@ -55,7 +65,7 @@ function S(e, n) {
55
65
  );
56
66
  return o;
57
67
  }
58
- if (g(n)) {
68
+ if (a(n)) {
59
69
  const o = [];
60
70
  for (const [r, h] of Object.entries(n))
61
71
  o.push(
@@ -65,33 +75,33 @@ function S(e, n) {
65
75
  }
66
76
  return [`${encodeURIComponent(e)}=${encodeURIComponent(String(n))}`];
67
77
  }
68
- function x(e) {
78
+ function Q(e) {
69
79
  const n = [];
70
80
  for (const [o, r] of Object.entries(e))
71
81
  n.push(...S(o, r));
72
82
  return n.length === 0 ? "" : `?${n.join("&")}`;
73
83
  }
74
- function $(e, n) {
84
+ function R(e, n) {
75
85
  if (e.length === 0)
76
86
  return [{}, void 0];
77
87
  const o = n;
78
88
  let r;
79
89
  if (o.length === 0)
80
- return e.length === 1 && g(e[0]) ? [{}, e[0]] : [{}, void 0];
90
+ return e.length === 1 && a(e[0]) ? [{}, e[0]] : [{}, void 0];
81
91
  const h = new Set(o.map((s) => s.name));
82
92
  if (e.length >= 2) {
83
93
  const s = e[e.length - 1];
84
- g(s) && !Object.keys(s).some(
94
+ a(s) && !Object.keys(s).some(
85
95
  (t) => h.has(t)
86
96
  ) && e.length > o.length && (r = s, e = e.slice(0, -1));
87
97
  }
88
- if (e.length === 1 && w(e[0])) {
98
+ if (e.length === 1 && q(e[0])) {
89
99
  const s = e[0], m = {};
90
100
  for (let t = 0; t < s.length && t < o.length; t++)
91
101
  m[o[t].name] = s[t];
92
102
  return [m, r];
93
103
  }
94
- if (e.length === 1 && g(e[0])) {
104
+ if (e.length === 1 && a(e[0])) {
95
105
  const s = e[0];
96
106
  if (Object.keys(s).some((t) => h.has(t))) {
97
107
  const t = {}, u = {};
@@ -112,7 +122,7 @@ function $(e, n) {
112
122
  }
113
123
  return [{}, { ...r, ...s }];
114
124
  }
115
- if (e.length >= 1 && e.every((s) => U(s) || K(s) || g(s))) {
125
+ if (e.length >= 1 && e.every((s) => U(s) || E(s) || a(s))) {
116
126
  const s = {};
117
127
  for (let m = 0; m < e.length && m < o.length; m++)
118
128
  s[o[m].name] = e[m];
@@ -123,7 +133,7 @@ function $(e, n) {
123
133
  function d(e, n, o, r) {
124
134
  let h = e;
125
135
  !h.startsWith("/") && !h.startsWith("//") && !h.includes("://") && (h = `//${h}`);
126
- const s = new Map(o.map((i) => [i.name, i])), m = b(), t = {};
136
+ const s = new Map(o.map((i) => [i.name, i])), m = y(), t = {};
127
137
  for (const i of o)
128
138
  i.name in m && !(i.name in n) && (t[i.name] = m[i.name]);
129
139
  for (const [i, f] of Object.entries(n))
@@ -149,33 +159,33 @@ function d(e, n, o, r) {
149
159
  const c = h.replace(
150
160
  /{([^{}?]+)(\??)}/g,
151
161
  (i, f, p) => {
152
- const y = t[f], q = s.get(f);
153
- if (!p && (j(y) || R(y)))
162
+ const b = t[f], C = s.get(f);
163
+ if (!p && ($(b) || j(b)))
154
164
  throw new Error(
155
165
  `Route error: '${f}' parameter is required.`
156
166
  );
157
- const D = O(y, q);
158
- return encodeURI(D).replace(/%7C/g, "|").replace(/%25/g, "%").replace(/\$/g, "%24");
167
+ const P = O(b, C);
168
+ return encodeURI(P).replace(/%7C/g, "|").replace(/%25/g, "%").replace(/\$/g, "%24");
159
169
  }
160
170
  ).replace(/([^:])\/{2,}/g, "$1/").replace(/\/+$/, "") || "/", l = {};
161
171
  if (r) {
162
172
  if (r.mergeQuery) {
163
- const i = j(globalThis.window) ? new URLSearchParams() : new URLSearchParams(globalThis.window.location.search);
173
+ const i = $(globalThis.window) ? new URLSearchParams() : new URLSearchParams(globalThis.window.location.search);
164
174
  for (const [f, p] of i.entries())
165
175
  l[f] = p;
166
176
  for (const [f, p] of Object.entries(r.mergeQuery))
167
- R(p) || j(p) ? delete l[f] : l[f] = p;
177
+ j(p) || $(p) ? delete l[f] : l[f] = p;
168
178
  }
169
179
  for (const [i, f] of Object.entries(r))
170
- if (i === "_query" && g(f))
171
- for (const [p, y] of Object.entries(
180
+ if (i === "_query" && a(f))
181
+ for (const [p, b] of Object.entries(
172
182
  f
173
183
  ))
174
- l[p] = y;
184
+ l[p] = b;
175
185
  else i !== "_query" && i !== "mergeQuery" && (l[i] = f);
176
186
  }
177
- const a = x(l);
178
- return c + a;
187
+ const g = Q(l);
188
+ return c + g;
179
189
  }
180
190
  function k(e, n) {
181
191
  return {
@@ -195,7 +205,7 @@ function _(e, n) {
195
205
  }
196
206
  };
197
207
  }
198
- function P(e, n) {
208
+ function x(e, n) {
199
209
  const o = n?.mergeQuery ? "mergeQuery" : "_query", r = n?.[o] ?? {};
200
210
  return {
201
211
  ...n,
@@ -205,13 +215,13 @@ function P(e, n) {
205
215
  }
206
216
  };
207
217
  }
208
- function v(e) {
218
+ function W(e) {
209
219
  const n = e.args ?? [], o = e.methods[0] ?? "get", r = (...t) => {
210
- const [u, c] = $(t, n), l = d(e.url, u, n, c);
220
+ const [u, c] = R(t, n), l = d(e.url, u, n, c);
211
221
  return k(l, o);
212
222
  }, s = Object.assign(r, {
213
223
  url: (...t) => {
214
- const [u, c] = $(t, n);
224
+ const [u, c] = R(t, n);
215
225
  return d(e.url, u, n, c);
216
226
  },
217
227
  definition: e,
@@ -222,19 +232,19 @@ function v(e) {
222
232
  for (const t of e.methods)
223
233
  Object.assign(s, {
224
234
  [t]: (...u) => {
225
- const [c, l] = $(u, n), a = d(e.url, c, n, l);
226
- return k(a, t);
235
+ const [c, l] = R(u, n), g = d(e.url, c, n, l);
236
+ return k(g, t);
227
237
  }
228
238
  });
229
239
  const m = (...t) => {
230
- const [u, c] = $(t, n), l = d(e.url, u, n, c);
240
+ const [u, c] = R(t, n), l = d(e.url, u, n, c);
231
241
  return _(l, o);
232
242
  };
233
243
  for (const t of e.methods) {
234
244
  const u = t !== "get" && t !== "post";
235
245
  Object.assign(m, {
236
246
  [t]: (...c) => {
237
- const [l, a] = $(c, n), i = u ? P(t, a) : a, f = d(e.url, l, n, i);
247
+ const [l, g] = R(c, n), i = u ? x(t, g) : g, f = d(e.url, l, n, i);
238
248
  return _(f, t);
239
249
  }
240
250
  });
@@ -252,10 +262,12 @@ function v(e) {
252
262
  return s;
253
263
  }
254
264
  export {
255
- N as addRouteDefault,
256
- v as defineRoute,
257
- P as formSafeOptions,
258
- T as getRouteDefaults,
259
- V as resetRouteDefaults,
260
- I as setRouteDefaults
265
+ V as addRouteDefault,
266
+ I as annotatePageProps,
267
+ N as annotateRequestPayload,
268
+ W as defineRoute,
269
+ x as formSafeOptions,
270
+ v as getRouteDefaults,
271
+ L as resetRouteDefaults,
272
+ T as setRouteDefaults
261
273
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tolki/ts",
3
- "version": "0.0.1",
3
+ "version": "0.2.0",
4
4
  "description": "A set of TypeScript utilities for the Laravel TypeScript Publish PHP package.",
5
5
  "author": "Abraham Arango <https://github.com/abetwothree>",
6
6
  "license": "MIT",
@@ -18,7 +18,7 @@
18
18
  "schemaVersion": 1,
19
19
  "vite": {
20
20
  "type": "compatible",
21
- "versions": "^7.0.0"
21
+ "versions": "^7.0.0||^8.0.0"
22
22
  }
23
23
  },
24
24
  "main": "dist/index.js",
@@ -39,11 +39,11 @@
39
39
  "tolki"
40
40
  ],
41
41
  "dependencies": {
42
- "@tolki/types": "^1.2.0",
42
+ "@tolki/types": "^1.4.0",
43
43
  "@tolki/utils": "^1.0.2"
44
44
  },
45
45
  "peerDependencies": {
46
- "vite": "^7.0.0"
46
+ "vite": "^7.0.0||^8.0.0"
47
47
  },
48
48
  "sideEffects": false,
49
49
  "files": [