@mauroandre/velojs 0.0.24 → 0.0.26

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.
@@ -4,14 +4,14 @@ function k(e) {
4
4
  if (!e) return;
5
5
  const r = Object.entries(e);
6
6
  if (r.length !== 0)
7
- return r.map(([a, u]) => `${encodeURIComponent(a)}=${encodeURIComponent(u)}`).join("; ");
7
+ return r.map(([u, l]) => `${encodeURIComponent(u)}=${encodeURIComponent(l)}`).join("; ");
8
8
  }
9
9
  function _(e) {
10
- const r = {}, a = e.getSetCookie?.() ?? [];
11
- for (const u of a) {
12
- const i = u.indexOf(";"), l = i === -1 ? u : u.slice(0, i), h = l.indexOf("=");
10
+ const r = {}, u = e.getSetCookie?.() ?? [];
11
+ for (const l of u) {
12
+ const i = l.indexOf(";"), a = i === -1 ? l : l.slice(0, i), h = a.indexOf("=");
13
13
  if (h === -1) continue;
14
- const c = l.slice(0, h).trim(), t = l.slice(h + 1).trim();
14
+ const c = a.slice(0, h).trim(), t = a.slice(h + 1).trim();
15
15
  c && (r[c] = decodeURIComponent(t));
16
16
  }
17
17
  return r;
@@ -19,18 +19,18 @@ function _(e) {
19
19
  function x(e) {
20
20
  if (!e) return "";
21
21
  const r = new URLSearchParams();
22
- for (const [u, i] of Object.entries(e))
22
+ for (const [l, i] of Object.entries(e))
23
23
  if (Array.isArray(i))
24
- for (const l of i) r.append(u, l);
24
+ for (const a of i) r.append(l, a);
25
25
  else
26
- r.set(u, i);
27
- const a = r.toString();
28
- return a ? `?${a}` : "";
26
+ r.set(l, i);
27
+ const u = r.toString();
28
+ return u ? `?${u}` : "";
29
29
  }
30
30
  function A(e) {
31
31
  const r = {};
32
- return e.forEach((a, u) => {
33
- r[u] = a;
32
+ return e.forEach((u, l) => {
33
+ r[l] = u;
34
34
  }), r;
35
35
  }
36
36
  function P(e) {
@@ -41,18 +41,18 @@ function P(e) {
41
41
  }
42
42
  const C = "http://velojs.test";
43
43
  function S(e, r) {
44
- const a = x(r);
45
- return e.startsWith("http://") || e.startsWith("https://") ? e + a : (e.startsWith("/") || (e = "/" + e), C + e + a);
44
+ const u = x(r);
45
+ return e.startsWith("http://") || e.startsWith("https://") ? e + u : (e.startsWith("/") || (e = "/" + e), C + e + u);
46
46
  }
47
47
  function I(e) {
48
- const r = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map();
49
- function u(i) {
50
- for (const l of i) {
51
- const h = l.module.metadata?.moduleId, c = l.module.metadata?.fullPath;
52
- if (h) {
53
- for (const o of Object.keys(l.module)) {
48
+ const r = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map();
49
+ function l(i) {
50
+ for (const a of i) {
51
+ const h = a.module?.metadata?.moduleId, c = a.module?.metadata?.fullPath;
52
+ if (a.module && h) {
53
+ for (const o of Object.keys(a.module)) {
54
54
  if (!o.startsWith("action_")) continue;
55
- const f = l.module[o];
55
+ const f = a.module[o];
56
56
  if (typeof f != "function") continue;
57
57
  const d = o.replace("action_", "");
58
58
  r.set(f, {
@@ -62,43 +62,43 @@ function I(e) {
62
62
  url: `/_action/${h}/${d}`
63
63
  });
64
64
  }
65
- const t = l.module.loader;
65
+ const t = a.module.loader;
66
66
  if (typeof t == "function" && c) {
67
- const o = a.get(t);
68
- o ? o.paths.push(c) : a.set(t, {
67
+ const o = u.get(t);
68
+ o ? o.paths.push(c) : u.set(t, {
69
69
  fn: t,
70
70
  moduleId: h,
71
71
  paths: [c]
72
72
  });
73
73
  }
74
74
  }
75
- l.children && u(l.children);
75
+ a.children && l(a.children);
76
76
  }
77
77
  }
78
- return u(e), { actions: r, loaders: a };
78
+ return l(e), { actions: r, loaders: u };
79
79
  }
80
80
  function q(e, r) {
81
81
  if (typeof r == "string") return r;
82
- const a = e.actions.get(r);
83
- if (!a)
82
+ const u = e.actions.get(r);
83
+ if (!u)
84
84
  throw new Error(
85
85
  `[velojs/testing] action_* function was not found in the route tree.
86
86
  Make sure your Vitest config loads the VeloJS Vite plugin so that metadata is injected. See https://github.com/mauro-andre/velojs#testing`
87
87
  );
88
- return a.url;
88
+ return u.url;
89
89
  }
90
- function J(e, r, a) {
90
+ function J(e, r, u) {
91
91
  if (typeof r == "string") return r;
92
- const u = e.loaders.get(r);
93
- if (!u)
92
+ const l = e.loaders.get(r);
93
+ if (!l)
94
94
  throw new Error(
95
95
  `[velojs/testing] loader function was not found in the route tree.
96
96
  Make sure your Vitest config loads the VeloJS Vite plugin so that metadata is injected.`
97
97
  );
98
- let i = u.paths[0];
99
- if (a)
100
- for (const [l, h] of Object.entries(a))
101
- i = i.replace(`:${l}`, encodeURIComponent(h));
98
+ let i = l.paths[0];
99
+ if (u)
100
+ for (const [a, h] of Object.entries(u))
101
+ i = i.replace(`:${a}`, encodeURIComponent(h));
102
102
  return i;
103
103
  }
104
104
  class $ {
@@ -109,36 +109,36 @@ class $ {
109
109
  };
110
110
  push(r) {
111
111
  this.buffer += r;
112
- const a = [];
113
- let u;
114
- for (; (u = this.buffer.indexOf(`
112
+ const u = [];
113
+ let l;
114
+ for (; (l = this.buffer.indexOf(`
115
115
  `)) !== -1; ) {
116
- const i = this.buffer.slice(0, u);
117
- if (this.buffer = this.buffer.slice(u + 1), i === "") {
116
+ const i = this.buffer.slice(0, l);
117
+ if (this.buffer = this.buffer.slice(l + 1), i === "") {
118
118
  if (this.current.data.length > 0 || this.current.event !== "message") {
119
119
  const t = {
120
120
  event: this.current.event,
121
121
  data: this.current.data.join(`
122
122
  `)
123
123
  };
124
- this.current.id !== void 0 && (t.id = this.current.id), a.push(t);
124
+ this.current.id !== void 0 && (t.id = this.current.id), u.push(t);
125
125
  }
126
126
  this.current = { event: "message", data: [] };
127
127
  continue;
128
128
  }
129
129
  if (i.startsWith(":")) continue;
130
- const l = i.indexOf(":");
131
- if (l === -1) continue;
132
- const h = i.slice(0, l);
133
- let c = i.slice(l + 1);
130
+ const a = i.indexOf(":");
131
+ if (a === -1) continue;
132
+ const h = i.slice(0, a);
133
+ let c = i.slice(a + 1);
134
134
  c.startsWith(" ") && (c = c.slice(1)), h === "event" ? this.current.event = c : h === "data" ? this.current.data.push(c) : h === "id" && (this.current.id = c);
135
135
  }
136
- return a;
136
+ return u;
137
137
  }
138
138
  }
139
139
  async function H(e) {
140
- const { response: r } = e, a = r.status, u = [];
141
- let i = null, l = !1, h = null;
140
+ const { response: r } = e, u = r.status, l = [];
141
+ let i = null, a = !1, h = null;
142
142
  const c = [];
143
143
  let t = null, o = Promise.resolve(void 0);
144
144
  const f = (p) => {
@@ -147,7 +147,7 @@ async function H(e) {
147
147
  p ? n.reject(p) : n.reject(new Error("[velojs/testing] subscription closed before next event"));
148
148
  }
149
149
  };
150
- if (a >= 200 && a < 300 && r.body) {
150
+ if (u >= 200 && u < 300 && r.body) {
151
151
  t = r.body.getReader();
152
152
  const p = new TextDecoder(), n = new $();
153
153
  o = (async () => {
@@ -155,7 +155,7 @@ async function H(e) {
155
155
  for (; ; ) {
156
156
  const { value: s, done: w } = await t.read();
157
157
  if (w) {
158
- l || (l = !0), f();
158
+ a || (a = !0), f();
159
159
  return;
160
160
  }
161
161
  const g = p.decode(s, { stream: !0 }), v = n.push(g);
@@ -169,14 +169,14 @@ async function H(e) {
169
169
  continue;
170
170
  }
171
171
  if (m.event === "close") {
172
- l = !0, f(), t.cancel().catch(() => {
172
+ a = !0, f(), t.cancel().catch(() => {
173
173
  });
174
174
  return;
175
175
  }
176
176
  if (m.event !== "heartbeat")
177
177
  try {
178
178
  const b = JSON.parse(m.data);
179
- u.push(b);
179
+ l.push(b);
180
180
  const j = c.shift();
181
181
  j && j.resolve(b);
182
182
  } catch (b) {
@@ -193,27 +193,27 @@ async function H(e) {
193
193
  }
194
194
  })();
195
195
  } else
196
- await r.text().catch(() => ""), l = !0;
196
+ await r.text().catch(() => ""), a = !0;
197
197
  const d = {
198
198
  get status() {
199
- return a;
199
+ return u;
200
200
  },
201
201
  get events() {
202
- return u;
202
+ return l;
203
203
  },
204
204
  get snapshot() {
205
205
  return i;
206
206
  },
207
207
  get closed() {
208
- return l;
208
+ return a;
209
209
  },
210
210
  async next({ timeoutMs: p }) {
211
211
  if (h) throw h;
212
- if (u.length > y) {
213
- const n = u[y];
212
+ if (l.length > y) {
213
+ const n = l[y];
214
214
  return y++, n;
215
215
  }
216
- if (l)
216
+ if (a)
217
217
  throw new Error("[velojs/testing] subscription is closed");
218
218
  return new Promise((n, s) => {
219
219
  const w = setTimeout(() => {
@@ -247,7 +247,7 @@ async function H(e) {
247
247
  await t.cancel();
248
248
  } catch {
249
249
  }
250
- l = !0, f(), await o.catch(() => {
250
+ a = !0, f(), await o.catch(() => {
251
251
  });
252
252
  }
253
253
  };
@@ -255,9 +255,9 @@ async function H(e) {
255
255
  return d;
256
256
  }
257
257
  function N(e = {}) {
258
- const { user: r, params: a = {}, query: u = {}, body: i, headers: l = {}, cookies: h } = e, c = k(h), t = new Headers(l);
258
+ const { user: r, params: u = {}, query: l = {}, body: i, headers: a = {}, cookies: h } = e, c = k(h), t = new Headers(a);
259
259
  c && !t.has("cookie") && t.set("cookie", c);
260
- const o = C + "/_mock" + x(u), f = {
260
+ const o = C + "/_mock" + x(l), f = {
261
261
  method: i == null ? "GET" : "POST",
262
262
  headers: t
263
263
  };
@@ -269,19 +269,19 @@ function N(e = {}) {
269
269
  url: d.url,
270
270
  method: d.method,
271
271
  param(n) {
272
- return n === void 0 ? a : a[n];
272
+ return n === void 0 ? u : u[n];
273
273
  },
274
274
  query(n) {
275
275
  if (n === void 0) {
276
276
  const s = {};
277
- for (const [w, g] of Object.entries(u))
277
+ for (const [w, g] of Object.entries(l))
278
278
  s[w] = String(g);
279
279
  return s;
280
280
  }
281
- return u[n];
281
+ return l[n];
282
282
  },
283
283
  queries(n) {
284
- const s = u[n];
284
+ const s = l[n];
285
285
  if (s !== void 0)
286
286
  return Array.isArray(s) ? s : [s];
287
287
  },
@@ -350,12 +350,12 @@ async function W(e) {
350
350
  const r = E();
351
351
  return await R(r, async () => {
352
352
  e.bootstrap && await e.bootstrap();
353
- const { createApp: a } = await import("../server.js"), u = await a(e.routes), i = I(e.routes);
354
- return V(u, r, i, e, void 0);
353
+ const { createApp: u } = await import("../server.js"), l = await u(e.routes), i = I(e.routes);
354
+ return V(l, r, i, e, void 0);
355
355
  });
356
356
  }
357
- function V(e, r, a, u, i) {
358
- async function l(t, o, f = {}) {
357
+ function V(e, r, u, l, i) {
358
+ async function a(t, o, f = {}) {
359
359
  const d = S(o, f.query), y = new Headers(f.headers ?? {}), p = { ...f.cookies ?? {} }, n = k(p);
360
360
  n && y.set("cookie", n);
361
361
  let s = null;
@@ -380,25 +380,25 @@ function V(e, r, a, u, i) {
380
380
  const c = {
381
381
  hono: e,
382
382
  // HTTP
383
- get: (t, o) => l("GET", t, o ?? {}),
384
- post: (t, o) => l("POST", t, o ?? {}),
385
- put: (t, o) => l("PUT", t, o ?? {}),
386
- patch: (t, o) => l("PATCH", t, o ?? {}),
387
- delete: (t, o) => l("DELETE", t, o ?? {}),
383
+ get: (t, o) => a("GET", t, o ?? {}),
384
+ post: (t, o) => a("POST", t, o ?? {}),
385
+ put: (t, o) => a("PUT", t, o ?? {}),
386
+ patch: (t, o) => a("PATCH", t, o ?? {}),
387
+ delete: (t, o) => a("DELETE", t, o ?? {}),
388
388
  async action(t, o = {}) {
389
- const f = q(a, t);
390
- return await l("POST", f, {
389
+ const f = q(u, t);
390
+ return await a("POST", f, {
391
391
  ...o,
392
392
  body: o.body ?? {}
393
393
  });
394
394
  },
395
395
  async loader(t, o = {}) {
396
- const f = J(a, t, o.params), d = await l("GET", f, {
396
+ const f = J(u, t, o.params), d = await a("GET", f, {
397
397
  ...o,
398
398
  query: { ...o.query ?? {}, _data: "1" }
399
399
  });
400
400
  if (d.status >= 300 && d.status < 400 || d.status >= 400) return d;
401
- const y = typeof t == "function" ? a.loaders.get(t)?.moduleId : void 0, p = await d.json();
401
+ const y = typeof t == "function" ? u.loaders.get(t)?.moduleId : void 0, p = await d.json();
402
402
  return y && p && typeof p == "object" && y in p ? p[y] : p;
403
403
  },
404
404
  async subscribe(t, o = {}) {
@@ -406,19 +406,19 @@ function V(e, r, a, u, i) {
406
406
  return await H({ response: d });
407
407
  },
408
408
  async sessionCookies({ user: t }) {
409
- if (!u.getSessionCookie)
409
+ if (!l.getSessionCookie)
410
410
  throw new Error(
411
411
  "[velojs/testing] sessionCookies/as requires `getSessionCookie` in createTestApp options."
412
412
  );
413
- return await u.getSessionCookie({ user: t });
413
+ return await l.getSessionCookie({ user: t });
414
414
  },
415
415
  as({ user: t }) {
416
- if (!u.getSessionCookie)
416
+ if (!l.getSessionCookie)
417
417
  throw new Error(
418
418
  "[velojs/testing] app.as() requires `getSessionCookie` in createTestApp options."
419
419
  );
420
420
  let o = null;
421
- const f = () => (o || (o = Promise.resolve(u.getSessionCookie({ user: t }))), o), d = (p) => (async (...n) => {
421
+ const f = () => (o || (o = Promise.resolve(l.getSessionCookie({ user: t }))), o), d = (p) => (async (...n) => {
422
422
  const s = await f(), [w, g] = n, v = {
423
423
  ...g ?? {},
424
424
  cookies: { ...s, ...g?.cookies ?? {} }
package/dist/types.d.ts CHANGED
@@ -24,9 +24,18 @@ export interface RouteModule {
24
24
  metadata?: Metadata;
25
25
  [key: `action_${string}`]: (args: ActionArgs<any>) => Promise<any>;
26
26
  }
27
+ export type HTTPMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
28
+ export interface EndpointHandlerArgs {
29
+ c: Context;
30
+ params: Record<string, string>;
31
+ query: Record<string, string>;
32
+ }
33
+ export type EndpointHandler = (args: EndpointHandlerArgs) => Response | Promise<Response>;
27
34
  export interface RouteNode {
28
35
  path?: string;
29
- module: RouteModule;
36
+ module?: RouteModule;
37
+ method?: HTTPMethod;
38
+ handler?: EndpointHandler;
30
39
  children?: RouteNode[];
31
40
  middlewares?: MiddlewareHandler[];
32
41
  isRoot?: boolean;
package/dist/vite.d.ts CHANGED
@@ -14,6 +14,16 @@ export declare function transformActionsForClient(code: string, moduleId: string
14
14
  export declare function transformStreamsForClient(code: string, moduleId: string): string;
15
15
  export declare function removeLoaders(code: string): string;
16
16
  export declare function removeMiddlewares(code: string): string;
17
+ /**
18
+ * On client builds, strip EndpointRoute objects from the default-export array
19
+ * of `routes.tsx` so server-only handler code (and its imports) don't ship to
20
+ * the browser. Mirrors the pattern used by `removeMiddlewares`.
21
+ *
22
+ * Detection: any ObjectExpression that has a `handler` property is treated as
23
+ * an endpoint and removed from its containing array. Imports referenced only
24
+ * by removed endpoints are dropped afterwards.
25
+ */
26
+ export declare function removeEndpointRoutes(code: string): string;
17
27
  export interface PathInfo {
18
28
  fullPath: string;
19
29
  path: string;