@analogjs/router 2.8.0-beta.1 → 2.8.0-beta.11

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.
Files changed (28) hide show
  1. package/fesm2022/analogjs-router-debug.page-MFY2u07E.mjs +119 -0
  2. package/fesm2022/analogjs-router-debug.page-MFY2u07E.mjs.map +1 -0
  3. package/fesm2022/analogjs-router-i18n.mjs +82 -210
  4. package/fesm2022/analogjs-router-i18n.mjs.map +1 -1
  5. package/fesm2022/analogjs-router-routes-L2tt-0ms.mjs +263 -0
  6. package/fesm2022/analogjs-router-routes-L2tt-0ms.mjs.map +1 -0
  7. package/fesm2022/analogjs-router-server-actions.mjs +240 -294
  8. package/fesm2022/analogjs-router-server-actions.mjs.map +1 -1
  9. package/fesm2022/analogjs-router-server.mjs +387 -832
  10. package/fesm2022/analogjs-router-server.mjs.map +1 -1
  11. package/fesm2022/analogjs-router-tanstack-query-server.mjs +35 -85
  12. package/fesm2022/analogjs-router-tanstack-query-server.mjs.map +1 -1
  13. package/fesm2022/analogjs-router-tanstack-query.mjs +79 -127
  14. package/fesm2022/analogjs-router-tanstack-query.mjs.map +1 -1
  15. package/fesm2022/analogjs-router-tokens.mjs +14 -19
  16. package/fesm2022/analogjs-router-tokens.mjs.map +1 -1
  17. package/fesm2022/analogjs-router.mjs +777 -1187
  18. package/fesm2022/analogjs-router.mjs.map +1 -1
  19. package/package.json +2 -2
  20. package/types/analogjs-router-i18n.d.ts +55 -24
  21. package/types/analogjs-router-server-actions.d.ts +179 -90
  22. package/types/analogjs-router-server.d.ts +76 -116
  23. package/types/analogjs-router-tanstack-query-server.d.ts +33 -37
  24. package/types/analogjs-router-tanstack-query.d.ts +19 -24
  25. package/types/analogjs-router-tokens.d.ts +18 -22
  26. package/types/analogjs-router.d.ts +271 -251
  27. package/fesm2022/analogjs-router-debug.page-COcFkgRu.mjs +0 -91
  28. package/fesm2022/analogjs-router-debug.page-COcFkgRu.mjs.map +0 -1
@@ -1,327 +1,273 @@
1
- import { toWebRequest, getRequestURL, readBody, readFormData } from 'h3';
2
-
1
+ import { getRequestURL, readBody, readFormData, toWebRequest } from "h3";
3
2
  function fail(status, errors) {
4
- return new Response(JSON.stringify(errors), {
5
- status,
6
- headers: {
7
- 'X-Analog-Errors': 'true',
8
- },
9
- });
3
+ return new Response(JSON.stringify(errors), {
4
+ status,
5
+ headers: { "X-Analog-Errors": "true" }
6
+ });
10
7
  }
11
8
  function json(data, config) {
12
- return new Response(JSON.stringify(data), {
13
- headers: {
14
- 'Content-Type': 'application/json; charset=utf-8',
15
- },
16
- ...config,
17
- });
9
+ return new Response(JSON.stringify(data), {
10
+ headers: { "Content-Type": "application/json; charset=utf-8" },
11
+ ...config
12
+ });
18
13
  }
19
14
  function redirect(url, config = 302) {
20
- if (typeof config === 'number') {
21
- return new Response(null, {
22
- status: config,
23
- headers: {
24
- Location: `${url}`,
25
- },
26
- });
27
- }
28
- return new Response(null, {
29
- headers: {
30
- Location: `${url}`,
31
- },
32
- ...config,
33
- });
15
+ if (typeof config === "number") return new Response(null, {
16
+ status: config,
17
+ headers: { Location: `${url}` }
18
+ });
19
+ return new Response(null, {
20
+ headers: { Location: `${url}` },
21
+ ...config
22
+ });
34
23
  }
35
-
36
24
  function appendEntry(target, key, value) {
37
- const existingValue = target[key];
38
- if (existingValue === undefined) {
39
- target[key] = value;
40
- return;
41
- }
42
- if (Array.isArray(existingValue)) {
43
- existingValue.push(value);
44
- return;
45
- }
46
- target[key] = [existingValue, value];
25
+ const existingValue = target[key];
26
+ if (existingValue === void 0) {
27
+ target[key] = value;
28
+ return;
29
+ }
30
+ if (Array.isArray(existingValue)) {
31
+ existingValue.push(value);
32
+ return;
33
+ }
34
+ target[key] = [existingValue, value];
47
35
  }
