@fragno-dev/forms 0.0.1

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.
@@ -0,0 +1,1950 @@
1
+ import { defineFragment, defineRoutes } from "@fragno-dev/core";
2
+ import { z } from "zod";
3
+ import "@cfworker/json-schema";
4
+
5
+ //#region ../fragno/dist/api/route.js
6
+ /**
7
+ * Helper to resolve route factories into routes
8
+ * @internal
9
+ */
10
+ function resolveRouteFactories(context, routesOrFactories) {
11
+ const routes$1 = [];
12
+ for (const item of routesOrFactories) if (typeof item === "function") {
13
+ const factoryRoutes = item(context);
14
+ routes$1.push(...factoryRoutes);
15
+ } else routes$1.push(item);
16
+ return routes$1;
17
+ }
18
+
19
+ //#endregion
20
+ //#region ../fragno/dist/api/internal/route.js
21
+ function getMountRoute(opts) {
22
+ const mountRoute = opts.mountRoute ?? `/api/${opts.name}`;
23
+ if (mountRoute.endsWith("/")) return mountRoute.slice(0, -1);
24
+ return mountRoute;
25
+ }
26
+
27
+ //#endregion
28
+ //#region ../fragno/dist/api/error.js
29
+ var FragnoApiError = class extends Error {
30
+ #status;
31
+ #code;
32
+ constructor({ message, code }, status) {
33
+ super(message);
34
+ this.name = "FragnoApiError";
35
+ this.#status = status;
36
+ this.#code = code;
37
+ }
38
+ get status() {
39
+ return this.#status;
40
+ }
41
+ get code() {
42
+ return this.#code;
43
+ }
44
+ toResponse() {
45
+ return Response.json({
46
+ message: this.message,
47
+ code: this.code
48
+ }, { status: this.status });
49
+ }
50
+ };
51
+ var FragnoApiValidationError = class extends FragnoApiError {
52
+ #issues;
53
+ constructor(message, issues) {
54
+ super({
55
+ message,
56
+ code: "FRAGNO_VALIDATION_ERROR"
57
+ }, 400);
58
+ this.name = "FragnoApiValidationError";
59
+ this.#issues = issues;
60
+ }
61
+ get issues() {
62
+ return this.#issues;
63
+ }
64
+ toResponse() {
65
+ return Response.json({
66
+ message: this.message,
67
+ issues: this.#issues,
68
+ code: this.code
69
+ }, { status: this.status });
70
+ }
71
+ };
72
+
73
+ //#endregion
74
+ //#region ../fragno/dist/api/request-input-context.js
75
+ var RequestInputContext = class RequestInputContext$1 {
76
+ #path;
77
+ #method;
78
+ #pathParams;
79
+ #searchParams;
80
+ #headers;
81
+ #body;
82
+ #parsedBody;
83
+ #inputSchema;
84
+ #shouldValidateInput;
85
+ constructor(config) {
86
+ this.#path = config.path;
87
+ this.#method = config.method;
88
+ this.#pathParams = config.pathParams;
89
+ this.#searchParams = config.searchParams;
90
+ this.#headers = config.headers;
91
+ this.#body = config.rawBody;
92
+ this.#parsedBody = config.parsedBody;
93
+ this.#inputSchema = config.inputSchema;
94
+ this.#shouldValidateInput = config.shouldValidateInput ?? true;
95
+ }
96
+ /**
97
+ * Create a RequestContext from a Request object for server-side handling
98
+ */
99
+ static async fromRequest(config) {
100
+ return new RequestInputContext$1({
101
+ method: config.method,
102
+ path: config.path,
103
+ pathParams: config.state.pathParams,
104
+ searchParams: config.state.searchParams,
105
+ headers: config.state.headers,
106
+ parsedBody: config.state.body,
107
+ rawBody: config.rawBody,
108
+ inputSchema: config.inputSchema,
109
+ shouldValidateInput: config.shouldValidateInput
110
+ });
111
+ }
112
+ /**
113
+ * Create a RequestContext for server-side rendering contexts (no Request object)
114
+ */
115
+ static fromSSRContext(config) {
116
+ return new RequestInputContext$1({
117
+ method: config.method,
118
+ path: config.path,
119
+ pathParams: config.pathParams,
120
+ searchParams: config.searchParams ?? new URLSearchParams(),
121
+ headers: config.headers ?? new Headers(),
122
+ parsedBody: "body" in config ? config.body : void 0,
123
+ inputSchema: "inputSchema" in config ? config.inputSchema : void 0,
124
+ shouldValidateInput: false
125
+ });
126
+ }
127
+ /**
128
+ * The HTTP method as string (e.g., `GET`, `POST`)
129
+ */
130
+ get method() {
131
+ return this.#method;
132
+ }
133
+ /**
134
+ * The matched route path (e.g., `/users/:id`)
135
+ * @remarks `string`
136
+ */
137
+ get path() {
138
+ return this.#path;
139
+ }
140
+ /**
141
+ * Extracted path parameters as object (e.g., `{ id: '123' }`)
142
+ * @remarks `Record<string, string>`
143
+ */
144
+ get pathParams() {
145
+ return this.#pathParams;
146
+ }
147
+ /**
148
+ * [URLSearchParams](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) object for query parameters
149
+ * @remarks `URLSearchParams`
150
+ */
151
+ get query() {
152
+ return this.#searchParams;
153
+ }
154
+ /**
155
+ * [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) object for request headers
156
+ * @remarks `Headers`
157
+ */
158
+ get headers() {
159
+ return this.#headers;
160
+ }
161
+ get rawBody() {
162
+ return this.#body;
163
+ }
164
+ /**
165
+ * Input validation context (only if inputSchema is defined)
166
+ * @remarks `InputContext`
167
+ */
168
+ get input() {
169
+ if (!this.#inputSchema) return;
170
+ return {
171
+ schema: this.#inputSchema,
172
+ valid: async () => {
173
+ if (!this.#shouldValidateInput) return this.#parsedBody;
174
+ return this.#validateInput();
175
+ }
176
+ };
177
+ }
178
+ async #validateInput() {
179
+ if (!this.#inputSchema) throw new Error("No input schema defined for this route");
180
+ if (this.#parsedBody instanceof FormData || this.#parsedBody instanceof Blob) throw new Error("Schema validation is only supported for JSON data, not FormData or Blob");
181
+ const result = await this.#inputSchema["~standard"].validate(this.#parsedBody);
182
+ if (result.issues) throw new FragnoApiValidationError("Validation failed", result.issues);
183
+ return result.value;
184
+ }
185
+ };
186
+
187
+ //#endregion
188
+ //#region ../fragno/dist/api/internal/response-stream.js
189
+ var ResponseStream = class {
190
+ #writer;
191
+ #encoder;
192
+ #abortSubscribers = [];
193
+ #responseReadable;
194
+ #aborted = false;
195
+ #closed = false;
196
+ /**
197
+ * Whether the stream has been aborted.
198
+ */
199
+ get aborted() {
200
+ return this.#aborted;
201
+ }
202
+ /**
203
+ * Whether the stream has been closed normally.
204
+ */
205
+ get closed() {
206
+ return this.#closed;
207
+ }
208
+ /**
209
+ * The readable stream that the response is piped to.
210
+ */
211
+ get responseReadable() {
212
+ return this.#responseReadable;
213
+ }
214
+ constructor(writable, readable) {
215
+ this.#writer = writable.getWriter();
216
+ this.#encoder = new TextEncoder();
217
+ const reader = readable.getReader();
218
+ this.#abortSubscribers.push(async () => {
219
+ await reader.cancel();
220
+ });
221
+ this.#responseReadable = new ReadableStream({
222
+ async pull(controller) {
223
+ const { done, value } = await reader.read();
224
+ if (done) controller.close();
225
+ else controller.enqueue(value);
226
+ },
227
+ cancel: () => {
228
+ this.abort();
229
+ }
230
+ });
231
+ }
232
+ async writeRaw(input) {
233
+ try {
234
+ if (typeof input === "string") input = this.#encoder.encode(input);
235
+ await this.#writer.write(input);
236
+ } catch {}
237
+ }
238
+ write(input) {
239
+ return this.writeRaw(JSON.stringify(input) + "\n");
240
+ }
241
+ sleep(ms) {
242
+ return new Promise((res) => setTimeout(res, ms));
243
+ }
244
+ async close() {
245
+ try {
246
+ await this.#writer.close();
247
+ } catch {} finally {
248
+ this.#closed = true;
249
+ }
250
+ }
251
+ onAbort(listener) {
252
+ this.#abortSubscribers.push(listener);
253
+ }
254
+ /**
255
+ * Abort the stream.
256
+ * You can call this method when stream is aborted by external event.
257
+ */
258
+ abort() {
259
+ if (!this.aborted) {
260
+ this.#aborted = true;
261
+ this.#abortSubscribers.forEach((subscriber) => subscriber());
262
+ }
263
+ }
264
+ };
265
+
266
+ //#endregion
267
+ //#region ../fragno/dist/api/request-output-context.js
268
+ /**
269
+ * Utility function to merge headers from multiple sources.
270
+ * Later headers override earlier ones.
271
+ */
272
+ function mergeHeaders$1(...headerSources) {
273
+ const mergedHeaders = new Headers();
274
+ for (const headerSource of headerSources) {
275
+ if (!headerSource) continue;
276
+ if (headerSource instanceof Headers) for (const [key, value] of headerSource.entries()) mergedHeaders.set(key, value);
277
+ else if (Array.isArray(headerSource)) for (const [key, value] of headerSource) mergedHeaders.set(key, value);
278
+ else for (const [key, value] of Object.entries(headerSource)) mergedHeaders.set(key, value);
279
+ }
280
+ return mergedHeaders;
281
+ }
282
+ var OutputContext = class {
283
+ /**
284
+ * Creates an error response.
285
+ *
286
+ * Shortcut for `throw new FragnoApiError(...)`
287
+ */
288
+ error = ({ message, code }, initOrStatus, headers) => {
289
+ if (typeof initOrStatus === "undefined") return Response.json({
290
+ message,
291
+ code
292
+ }, {
293
+ status: 500,
294
+ headers
295
+ });
296
+ if (typeof initOrStatus === "number") return Response.json({
297
+ message,
298
+ code
299
+ }, {
300
+ status: initOrStatus,
301
+ headers
302
+ });
303
+ const mergedHeaders = mergeHeaders$1(initOrStatus.headers, headers);
304
+ return Response.json({
305
+ message,
306
+ code
307
+ }, {
308
+ status: initOrStatus.status,
309
+ headers: mergedHeaders
310
+ });
311
+ };
312
+ empty = (initOrStatus, headers) => {
313
+ const defaultHeaders = {};
314
+ if (typeof initOrStatus === "undefined") {
315
+ const mergedHeaders$1 = mergeHeaders$1(defaultHeaders, headers);
316
+ return new Response(null, {
317
+ status: 201,
318
+ headers: mergedHeaders$1
319
+ });
320
+ }
321
+ if (typeof initOrStatus === "number") {
322
+ const mergedHeaders$1 = mergeHeaders$1(defaultHeaders, headers);
323
+ return new Response(null, {
324
+ status: initOrStatus,
325
+ headers: mergedHeaders$1
326
+ });
327
+ }
328
+ const mergedHeaders = mergeHeaders$1(defaultHeaders, initOrStatus.headers, headers);
329
+ return new Response(null, {
330
+ status: initOrStatus.status,
331
+ headers: mergedHeaders
332
+ });
333
+ };
334
+ json = (object, initOrStatus, headers) => {
335
+ if (typeof initOrStatus === "undefined") return Response.json(object, {
336
+ status: 200,
337
+ headers
338
+ });
339
+ if (typeof initOrStatus === "number") return Response.json(object, {
340
+ status: initOrStatus,
341
+ headers
342
+ });
343
+ const mergedHeaders = mergeHeaders$1(initOrStatus.headers, headers);
344
+ return Response.json(object, {
345
+ status: initOrStatus.status,
346
+ headers: mergedHeaders
347
+ });
348
+ };
349
+ jsonStream = (cb, { onError, headers } = {}) => {
350
+ const defaultHeaders = {
351
+ "content-type": "application/x-ndjson; charset=utf-8",
352
+ "transfer-encoding": "chunked",
353
+ "cache-control": "no-cache"
354
+ };
355
+ const { readable, writable } = new TransformStream();
356
+ const stream = new ResponseStream(writable, readable);
357
+ (async () => {
358
+ try {
359
+ await cb(stream);
360
+ } catch (e) {
361
+ if (e === void 0) {} else if (e instanceof Error && onError) await onError(e, stream);
362
+ else console.error(e);
363
+ } finally {
364
+ stream.close();
365
+ }
366
+ })();
367
+ return new Response(stream.responseReadable, {
368
+ status: 200,
369
+ headers: mergeHeaders$1(defaultHeaders, headers)
370
+ });
371
+ };
372
+ };
373
+ var RequestOutputContext = class extends OutputContext {
374
+ #outputSchema;
375
+ constructor(outputSchema) {
376
+ super();
377
+ this.#outputSchema = outputSchema;
378
+ }
379
+ };
380
+
381
+ //#endregion
382
+ //#region ../fragno/dist/api/internal/path.js
383
+ /**
384
+ * Extract parameter names from a path pattern at runtime.
385
+ * Examples:
386
+ * - "/users/:id" => ["id"]
387
+ * - "/files/**" => ["**"]
388
+ * - "/files/**:rest" => ["rest"]
389
+ */
390
+ function extractPathParams(pathPattern) {
391
+ const segments = pathPattern.split("/").filter((s) => s.length > 0);
392
+ const names = [];
393
+ for (const segment of segments) {
394
+ if (segment.startsWith(":")) {
395
+ names.push(segment.slice(1));
396
+ continue;
397
+ }
398
+ if (segment === "**") {
399
+ names.push("**");
400
+ continue;
401
+ }
402
+ if (segment.startsWith("**:")) {
403
+ names.push(segment.slice(3));
404
+ continue;
405
+ }
406
+ }
407
+ return names;
408
+ }
409
+ /**
410
+ * Build a concrete path by replacing placeholders in a path pattern with values.
411
+ *
412
+ * Supports the same placeholder syntax as the matcher:
413
+ * - Named parameter ":name" is URL-encoded as a single segment
414
+ * - Anonymous wildcard "**" inserts the remainder as-is (slashes preserved)
415
+ * - Named wildcard "**:name" inserts the remainder from the named key
416
+ *
417
+ * Examples:
418
+ * - buildPath("/users/:id", { id: "123" }) => "/users/123"
419
+ * - buildPath("/files/**", { "**": "a/b" }) => "/files/a/b"
420
+ * - buildPath("/files/**:rest", { rest: "a/b" }) => "/files/a/b"
421
+ */
422
+ function buildPath(pathPattern, params) {
423
+ const patternSegments = pathPattern.split("/");
424
+ const builtSegments = [];
425
+ for (const segment of patternSegments) {
426
+ if (segment.length === 0) {
427
+ builtSegments.push("");
428
+ continue;
429
+ }
430
+ if (segment.startsWith(":")) {
431
+ const name = segment.slice(1);
432
+ const value = params[name];
433
+ if (value === void 0) throw new Error(`Missing value for path parameter :${name}`);
434
+ builtSegments.push(encodeURIComponent(value));
435
+ continue;
436
+ }
437
+ if (segment === "**") {
438
+ const value = params["**"];
439
+ if (value === void 0) throw new Error("Missing value for path wildcard **");
440
+ builtSegments.push(value);
441
+ continue;
442
+ }
443
+ if (segment.startsWith("**:")) {
444
+ const name = segment.slice(3);
445
+ const value = params[name];
446
+ if (value === void 0) throw new Error(`Missing value for path wildcard **:${name}`);
447
+ builtSegments.push(value);
448
+ continue;
449
+ }
450
+ builtSegments.push(segment);
451
+ }
452
+ return builtSegments.join("/");
453
+ }
454
+
455
+ //#endregion
456
+ //#region ../fragno/dist/client/client-error.js
457
+ /**
458
+ * Base error class for all Fragno client errors.
459
+ */
460
+ var FragnoClientError = class extends Error {
461
+ #code;
462
+ constructor(message, code, options = {}) {
463
+ super(message, { cause: options.cause });
464
+ this.name = "FragnoClientError";
465
+ this.#code = code;
466
+ }
467
+ get code() {
468
+ return this.#code;
469
+ }
470
+ };
471
+ var FragnoClientFetchError = class extends FragnoClientError {
472
+ constructor(message, code, options = {}) {
473
+ super(message, code, options);
474
+ this.name = "FragnoClientFetchError";
475
+ }
476
+ static fromUnknownFetchError(error) {
477
+ if (!(error instanceof Error)) return new FragnoClientFetchNetworkError("Network request failed", { cause: error });
478
+ if (error.name === "AbortError") return new FragnoClientFetchAbortError("Request was aborted", { cause: error });
479
+ return new FragnoClientFetchNetworkError("Network request failed", { cause: error });
480
+ }
481
+ };
482
+ /**
483
+ * Error thrown when a network request fails (e.g., no internet connection, DNS failure).
484
+ */
485
+ var FragnoClientFetchNetworkError = class extends FragnoClientFetchError {
486
+ constructor(message = "Network request failed", options = {}) {
487
+ super(message, "NETWORK_ERROR", options);
488
+ this.name = "FragnoClientFetchNetworkError";
489
+ }
490
+ };
491
+ /**
492
+ * Error thrown when a request is aborted (e.g., user cancels request, timeout).
493
+ */
494
+ var FragnoClientFetchAbortError = class extends FragnoClientFetchError {
495
+ constructor(message = "Request was aborted", options = {}) {
496
+ super(message, "ABORT_ERROR", options);
497
+ this.name = "FragnoClientFetchAbortError";
498
+ }
499
+ };
500
+ /**
501
+ * Error thrown when the API result is unexpected, e.g. no json is returned.
502
+ */
503
+ var FragnoClientUnknownApiError = class extends FragnoClientError {
504
+ #status;
505
+ constructor(message = "Unknown API error", status, options = {}) {
506
+ super(message, "UNKNOWN_API_ERROR", options);
507
+ this.name = "FragnoClientUnknownApiError";
508
+ this.#status = status;
509
+ }
510
+ get status() {
511
+ return this.#status;
512
+ }
513
+ };
514
+ var FragnoClientApiError = class FragnoClientApiError$1 extends FragnoClientError {
515
+ #status;
516
+ constructor({ message, code }, status, options = {}) {
517
+ super(message, code, options);
518
+ this.name = "FragnoClientApiError";
519
+ this.#status = status;
520
+ }
521
+ get status() {
522
+ return this.#status;
523
+ }
524
+ /**
525
+ * The error code returned by the API.
526
+ *
527
+ * The type is `TErrorCode` (the set of known error codes for this route), but may also be a string
528
+ * for forward compatibility with future error codes.
529
+ */
530
+ get code() {
531
+ return super.code;
532
+ }
533
+ static async fromResponse(response) {
534
+ const unknown = await response.json();
535
+ const status = response.status;
536
+ if (!("message" in unknown || "code" in unknown)) return new FragnoClientUnknownApiError("Unknown API error", status);
537
+ if (!(typeof unknown.message === "string" && typeof unknown.code === "string")) return new FragnoClientUnknownApiError("Unknown API error", status);
538
+ return new FragnoClientApiError$1({
539
+ message: unknown.message,
540
+ code: unknown.code
541
+ }, status);
542
+ }
543
+ };
544
+
545
+ //#endregion
546
+ //#region ../fragno/dist/util/content-type.js
547
+ /**
548
+ * Parses a content-type header string into its components
549
+ *
550
+ * @param contentType - The content-type header value to parse
551
+ * @returns A ParsedContentType object or null if the input is invalid
552
+ *
553
+ * @example
554
+ * ```ts
555
+ * const { type, subtype, mediaType, parameters }
556
+ * = parseContentType("application/json; charset=utf-8");
557
+ * console.assert(type === "application");
558
+ * console.assert(subtype === "json");
559
+ * console.assert(mediaType === "application/json");
560
+ * console.assert(parameters["charset"] === "utf-8");
561
+ */
562
+ function parseContentType(contentType) {
563
+ if (!contentType || typeof contentType !== "string") return null;
564
+ const trimmed = contentType.trim();
565
+ if (!trimmed) return null;
566
+ const parts = trimmed.split(";").map((part) => part.trim());
567
+ const mediaType = parts[0];
568
+ if (!mediaType) return null;
569
+ const typeParts = mediaType.split("/");
570
+ if (typeParts.length !== 2) return null;
571
+ const [type, subtype] = typeParts.map((part) => part.trim().toLowerCase());
572
+ if (!type || !subtype) return null;
573
+ const parameters = {};
574
+ for (let i = 1; i < parts.length; i++) {
575
+ const param = parts[i];
576
+ const equalIndex = param.indexOf("=");
577
+ if (equalIndex > 0) {
578
+ const key = param.slice(0, equalIndex).trim().toLowerCase();
579
+ let value = param.slice(equalIndex + 1).trim();
580
+ if (value.startsWith("\"") && value.endsWith("\"")) value = value.slice(1, -1);
581
+ if (key) parameters[key] = value;
582
+ }
583
+ }
584
+ return {
585
+ type,
586
+ subtype,
587
+ mediaType: `${type}/${subtype}`,
588
+ parameters
589
+ };
590
+ }
591
+
592
+ //#endregion
593
+ //#region ../fragno/dist/client/internal/ndjson-streaming.js
594
+ /**
595
+ * Creates a promise that rejects when the abort signal is triggered
596
+ */
597
+ function createAbortPromise(abortSignal) {
598
+ return new Promise((_, reject) => {
599
+ const abortHandler = () => {
600
+ reject(new FragnoClientFetchAbortError("Operation was aborted"));
601
+ };
602
+ if (abortSignal.aborted) abortHandler();
603
+ else abortSignal.addEventListener("abort", abortHandler, { once: true });
604
+ });
605
+ }
606
+ /**
607
+ * Handles NDJSON streaming responses by returning the first item from the fetcher
608
+ * and then continuing to stream updates via the store's mutate method.
609
+ *
610
+ * This makes it so that we can wait until the first chunk before updating the store, if we did
611
+ * not do this, `loading` would briefly be false before the first item would be populated in the
612
+ * result.
613
+ *
614
+ * @param response - The fetch Response object containing the NDJSON stream
615
+ * @param store - The fetcher store to update with streaming data
616
+ * @param abortSignal - Optional AbortSignal to cancel the streaming operation
617
+ * @returns A promise that resolves to an object containing the first item and a streaming promise
618
+ */
619
+ async function handleNdjsonStreamingFirstItem(response, store, options = {}) {
620
+ if (!response.body) throw new FragnoClientFetchError("Streaming response has no body", "NO_BODY");
621
+ const { abortSignal } = options;
622
+ if (abortSignal?.aborted) throw new FragnoClientFetchAbortError("Operation was aborted");
623
+ const decoder = new TextDecoder();
624
+ const reader = response.body.getReader();
625
+ let buffer = "";
626
+ let firstItem = null;
627
+ const items = [];
628
+ try {
629
+ while (firstItem === null) {
630
+ if (abortSignal?.aborted) {
631
+ reader.releaseLock();
632
+ throw new FragnoClientFetchAbortError("Operation was aborted");
633
+ }
634
+ const { done, value } = await (abortSignal ? Promise.race([reader.read(), createAbortPromise(abortSignal)]) : reader.read());
635
+ if (done) break;
636
+ buffer += decoder.decode(value, { stream: true });
637
+ const lines = buffer.split("\n");
638
+ buffer = lines.pop() || "";
639
+ for (const line of lines) {
640
+ if (!line.trim()) continue;
641
+ try {
642
+ const jsonObject = JSON.parse(line);
643
+ items.push(jsonObject);
644
+ if (firstItem === null) {
645
+ firstItem = jsonObject;
646
+ const streamingPromise = continueStreaming(reader, decoder, buffer, items, store, abortSignal);
647
+ return {
648
+ firstItem,
649
+ streamingPromise
650
+ };
651
+ }
652
+ } catch (parseError) {
653
+ throw new FragnoClientUnknownApiError("Failed to parse NDJSON line", 500, { cause: parseError });
654
+ }
655
+ }
656
+ }
657
+ if (firstItem === null) {
658
+ reader.releaseLock();
659
+ throw new FragnoClientUnknownApiError("NDJSON stream contained no valid items", 500);
660
+ }
661
+ reader.releaseLock();
662
+ throw new FragnoClientFetchError("Unexpected end of stream processing", "NO_BODY");
663
+ } catch (error) {
664
+ if (error instanceof FragnoClientError) {
665
+ store?.setError(error);
666
+ throw error;
667
+ } else {
668
+ const clientError = new FragnoClientUnknownApiError("Unknown streaming error", 500, { cause: error });
669
+ store?.setError(clientError);
670
+ throw clientError;
671
+ }
672
+ }
673
+ }
674
+ /**
675
+ * Continues streaming the remaining items in the background
676
+ */
677
+ async function continueStreaming(reader, decoder, initialBuffer, items, store, abortSignal) {
678
+ let buffer = initialBuffer;
679
+ try {
680
+ while (true) {
681
+ if (abortSignal?.aborted) throw new FragnoClientFetchAbortError("Operation was aborted");
682
+ const { done, value } = await (abortSignal ? Promise.race([reader.read(), createAbortPromise(abortSignal)]) : reader.read());
683
+ if (done) {
684
+ if (buffer.trim()) {
685
+ const lines$1 = buffer.split("\n");
686
+ for (const line of lines$1) {
687
+ if (!line.trim()) continue;
688
+ try {
689
+ const jsonObject = JSON.parse(line);
690
+ items.push(jsonObject);
691
+ store?.setData([...items]);
692
+ } catch (parseError) {
693
+ throw new FragnoClientUnknownApiError("Failed to parse NDJSON line", 400, { cause: parseError });
694
+ }
695
+ }
696
+ }
697
+ break;
698
+ }
699
+ buffer += decoder.decode(value, { stream: true });
700
+ const lines = buffer.split("\n");
701
+ buffer = lines.pop() || "";
702
+ for (const line of lines) {
703
+ if (!line.trim()) continue;
704
+ try {
705
+ const jsonObject = JSON.parse(line);
706
+ items.push(jsonObject);
707
+ store?.setData([...items]);
708
+ } catch (parseError) {
709
+ throw new FragnoClientUnknownApiError("Failed to parse NDJSON line", 400, { cause: parseError });
710
+ }
711
+ }
712
+ }
713
+ } catch (error) {
714
+ if (error instanceof FragnoClientError) store?.setError(error);
715
+ else {
716
+ const clientError = new FragnoClientUnknownApiError("Unknown streaming error", 400, { cause: error });
717
+ store?.setError(clientError);
718
+ throw clientError;
719
+ }
720
+ throw error;
721
+ } finally {
722
+ reader.releaseLock();
723
+ }
724
+ return items;
725
+ }
726
+
727
+ //#endregion
728
+ //#region ../../node_modules/.pnpm/nanostores@1.1.0/node_modules/nanostores/task/index.js
729
+ let tasks = 0;
730
+ let resolves = [];
731
+ function startTask() {
732
+ tasks += 1;
733
+ return () => {
734
+ tasks -= 1;
735
+ if (tasks === 0) {
736
+ let prevResolves = resolves;
737
+ resolves = [];
738
+ for (let i of prevResolves) i();
739
+ }
740
+ };
741
+ }
742
+ function task(cb) {
743
+ let endTask = startTask();
744
+ let promise = cb().finally(endTask);
745
+ promise.t = true;
746
+ return promise;
747
+ }
748
+
749
+ //#endregion
750
+ //#region ../../node_modules/.pnpm/nanostores@1.1.0/node_modules/nanostores/clean-stores/index.js
751
+ let clean = Symbol("clean");
752
+
753
+ //#endregion
754
+ //#region ../../node_modules/.pnpm/nanostores@1.1.0/node_modules/nanostores/atom/index.js
755
+ let listenerQueue = [];
756
+ let lqIndex = 0;
757
+ const QUEUE_ITEMS_PER_LISTENER = 4;
758
+ let epoch = 0;
759
+ const atom = /* @__NO_SIDE_EFFECTS__ */ (initialValue) => {
760
+ let listeners = [];
761
+ let $atom = {
762
+ get() {
763
+ if (!$atom.lc) $atom.listen(() => {})();
764
+ return $atom.value;
765
+ },
766
+ lc: 0,
767
+ listen(listener) {
768
+ $atom.lc = listeners.push(listener);
769
+ return () => {
770
+ for (let i = lqIndex + QUEUE_ITEMS_PER_LISTENER; i < listenerQueue.length;) if (listenerQueue[i] === listener) listenerQueue.splice(i, QUEUE_ITEMS_PER_LISTENER);
771
+ else i += QUEUE_ITEMS_PER_LISTENER;
772
+ let index = listeners.indexOf(listener);
773
+ if (~index) {
774
+ listeners.splice(index, 1);
775
+ if (!--$atom.lc) $atom.off();
776
+ }
777
+ };
778
+ },
779
+ notify(oldValue, changedKey) {
780
+ epoch++;
781
+ let runListenerQueue = !listenerQueue.length;
782
+ for (let listener of listeners) listenerQueue.push(listener, $atom.value, oldValue, changedKey);
783
+ if (runListenerQueue) {
784
+ for (lqIndex = 0; lqIndex < listenerQueue.length; lqIndex += QUEUE_ITEMS_PER_LISTENER) listenerQueue[lqIndex](listenerQueue[lqIndex + 1], listenerQueue[lqIndex + 2], listenerQueue[lqIndex + 3]);
785
+ listenerQueue.length = 0;
786
+ }
787
+ },
788
+ off() {},
789
+ set(newValue) {
790
+ let oldValue = $atom.value;
791
+ if (oldValue !== newValue) {
792
+ $atom.value = newValue;
793
+ $atom.notify(oldValue);
794
+ }
795
+ },
796
+ subscribe(listener) {
797
+ let unbind = $atom.listen(listener);
798
+ listener($atom.value);
799
+ return unbind;
800
+ },
801
+ value: initialValue
802
+ };
803
+ $atom[clean] = () => {
804
+ listeners = [];
805
+ $atom.lc = 0;
806
+ $atom.off();
807
+ };
808
+ return $atom;
809
+ };
810
+
811
+ //#endregion
812
+ //#region ../../node_modules/.pnpm/nanostores@1.1.0/node_modules/nanostores/lifecycle/index.js
813
+ const START = 0;
814
+ const STOP = 1;
815
+ const MOUNT = 5;
816
+ const UNMOUNT = 6;
817
+ const REVERT_MUTATION = 10;
818
+ let on = (object, listener, eventKey, mutateStore) => {
819
+ object.events = object.events || {};
820
+ if (!object.events[eventKey + REVERT_MUTATION]) object.events[eventKey + REVERT_MUTATION] = mutateStore((eventProps) => {
821
+ object.events[eventKey].reduceRight((event, l) => (l(event), event), {
822
+ shared: {},
823
+ ...eventProps
824
+ });
825
+ });
826
+ object.events[eventKey] = object.events[eventKey] || [];
827
+ object.events[eventKey].push(listener);
828
+ return () => {
829
+ let currentListeners = object.events[eventKey];
830
+ let index = currentListeners.indexOf(listener);
831
+ currentListeners.splice(index, 1);
832
+ if (!currentListeners.length) {
833
+ delete object.events[eventKey];
834
+ object.events[eventKey + REVERT_MUTATION]();
835
+ delete object.events[eventKey + REVERT_MUTATION];
836
+ }
837
+ };
838
+ };
839
+ let onStart = ($store, listener) => on($store, listener, START, (runListeners) => {
840
+ let originListen = $store.listen;
841
+ $store.listen = (arg) => {
842
+ if (!$store.lc && !$store.starting) {
843
+ $store.starting = true;
844
+ runListeners();
845
+ delete $store.starting;
846
+ }
847
+ return originListen(arg);
848
+ };
849
+ return () => {
850
+ $store.listen = originListen;
851
+ };
852
+ });
853
+ let onStop = ($store, listener) => on($store, listener, STOP, (runListeners) => {
854
+ let originOff = $store.off;
855
+ $store.off = () => {
856
+ runListeners();
857
+ originOff();
858
+ };
859
+ return () => {
860
+ $store.off = originOff;
861
+ };
862
+ });
863
+ let STORE_UNMOUNT_DELAY = 1e3;
864
+ let onMount = ($store, initialize) => {
865
+ let listener = (payload) => {
866
+ let destroy = initialize(payload);
867
+ if (destroy) $store.events[UNMOUNT].push(destroy);
868
+ };
869
+ return on($store, listener, MOUNT, (runListeners) => {
870
+ let originListen = $store.listen;
871
+ $store.listen = (...args) => {
872
+ if (!$store.lc && !$store.active) {
873
+ $store.active = true;
874
+ runListeners();
875
+ }
876
+ return originListen(...args);
877
+ };
878
+ let originOff = $store.off;
879
+ $store.events[UNMOUNT] = [];
880
+ $store.off = () => {
881
+ originOff();
882
+ setTimeout(() => {
883
+ if ($store.active && !$store.lc) {
884
+ $store.active = false;
885
+ for (let destroy of $store.events[UNMOUNT]) destroy();
886
+ $store.events[UNMOUNT] = [];
887
+ }
888
+ }, STORE_UNMOUNT_DELAY);
889
+ };
890
+ {
891
+ let originClean = $store[clean];
892
+ $store[clean] = () => {
893
+ for (let destroy of $store.events[UNMOUNT]) destroy();
894
+ $store.events[UNMOUNT] = [];
895
+ $store.active = false;
896
+ originClean();
897
+ };
898
+ }
899
+ return () => {
900
+ $store.listen = originListen;
901
+ $store.off = originOff;
902
+ };
903
+ });
904
+ };
905
+
906
+ //#endregion
907
+ //#region ../../node_modules/.pnpm/nanostores@1.1.0/node_modules/nanostores/computed/index.js
908
+ let computedStore = (stores$1, cb, batched$1) => {
909
+ if (!Array.isArray(stores$1)) stores$1 = [stores$1];
910
+ let previousArgs;
911
+ let currentEpoch;
912
+ let set = () => {
913
+ if (currentEpoch === epoch) return;
914
+ currentEpoch = epoch;
915
+ let args = stores$1.map(($store) => $store.get());
916
+ if (!previousArgs || args.some((arg, i) => arg !== previousArgs[i])) {
917
+ previousArgs = args;
918
+ let value = cb(...args);
919
+ if (value && value.then && value.t) value.then((asyncValue) => {
920
+ if (previousArgs === args) $computed.set(asyncValue);
921
+ });
922
+ else {
923
+ $computed.set(value);
924
+ currentEpoch = epoch;
925
+ }
926
+ }
927
+ };
928
+ let $computed = atom(void 0);
929
+ let get = $computed.get;
930
+ $computed.get = () => {
931
+ set();
932
+ return get();
933
+ };
934
+ let timer;
935
+ let run = batched$1 ? () => {
936
+ clearTimeout(timer);
937
+ timer = setTimeout(set);
938
+ } : set;
939
+ onMount($computed, () => {
940
+ let unbinds = stores$1.map(($store) => $store.listen(run));
941
+ set();
942
+ return () => {
943
+ for (let unbind of unbinds) unbind();
944
+ };
945
+ });
946
+ return $computed;
947
+ };
948
+ const computed = /* @__NO_SIDE_EFFECTS__ */ (stores$1, fn) => computedStore(stores$1, fn);
949
+ const batched = /* @__NO_SIDE_EFFECTS__ */ (stores$1, fn) => computedStore(stores$1, fn, true);
950
+
951
+ //#endregion
952
+ //#region ../../node_modules/.pnpm/nanostores@1.1.0/node_modules/nanostores/map/index.js
953
+ const map = /* @__NO_SIDE_EFFECTS__ */ (initial = {}) => {
954
+ let $map = atom(initial);
955
+ $map.setKey = function(key, value) {
956
+ let oldMap = $map.value;
957
+ if (typeof value === "undefined" && key in $map.value) {
958
+ $map.value = { ...$map.value };
959
+ delete $map.value[key];
960
+ $map.notify(oldMap, key);
961
+ } else if ($map.value[key] !== value) {
962
+ $map.value = {
963
+ ...$map.value,
964
+ [key]: value
965
+ };
966
+ $map.notify(oldMap, key);
967
+ }
968
+ };
969
+ return $map;
970
+ };
971
+
972
+ //#endregion
973
+ //#region ../fragno/dist/util/ssr.js
974
+ let stores = [];
975
+ const SSR_ENABLED = false;
976
+ function addStore(store) {
977
+ stores.push(store);
978
+ }
979
+ let clientInitialData;
980
+ function getInitialData(key) {
981
+ if (clientInitialData?.has(key)) {
982
+ const data = clientInitialData.get(key);
983
+ clientInitialData.delete(key);
984
+ return data;
985
+ }
986
+ }
987
+
988
+ //#endregion
989
+ //#region ../fragno/dist/util/nanostores.js
990
+ /**
991
+ * Normalizes a value that could be a plain value, an Atom, or a Vue Ref to a plain value.
992
+ */
993
+ function unwrapAtom(value) {
994
+ if (value && typeof value === "object" && "get" in value && typeof value.get === "function") return value.get();
995
+ return value;
996
+ }
997
+ /**
998
+ * Normalizes an object where values can be plain values, Atoms, or Vue Refs.
999
+ * Returns a new object with all values normalized to plain values.
1000
+ */
1001
+ function unwrapObject(params) {
1002
+ if (!params) return;
1003
+ return Object.fromEntries(Object.entries(params).map(([key, value]) => [key, unwrapAtom(value)]));
1004
+ }
1005
+ function isReadableAtom(value) {
1006
+ if (!value) return false;
1007
+ if (typeof value !== "object" || value === null) return false;
1008
+ if (!("get" in value) || typeof value.get !== "function") return false;
1009
+ if (!("lc" in value) || typeof value.lc !== "number") return false;
1010
+ if (!("notify" in value) || typeof value.notify !== "function") return false;
1011
+ if (!("off" in value) || typeof value.off !== "function") return false;
1012
+ if (!("subscribe" in value) || typeof value.subscribe !== "function") return false;
1013
+ if (!("value" in value)) return false;
1014
+ return true;
1015
+ }
1016
+
1017
+ //#endregion
1018
+ //#region ../fragno/dist/client/internal/fetcher-merge.js
1019
+ /**
1020
+ * Merge two fetcher configurations, with user config taking precedence.
1021
+ * If user provides a custom function, it takes full precedence.
1022
+ * Otherwise, deep merge RequestInit options.
1023
+ */
1024
+ function mergeFetcherConfigs(authorConfig, userConfig) {
1025
+ if (userConfig?.type === "function") return userConfig;
1026
+ if (!userConfig && authorConfig?.type === "function") return authorConfig;
1027
+ const authorOpts = authorConfig?.type === "options" ? authorConfig.options : {};
1028
+ const userOpts = userConfig?.type === "options" ? userConfig.options : {};
1029
+ if (Object.keys(authorOpts).length === 0 && Object.keys(userOpts).length === 0) return;
1030
+ return {
1031
+ type: "options",
1032
+ options: {
1033
+ ...authorOpts,
1034
+ ...userOpts,
1035
+ headers: mergeHeaders(authorOpts.headers, userOpts.headers)
1036
+ }
1037
+ };
1038
+ }
1039
+ /**
1040
+ * Merge headers from author and user configs.
1041
+ * User headers override author headers.
1042
+ */
1043
+ function mergeHeaders(author, user) {
1044
+ if (!author && !user) return;
1045
+ const merged = new Headers(author);
1046
+ new Headers(user).forEach((value, key) => merged.set(key, value));
1047
+ if (merged.keys().next().done) return;
1048
+ return merged;
1049
+ }
1050
+
1051
+ //#endregion
1052
+ //#region ../../node_modules/.pnpm/nanoevents@9.1.0/node_modules/nanoevents/index.js
1053
+ let createNanoEvents = () => ({
1054
+ emit(event, ...args) {
1055
+ for (let callbacks = this.events[event] || [], i = 0, length = callbacks.length; i < length; i++) callbacks[i](...args);
1056
+ },
1057
+ events: {},
1058
+ on(event, cb) {
1059
+ (this.events[event] ||= []).push(cb);
1060
+ return () => {
1061
+ this.events[event] = this.events[event]?.filter((i) => cb !== i);
1062
+ };
1063
+ }
1064
+ });
1065
+
1066
+ //#endregion
1067
+ //#region ../../node_modules/.pnpm/@nanostores+query@0.3.4_nanostores@1.1.0/node_modules/@nanostores/query/dist/nanoquery.js
1068
+ function defaultOnErrorRetry({ retryCount }) {
1069
+ return ~~((Math.random() + .5) * (1 << (retryCount < 8 ? retryCount : 8))) * 2e3;
1070
+ }
1071
+ const nanoqueryFactory = ([isAppVisible, visibilityChangeSubscribe, reconnectChangeSubscribe]) => {
1072
+ const nanoquery$1 = ({ cache = /* @__PURE__ */ new Map(), fetcher: globalFetcher,...globalSettings } = {}) => {
1073
+ const events = createNanoEvents();
1074
+ let focus = true;
1075
+ visibilityChangeSubscribe(() => {
1076
+ focus = isAppVisible();
1077
+ focus && events.emit(FOCUS);
1078
+ });
1079
+ reconnectChangeSubscribe(() => events.emit(RECONNECT));
1080
+ const _revalidateOnInterval = /* @__PURE__ */ new Map(), _errorInvalidateTimeouts = /* @__PURE__ */ new Map(), _runningFetches = /* @__PURE__ */ new Map();
1081
+ let rewrittenSettings = {};
1082
+ const getCachedValueByKey = (key) => {
1083
+ const fromCache = cache.get(key);
1084
+ if (!fromCache) return [];
1085
+ const cacheHit = (fromCache.expires || 0) > getNow();
1086
+ return cacheHit ? [fromCache.data, fromCache.error] : [];
1087
+ };
1088
+ const runFetcher = async ([key, keyParts], store, settings) => {
1089
+ if (!focus) return;
1090
+ const set = (v) => {
1091
+ if (store.key === key) {
1092
+ store.set(v);
1093
+ events.emit(SET_CACHE, key, v, true);
1094
+ }
1095
+ };
1096
+ const setAsLoading = (prev) => {
1097
+ const toSet = prev === void 0 ? {} : { data: prev };
1098
+ set({
1099
+ ...toSet,
1100
+ ...loading,
1101
+ promise: _runningFetches.get(key)
1102
+ });
1103
+ };
1104
+ let { dedupeTime = 4e3, cacheLifetime = Infinity, fetcher, onErrorRetry = defaultOnErrorRetry } = {
1105
+ ...settings,
1106
+ ...rewrittenSettings
1107
+ };
1108
+ if (cacheLifetime < dedupeTime) cacheLifetime = dedupeTime;
1109
+ const now = getNow();
1110
+ if (_runningFetches.has(key)) {
1111
+ if (!store.value.loading) setAsLoading(getCachedValueByKey(key)[0]);
1112
+ return;
1113
+ }
1114
+ let cachedValue, cachedError;
1115
+ const fromCache = cache.get(key);
1116
+ if (fromCache?.data !== void 0 || fromCache?.error) {
1117
+ [cachedValue, cachedError] = getCachedValueByKey(key);
1118
+ if ((fromCache.created || 0) + dedupeTime > now) {
1119
+ if (store.value.data != cachedValue || store.value.error != cachedError) set({
1120
+ ...notLoading,
1121
+ data: cachedValue,
1122
+ error: cachedError
1123
+ });
1124
+ return;
1125
+ }
1126
+ }
1127
+ const finishTask = startTask();
1128
+ try {
1129
+ clearTimeout(_errorInvalidateTimeouts.get(key));
1130
+ const promise = fetcher(...keyParts);
1131
+ _runningFetches.set(key, promise);
1132
+ setAsLoading(cachedValue);
1133
+ const res = await promise;
1134
+ cache.set(key, {
1135
+ data: res,
1136
+ created: getNow(),
1137
+ expires: getNow() + cacheLifetime
1138
+ });
1139
+ set({
1140
+ data: res,
1141
+ ...notLoading
1142
+ });
1143
+ } catch (error) {
1144
+ settings.onError?.(error);
1145
+ const retryCount = (cache.get(key)?.retryCount || 0) + 1;
1146
+ cache.set(key, {
1147
+ error,
1148
+ created: getNow(),
1149
+ expires: getNow() + cacheLifetime,
1150
+ retryCount
1151
+ });
1152
+ if (onErrorRetry) {
1153
+ const timer = onErrorRetry({
1154
+ error,
1155
+ key,
1156
+ retryCount
1157
+ });
1158
+ if (timer) _errorInvalidateTimeouts.set(key, setTimeout(() => {
1159
+ invalidateKeys(key);
1160
+ cache.set(key, { retryCount });
1161
+ }, timer));
1162
+ }
1163
+ set({
1164
+ data: store.value.data,
1165
+ error,
1166
+ ...notLoading
1167
+ });
1168
+ } finally {
1169
+ finishTask();
1170
+ _runningFetches.delete(key);
1171
+ }
1172
+ };
1173
+ const createFetcherStore = (keyInput, { fetcher = globalFetcher,...fetcherSettings } = {}) => {
1174
+ if (!fetcher) throw new Error("You need to set up either global fetcher of fetcher in createFetcherStore");
1175
+ const fetcherStore = map({ ...notLoading }), settings = {
1176
+ ...globalSettings,
1177
+ ...fetcherSettings,
1178
+ fetcher
1179
+ };
1180
+ fetcherStore._ = fetcherSymbol;
1181
+ fetcherStore.invalidate = () => {
1182
+ const { key } = fetcherStore;
1183
+ if (key) invalidateKeys(key);
1184
+ };
1185
+ fetcherStore.revalidate = () => {
1186
+ const { key } = fetcherStore;
1187
+ if (key) revalidateKeys(key);
1188
+ };
1189
+ fetcherStore.mutate = (data) => {
1190
+ const { key } = fetcherStore;
1191
+ if (key) mutateCache(key, data);
1192
+ };
1193
+ fetcherStore.fetch = async () => {
1194
+ let resolve;
1195
+ const promise = new Promise((r) => resolve = r);
1196
+ const unsub = fetcherStore.listen(({ error, data }) => {
1197
+ if (error !== void 0) resolve({ error });
1198
+ if (data !== void 0) resolve({ data });
1199
+ });
1200
+ return promise.finally(unsub);
1201
+ };
1202
+ let keysInternalUnsub, prevKey, prevKeyParts, keyUnsub, keyStore;
1203
+ let evtUnsubs = [];
1204
+ onStart(fetcherStore, () => {
1205
+ const firstRun = !keysInternalUnsub;
1206
+ [keyStore, keysInternalUnsub] = getKeyStore(keyInput);
1207
+ keyUnsub = keyStore.subscribe((currentKeys) => {
1208
+ if (currentKeys) {
1209
+ const [newKey, keyParts] = currentKeys;
1210
+ fetcherStore.key = newKey;
1211
+ runFetcher([newKey, keyParts], fetcherStore, settings);
1212
+ prevKey = newKey;
1213
+ prevKeyParts = keyParts;
1214
+ } else {
1215
+ fetcherStore.key = prevKey = prevKeyParts = void 0;
1216
+ fetcherStore.set({ ...notLoading });
1217
+ }
1218
+ });
1219
+ const currentKeyValue = keyStore.get();
1220
+ if (currentKeyValue) {
1221
+ [prevKey, prevKeyParts] = currentKeyValue;
1222
+ if (firstRun) handleNewListener();
1223
+ }
1224
+ const { revalidateInterval = 0, revalidateOnFocus, revalidateOnReconnect } = settings;
1225
+ const runRefetcher = () => {
1226
+ if (prevKey) runFetcher([prevKey, prevKeyParts], fetcherStore, settings);
1227
+ };
1228
+ if (revalidateInterval > 0) _revalidateOnInterval.set(keyInput, setInterval(runRefetcher, revalidateInterval));
1229
+ if (revalidateOnFocus) evtUnsubs.push(events.on(FOCUS, runRefetcher));
1230
+ if (revalidateOnReconnect) evtUnsubs.push(events.on(RECONNECT, runRefetcher));
1231
+ const cacheKeyChangeHandler = (keySelector) => {
1232
+ if (prevKey && testKeyAgainstSelector(prevKey, keySelector)) runFetcher([prevKey, prevKeyParts], fetcherStore, settings);
1233
+ };
1234
+ evtUnsubs.push(events.on(INVALIDATE_KEYS, cacheKeyChangeHandler), events.on(REVALIDATE_KEYS, cacheKeyChangeHandler), events.on(SET_CACHE, (keySelector, data, full) => {
1235
+ if (prevKey && testKeyAgainstSelector(prevKey, keySelector) && fetcherStore.value !== data && fetcherStore.value.data !== data) fetcherStore.set(full ? data : {
1236
+ data,
1237
+ ...notLoading
1238
+ });
1239
+ }));
1240
+ });
1241
+ const handleNewListener = () => {
1242
+ if (prevKey && prevKeyParts) runFetcher([prevKey, prevKeyParts], fetcherStore, settings);
1243
+ };
1244
+ const originListen = fetcherStore.listen;
1245
+ fetcherStore.listen = (listener) => {
1246
+ const unsub = originListen(listener);
1247
+ listener(fetcherStore.value);
1248
+ handleNewListener();
1249
+ return unsub;
1250
+ };
1251
+ onStop(fetcherStore, () => {
1252
+ fetcherStore.value = { ...notLoading };
1253
+ keysInternalUnsub?.();
1254
+ evtUnsubs.forEach((fn) => fn());
1255
+ evtUnsubs = [];
1256
+ keyUnsub?.();
1257
+ clearInterval(_revalidateOnInterval.get(keyInput));
1258
+ });
1259
+ return fetcherStore;
1260
+ };
1261
+ const iterOverCache = (keySelector, cb) => {
1262
+ for (const key of cache.keys()) if (testKeyAgainstSelector(key, keySelector)) cb(key);
1263
+ };
1264
+ const invalidateKeys = (keySelector) => {
1265
+ iterOverCache(keySelector, (key) => {
1266
+ cache.delete(key);
1267
+ });
1268
+ events.emit(INVALIDATE_KEYS, keySelector);
1269
+ };
1270
+ const revalidateKeys = (keySelector) => {
1271
+ iterOverCache(keySelector, (key) => {
1272
+ const cached = cache.get(key);
1273
+ if (cached) cache.set(key, {
1274
+ ...cached,
1275
+ created: -Infinity
1276
+ });
1277
+ });
1278
+ events.emit(REVALIDATE_KEYS, keySelector);
1279
+ };
1280
+ const mutateCache = (keySelector, data) => {
1281
+ iterOverCache(keySelector, (key) => {
1282
+ if (data === void 0) cache.delete(key);
1283
+ else cache.set(key, {
1284
+ data,
1285
+ created: getNow(),
1286
+ expires: getNow() + (globalSettings.cacheLifetime ?? 8e3)
1287
+ });
1288
+ });
1289
+ events.emit(SET_CACHE, keySelector, data);
1290
+ };
1291
+ function createMutatorStore(mutator, opts) {
1292
+ const { throttleCalls, onError } = opts ?? {
1293
+ throttleCalls: true,
1294
+ onError: globalSettings?.onError
1295
+ };
1296
+ const mutate = async (data) => {
1297
+ if (throttleCalls && store.value?.loading) return;
1298
+ const newMutator = rewrittenSettings.fetcher ?? mutator;
1299
+ const keysToInvalidate = [], keysToRevalidate = [];
1300
+ const safeKeySet = (k, v) => {
1301
+ if (store.lc) store.setKey(k, v);
1302
+ };
1303
+ try {
1304
+ store.set({
1305
+ error: void 0,
1306
+ data: void 0,
1307
+ mutate,
1308
+ ...loading
1309
+ });
1310
+ const result = await newMutator({
1311
+ data,
1312
+ invalidate: (key) => {
1313
+ keysToInvalidate.push(key);
1314
+ },
1315
+ revalidate: (key) => {
1316
+ keysToRevalidate.push(key);
1317
+ },
1318
+ getCacheUpdater: (key, shouldRevalidate = true) => [(newVal) => {
1319
+ mutateCache(key, newVal);
1320
+ if (shouldRevalidate) keysToRevalidate.push(key);
1321
+ }, cache.get(key)?.data]
1322
+ });
1323
+ safeKeySet("data", result);
1324
+ return result;
1325
+ } catch (error) {
1326
+ onError?.(error);
1327
+ safeKeySet("error", error);
1328
+ store.setKey("error", error);
1329
+ } finally {
1330
+ safeKeySet("loading", false);
1331
+ keysToInvalidate.forEach(invalidateKeys);
1332
+ keysToRevalidate.forEach(revalidateKeys);
1333
+ }
1334
+ };
1335
+ const store = map({
1336
+ mutate,
1337
+ ...notLoading
1338
+ });
1339
+ onStop(store, () => store.set({
1340
+ mutate,
1341
+ ...notLoading
1342
+ }));
1343
+ store.mutate = mutate;
1344
+ return store;
1345
+ }
1346
+ const __unsafeOverruleSettings = (data) => {
1347
+ console.warn(`You should only use __unsafeOverruleSettings in test environment`);
1348
+ rewrittenSettings = data;
1349
+ };
1350
+ return [
1351
+ createFetcherStore,
1352
+ createMutatorStore,
1353
+ {
1354
+ __unsafeOverruleSettings,
1355
+ invalidateKeys,
1356
+ revalidateKeys,
1357
+ mutateCache
1358
+ }
1359
+ ];
1360
+ };
1361
+ function isSomeKey(key) {
1362
+ return typeof key === "string" || typeof key === "number" || key === true;
1363
+ }
1364
+ const getKeyStore = (keys) => {
1365
+ if (isSomeKey(keys)) return [atom(["" + keys, [keys]]), () => {}];
1366
+ const keyParts = [];
1367
+ const $key = atom(null);
1368
+ const keysAsStoresToIndexes = /* @__PURE__ */ new Map();
1369
+ const setKeyStoreValue = () => {
1370
+ if (keyParts.some((v) => v === null || v === void 0 || v === false)) $key.set(null);
1371
+ else $key.set([keyParts.join(""), keyParts]);
1372
+ };
1373
+ for (let i = 0; i < keys.length; i++) {
1374
+ const keyOrStore = keys[i];
1375
+ if (isSomeKey(keyOrStore)) keyParts.push(keyOrStore);
1376
+ else {
1377
+ keyParts.push(null);
1378
+ keysAsStoresToIndexes.set(keyOrStore, i);
1379
+ }
1380
+ }
1381
+ const storesAsArray = [...keysAsStoresToIndexes.keys()];
1382
+ const $storeKeys = batched(storesAsArray, (...storeValues) => {
1383
+ for (let i = 0; i < storeValues.length; i++) {
1384
+ const store = storesAsArray[i], partIndex = keysAsStoresToIndexes.get(store);
1385
+ keyParts[partIndex] = store._ === fetcherSymbol ? store.value && "data" in store.value ? store.key : null : storeValues[i];
1386
+ }
1387
+ setKeyStoreValue();
1388
+ });
1389
+ setKeyStoreValue();
1390
+ return [$key, $storeKeys.subscribe(noop)];
1391
+ };
1392
+ function noop() {}
1393
+ const FOCUS = 1, RECONNECT = 2, INVALIDATE_KEYS = 3, REVALIDATE_KEYS = 4, SET_CACHE = 5;
1394
+ const testKeyAgainstSelector = (key, selector) => {
1395
+ if (Array.isArray(selector)) return selector.includes(key);
1396
+ else if (typeof selector === "function") return selector(key);
1397
+ else return key === selector;
1398
+ };
1399
+ const getNow = () => (/* @__PURE__ */ new Date()).getTime();
1400
+ const fetcherSymbol = Symbol();
1401
+ const loading = { loading: true }, notLoading = { loading: false };
1402
+ return nanoquery$1;
1403
+ };
1404
+ const subscribe = (name, fn) => {
1405
+ const isServer = typeof window === "undefined";
1406
+ if (!isServer) addEventListener(name, fn);
1407
+ };
1408
+ const browserCompat = [
1409
+ () => !document.hidden,
1410
+ (cb) => subscribe("visibilitychange", cb),
1411
+ (cb) => subscribe("online", cb)
1412
+ ];
1413
+ const nanoquery = nanoqueryFactory(browserCompat);
1414
+
1415
+ //#endregion
1416
+ //#region ../fragno/dist/client/client.js
1417
+ /**
1418
+ * Symbols used to identify hook types
1419
+ */
1420
+ const GET_HOOK_SYMBOL = Symbol("fragno-get-hook");
1421
+ const MUTATOR_HOOK_SYMBOL = Symbol("fragno-mutator-hook");
1422
+ const STORE_SYMBOL = Symbol("fragno-store");
1423
+ /**
1424
+ * @internal
1425
+ */
1426
+ function buildUrl(config, params) {
1427
+ const { baseUrl = "", mountRoute, path } = config;
1428
+ const { pathParams, queryParams } = params ?? {};
1429
+ const normalizedPathParams = unwrapObject(pathParams);
1430
+ const normalizedQueryParams = unwrapObject(queryParams) ?? {};
1431
+ const filteredQueryParams = Object.fromEntries(Object.entries(normalizedQueryParams).filter(([_, value]) => value !== void 0));
1432
+ const searchParams = new URLSearchParams(filteredQueryParams);
1433
+ return `${baseUrl}${mountRoute}${buildPath(path, normalizedPathParams ?? {})}${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
1434
+ }
1435
+ /**
1436
+ * This method returns an array, which can be passed directly to nanostores.
1437
+ *
1438
+ * The returned array is always: path, pathParams (In order they appear in the path), queryParams (In alphabetical order)
1439
+ * Missing pathParams are replaced with "<missing>".
1440
+ * Atoms with undefined values are wrapped in computed atoms that map undefined to "" to avoid nanoquery treating the key as incomplete.
1441
+ * @param path
1442
+ * @param params
1443
+ * @returns
1444
+ * @internal
1445
+ */
1446
+ function getCacheKey(method, path, params) {
1447
+ if (!params) return [method, path];
1448
+ const { pathParams, queryParams } = params;
1449
+ const pathParamValues = extractPathParams(path).map((name) => pathParams?.[name] ?? "<missing>");
1450
+ const queryParamValues = queryParams ? Object.keys(queryParams).sort().map((key) => {
1451
+ const value = queryParams[key];
1452
+ if (value && typeof value === "object" && "get" in value) return computed(value, (v) => v ?? "");
1453
+ return value ?? "";
1454
+ }) : [];
1455
+ return [
1456
+ method,
1457
+ path,
1458
+ ...pathParamValues,
1459
+ ...queryParamValues
1460
+ ];
1461
+ }
1462
+ function isStreamingResponse(response) {
1463
+ const contentType = parseContentType(response.headers.get("content-type"));
1464
+ if (!contentType) return false;
1465
+ if (!(response.headers.get("transfer-encoding") === "chunked")) return false;
1466
+ if (contentType.subtype === "octet-stream") return "octet-stream";
1467
+ if (contentType.subtype === "x-ndjson") return "ndjson";
1468
+ return false;
1469
+ }
1470
+ /**
1471
+ * @internal
1472
+ */
1473
+ function isGetHook(hook) {
1474
+ return typeof hook === "object" && hook !== null && GET_HOOK_SYMBOL in hook && hook[GET_HOOK_SYMBOL] === true;
1475
+ }
1476
+ /**
1477
+ * @internal
1478
+ */
1479
+ function isMutatorHook(hook) {
1480
+ return typeof hook === "object" && hook !== null && MUTATOR_HOOK_SYMBOL in hook && hook[MUTATOR_HOOK_SYMBOL] === true;
1481
+ }
1482
+ /**
1483
+ * @internal
1484
+ */
1485
+ function isStore(obj) {
1486
+ return typeof obj === "object" && obj !== null && STORE_SYMBOL in obj && obj[STORE_SYMBOL] === true;
1487
+ }
1488
+ var ClientBuilder = class {
1489
+ #publicConfig;
1490
+ #fragmentConfig;
1491
+ #fetcherConfig;
1492
+ #cache = /* @__PURE__ */ new Map();
1493
+ #createFetcherStore;
1494
+ #createMutatorStore;
1495
+ #invalidateKeys;
1496
+ constructor(publicConfig, fragmentConfig) {
1497
+ this.#publicConfig = publicConfig;
1498
+ this.#fragmentConfig = fragmentConfig;
1499
+ this.#fetcherConfig = publicConfig.fetcherConfig;
1500
+ const [createFetcherStore, createMutatorStore, { invalidateKeys }] = nanoquery({ cache: this.#cache });
1501
+ this.#createFetcherStore = createFetcherStore;
1502
+ this.#createMutatorStore = createMutatorStore;
1503
+ this.#invalidateKeys = invalidateKeys;
1504
+ }
1505
+ get cacheEntries() {
1506
+ return Object.fromEntries(this.#cache.entries());
1507
+ }
1508
+ createStore(obj) {
1509
+ return {
1510
+ obj,
1511
+ [STORE_SYMBOL]: true
1512
+ };
1513
+ }
1514
+ /**
1515
+ * Build a URL for a custom backend call using the configured baseUrl and mountRoute.
1516
+ * Useful for fragment authors who need to make custom fetch calls.
1517
+ */
1518
+ buildUrl(path, params) {
1519
+ return buildUrl({
1520
+ baseUrl: this.#publicConfig.baseUrl ?? "",
1521
+ mountRoute: getMountRoute(this.#fragmentConfig),
1522
+ path
1523
+ }, {
1524
+ pathParams: params?.path,
1525
+ queryParams: params?.query
1526
+ });
1527
+ }
1528
+ /**
1529
+ * Get the configured fetcher function for custom backend calls.
1530
+ * Returns fetch with merged options applied.
1531
+ */
1532
+ getFetcher() {
1533
+ return {
1534
+ fetcher: this.#getFetcher(),
1535
+ defaultOptions: this.#getFetcherOptions()
1536
+ };
1537
+ }
1538
+ #getFetcher() {
1539
+ if (this.#fetcherConfig?.type === "function") return this.#fetcherConfig.fetcher;
1540
+ return fetch;
1541
+ }
1542
+ #getFetcherOptions() {
1543
+ if (this.#fetcherConfig?.type === "options") return this.#fetcherConfig.options;
1544
+ }
1545
+ createHook(path, options) {
1546
+ const route = this.#fragmentConfig.routes.find((r) => r.path === path && r.method === "GET" && r.outputSchema !== void 0);
1547
+ if (!route) throw new Error(`Route '${path}' not found or is not a GET route with an output schema.`);
1548
+ return this.#createRouteQueryHook(route, options);
1549
+ }
1550
+ createMutator(method, path, onInvalidate) {
1551
+ const route = this.#fragmentConfig.routes.find((r) => r.method !== "GET" && r.path === path && r.method === method);
1552
+ if (!route) throw new Error(`Route '${path}' not found or is a GET route with an input and output schema.`);
1553
+ return this.#createRouteQueryMutator(route, onInvalidate);
1554
+ }
1555
+ #createRouteQueryHook(route, options = {}) {
1556
+ if (route.method !== "GET") throw new Error(`Only GET routes are supported for hooks. Route '${route.path}' is a ${route.method} route.`);
1557
+ if (!route.outputSchema) throw new Error(`Output schema is required for GET routes. Route '${route.path}' has no output schema.`);
1558
+ const baseUrl = this.#publicConfig.baseUrl ?? "";
1559
+ const mountRoute = getMountRoute(this.#fragmentConfig);
1560
+ const fetcher = this.#getFetcher();
1561
+ const fetcherOptions = this.#getFetcherOptions();
1562
+ async function callServerSideHandler(params) {
1563
+ const { pathParams, queryParams } = params ?? {};
1564
+ const normalizedPathParams = unwrapObject(pathParams);
1565
+ const normalizedQueryParams = unwrapObject(queryParams) ?? {};
1566
+ const filteredQueryParams = Object.fromEntries(Object.entries(normalizedQueryParams).filter(([_, value]) => value !== void 0));
1567
+ const searchParams = new URLSearchParams(filteredQueryParams);
1568
+ return await route.handler(RequestInputContext.fromSSRContext({
1569
+ method: route.method,
1570
+ path: route.path,
1571
+ pathParams: normalizedPathParams,
1572
+ searchParams
1573
+ }), new RequestOutputContext(route.outputSchema));
1574
+ }
1575
+ async function executeQuery(params) {
1576
+ const { pathParams, queryParams } = params ?? {};
1577
+ if (typeof window === "undefined") return task(async () => callServerSideHandler({
1578
+ pathParams,
1579
+ queryParams
1580
+ }));
1581
+ const url = buildUrl({
1582
+ baseUrl,
1583
+ mountRoute,
1584
+ path: route.path
1585
+ }, {
1586
+ pathParams,
1587
+ queryParams
1588
+ });
1589
+ let response;
1590
+ try {
1591
+ response = fetcherOptions ? await fetcher(url, fetcherOptions) : await fetcher(url);
1592
+ } catch (error) {
1593
+ throw FragnoClientFetchError.fromUnknownFetchError(error);
1594
+ }
1595
+ if (!response.ok) throw await FragnoClientApiError.fromResponse(response);
1596
+ return response;
1597
+ }
1598
+ return {
1599
+ route,
1600
+ store: (args) => {
1601
+ const { path, query } = args ?? {};
1602
+ const key = getCacheKey(route.method, route.path, {
1603
+ pathParams: path,
1604
+ queryParams: query
1605
+ });
1606
+ const store = this.#createFetcherStore(key, {
1607
+ fetcher: async () => {
1608
+ if (SSR_ENABLED) {
1609
+ const initialData = getInitialData(key.map((d) => typeof d === "string" ? d : d.get()).join(""));
1610
+ if (initialData) return initialData;
1611
+ }
1612
+ const response = await executeQuery({
1613
+ pathParams: path,
1614
+ queryParams: query
1615
+ });
1616
+ const isStreaming = isStreamingResponse(response);
1617
+ if (!isStreaming) return response.json();
1618
+ if (typeof window === "undefined") return [];
1619
+ if (isStreaming === "ndjson") {
1620
+ const { firstItem } = await handleNdjsonStreamingFirstItem(response, {
1621
+ setData: (value) => {
1622
+ store.set({
1623
+ ...store.get(),
1624
+ loading: !(Array.isArray(value) && value.length > 0),
1625
+ data: value
1626
+ });
1627
+ },
1628
+ setError: (value) => {
1629
+ store.set({
1630
+ ...store.get(),
1631
+ error: value
1632
+ });
1633
+ }
1634
+ });
1635
+ return [firstItem];
1636
+ }
1637
+ if (isStreaming === "octet-stream") throw new Error("Octet-stream streaming is not supported.");
1638
+ throw new Error("Unreachable");
1639
+ },
1640
+ onErrorRetry: options?.onErrorRetry,
1641
+ dedupeTime: Infinity
1642
+ });
1643
+ if (typeof window === "undefined") addStore(store);
1644
+ return store;
1645
+ },
1646
+ query: async (args) => {
1647
+ const { path, query } = args ?? {};
1648
+ const response = await executeQuery({
1649
+ pathParams: path,
1650
+ queryParams: query
1651
+ });
1652
+ const isStreaming = isStreamingResponse(response);
1653
+ if (!isStreaming) return await response.json();
1654
+ if (isStreaming === "ndjson") {
1655
+ const { streamingPromise } = await handleNdjsonStreamingFirstItem(response);
1656
+ return await streamingPromise;
1657
+ }
1658
+ if (isStreaming === "octet-stream") throw new Error("Octet-stream streaming is not supported.");
1659
+ throw new Error("Unreachable");
1660
+ },
1661
+ [GET_HOOK_SYMBOL]: true
1662
+ };
1663
+ }
1664
+ #createRouteQueryMutator(route, onInvalidate = (invalidate, params) => invalidate("GET", route.path, params)) {
1665
+ const method = route.method;
1666
+ const baseUrl = this.#publicConfig.baseUrl ?? "";
1667
+ const mountRoute = getMountRoute(this.#fragmentConfig);
1668
+ const fetcher = this.#getFetcher();
1669
+ const fetcherOptions = this.#getFetcherOptions();
1670
+ async function executeMutateQuery({ body, path, query }) {
1671
+ if (typeof window === "undefined") return task(async () => route.handler(RequestInputContext.fromSSRContext({
1672
+ inputSchema: route.inputSchema,
1673
+ method,
1674
+ path: route.path,
1675
+ pathParams: path ?? {},
1676
+ searchParams: new URLSearchParams(query),
1677
+ body
1678
+ }), new RequestOutputContext(route.outputSchema)));
1679
+ const url = buildUrl({
1680
+ baseUrl,
1681
+ mountRoute,
1682
+ path: route.path
1683
+ }, {
1684
+ pathParams: path,
1685
+ queryParams: query
1686
+ });
1687
+ let response;
1688
+ try {
1689
+ response = await fetcher(url, {
1690
+ ...fetcherOptions,
1691
+ method,
1692
+ body: body !== void 0 ? JSON.stringify(body) : void 0
1693
+ });
1694
+ } catch (error) {
1695
+ throw FragnoClientFetchError.fromUnknownFetchError(error);
1696
+ }
1697
+ if (!response.ok) throw await FragnoClientApiError.fromResponse(response);
1698
+ return response;
1699
+ }
1700
+ const mutatorStore = this.#createMutatorStore(async ({ data }) => {
1701
+ if (typeof window === "undefined") {}
1702
+ const { body, path, query } = data;
1703
+ if (typeof body === "undefined" && route.inputSchema !== void 0) throw new Error("Body is required.");
1704
+ const response = await executeMutateQuery({
1705
+ body,
1706
+ path,
1707
+ query
1708
+ });
1709
+ onInvalidate(this.#invalidate.bind(this), {
1710
+ pathParams: path ?? {},
1711
+ queryParams: query
1712
+ });
1713
+ if (response.status === 201 || response.status === 204) return;
1714
+ const isStreaming = isStreamingResponse(response);
1715
+ if (!isStreaming) return response.json();
1716
+ if (typeof window === "undefined") return [];
1717
+ if (isStreaming === "ndjson") {
1718
+ const { firstItem } = await handleNdjsonStreamingFirstItem(response, {
1719
+ setData: (value) => {
1720
+ mutatorStore.set({
1721
+ ...mutatorStore.get(),
1722
+ loading: !(Array.isArray(value) && value.length > 0),
1723
+ data: value
1724
+ });
1725
+ },
1726
+ setError: (value) => {
1727
+ mutatorStore.set({
1728
+ ...mutatorStore.get(),
1729
+ error: value
1730
+ });
1731
+ }
1732
+ });
1733
+ return [firstItem];
1734
+ }
1735
+ if (isStreaming === "octet-stream") throw new Error("Octet-stream streaming is not supported.");
1736
+ throw new Error("Unreachable");
1737
+ }, { onError: (error) => {
1738
+ console.error("Error in mutatorStore", error);
1739
+ } });
1740
+ const mutateQuery = async (data) => {
1741
+ const { body, path, query } = data;
1742
+ if (typeof body === "undefined" && route.inputSchema !== void 0) throw new Error("Body is required for mutateQuery");
1743
+ const response = await executeMutateQuery({
1744
+ body,
1745
+ path,
1746
+ query
1747
+ });
1748
+ if (response.status === 201 || response.status === 204) return;
1749
+ const isStreaming = isStreamingResponse(response);
1750
+ if (!isStreaming) return response.json();
1751
+ if (isStreaming === "ndjson") {
1752
+ const { streamingPromise } = await handleNdjsonStreamingFirstItem(response);
1753
+ return await streamingPromise;
1754
+ }
1755
+ if (isStreaming === "octet-stream") throw new Error("Octet-stream streaming is not supported for mutations");
1756
+ throw new Error("Unreachable");
1757
+ };
1758
+ return {
1759
+ route,
1760
+ mutateQuery,
1761
+ mutatorStore,
1762
+ [MUTATOR_HOOK_SYMBOL]: true
1763
+ };
1764
+ }
1765
+ #invalidate(method, path, params) {
1766
+ const prefix = getCacheKey(method, path, {
1767
+ pathParams: params?.pathParams,
1768
+ queryParams: params?.queryParams
1769
+ }).map((k) => typeof k === "string" ? k : k.get()).join("");
1770
+ this.#invalidateKeys((key) => key.startsWith(prefix));
1771
+ }
1772
+ };
1773
+ /**
1774
+ * Create a client builder for fragments using the new fragment definition API.
1775
+ * This is the same as createClientBuilder but works with FragmentDefinition.
1776
+ */
1777
+ function createClientBuilder(definition, publicConfig, routesOrFactories, authorFetcherConfig) {
1778
+ const routes$1 = resolveRouteFactories({
1779
+ config: {},
1780
+ deps: {},
1781
+ services: {},
1782
+ serviceDeps: {}
1783
+ }, routesOrFactories);
1784
+ const fragmentConfig = {
1785
+ name: definition.name,
1786
+ routes: routes$1
1787
+ };
1788
+ const mountRoute = publicConfig.mountRoute ?? `/${definition.name}`;
1789
+ const mergedFetcherConfig = mergeFetcherConfigs(authorFetcherConfig, publicConfig.fetcherConfig);
1790
+ return new ClientBuilder({
1791
+ ...publicConfig,
1792
+ mountRoute,
1793
+ fetcherConfig: mergedFetcherConfig
1794
+ }, fragmentConfig);
1795
+ }
1796
+
1797
+ //#endregion
1798
+ //#region src/definition.ts
1799
+ const formsFragmentDef = defineFragment("forms").extend((x) => x).withDependencies(() => {}).providesBaseService(() => {}).build();
1800
+
1801
+ //#endregion
1802
+ //#region src/models.ts
1803
+ const JSONSchemaSchema = z.record(z.string(), z.unknown());
1804
+ const UISchemaElementSchema = z.record(z.string(), z.unknown());
1805
+ const FormStatusSchema = z.enum([
1806
+ "draft",
1807
+ "open",
1808
+ "closed",
1809
+ "static"
1810
+ ]);
1811
+ const FormSchema = z.object({
1812
+ id: z.string(),
1813
+ title: z.string(),
1814
+ description: z.string().nullable(),
1815
+ slug: z.string(),
1816
+ status: FormStatusSchema,
1817
+ dataSchema: JSONSchemaSchema,
1818
+ uiSchema: UISchemaElementSchema,
1819
+ version: z.number(),
1820
+ createdAt: z.date(),
1821
+ updatedAt: z.date()
1822
+ });
1823
+ const NewFormSchema = FormSchema.omit({
1824
+ id: true,
1825
+ createdAt: true,
1826
+ updatedAt: true,
1827
+ version: true
1828
+ });
1829
+ const UpdateFormSchema = NewFormSchema.partial();
1830
+ const ResponseMetadataSchema = z.object({
1831
+ ip: z.union([z.ipv4(), z.ipv6()]).nullable(),
1832
+ userAgent: z.string().max(4096).nullable()
1833
+ });
1834
+ const ResponseSchema = z.object({
1835
+ id: z.string(),
1836
+ formId: z.string().nullable().describe("Form ID (static form ID or database form external ID)"),
1837
+ formVersion: z.number(),
1838
+ data: z.record(z.string(), z.unknown()),
1839
+ submittedAt: z.date(),
1840
+ ip: z.string().max(45).nullable(),
1841
+ userAgent: z.string().max(512).nullable()
1842
+ });
1843
+ const NewResponseSchema = ResponseSchema.omit({
1844
+ id: true,
1845
+ submittedAt: true,
1846
+ formId: true,
1847
+ formVersion: true,
1848
+ ip: true,
1849
+ userAgent: true
1850
+ }).extend({ securityToken: z.string().optional() });
1851
+
1852
+ //#endregion
1853
+ //#region src/routes.ts
1854
+ /** Extract and validate request metadata from headers (untrusted input) */
1855
+ const publicRoutes = defineRoutes(formsFragmentDef).create(({ services, defineRoute, config }) => {
1856
+ return [defineRoute({
1857
+ method: "GET",
1858
+ path: "/:slug",
1859
+ outputSchema: FormSchema,
1860
+ errorCodes: ["NOT_FOUND"],
1861
+ handler: () => {}
1862
+ }), defineRoute({
1863
+ method: "POST",
1864
+ path: "/:slug/submit",
1865
+ inputSchema: NewResponseSchema,
1866
+ outputSchema: z.string(),
1867
+ errorCodes: [
1868
+ "NOT_FOUND",
1869
+ "VALIDATION_ERROR",
1870
+ "FORM_NOT_OPEN"
1871
+ ],
1872
+ handler: () => {}
1873
+ })];
1874
+ });
1875
+ const adminRoutes = defineRoutes(formsFragmentDef).create(({ services, defineRoute, config }) => {
1876
+ return [
1877
+ defineRoute({
1878
+ method: "GET",
1879
+ path: "/admin/forms",
1880
+ outputSchema: z.array(FormSchema),
1881
+ handler: () => {}
1882
+ }),
1883
+ defineRoute({
1884
+ method: "POST",
1885
+ path: "/admin/forms",
1886
+ inputSchema: NewFormSchema,
1887
+ outputSchema: z.string(),
1888
+ errorCodes: ["CREATE_FAILED"],
1889
+ handler: () => {}
1890
+ }),
1891
+ defineRoute({
1892
+ method: "PUT",
1893
+ path: "/admin/forms/:id",
1894
+ inputSchema: UpdateFormSchema,
1895
+ errorCodes: ["NOT_FOUND", "STATIC_FORM_READ_ONLY"],
1896
+ handler: () => {}
1897
+ }),
1898
+ defineRoute({
1899
+ method: "DELETE",
1900
+ path: "/admin/forms/:id",
1901
+ errorCodes: ["NOT_FOUND", "STATIC_FORM_READ_ONLY"],
1902
+ handler: () => {}
1903
+ }),
1904
+ defineRoute({
1905
+ method: "GET",
1906
+ path: "/admin/forms/:id/submissions",
1907
+ queryParameters: ["sortOrder"],
1908
+ outputSchema: z.array(ResponseSchema),
1909
+ handler: () => {}
1910
+ }),
1911
+ defineRoute({
1912
+ method: "GET",
1913
+ path: "/admin/submissions/:id",
1914
+ outputSchema: ResponseSchema,
1915
+ errorCodes: ["NOT_FOUND"],
1916
+ handler: () => {}
1917
+ }),
1918
+ defineRoute({
1919
+ method: "DELETE",
1920
+ path: "/admin/submissions/:id",
1921
+ errorCodes: ["NOT_FOUND"],
1922
+ handler: () => {}
1923
+ })
1924
+ ];
1925
+ });
1926
+
1927
+ //#endregion
1928
+ //#region src/index.ts
1929
+ const routes = [publicRoutes, adminRoutes];
1930
+ function createFormsFragment(config = {}, options) {
1931
+ return {};
1932
+ }
1933
+ function createFormsClients(fragnoConfig) {
1934
+ const b = createClientBuilder(formsFragmentDef, fragnoConfig, routes);
1935
+ return {
1936
+ useForm: b.createHook("/:slug"),
1937
+ useSubmitForm: b.createMutator("POST", "/:slug/submit"),
1938
+ useForms: b.createHook("/admin/forms"),
1939
+ useCreateForm: b.createMutator("POST", "/admin/forms"),
1940
+ useUpdateForm: b.createMutator("PUT", "/admin/forms/:id"),
1941
+ useDeleteForm: b.createMutator("DELETE", "/admin/forms/:id"),
1942
+ useSubmissions: b.createHook("/admin/forms/:id/submissions"),
1943
+ useSubmission: b.createHook("/admin/submissions/:id"),
1944
+ useDeleteSubmission: b.createMutator("DELETE", "/admin/submissions/:id")
1945
+ };
1946
+ }
1947
+
1948
+ //#endregion
1949
+ export { atom, createFormsClients, createFormsFragment, formsFragmentDef, isGetHook, isMutatorHook, isReadableAtom, isStore, routes };
1950
+ //# sourceMappingURL=src-DElBPLyi.js.map