@analogjs/router 3.0.0-alpha.56 → 3.0.0-alpha.58
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-server-actions.mjs +1 -334
- package/fesm2022/analogjs-router-tanstack-query-server.mjs +64 -15
- package/fesm2022/analogjs-router-tanstack-query-server.mjs.map +1 -1
- package/fesm2022/analogjs-router.mjs +6 -6
- package/fesm2022/analogjs-router.mjs.map +1 -1
- package/fesm2022/debug.page.mjs +3 -3
- package/fesm2022/provide-analog-query.mjs +51 -2
- package/fesm2022/provide-analog-query.mjs.map +1 -1
- package/fesm2022/src.mjs +335 -0
- package/fesm2022/src.mjs.map +1 -0
- package/package.json +7 -7
- package/types/tanstack-query/server/src/define-page-load-queries.d.ts +66 -0
- package/types/tanstack-query/server/src/index.d.ts +2 -0
- package/types/tanstack-query/src/constants.d.ts +9 -0
- package/fesm2022/analogjs-router-server-actions.mjs.map +0 -1
|
@@ -1,335 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
//#region packages/router/server/actions/src/actions.ts
|
|
3
|
-
function fail(status, errors) {
|
|
4
|
-
return new Response(JSON.stringify(errors), {
|
|
5
|
-
status,
|
|
6
|
-
headers: { "X-Analog-Errors": "true" }
|
|
7
|
-
});
|
|
8
|
-
}
|
|
9
|
-
function json(data, config) {
|
|
10
|
-
return new Response(JSON.stringify(data), {
|
|
11
|
-
headers: { "Content-Type": "application/json; charset=utf-8" },
|
|
12
|
-
...config
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
function redirect(url, config = 302) {
|
|
16
|
-
if (typeof config === "number") return new Response(null, {
|
|
17
|
-
status: config,
|
|
18
|
-
headers: { Location: `${url}` }
|
|
19
|
-
});
|
|
20
|
-
return new Response(null, {
|
|
21
|
-
headers: { Location: `${url}` },
|
|
22
|
-
...config
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
//#endregion
|
|
26
|
-
//#region packages/router/server/actions/src/parse-request-data.ts
|
|
27
|
-
function appendEntry(target, key, value) {
|
|
28
|
-
const existingValue = target[key];
|
|
29
|
-
if (existingValue === void 0) {
|
|
30
|
-
target[key] = value;
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
if (Array.isArray(existingValue)) {
|
|
34
|
-
existingValue.push(value);
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
target[key] = [existingValue, value];
|
|
38
|
-
}
|
|
39
|
-
function getRequest(event) {
|
|
40
|
-
const maybeRequest = event.request;
|
|
41
|
-
if (maybeRequest) return maybeRequest;
|
|
42
|
-
return toRequest(event);
|
|
43
|
-
}
|
|
44
|
-
function getContentType(event) {
|
|
45
|
-
return getRequest(event).headers.get("content-type") ?? event.headers.get("content-type") ?? event.headers.get("Content-Type") ?? "";
|
|
46
|
-
}
|
|
47
|
-
function isJsonContentType(contentType) {
|
|
48
|
-
const mimeType = contentType.split(";", 1)[0]?.trim().toLowerCase() ?? "";
|
|
49
|
-
return mimeType === "application/json" || mimeType.endsWith("+json");
|
|
50
|
-
}
|
|
51
|
-
function isFormContentType(contentType) {
|
|
52
|
-
return contentType.includes("multipart/form-data") || contentType.includes("application/x-www-form-urlencoded");
|
|
53
|
-
}
|
|
54
|
-
function parseSearchParams(searchParams) {
|
|
55
|
-
const result = {};
|
|
56
|
-
searchParams.forEach((value, key) => {
|
|
57
|
-
appendEntry(result, key, value);
|
|
58
|
-
});
|
|
59
|
-
return result;
|
|
60
|
-
}
|
|
61
|
-
function parseFormData(formData) {
|
|
62
|
-
const result = {};
|
|
63
|
-
formData.forEach((value, key) => {
|
|
64
|
-
appendEntry(result, key, value);
|
|
65
|
-
});
|
|
66
|
-
return result;
|
|
67
|
-
}
|
|
68
|
-
async function parseRequestData(event) {
|
|
69
|
-
const request = getRequest(event);
|
|
70
|
-
const httpEvent = event;
|
|
71
|
-
const h3Event = event;
|
|
72
|
-
const method = event.method.toUpperCase();
|
|
73
|
-
if (method === "GET" || method === "HEAD") return parseSearchParams(new URL(request.url, "http://localhost").searchParams);
|
|
74
|
-
const contentType = getContentType(event);
|
|
75
|
-
if (isJsonContentType(contentType)) try {
|
|
76
|
-
return await readBody(httpEvent) ?? {};
|
|
77
|
-
} catch {
|
|
78
|
-
try {
|
|
79
|
-
return await request.json();
|
|
80
|
-
} catch {
|
|
81
|
-
return {};
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
if (isFormContentType(contentType)) try {
|
|
85
|
-
return parseFormData(await readFormData(h3Event));
|
|
86
|
-
} catch {
|
|
87
|
-
if (typeof request.formData === "function") return parseFormData(await request.formData());
|
|
88
|
-
return {};
|
|
89
|
-
}
|
|
90
|
-
try {
|
|
91
|
-
return await readBody(httpEvent) ?? {};
|
|
92
|
-
} catch {
|
|
93
|
-
try {
|
|
94
|
-
return await request.json();
|
|
95
|
-
} catch {
|
|
96
|
-
return {};
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
//#endregion
|
|
101
|
-
//#region packages/router/server/actions/src/validate.ts
|
|
102
|
-
/**
|
|
103
|
-
* Validates unknown input against a Standard Schema.
|
|
104
|
-
*
|
|
105
|
-
* Handles both sync and async `validate` implementations — the Standard
|
|
106
|
-
* Schema spec allows either return shape.
|
|
107
|
-
*/
|
|
108
|
-
async function validateWithSchema(schema, data) {
|
|
109
|
-
return schema["~standard"].validate(data);
|
|
110
|
-
}
|
|
111
|
-
//#endregion
|
|
112
|
-
//#region packages/router/server/actions/src/define-action.ts
|
|
113
|
-
/**
|
|
114
|
-
* Creates a server action handler with Standard Schema input validation.
|
|
115
|
-
*
|
|
116
|
-
* Parses the request body (JSON or FormData) and validates it against the
|
|
117
|
-
* provided schema before invoking the handler. On validation failure,
|
|
118
|
-
* returns `fail(422, issues)` with `StandardSchemaV1.Issue[]`.
|
|
119
|
-
* Repeated form fields are preserved as arrays instead of being collapsed
|
|
120
|
-
* to the last value.
|
|
121
|
-
*
|
|
122
|
-
* @example
|
|
123
|
-
* ```typescript
|
|
124
|
-
* import { defineAction, json } from '@analogjs/router/server/actions';
|
|
125
|
-
* import * as v from 'valibot';
|
|
126
|
-
*
|
|
127
|
-
* const Schema = v.object({
|
|
128
|
-
* email: v.pipe(v.string(), v.email()),
|
|
129
|
-
* });
|
|
130
|
-
*
|
|
131
|
-
* export const action = defineAction({
|
|
132
|
-
* schema: Schema,
|
|
133
|
-
* handler: async ({ data }) => {
|
|
134
|
-
* // data is typed as { email: string }
|
|
135
|
-
* return json({ ok: true });
|
|
136
|
-
* },
|
|
137
|
-
* });
|
|
138
|
-
* ```
|
|
139
|
-
*/
|
|
140
|
-
function defineAction(options) {
|
|
141
|
-
function getParams(params) {
|
|
142
|
-
return params ?? {};
|
|
143
|
-
}
|
|
144
|
-
return async (ctx) => {
|
|
145
|
-
const rawParams = getParams(ctx.params);
|
|
146
|
-
if (options.params) {
|
|
147
|
-
const paramsResult = await validateWithSchema(options.params, rawParams);
|
|
148
|
-
if (paramsResult.issues) return fail(422, paramsResult.issues);
|
|
149
|
-
return handleValidatedRequest(ctx, options, paramsResult.value);
|
|
150
|
-
}
|
|
151
|
-
return handleValidatedRequest(ctx, options, rawParams);
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
async function handleValidatedRequest(ctx, options, params) {
|
|
155
|
-
const body = await parseRequestData(ctx.event);
|
|
156
|
-
let data = body;
|
|
157
|
-
if (options.schema) {
|
|
158
|
-
const result = await validateWithSchema(options.schema, body);
|
|
159
|
-
if (result.issues) return fail(422, result.issues);
|
|
160
|
-
data = result.value;
|
|
161
|
-
}
|
|
162
|
-
return options.handler({
|
|
163
|
-
data,
|
|
164
|
-
params,
|
|
165
|
-
req: ctx.req,
|
|
166
|
-
res: ctx.res,
|
|
167
|
-
fetch: ctx.fetch,
|
|
168
|
-
event: ctx.event
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
//#endregion
|
|
172
|
-
//#region packages/router/server/actions/src/define-server-route.ts
|
|
173
|
-
function isDevEnvironment() {
|
|
174
|
-
return typeof process !== "undefined" && (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test");
|
|
175
|
-
}
|
|
176
|
-
function warnOnOutputIssues(issues) {
|
|
177
|
-
console.warn(`[analog] Server route output validation failed:\n` + issues.map((i) => {
|
|
178
|
-
const path = i.path ? ` at "${i.path.map((p) => typeof p === "object" ? p.key : p).join(".")}"` : "";
|
|
179
|
-
return ` - ${i.message}${path}`;
|
|
180
|
-
}).join("\n"));
|
|
181
|
-
}
|
|
182
|
-
function getRequestUrl(event) {
|
|
183
|
-
try {
|
|
184
|
-
return getRequestURL(event).href;
|
|
185
|
-
} catch {
|
|
186
|
-
return event.request.url;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Creates an h3-compatible event handler with Standard Schema validation.
|
|
191
|
-
*
|
|
192
|
-
* - `input` schema validates the request body (POST/PUT/PATCH) or query
|
|
193
|
-
* params (GET). Returns 422 with `StandardSchemaV1.Issue[]` on failure.
|
|
194
|
-
* - `output` schema validates the response in development only (stripped
|
|
195
|
-
* in production for zero overhead). Logs a warning on mismatch.
|
|
196
|
-
* - Plain return values are serialized with `json(...)`; raw `Response`
|
|
197
|
-
* objects are returned unchanged.
|
|
198
|
-
*
|
|
199
|
-
* @example
|
|
200
|
-
* ```typescript
|
|
201
|
-
* import { defineServerRoute } from '@analogjs/router/server/actions';
|
|
202
|
-
* import * as v from 'valibot';
|
|
203
|
-
*
|
|
204
|
-
* const Input = v.object({
|
|
205
|
-
* name: v.pipe(v.string(), v.minLength(1)),
|
|
206
|
-
* email: v.pipe(v.string(), v.email()),
|
|
207
|
-
* });
|
|
208
|
-
* const Output = v.object({
|
|
209
|
-
* id: v.string(),
|
|
210
|
-
* name: v.string(),
|
|
211
|
-
* });
|
|
212
|
-
*
|
|
213
|
-
* export default defineServerRoute({
|
|
214
|
-
* input: Input,
|
|
215
|
-
* output: Output,
|
|
216
|
-
* handler: async ({ data }) => {
|
|
217
|
-
* const user = await db.users.create(data);
|
|
218
|
-
* return user;
|
|
219
|
-
* },
|
|
220
|
-
* });
|
|
221
|
-
* ```
|
|
222
|
-
*/
|
|
223
|
-
function defineServerRoute(options) {
|
|
224
|
-
return (async (event) => {
|
|
225
|
-
const method = event.method.toUpperCase();
|
|
226
|
-
let data;
|
|
227
|
-
let query;
|
|
228
|
-
let body;
|
|
229
|
-
let params = event.context?.params ?? {};
|
|
230
|
-
if (options.params) {
|
|
231
|
-
const paramsResult = await validateWithSchema(options.params, params);
|
|
232
|
-
if (paramsResult.issues) return fail(422, paramsResult.issues);
|
|
233
|
-
params = paramsResult.value;
|
|
234
|
-
}
|
|
235
|
-
if (options.input) {
|
|
236
|
-
data = await parseRequestData(event);
|
|
237
|
-
const inputResult = await validateWithSchema(options.input, data);
|
|
238
|
-
if (inputResult.issues) return fail(422, inputResult.issues);
|
|
239
|
-
data = inputResult.value;
|
|
240
|
-
} else {
|
|
241
|
-
if (options.query) {
|
|
242
|
-
const url = new URL(getRequestUrl(event), "http://localhost");
|
|
243
|
-
const queryResult = await validateWithSchema(options.query, parseSearchParams(url.searchParams));
|
|
244
|
-
if (queryResult.issues) return fail(422, queryResult.issues);
|
|
245
|
-
query = queryResult.value;
|
|
246
|
-
}
|
|
247
|
-
if (options.body && method !== "GET" && method !== "HEAD") {
|
|
248
|
-
body = await parseRequestData(event);
|
|
249
|
-
const bodyResult = await validateWithSchema(options.body, body);
|
|
250
|
-
if (bodyResult.issues) return fail(422, bodyResult.issues);
|
|
251
|
-
body = bodyResult.value;
|
|
252
|
-
}
|
|
253
|
-
if (method === "GET" || method === "HEAD") data = query;
|
|
254
|
-
else if (body !== void 0) data = body;
|
|
255
|
-
else data = query;
|
|
256
|
-
}
|
|
257
|
-
const result = await options.handler({
|
|
258
|
-
data,
|
|
259
|
-
query,
|
|
260
|
-
body,
|
|
261
|
-
params,
|
|
262
|
-
event
|
|
263
|
-
});
|
|
264
|
-
if (result instanceof Response) return result;
|
|
265
|
-
if (options.output && isDevEnvironment()) {
|
|
266
|
-
const outputResult = await validateWithSchema(options.output, result);
|
|
267
|
-
if (outputResult.issues) warnOnOutputIssues(outputResult.issues);
|
|
268
|
-
}
|
|
269
|
-
return json(result);
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
//#endregion
|
|
273
|
-
//#region packages/router/server/actions/src/define-page-load.ts
|
|
274
|
-
/**
|
|
275
|
-
* Creates a typed page server load function with optional
|
|
276
|
-
* Standard Schema validation for route params and query.
|
|
277
|
-
*
|
|
278
|
-
* Follows the same validation patterns as `defineAction` and
|
|
279
|
-
* `defineServerRoute`: validates before invoking the handler,
|
|
280
|
-
* returns `fail(422, issues)` on validation failure.
|
|
281
|
-
*
|
|
282
|
-
* @example
|
|
283
|
-
* ```typescript
|
|
284
|
-
* // src/app/pages/users/[id].server.ts
|
|
285
|
-
* import { definePageLoad } from '@analogjs/router/server/actions';
|
|
286
|
-
* import * as v from 'valibot';
|
|
287
|
-
*
|
|
288
|
-
* export const routeParamsSchema = v.object({
|
|
289
|
-
* id: v.pipe(v.string(), v.regex(/^\d+$/)),
|
|
290
|
-
* });
|
|
291
|
-
*
|
|
292
|
-
* export const load = definePageLoad({
|
|
293
|
-
* params: routeParamsSchema,
|
|
294
|
-
* handler: async ({ params, fetch }) => {
|
|
295
|
-
* // params.id is typed as string (validated)
|
|
296
|
-
* const user = await fetch(`/api/users/${params.id}`);
|
|
297
|
-
* return user;
|
|
298
|
-
* },
|
|
299
|
-
* });
|
|
300
|
-
* ```
|
|
301
|
-
*/
|
|
302
|
-
function definePageLoad(options) {
|
|
303
|
-
return async (ctx) => {
|
|
304
|
-
let params = ctx.params ?? {};
|
|
305
|
-
let requestUrl;
|
|
306
|
-
try {
|
|
307
|
-
requestUrl = getRequestURL(ctx.event).href;
|
|
308
|
-
} catch {
|
|
309
|
-
requestUrl = ctx.event.request.url;
|
|
310
|
-
}
|
|
311
|
-
let query = parseSearchParams(new URL(requestUrl, "http://localhost").searchParams);
|
|
312
|
-
if (options.params) {
|
|
313
|
-
const result = await validateWithSchema(options.params, params);
|
|
314
|
-
if (result.issues) return fail(422, result.issues);
|
|
315
|
-
params = result.value;
|
|
316
|
-
}
|
|
317
|
-
if (options.query) {
|
|
318
|
-
const result = await validateWithSchema(options.query, query);
|
|
319
|
-
if (result.issues) return fail(422, result.issues);
|
|
320
|
-
query = result.value;
|
|
321
|
-
}
|
|
322
|
-
return options.handler({
|
|
323
|
-
params,
|
|
324
|
-
query,
|
|
325
|
-
req: ctx.req,
|
|
326
|
-
res: ctx.res,
|
|
327
|
-
fetch: ctx.fetch,
|
|
328
|
-
event: ctx.event
|
|
329
|
-
});
|
|
330
|
-
};
|
|
331
|
-
}
|
|
332
|
-
//#endregion
|
|
1
|
+
import { a as fail, i as validateWithSchema, n as defineServerRoute, o as json, r as defineAction, s as redirect, t as definePageLoad } from "./src.mjs";
|
|
333
2
|
export { defineAction, definePageLoad, defineServerRoute, fail, json, redirect, validateWithSchema };
|
|
334
|
-
|
|
335
|
-
//# sourceMappingURL=analogjs-router-server-actions.mjs.map
|
|
@@ -1,22 +1,71 @@
|
|
|
1
|
-
import { t as ANALOG_QUERY_STATE_KEY } from "./provide-analog-query.mjs";
|
|
2
|
-
import {
|
|
1
|
+
import { r as ANALOG_QUERIES_KEY, t as ANALOG_QUERY_STATE_KEY } from "./provide-analog-query.mjs";
|
|
2
|
+
import { t as definePageLoad } from "./src.mjs";
|
|
3
|
+
import { ApplicationRef, ENVIRONMENT_INITIALIZER, TransferState, inject, makeEnvironmentProviders } from "@angular/core";
|
|
4
|
+
import { filter, skipWhile, take } from "rxjs/operators";
|
|
3
5
|
import { QueryClient, dehydrate } from "@tanstack/angular-query-experimental";
|
|
4
|
-
import { BEFORE_APP_SERIALIZED } from "@angular/platform-server";
|
|
5
6
|
//#region packages/router/tanstack-query/src/provide-server-analog-query.ts
|
|
6
|
-
var SERVER_ANALOG_QUERY_PROVIDER = {
|
|
7
|
-
provide: BEFORE_APP_SERIALIZED,
|
|
8
|
-
multi: true,
|
|
9
|
-
useFactory: (queryClient, transferState) => {
|
|
10
|
-
return () => {
|
|
11
|
-
transferState.set(ANALOG_QUERY_STATE_KEY, dehydrate(queryClient));
|
|
12
|
-
};
|
|
13
|
-
},
|
|
14
|
-
deps: [QueryClient, TransferState]
|
|
15
|
-
};
|
|
16
7
|
function provideServerAnalogQuery() {
|
|
17
|
-
return makeEnvironmentProviders([
|
|
8
|
+
return makeEnvironmentProviders([{
|
|
9
|
+
provide: ENVIRONMENT_INITIALIZER,
|
|
10
|
+
multi: true,
|
|
11
|
+
useValue() {
|
|
12
|
+
const appRef = inject(ApplicationRef);
|
|
13
|
+
const queryClient = inject(QueryClient);
|
|
14
|
+
const transferState = inject(TransferState);
|
|
15
|
+
const subscription = appRef.isStable.pipe(skipWhile((stable) => stable), filter((stable) => stable), take(1)).subscribe(() => {
|
|
16
|
+
transferState.set(ANALOG_QUERY_STATE_KEY, dehydrate(queryClient));
|
|
17
|
+
});
|
|
18
|
+
appRef.onDestroy(() => subscription.unsubscribe());
|
|
19
|
+
}
|
|
20
|
+
}]);
|
|
18
21
|
}
|
|
19
22
|
//#endregion
|
|
20
|
-
|
|
23
|
+
//#region packages/router/tanstack-query/server/src/define-page-load-queries.ts
|
|
24
|
+
/**
|
|
25
|
+
* Page load helper that prefetches TanStack Query queries inside the
|
|
26
|
+
* `.server.ts` handler and ships the dehydrated cache alongside any
|
|
27
|
+
* additional data. The router-side hydrator in `provideAnalogQuery()`
|
|
28
|
+
* merges the dehydrated payload into the active `QueryClient` on
|
|
29
|
+
* `ResolveEnd`, so components reading the same query options see a
|
|
30
|
+
* warm cache on first render.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* // src/app/pages/posts.server.ts
|
|
35
|
+
* import { definePageLoadQueries } from '@analogjs/router/tanstack-query/server';
|
|
36
|
+
* import { queryOptions } from '@tanstack/angular-query-experimental';
|
|
37
|
+
*
|
|
38
|
+
* export const postsQuery = queryOptions({
|
|
39
|
+
* queryKey: ['posts'],
|
|
40
|
+
* queryFn: async ({ signal }) =>
|
|
41
|
+
* fetch('https://api.example.com/posts', { signal }).then((r) => r.json()),
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* export const load = definePageLoadQueries({
|
|
45
|
+
* handler: async ({ client }) => {
|
|
46
|
+
* await client.prefetchQuery(postsQuery);
|
|
47
|
+
* },
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
function definePageLoadQueries(options) {
|
|
52
|
+
return definePageLoad({
|
|
53
|
+
params: options.params,
|
|
54
|
+
query: options.query,
|
|
55
|
+
handler: async (ctx) => {
|
|
56
|
+
const client = options.client?.() ?? new QueryClient();
|
|
57
|
+
const data = await options.handler({
|
|
58
|
+
...ctx,
|
|
59
|
+
client
|
|
60
|
+
});
|
|
61
|
+
return {
|
|
62
|
+
[ANALOG_QUERIES_KEY]: dehydrate(client),
|
|
63
|
+
data
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
//#endregion
|
|
69
|
+
export { ANALOG_QUERIES_KEY, definePageLoadQueries, provideServerAnalogQuery };
|
|
21
70
|
|
|
22
71
|
//# sourceMappingURL=analogjs-router-tanstack-query-server.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analogjs-router-tanstack-query-server.mjs","names":[],"sources":["../../tanstack-query/src/provide-server-analog-query.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"analogjs-router-tanstack-query-server.mjs","names":[],"sources":["../../tanstack-query/src/provide-server-analog-query.ts","../../tanstack-query/server/src/define-page-load-queries.ts"],"sourcesContent":["import {\n ApplicationRef,\n ENVIRONMENT_INITIALIZER,\n TransferState,\n inject,\n makeEnvironmentProviders,\n} from '@angular/core';\nimport type { EnvironmentProviders } from '@angular/core';\nimport { filter, skipWhile, take } from 'rxjs/operators';\nimport { QueryClient, dehydrate } from '@tanstack/angular-query-experimental';\n\nimport { ANALOG_QUERY_STATE_KEY } from './provide-analog-query';\n\nexport function provideServerAnalogQuery(): EnvironmentProviders {\n return makeEnvironmentProviders([\n {\n provide: ENVIRONMENT_INITIALIZER,\n multi: true,\n useValue() {\n // Dehydrate the QueryClient into `TransferState` once the app\n // becomes stable POST-RENDER on the server. `renderApplication`\n // awaits `whenStable()` itself before invoking\n // `BEFORE_APP_SERIALIZED` (which is where Angular's\n // `TRANSFER_STATE_SERIALIZATION_PROVIDERS` reads the store and\n // writes the `ng-state` script). Settling our write at the same\n // point — but ahead of Angular's own subscriber — lets the\n // dehydrated cache land in `TransferState` before the serializer\n // snapshots it, so component-issued queries make it to the client\n // without depending on provider declaration order.\n //\n // `skipWhile(stable => stable)` waits past the initial \"no\n // pending tasks yet\" emission `BehaviorSubject` semantics give\n // us at subscribe time; the next stable transition is the\n // post-render one with all queries settled.\n const appRef = inject(ApplicationRef);\n const queryClient = inject(QueryClient);\n const transferState = inject(TransferState);\n\n const subscription = appRef.isStable\n .pipe(\n skipWhile((stable) => stable),\n filter((stable) => stable),\n take(1),\n )\n .subscribe(() => {\n transferState.set(ANALOG_QUERY_STATE_KEY, dehydrate(queryClient));\n });\n\n appRef.onDestroy(() => subscription.unsubscribe());\n },\n },\n ]);\n}\n","import type { StandardSchemaV1 } from '@standard-schema/spec';\nimport { QueryClient, dehydrate } from '@tanstack/angular-query-experimental';\nimport type { DehydratedState } from '@tanstack/angular-query-experimental';\nimport type { H3Event, H3EventContext } from 'nitro/h3';\nimport type { $Fetch } from 'nitro/types';\n\nimport {\n definePageLoad,\n type PageLoadContext,\n} from '../../../server/actions/src/index.js';\nimport { ANALOG_QUERIES_KEY } from '../../src/constants.js';\n\nexport { ANALOG_QUERIES_KEY } from '../../src/constants.js';\n\ntype NodeContext = NonNullable<H3Event['node']>;\ntype OptionalSchema = StandardSchemaV1 | undefined;\n\nexport interface PageLoadQueriesResult<TData> {\n __analogQueries: DehydratedState;\n data: TData;\n}\n\nexport interface DefinePageLoadQueriesOptions<\n TParamsSchema extends OptionalSchema,\n TQuerySchema extends OptionalSchema,\n TData,\n> {\n params?: TParamsSchema;\n query?: TQuerySchema;\n /**\n * Optional QueryClient factory. Defaults to `new QueryClient()`.\n * Override to set `defaultOptions` (e.g. `queries: { staleTime: Infinity }`).\n */\n client?: () => QueryClient;\n /**\n * Handler receives the standard PageLoadContext plus a per-request\n * QueryClient. Use `client.prefetchQuery` / `ensureQueryData` /\n * `prefetchInfiniteQuery` to warm the cache; the dehydrated client\n * is returned as `__analogQueries` on the load result. Anything you\n * return from the handler becomes `data` on the same result.\n */\n handler: (\n ctx: PageLoadContext<TParamsSchema, TQuerySchema> & { client: QueryClient },\n ) => Promise<TData> | TData;\n}\n\n/**\n * Page load helper that prefetches TanStack Query queries inside the\n * `.server.ts` handler and ships the dehydrated cache alongside any\n * additional data. The router-side hydrator in `provideAnalogQuery()`\n * merges the dehydrated payload into the active `QueryClient` on\n * `ResolveEnd`, so components reading the same query options see a\n * warm cache on first render.\n *\n * @example\n * ```ts\n * // src/app/pages/posts.server.ts\n * import { definePageLoadQueries } from '@analogjs/router/tanstack-query/server';\n * import { queryOptions } from '@tanstack/angular-query-experimental';\n *\n * export const postsQuery = queryOptions({\n * queryKey: ['posts'],\n * queryFn: async ({ signal }) =>\n * fetch('https://api.example.com/posts', { signal }).then((r) => r.json()),\n * });\n *\n * export const load = definePageLoadQueries({\n * handler: async ({ client }) => {\n * await client.prefetchQuery(postsQuery);\n * },\n * });\n * ```\n */\nexport function definePageLoadQueries<\n TParamsSchema extends OptionalSchema = undefined,\n TQuerySchema extends OptionalSchema = undefined,\n TData = void,\n>(\n options: DefinePageLoadQueriesOptions<TParamsSchema, TQuerySchema, TData>,\n): (ctx: {\n params: H3EventContext['params'];\n req: NodeContext['req'];\n res: NonNullable<NodeContext['res']>;\n fetch: $Fetch;\n event: H3Event;\n}) => Promise<PageLoadQueriesResult<TData> | Response> {\n return definePageLoad({\n params: options.params,\n query: options.query,\n handler: async (ctx): Promise<PageLoadQueriesResult<TData>> => {\n const client = options.client?.() ?? new QueryClient();\n const data = await options.handler({ ...ctx, client });\n return {\n [ANALOG_QUERIES_KEY]: dehydrate(client),\n data,\n };\n },\n });\n}\n"],"mappings":";;;;;;AAaA,SAAgB,2BAAiD;AAC/D,QAAO,yBACL,CACE;EACO,SAAA;EACP,OAAW;EAgBH,WAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACuCvB,SAAgB,sBAKd,SAOqD;AACrD,QAAO,eAAe;EACpB,QAAQ,QAAQ;EAChB,OAAO,QAAQ;EACf,SAAS,OAAO,QAA+C;GACvD,MAAS,SAAQ,QAAA,UAAkB,IAAA,IAAA,aAAa;GAChD,MAAO,OAAM,MAAQ,QAAQ,QAAA;IAAA,GAAA;IAAA;IAAA,CAAA;AAAK,UAAA;KAAK,qBAAA,UAAA,OAAA;IAAS;IAC/C;;EAEL,CAAA"}
|
|
@@ -356,7 +356,7 @@ var FormAction = class FormAction {
|
|
|
356
356
|
static {
|
|
357
357
|
this.ɵfac = i0.ɵɵngDeclareFactory({
|
|
358
358
|
minVersion: "12.0.0",
|
|
359
|
-
version: "
|
|
359
|
+
version: "22.0.0",
|
|
360
360
|
ngImport: i0,
|
|
361
361
|
type: FormAction,
|
|
362
362
|
deps: [],
|
|
@@ -366,7 +366,7 @@ var FormAction = class FormAction {
|
|
|
366
366
|
static {
|
|
367
367
|
this.ɵdir = i0.ɵɵngDeclareDirective({
|
|
368
368
|
minVersion: "17.1.0",
|
|
369
|
-
version: "
|
|
369
|
+
version: "22.0.0",
|
|
370
370
|
type: FormAction,
|
|
371
371
|
isStandalone: true,
|
|
372
372
|
selector: "form[action],form[method]",
|
|
@@ -395,7 +395,7 @@ var FormAction = class FormAction {
|
|
|
395
395
|
};
|
|
396
396
|
i0.ɵɵngDeclareClassMetadata({
|
|
397
397
|
minVersion: "12.0.0",
|
|
398
|
-
version: "
|
|
398
|
+
version: "22.0.0",
|
|
399
399
|
ngImport: i0,
|
|
400
400
|
type: FormAction,
|
|
401
401
|
decorators: [{
|
|
@@ -513,7 +513,7 @@ var ServerOnly = class ServerOnly {
|
|
|
513
513
|
static {
|
|
514
514
|
this.ɵfac = i0.ɵɵngDeclareFactory({
|
|
515
515
|
minVersion: "12.0.0",
|
|
516
|
-
version: "
|
|
516
|
+
version: "22.0.0",
|
|
517
517
|
ngImport: i0,
|
|
518
518
|
type: ServerOnly,
|
|
519
519
|
deps: [],
|
|
@@ -523,7 +523,7 @@ var ServerOnly = class ServerOnly {
|
|
|
523
523
|
static {
|
|
524
524
|
this.ɵcmp = i0.ɵɵngDeclareComponent({
|
|
525
525
|
minVersion: "17.1.0",
|
|
526
|
-
version: "
|
|
526
|
+
version: "22.0.0",
|
|
527
527
|
type: ServerOnly,
|
|
528
528
|
isStandalone: true,
|
|
529
529
|
selector: "server-only,ServerOnly,Server",
|
|
@@ -553,7 +553,7 @@ var ServerOnly = class ServerOnly {
|
|
|
553
553
|
};
|
|
554
554
|
i0.ɵɵngDeclareClassMetadata({
|
|
555
555
|
minVersion: "12.0.0",
|
|
556
|
-
version: "
|
|
556
|
+
version: "22.0.0",
|
|
557
557
|
ngImport: i0,
|
|
558
558
|
type: ServerOnly,
|
|
559
559
|
decorators: [{
|