48
36
  function getRequest(event) {
49
- return event.web?.request ?? toWebRequest(event);
37
+ return event.web?.request ?? toWebRequest(event);
50
38
  }
51
39
  function getRequestUrl(event) {
52
- return event.web?.request?.url ?? getRequestURL(event).href;
40
+ return event.web?.request?.url ?? getRequestURL(event).href;
53
41
  }
54
42
  function getContentType(event) {
55
- return (event.web?.request?.headers.get('content-type') ??
56
- event.headers.get('content-type') ??
57
- '');
43
+ return event.web?.request?.headers.get("content-type") ?? event.headers.get("content-type") ?? "";
58
44
  }
59
45
  function isJsonContentType(contentType) {
60
- const mimeType = contentType.split(';', 1)[0]?.trim().toLowerCase() ?? '';
61
- return mimeType === 'application/json' || mimeType.endsWith('+json');
46
+ const mimeType = contentType.split(";", 1)[0]?.trim().toLowerCase() ?? "";
47
+ return mimeType === "application/json" || mimeType.endsWith("+json");
62
48
  }
63
49
  function isFormContentType(contentType) {
64
- return (contentType.includes('multipart/form-data') ||
65
- contentType.includes('application/x-www-form-urlencoded'));
50
+ return contentType.includes("multipart/form-data") || contentType.includes("application/x-www-form-urlencoded");
66
51
  }
67
52
  function parseSearchParams(searchParams) {
68
- const result = {};
69
- searchParams.forEach((value, key) => {
70
- appendEntry(result, key, value);
71
- });
72
- return result;
53
+ const result = Object.create(null);
54
+ searchParams.forEach((value, key) => {
55
+ appendEntry(result, key, value);
56
+ });
57
+ return result;
73
58
  }
74
59
  function parseFormData(formData) {
75
- const result = {};
76
- formData.forEach((value, key) => {
77
- appendEntry(result, key, value);
78
- });
79
- return result;
60
+ const result = Object.create(null);
61
+ formData.forEach((value, key) => {
62
+ appendEntry(result, key, value);
63
+ });
64
+ return result;
80
65
  }
81
66
  async function parseRequestData(event) {
82
- const method = event.method.toUpperCase();
83
- if (method === 'GET' || method === 'HEAD') {
84
- const url = new URL(getRequestUrl(event), 'http://localhost');
85
- return parseSearchParams(url.searchParams);
86
- }
87
- const contentType = getContentType(event);
88
- if (isJsonContentType(contentType)) {
89
- try {
90
- return (await readBody(event)) ?? {};
91
- }
92
- catch {
93
- try {
94
- return await getRequest(event).json();
95
- }
96
- catch {
97
- return {};
98
- }
99
- }
100
- }
101
- if (isFormContentType(contentType)) {
102
- try {
103
- return parseFormData(await readFormData(event));
104
- }
105
- catch {
106
- const request = getRequest(event);
107
- if (typeof request.formData === 'function') {
108
- try {
109
- return parseFormData(await request.formData());
110
- }
111
- catch {
112
- return {};
113
- }
114
- }
115
- return {};
116
- }
117
- }
118
- try {
119
- return (await readBody(event)) ?? {};
120
- }
121
- catch {
122
- try {
123
- return await getRequest(event).json();
124
- }
125
- catch {
126
- return {};
127
- }
128
- }
67
+ const method = event.method.toUpperCase();
68
+ if (method === "GET" || method === "HEAD") return parseSearchParams(new URL(getRequestUrl(event), "http://localhost").searchParams);
69
+ const contentType = getContentType(event);
70
+ if (isJsonContentType(contentType)) try {
71
+ return await readBody(event) ?? {};
72
+ } catch {
73
+ try {
74
+ return await getRequest(event).json();
75
+ } catch {
76
+ return {};
77
+ }
78
+ }
79
+ if (isFormContentType(contentType)) try {
80
+ return parseFormData(await readFormData(event));
81
+ } catch {
82
+ const request = getRequest(event);
83
+ if (typeof request.formData === "function") try {
84
+ return parseFormData(await request.formData());
85
+ } catch {
86
+ return {};
87
+ }
88
+ return {};
89
+ }
90
+ try {
91
+ return await readBody(event) ?? {};
92
+ } catch {
93
+ try {
94
+ return await getRequest(event).json();
95
+ } catch {
96
+ return {};
97
+ }
98
+ }
129
99
  }
