@tolki/ts 0.1.0 → 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,15 +1,16 @@
1
- import { cases as o, defineEnum as f, from as a, tryFrom as r } from "./enums.js";
2
- import { addRouteDefault as s, annotatePageProps as m, defineRoute as n, formSafeOptions as R, getRouteDefaults as d, resetRouteDefaults as l, setRouteDefaults as p } 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
4
  s as addRouteDefault,
5
- m as annotatePageProps,
5
+ n as annotatePageProps,
6
+ m as annotateRequestPayload,
6
7
  o as cases,
7
- f as defineEnum,
8
- n as defineRoute,
9
- R as formSafeOptions,
10
- a as from,
11
- d as getRouteDefaults,
12
- l as resetRouteDefaults,
13
- p as setRouteDefaults,
14
- 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
15
16
  };
package/dist/routes.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { DefineRouteResult, InferPageProps, RouteArgMeta, RouteCallResult, RouteCallResultWithComponent, RouteComponentType, RouteFormResult, RouteMetadata, RouteQueryOptions } from '../packages/types/src/index.ts';
2
- export type { DefineRouteResult, InferPageProps, 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
3
  /**
4
4
  * Attach a phantom page props type to a `defineRoute` result without losing
5
5
  * the inferred TMethods / TArgs / TComponent specificity.
@@ -18,7 +18,26 @@ export type { DefineRouteResult, InferPageProps, RouteArgMeta, RouteCallResult,
18
18
  * @returns A one-argument function that accepts a `defineRoute` result and returns
19
19
  * the same value cast to carry `TPageProps`.
20
20
  */
21
- export declare function annotatePageProps<TPageProps>(): <TMethods extends readonly string[], TArgs extends readonly RouteArgMeta[], TComponent extends RouteComponentType | undefined, TExistingPageProps>(route: DefineRouteResult<TMethods, TArgs, TComponent, TExistingPageProps>) => DefineRouteResult<TMethods, TArgs, TComponent, TPageProps>;
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>;
22
41
  /**
23
42
  * Set global default route parameter values.
24
43
  *
@@ -1 +1 @@
1
- {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,iBAAiB,EACjB,cAAc,EACd,YAAY,EACZ,eAAe,EACf,4BAA4B,EAC5B,kBAAkB,EAClB,eAAe,EACf,aAAa,EACb,iBAAiB,EACpB,MAAM,cAAc,CAAC;AAYtB,YAAY,EACR,iBAAiB,EACjB,cAAc,EACd,YAAY,EACZ,eAAe,EACf,4BAA4B,EAC5B,kBAAkB,EAClB,eAAe,EACf,aAAa,EACb,iBAAiB,GACpB,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,iBAAiB,CAAC,UAAU,KAAK,CAC7C,QAAQ,SAAS,SAAS,MAAM,EAAE,EAClC,KAAK,SAAS,SAAS,YAAY,EAAE,EACrC,UAAU,SAAS,kBAAkB,GAAG,SAAS,EACjD,kBAAkB,EAElB,KAAK,EAAE,iBAAiB,CAAC,QAAQ,EAAE,KAAK,EAAE,UAAU,EAAE,kBAAkB,CAAC,KACxE,iBAAiB,CAAC,QAAQ,EAAE,KAAK,EAAE,UAAU,EAAE,UAAU,CAAC,CAqB9D;AAiBD;;;;;;;;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,MAA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,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,iBAAiB,GAC5B,iBAAiB,CAWnB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,WAAW,CACvB,KAAK,CAAC,QAAQ,SAAS,SAAS,MAAM,EAAE,EACxC,KAAK,CAAC,KAAK,SAAS,SAAS,YAAY,EAAE,GAAG,SAAS,EAAE,EACzD,KAAK,CAAC,UAAU,SAAS,kBAAkB,GAAG,SAAS,GAAG,SAAS,EAEnE,QAAQ,EAAE,aAAa,CAAC,QAAQ,EAAE,KAAK,EAAE,UAAU,CAAC,GACrD,iBAAiB,CAAC,QAAQ,EAAE,KAAK,EAAE,UAAU,CAAC,CAyFhD"}
package/dist/routes.js CHANGED
@@ -1,31 +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 P } from "@tolki/utils";
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
2
  function I() {
3
3
  return function(e) {
4
4
  return e;
5
5
  };
6
6
  }
7
- const C = () => ({});
8
- let b = C;
9
- function N(e) {
10
- b = E(e) ? e : () => ({ ...e });
7
+ function N() {
8
+ return function(e) {
9
+ return e;
10
+ };
11
11
  }
12
- function T(e, n) {
13
- const o = b;
14
- b = () => ({
12
+ const w = () => ({});
13
+ let y = w;
14
+ function T(e) {
15
+ y = D(e) ? e : () => ({ ...e });
16
+ }
17
+ function V(e, n) {
18
+ const o = y;
19
+ y = () => ({
15
20
  ...o(),
16
21
  [e]: n
17
22
  });
18
23
  }
19
- function V() {
20
- return { ...b() };
21
- }
22
24
  function v() {
23
- b = C;
25
+ return { ...y() };
26
+ }
27
+ function L() {
28
+ y = w;
24
29
  }
25
30
  function O(e, n) {
26
- if (j(e) || R(e))
31
+ if ($(e) || j(e))
27
32
  return "";
28
- if (g(e)) {
33
+ if (a(e)) {
29
34
  if (n?._routeKey) {
30
35
  const o = n._routeKey;
31
36
  if (o in e)
@@ -48,11 +53,11 @@ function O(e, n) {
48
53
  return String(e);
49
54
  }
50
55
  function S(e, n) {
51
- if (R(n) || j(n))
56
+ if (j(n) || $(n))
52
57
  return [];
53
- if (P(n))
58
+ if (K(n))
54
59
  return [`${encodeURIComponent(e)}=${n ? "1" : "0"}`];
55
- if (w(n)) {
60
+ if (q(n)) {
56
61
  const o = [];
57
62
  for (let r = 0; r < n.length; r++)
58
63
  o.push(
@@ -60,7 +65,7 @@ function S(e, n) {
60
65
  );
61
66
  return o;
62
67
  }
63
- if (g(n)) {
68
+ if (a(n)) {
64
69
  const o = [];
65
70
  for (const [r, h] of Object.entries(n))
66
71
  o.push(
@@ -76,27 +81,27 @@ function Q(e) {
76
81
  n.push(...S(o, r));
77
82
  return n.length === 0 ? "" : `?${n.join("&")}`;
78
83
  }
79
- function $(e, n) {
84
+ function R(e, n) {
80
85
  if (e.length === 0)
81
86
  return [{}, void 0];
82
87
  const o = n;
83
88
  let r;
84
89
  if (o.length === 0)
85
- return e.length === 1 && g(e[0]) ? [{}, e[0]] : [{}, void 0];
90
+ return e.length === 1 && a(e[0]) ? [{}, e[0]] : [{}, void 0];
86
91
  const h = new Set(o.map((s) => s.name));
87
92
  if (e.length >= 2) {
88
93
  const s = e[e.length - 1];
89
- g(s) && !Object.keys(s).some(
94
+ a(s) && !Object.keys(s).some(
90
95
  (t) => h.has(t)
91
96
  ) && e.length > o.length && (r = s, e = e.slice(0, -1));
92
97
  }
93
- if (e.length === 1 && w(e[0])) {
98
+ if (e.length === 1 && q(e[0])) {
94
99
  const s = e[0], m = {};
95
100
  for (let t = 0; t < s.length && t < o.length; t++)
96
101
  m[o[t].name] = s[t];
97
102
  return [m, r];
98
103
  }
99
- if (e.length === 1 && g(e[0])) {
104
+ if (e.length === 1 && a(e[0])) {
100
105
  const s = e[0];
101
106
  if (Object.keys(s).some((t) => h.has(t))) {
102
107
  const t = {}, u = {};
@@ -117,7 +122,7 @@ function $(e, n) {
117
122
  }
118
123
  return [{}, { ...r, ...s }];
119
124
  }
120
- 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))) {
121
126
  const s = {};
122
127
  for (let m = 0; m < e.length && m < o.length; m++)
123
128
  s[o[m].name] = e[m];
@@ -128,7 +133,7 @@ function $(e, n) {
128
133
  function d(e, n, o, r) {
129
134
  let h = e;
130
135
  !h.startsWith("/") && !h.startsWith("//") && !h.includes("://") && (h = `//${h}`);
131
- 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 = {};
132
137
  for (const i of o)
133
138
  i.name in m && !(i.name in n) && (t[i.name] = m[i.name]);
134
139
  for (const [i, f] of Object.entries(n))
@@ -154,33 +159,33 @@ function d(e, n, o, r) {
154
159
  const c = h.replace(
155
160
  /{([^{}?]+)(\??)}/g,
156
161
  (i, f, p) => {
157
- const y = t[f], q = s.get(f);
158
- if (!p && (j(y) || R(y)))
162
+ const b = t[f], C = s.get(f);
163
+ if (!p && ($(b) || j(b)))
159
164
  throw new Error(
160
165
  `Route error: '${f}' parameter is required.`
161
166
  );
162
- const D = O(y, q);
163
- 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");
164
169
  }
165
170
  ).replace(/([^:])\/{2,}/g, "$1/").replace(/\/+$/, "") || "/", l = {};
166
171
  if (r) {
167
172
  if (r.mergeQuery) {
168
- 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);
169
174
  for (const [f, p] of i.entries())
170
175
  l[f] = p;
171
176
  for (const [f, p] of Object.entries(r.mergeQuery))
172
- R(p) || j(p) ? delete l[f] : l[f] = p;
177
+ j(p) || $(p) ? delete l[f] : l[f] = p;
173
178
  }
174
179
  for (const [i, f] of Object.entries(r))
175
- if (i === "_query" && g(f))
176
- for (const [p, y] of Object.entries(
180
+ if (i === "_query" && a(f))
181
+ for (const [p, b] of Object.entries(
177
182
  f
178
183
  ))
179
- l[p] = y;
184
+ l[p] = b;
180
185
  else i !== "_query" && i !== "mergeQuery" && (l[i] = f);
181
186
  }
182
- const a = Q(l);
183
- return c + a;
187
+ const g = Q(l);
188
+ return c + g;
184
189
  }
185
190
  function k(e, n) {
186
191
  return {
@@ -210,13 +215,13 @@ function x(e, n) {
210
215
  }
211
216
  };
212
217
  }
213
- function L(e) {
218
+ function W(e) {
214
219
  const n = e.args ?? [], o = e.methods[0] ?? "get", r = (...t) => {
215
- 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);
216
221
  return k(l, o);
217
222
  }, s = Object.assign(r, {
218
223
  url: (...t) => {
219
- const [u, c] = $(t, n);
224
+ const [u, c] = R(t, n);
220
225
  return d(e.url, u, n, c);
221
226
  },
222
227
  definition: e,
@@ -227,19 +232,19 @@ function L(e) {
227
232
  for (const t of e.methods)
228
233
  Object.assign(s, {
229
234
  [t]: (...u) => {
230
- const [c, l] = $(u, n), a = d(e.url, c, n, l);
231
- return k(a, t);
235
+ const [c, l] = R(u, n), g = d(e.url, c, n, l);
236
+ return k(g, t);
232
237
  }
233
238
  });
234
239
  const m = (...t) => {
235
- 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);
236
241
  return _(l, o);
237
242
  };
238
243
  for (const t of e.methods) {
239
244
  const u = t !== "get" && t !== "post";
240
245
  Object.assign(m, {
241
246
  [t]: (...c) => {
242
- const [l, a] = $(c, n), i = u ? x(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);
243
248
  return _(f, t);
244
249
  }
245
250
  });
@@ -257,11 +262,12 @@ function L(e) {
257
262
  return s;
258
263
  }
259
264
  export {
260
- T as addRouteDefault,
265
+ V as addRouteDefault,
261
266
  I as annotatePageProps,
262
- L as defineRoute,
267
+ N as annotateRequestPayload,
268
+ W as defineRoute,
263
269
  x as formSafeOptions,
264
- V as getRouteDefaults,
265
- v as resetRouteDefaults,
266
- N as setRouteDefaults
270
+ v as getRouteDefaults,
271
+ L as resetRouteDefaults,
272
+ T as setRouteDefaults
267
273
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tolki/ts",
3
- "version": "0.1.0",
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",
@@ -39,7 +39,7 @@
39
39
  "tolki"
40
40
  ],
41
41
  "dependencies": {
42
- "@tolki/types": "^1.3.0",
42
+ "@tolki/types": "^1.4.0",
43
43
  "@tolki/utils": "^1.0.2"
44
44
  },
45
45
  "peerDependencies": {