@analogjs/router 2.8.0-beta.9 → 2.8.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/fesm2022/analogjs-router-debug.page-MFY2u07E.mjs +119 -0
- package/fesm2022/analogjs-router-debug.page-MFY2u07E.mjs.map +1 -0
- package/fesm2022/analogjs-router-i18n.mjs +82 -219
- package/fesm2022/analogjs-router-i18n.mjs.map +1 -1
- package/fesm2022/analogjs-router-routes-L2tt-0ms.mjs +263 -0
- package/fesm2022/analogjs-router-routes-L2tt-0ms.mjs.map +1 -0
- package/fesm2022/analogjs-router-server-actions.mjs +228 -463
- package/fesm2022/analogjs-router-server-actions.mjs.map +1 -1
- package/fesm2022/analogjs-router-server.mjs +608 -838
- package/fesm2022/analogjs-router-server.mjs.map +1 -1
- package/fesm2022/analogjs-router-tanstack-query-server.mjs +35 -85
- package/fesm2022/analogjs-router-tanstack-query-server.mjs.map +1 -1
- package/fesm2022/analogjs-router-tanstack-query.mjs +79 -127
- package/fesm2022/analogjs-router-tanstack-query.mjs.map +1 -1
- package/fesm2022/analogjs-router-tokens.mjs +14 -19
- package/fesm2022/analogjs-router-tokens.mjs.map +1 -1
- package/fesm2022/analogjs-router.mjs +882 -1324
- package/fesm2022/analogjs-router.mjs.map +1 -1
- package/package.json +2 -2
- package/types/analogjs-router-i18n.d.ts +55 -24
- package/types/analogjs-router-server-actions.d.ts +98 -107
- package/types/analogjs-router-server.d.ts +76 -116
- package/types/analogjs-router-tanstack-query-server.d.ts +33 -37
- package/types/analogjs-router-tanstack-query.d.ts +19 -24
- package/types/analogjs-router-tokens.d.ts +26 -22
- package/types/analogjs-router.d.ts +528 -348
- package/fesm2022/analogjs-router-debug.page-COcFkgRu.mjs +0 -91
- package/fesm2022/analogjs-router-debug.page-COcFkgRu.mjs.map +0 -1
|
@@ -1,508 +1,273 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { getRequestURL, readBody, readFormData, toWebRequest } from "h3";
|
|
3
2
|
function fail(status, errors) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
37
|
+
return event.web?.request ?? toWebRequest(event);
|
|
50
38
|
}
|
|
51
39
|
function getRequestUrl(event) {
|
|
52
|
-
|
|
40
|
+
return event.web?.request?.url ?? getRequestURL(event).href;
|
|
53
41
|
}
|
|
54
42
|
function getContentType(event) {
|
|
55
|
-
|
|
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
|
-
|
|
61
|
-
|
|
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
|
-
|
|
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
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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
|
-
|
|
101
|
+
return schema["~standard"].validate(data);
|
|
139
102
|
}
|
|
140
|
-
|
|
141
103
|
function isDevEnvironment$1() {
|
|
142
|
-
|
|
143
|
-
(process.env['NODE_ENV'] === 'development' ||
|
|
144
|
-
process.env['NODE_ENV'] === 'test'));
|
|
104
|
+
return typeof process !== "undefined" && true;
|
|
145
105
|
}
|
|
146
106
|
function warnOnOutputIssues$1(issues) {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
? ` at "${i.path.map((p) => (typeof p === 'object' ? p.key : p)).join('.')}"`
|
|
152
|
-
: '';
|
|
153
|
-
return ` - ${i.message}${path}`;
|
|
154
|
-
})
|
|
155
|
-
.join('\n'));
|
|
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
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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$1()) {
|
|
253
|
-
const outputResult = await validateWithSchema(options.output, result);
|
|
254
|
-
if (outputResult.issues) {
|
|
255
|
-
warnOnOutputIssues$1(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
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
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
|
}
|
|
321
|
-
|
|
322
|
-
/**
|
|
323
|
-
* Creates a server action handler with Standard Schema input validation.
|
|
324
|
-
*
|
|
325
|
-
* Parses the request body (JSON or FormData) and validates it against the
|
|
326
|
-
* provided schema before invoking the handler. On validation failure,
|
|
327
|
-
* returns `fail(422, issues)` with `StandardSchemaV1.Issue[]`.
|
|
328
|
-
* Repeated form fields are preserved as arrays instead of being collapsed
|
|
329
|
-
* to the last value.
|
|
330
|
-
*
|
|
331
|
-
* @example
|
|
332
|
-
* ```typescript
|
|
333
|
-
* import { defineAction, json } from '@analogjs/router/server/actions';
|
|
334
|
-
* import * as v from 'valibot';
|
|
335
|
-
*
|
|
336
|
-
* const Schema = v.object({
|
|
337
|
-
* email: v.pipe(v.string(), v.email()),
|
|
338
|
-
* });
|
|
339
|
-
*
|
|
340
|
-
* export const action = defineAction({
|
|
341
|
-
* schema: Schema,
|
|
342
|
-
* handler: async ({ data }) => {
|
|
343
|
-
* // data is typed as { email: string }
|
|
344
|
-
* return json({ ok: true });
|
|
345
|
-
* },
|
|
346
|
-
* });
|
|
347
|
-
* ```
|
|
348
|
-
*/
|
|
349
184
|
function defineAction(options) {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
return handleValidatedRequest(ctx, options, rawParams);
|
|
363
|
-
};
|
|
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
|
+
};
|
|
364
197
|
}
|
|
365
198
|
async function handleValidatedRequest(ctx, options, params) {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
event: ctx.event,
|
|
382
|
-
});
|
|
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
|
+
});
|
|
383
214
|
}
|
|
384
|
-
|
|
385
215
|
function isDevEnvironment() {
|
|
386
|
-
|
|
387
|
-
(process.env['NODE_ENV'] === 'development' ||
|
|
388
|
-
process.env['NODE_ENV'] === 'test'));
|
|
216
|
+
return typeof process !== "undefined" && true;
|
|
389
217
|
}
|
|
390
218
|
function warnOnOutputIssues(issues) {
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
? ` at "${i.path.map((p) => (typeof p === 'object' ? p.key : p)).join('.')}"`
|
|
396
|
-
: '';
|
|
397
|
-
return ` - ${i.message}${path}`;
|
|
398
|
-
})
|
|
399
|
-
.join('\n'));
|
|
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"));
|
|
400
223
|
}
|
|
401
|
-
/**
|
|
402
|
-
* Creates an h3-compatible event handler with Standard Schema validation.
|
|
403
|
-
*
|
|
404
|
-
* - `input` schema validates the request body (POST/PUT/PATCH) or query
|
|
405
|
-
* params (GET). Returns 422 with `StandardSchemaV1.Issue[]` on failure.
|
|
406
|
-
* - `output` schema validates the response in development and tests only. Logs a warning on mismatch.
|
|
407
|
-
* - Plain return values are serialized with `json(...)`; raw `Response`
|
|
408
|
-
* objects are returned unchanged.
|
|
409
|
-
*
|
|
410
|
-
* @example
|
|
411
|
-
* ```typescript
|
|
412
|
-
* import { defineApiRoute } from '@analogjs/router/server/actions';
|
|
413
|
-
* import * as v from 'valibot';
|
|
414
|
-
*
|
|
415
|
-
* const Input = v.object({
|
|
416
|
-
* name: v.pipe(v.string(), v.minLength(1)),
|
|
417
|
-
* email: v.pipe(v.string(), v.email()),
|
|
418
|
-
* });
|
|
419
|
-
* const Output = v.object({
|
|
420
|
-
* id: v.string(),
|
|
421
|
-
* name: v.string(),
|
|
422
|
-
* });
|
|
423
|
-
*
|
|
424
|
-
* export default defineApiRoute({
|
|
425
|
-
* input: Input,
|
|
426
|
-
* output: Output,
|
|
427
|
-
* handler: async ({ data }) => {
|
|
428
|
-
* const user = await db.users.create(data);
|
|
429
|
-
* return user;
|
|
430
|
-
* },
|
|
431
|
-
* });
|
|
432
|
-
* ```
|
|
433
|
-
*/
|
|
434
224
|
function defineApiRoute(options) {
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
? body
|
|
481
|
-
: query;
|
|
482
|
-
}
|
|
483
|
-
const result = await options.handler({
|
|
484
|
-
data: data,
|
|
485
|
-
query: query,
|
|
486
|
-
body: body,
|
|
487
|
-
params: params,
|
|
488
|
-
event,
|
|
489
|
-
});
|
|
490
|
-
if (result instanceof Response) {
|
|
491
|
-
return result;
|
|
492
|
-
}
|
|
493
|
-
if (options.output && isDevEnvironment()) {
|
|
494
|
-
const outputResult = await validateWithSchema(options.output, result);
|
|
495
|
-
if (outputResult.issues) {
|
|
496
|
-
warnOnOutputIssues(outputResult.issues);
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
return json(result);
|
|
500
|
-
};
|
|
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
|
+
};
|
|
501
270
|
}
|
|
502
|
-
|
|
503
|
-
/**
|
|
504
|
-
* Generated bundle index. Do not edit.
|
|
505
|
-
*/
|
|
506
|
-
|
|
507
271
|
export { defineAction, defineApiRoute, definePageLoad, defineServerRoute, fail, json, redirect, validateWithSchema };
|
|
508
|
-
|
|
272
|
+
|
|
273
|
+
//# sourceMappingURL=analogjs-router-server-actions.mjs.map
|