130
-
131
- /**
132
- * Validates unknown input against a Standard Schema.
133
- *
134
- * Handles both sync and async `validate` implementations — the Standard
135
- * Schema spec allows either return shape.
136
- */
137
100
  async function validateWithSchema(schema, data) {
138
- return schema['~standard'].validate(data);
101
+ return schema["~standard"].validate(data);
139
102
  }
140
-
141
- function isDevEnvironment() {
142
- return (typeof process !== 'undefined' &&
143
- (process.env['NODE_ENV'] === 'development' ||
144
- process.env['NODE_ENV'] === 'test'));
103
+ function isDevEnvironment$1() {
104
+ return typeof process !== "undefined" && true;
145
105
  }
146
- function warnOnOutputIssues(issues) {
147
- console.warn(`[analog] Server route output validation failed:\n` +
148
- issues
149
- .map((i) => {
150
- const path = i.path
151
- ? ` at "${i.path.map((p) => (typeof p === 'object' ? p.key : p)).join('.')}"`
152
- : '';
153
- return ` - ${i.message}${path}`;
154
- })
155
- .join('\n'));
106
+ function warnOnOutputIssues$1(issues) {
107
+ console.warn(`[analog] Server route output validation failed:\n` + issues.map((i) => {
108
+ const path = i.path ? ` at "${i.path.map((p) => typeof p === "object" ? p.key : p).join(".")}"` : "";
109
+ return ` - ${i.message}${path}`;
110
+ }).join("\n"));
156
111
  }
157
- /**
158
- * Creates an h3-compatible event handler with Standard Schema validation.
159
- *
160
- * - `input` schema validates the request body (POST/PUT/PATCH) or query
161
- * params (GET). Returns 422 with `StandardSchemaV1.Issue[]` on failure.
162
- * - `output` schema validates the response in development only (stripped
163
- * in production for zero overhead). Logs a warning on mismatch.
164
- * - Plain return values are serialized with `json(...)`; raw `Response`
165
- * objects are returned unchanged.
166
- *
167
- * @example
168
- * ```typescript
169
- * import { defineServerRoute } from '@analogjs/router/server/actions';
170
- * import * as v from 'valibot';
171
- *
172
- * const Input = v.object({
173
- * name: v.pipe(v.string(), v.minLength(1)),
174
- * email: v.pipe(v.string(), v.email()),
175
- * });
176
- * const Output = v.object({
177
- * id: v.string(),
178
- * name: v.string(),
179
- * });
180
- *
181
- * export default defineServerRoute({
182
- * input: Input,
183
- * output: Output,
184
- * handler: async ({ data }) => {
185
- * const user = await db.users.create(data);
186
- * return user;
187
- * },
188
- * });
189
- * ```
190
- */
191
112
  function defineServerRoute(options) {
192
- return (async (event) => {
193
- const method = event.method.toUpperCase();
194
- let data;
195
- let query;
196
- let body;
197
- let params = event.context?.params ?? {};
198
- if (options.params) {
199
- const paramsResult = await validateWithSchema(options.params, params);
200
- if (paramsResult.issues) {
201
- return fail(422, paramsResult.issues);
202
- }
203
- params = paramsResult.value;
204
- }
205
- const hasBody = method !== 'GET' && method !== 'HEAD';
206
- const requestData = options.input || (options.body && hasBody)
207
- ? await parseRequestData(event)
208
- : undefined;
209
- if (options.input) {
210
- const inputResult = await validateWithSchema(options.input, requestData);
211
- if (inputResult.issues) {
212
- return fail(422, inputResult.issues);
213
- }
214
- data = inputResult.value;
215
- }
216
- if (options.query) {
217
- const url = new URL(getRequestUrl(event), 'http://localhost');
218
- const queryResult = await validateWithSchema(options.query, parseSearchParams(url.searchParams));
219
- if (queryResult.issues) {
220
- return fail(422, queryResult.issues);
221
- }
222
- query = queryResult.value;
223
- }
224
- if (options.body && hasBody) {
225
- const bodyResult = await validateWithSchema(options.body, requestData);
226
- if (bodyResult.issues) {
227
- return fail(422, bodyResult.issues);
228
- }
229
- body = bodyResult.value;
230
- }
231
- if (!options.input) {
232
- if (!hasBody) {
233
- data = query;
234
- }
235
- else if (body !== undefined) {
236
- data = body;
237
- }
238
- else {
239
- data = query;
240
- }
241
- }
242
- const result = await options.handler({
243
- data: data,
244
- query: query,
245
- body: body,
246
- params: params,
247
- event,
248
- });
249
- if (result instanceof Response) {
250
- return result;
251
- }
252
- if (options.output && isDevEnvironment()) {
253
- const outputResult = await validateWithSchema(options.output, result);
254
- if (outputResult.issues) {
255
- warnOnOutputIssues(outputResult.issues);
256
- }
257
- }
258
- return json(result);
259
- });
113
+ return (async (event) => {
114
+ const method = event.method.toUpperCase();
115
+ let data;
116
+ let query;
117
+ let body;
118
+ let params = event.context?.params ?? {};
119
+ if (options.params) {
120
+ const paramsResult = await validateWithSchema(options.params, params);
121
+ if (paramsResult.issues) return fail(422, paramsResult.issues);
122
+ params = paramsResult.value;
123
+ }
124
+ const hasBody = method !== "GET" && method !== "HEAD";
125
+ const requestData = options.input || options.body && hasBody ? await parseRequestData(event) : void 0;
126
+ if (options.input) {
127
+ const inputResult = await validateWithSchema(options.input, requestData);
128
+ if (inputResult.issues) return fail(422, inputResult.issues);
129
+ data = inputResult.value;
130
+ }
131
+ if (options.query) {
132
+ const url = new URL(getRequestUrl(event), "http://localhost");
133
+ const queryResult = await validateWithSchema(options.query, parseSearchParams(url.searchParams));
134
+ if (queryResult.issues) return fail(422, queryResult.issues);
135
+ query = queryResult.value;
136
+ }
137
+ if (options.body && hasBody) {
138
+ const bodyResult = await validateWithSchema(options.body, requestData);
139
+ if (bodyResult.issues) return fail(422, bodyResult.issues);
140
+ body = bodyResult.value;
141
+ }
142
+ if (!options.input) if (!hasBody) data = query;
143
+ else if (body !== void 0) data = body;
144
+ else data = query;
145
+ const result = await options.handler({
146
+ data,
147
+ query,
148
+ body,
149
+ params,
150
+ event
151
+ });
152
+ if (result instanceof Response) return result;
153
+ if (options.output && isDevEnvironment$1()) {
154
+ const outputResult = await validateWithSchema(options.output, result);
155
+ if (outputResult.issues) warnOnOutputIssues$1(outputResult.issues);
156
+ }
157
+ return json(result);
158
+ });
260
159
  }
261
-
262
- /**
263
- * Creates a typed page server load function with optional
264
- * Standard Schema validation for route params and query.
265
- *
266
- * Follows the same validation pattern as `defineServerRoute`:
267
- * validates before invoking the handler and returns
268
- * `fail(422, issues)` on validation failure.
269
- *
270
- * @example
271
- * ```typescript
272
- * // src/app/pages/users/[id].server.ts
273
- * import { definePageLoad } from '@analogjs/router/server/actions';
274
- * import * as v from 'valibot';
275
- *
276
- * export const routeParamsSchema = v.object({
277
- * id: v.pipe(v.string(), v.regex(/^\d+$/)),
278
- * });
279
- *
280
- * export const load = definePageLoad({
281
- * params: routeParamsSchema,
282
- * handler: async ({ params, fetch }) => {
283
- * // params.id is typed as string (validated)
284
- * const user = await fetch(`/api/users/${params.id}`);
285
- * return user;
286
- * },
287
- * });
288
- * ```
289
- */
290
160
  function definePageLoad(options) {
291
- return async (ctx) => {
292
- let params = ctx.params ?? {};
293
- const url = new URL(getRequestUrl(ctx.event), 'http://localhost');
294
- let query = parseSearchParams(url.searchParams);
295
- // Validate params
296
- if (options.params) {
297
- const result = await validateWithSchema(options.params, params);
298
- if (result.issues) {
299
- return fail(422, result.issues);
300
- }
301
- params = result.value;
302
- }
303
- // Validate query
304
- if (options.query) {
305
- const result = await validateWithSchema(options.query, query);
306
- if (result.issues) {
307
- return fail(422, result.issues);
308
- }
309
- query = result.value;
310
- }
311
- return options.handler({
312
- params: params,
313
- query: query,
314
- req: ctx.req,
315
- res: ctx.res,
316
- fetch: ctx.fetch,
317
- event: ctx.event,
318
- });
319
- };
161
+ return async (ctx) => {
162
+ let params = ctx.params ?? {};
163
+ let query = parseSearchParams(new URL(getRequestUrl(ctx.event), "http://localhost").searchParams);
164
+ if (options.params) {
165
+ const result = await validateWithSchema(options.params, params);
166
+ if (result.issues) return fail(422, result.issues);
167
+ params = result.value;
168
+ }
169
+ if (options.query) {
170
+ const result = await validateWithSchema(options.query, query);
171
+ if (result.issues) return fail(422, result.issues);
172
+ query = result.value;
173
+ }
174
+ return options.handler({
175
+ params,
176
+ query,
177
+ req: ctx.req,
178
+ res: ctx.res,
179
+ fetch: ctx.fetch,
180
+ event: ctx.event
181
+ });
182
+ };
320
183
  }
184
+ function defineAction(options) {
185
+ function getParams(params) {
186
+ return params ?? {};
187
+ }
188
+ return async (ctx) => {
189
+ const rawParams = getParams(ctx.params);
190
+ if (options.params) {
191
+ const paramsResult = await validateWithSchema(options.params, rawParams);
192
+ if (paramsResult.issues) return fail(422, paramsResult.issues);
193
+ return handleValidatedRequest(ctx, options, paramsResult.value);
194
+ }
195
+ return handleValidatedRequest(ctx, options, rawParams);
196
+ };
197
+ }
198
+ async function handleValidatedRequest(ctx, options, params) {
199
+ const body = await parseRequestData(ctx.event);
200
+ let data = body;
201
+ if (options.schema) {
202
+ const result = await validateWithSchema(options.schema, body);
203
+ if (result.issues) return fail(422, result.issues);
204
+ data = result.value;
205
+ }
206
+ return options.handler({
207
+ data,
208
+ params,
209
+ req: ctx.req,
210
+ res: ctx.res,
211
+ fetch: ctx.fetch,
212
+ event: ctx.event
213
+ });
214
+ }
215
+ function isDevEnvironment() {
216
+ return typeof process !== "undefined" && true;
217
+ }
218
+ function warnOnOutputIssues(issues) {
219
+ console.warn(`[analog] API route output validation failed:\n` + issues.map((i) => {
220
+ const path = i.path ? ` at "${i.path.map((p) => typeof p === "object" ? p.key : p).join(".")}"` : "";
221
+ return ` - ${i.message}${path}`;
222
+ }).join("\n"));
223
+ }
224
+ function defineApiRoute(options) {
225
+ return async (event) => {
226
+ const method = event.method.toUpperCase();
227
+ let data;
228
+ let query;
229
+ let body;
230
+ let params = event.context?.params ?? {};
231
+ if (options.params) {
232
+ const paramsResult = await validateWithSchema(options.params, params);
233
+ if (paramsResult.issues) return fail(422, paramsResult.issues);
234
+ params = paramsResult.value;
235
+ }
236
+ const requestData = options.input || options.body && method !== "GET" && method !== "HEAD" ? await parseRequestData(event) : void 0;
237
+ if (options.input) {
238
+ data = requestData;
239
+ const inputResult = await validateWithSchema(options.input, data);
240
+ if (inputResult.issues) return fail(422, inputResult.issues);
241
+ data = inputResult.value;
242
+ }
243
+ if (options.query) {
244
+ const url = new URL(getRequestUrl(event), "http://localhost");
245
+ const queryResult = await validateWithSchema(options.query, parseSearchParams(url.searchParams));
246
+ if (queryResult.issues) return fail(422, queryResult.issues);
247
+ query = queryResult.value;
248
+ }
249
+ if (options.body && method !== "GET" && method !== "HEAD") {
250
+ body = requestData;
251
+ const bodyResult = await validateWithSchema(options.body, body);
252
+ if (bodyResult.issues) return fail(422, bodyResult.issues);
253
+ body = bodyResult.value;
254
+ }
255
+ if (!options.input) data = method === "GET" || method === "HEAD" ? query : body !== void 0 ? body : query;
256
+ const result = await options.handler({
257
+ data,
258
+ query,
259
+ body,
260
+ params,
261
+ event
262
+ });
263
+ if (result instanceof Response) return result;
264
+ if (options.output && isDevEnvironment()) {
265
+ const outputResult = await validateWithSchema(options.output, result);
266
+ if (outputResult.issues) warnOnOutputIssues(outputResult.issues);
267
+ }
268
+ return json(result);
269
+ };
270
+ }
271
+ export { defineAction, defineApiRoute, definePageLoad, defineServerRoute, fail, json, redirect, validateWithSchema };
321
272
 
322
- /**
323
- * Generated bundle index. Do not edit.
324
- */
325
-
326
- export { definePageLoad, defineServerRoute, fail, json, redirect, validateWithSchema };
327
- //# sourceMappingURL=analogjs-router-server-actions.mjs.map
273
+ //# sourceMappingURL=analogjs-router-server-actions.mjs.map