svelte-effect-runtime 3.4.4 → 3.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/.dist/chunks/{client-5cizmHtc.js → client-CsVu54pU.js} +119 -56
  2. package/.dist/chunks/client-CsVu54pU.js.map +1 -0
  3. package/.dist/chunks/{descriptors-OS4VfJuW.js → descriptors-cOe9uRJz.js} +2 -2
  4. package/.dist/chunks/descriptors-cOe9uRJz.js.map +1 -0
  5. package/.dist/chunks/{dispatcher-C38yhZRs.js → dispatcher-LtvFKYUp.js} +3 -3
  6. package/.dist/chunks/dispatcher-LtvFKYUp.js.map +1 -0
  7. package/.dist/chunks/{dispatcher-DuBLHNa6.js → dispatcher-pHH4JcFR.js} +12 -11
  8. package/.dist/chunks/dispatcher-pHH4JcFR.js.map +1 -0
  9. package/.dist/chunks/{errors-0PaWf7Bq.js → errors-Dcf0MVbq.js} +3 -3
  10. package/.dist/chunks/errors-Dcf0MVbq.js.map +1 -0
  11. package/.dist/chunks/{runtime-C51Dv3K6.js → runtime-ekkMQ-wL.js} +4 -4
  12. package/.dist/chunks/runtime-ekkMQ-wL.js.map +1 -0
  13. package/.dist/chunks/{transform-Cdsx7ygD.js → transform-02e-RvXH.js} +246 -172
  14. package/.dist/chunks/transform-02e-RvXH.js.map +1 -0
  15. package/.dist/chunks/{vite-DR3-WaqQ.js → vite-Dkwdtbgj.js} +118 -34
  16. package/.dist/chunks/vite-Dkwdtbgj.js.map +1 -0
  17. package/.dist/detect.js.map +1 -1
  18. package/.dist/dispatcher.d.ts +1 -1
  19. package/.dist/dispatcher.js +1 -1
  20. package/.dist/errors.d.ts +1 -1
  21. package/.dist/internal/generators.js +2 -2
  22. package/.dist/internal/remote-client.js +1 -1
  23. package/.dist/markup/promise.js +1 -1
  24. package/.dist/markup/promise.js.map +1 -1
  25. package/.dist/markup/run.js +3 -3
  26. package/.dist/markup/run.js.map +1 -1
  27. package/.dist/markup/transform/index.d.ts +8 -0
  28. package/.dist/markup/transform/scan.d.ts +20 -0
  29. package/.dist/markup/transform/types.d.ts +136 -2
  30. package/.dist/markup/transform.js +1 -1
  31. package/.dist/markup/value.js +1 -1
  32. package/.dist/markup/value.js.map +1 -1
  33. package/.dist/mod.d.ts +1 -1
  34. package/.dist/mod.js +3 -3
  35. package/.dist/mod.js.map +1 -1
  36. package/.dist/remote/client/command.d.ts +1 -1
  37. package/.dist/remote/client/effect.d.ts +15 -1
  38. package/.dist/remote/client/index.d.ts +1 -1
  39. package/.dist/remote/client/types.d.ts +3 -1
  40. package/.dist/remote/client.js +1 -1
  41. package/.dist/remote/server.d.ts +8 -5
  42. package/.dist/remote/server.js +38 -15
  43. package/.dist/remote/server.js.map +1 -1
  44. package/.dist/remote/shared.d.ts +126 -0
  45. package/.dist/remote/shared.js +117 -5
  46. package/.dist/remote/shared.js.map +1 -1
  47. package/.dist/runtime/transform.js +2 -2
  48. package/.dist/runtime/transform.js.map +1 -1
  49. package/.dist/script-transform/types.d.ts +95 -0
  50. package/.dist/server/control-flow.d.ts +70 -4
  51. package/.dist/server/effects.d.ts +15 -1
  52. package/.dist/server/factories.d.ts +128 -9
  53. package/.dist/server/index.d.ts +2 -2
  54. package/.dist/server/runtime.d.ts +1 -1
  55. package/.dist/server/types.d.ts +128 -7
  56. package/.dist/server.d.ts +1 -1
  57. package/.dist/server.js +40 -23
  58. package/.dist/server.js.map +1 -1
  59. package/.dist/vite.js +1 -1
  60. package/LICENSE +29 -0
  61. package/package.json +21 -21
  62. package/.dist/chunks/client-5cizmHtc.js.map +0 -1
  63. package/.dist/chunks/descriptors-OS4VfJuW.js.map +0 -1
  64. package/.dist/chunks/dispatcher-C38yhZRs.js.map +0 -1
  65. package/.dist/chunks/dispatcher-DuBLHNa6.js.map +0 -1
  66. package/.dist/chunks/errors-0PaWf7Bq.js.map +0 -1
  67. package/.dist/chunks/runtime-C51Dv3K6.js.map +0 -1
  68. package/.dist/chunks/transform-Cdsx7ygD.js.map +0 -1
  69. package/.dist/chunks/vite-DR3-WaqQ.js.map +0 -1
@@ -1,5 +1,7 @@
1
1
  import type { FormIssue } from "./shared.js";
2
2
  import { Cause, Effect } from "effect";
3
+ type SvelteInvalid = (...issues: readonly (FormIssue | string)[]) => never;
4
+ type SvelteError = (status: number, body: unknown) => never;
3
5
  /**
4
6
  * Runs a user-supplied Effect program through a ManagedRuntime, maps its
5
7
  * exit into the shape expected by SvelteKit, and returns the result or
@@ -15,7 +17,7 @@ import { Cause, Effect } from "effect";
15
17
  */
16
18
  export declare function run_remote_effect<A>(effect: Effect.Effect<A, unknown, unknown>, runtime: {
17
19
  runPromise: (e: Effect.Effect<unknown, unknown, unknown>) => Promise<unknown>;
18
- }, invalid: (status: number, body: unknown) => never, error: (status: number, body: unknown) => never): Promise<A>;
20
+ }, invalid: SvelteInvalid, error: SvelteError): Promise<A>;
19
21
  /**
20
22
  * Encodes an Effect Cause into a string that the client-side adapter can
21
23
  * decode back into a typed `RemoteFailure`.
@@ -43,10 +45,10 @@ export declare function encode_remote_failure(cause: Cause.Cause<unknown>): stri
43
45
  * @param invalid - SvelteKit's `invalid` helper bound to the current request.
44
46
  * @internal
45
47
  */
46
- export declare function throw_form_error(issues: readonly FormIssue[], invalid: (status: number, body: unknown) => never): never;
48
+ export declare function throw_form_error(issues: readonly FormIssue[], invalid: SvelteInvalid): never;
47
49
  /**
48
- * Remaps low-level SvelteKit errors (e.g. "Cannot use ___ outside a route")
49
- * into clear, actionable messages.
50
+ * Remaps SvelteKit's unbranded request-context errors into clear, actionable
51
+ * messages.
50
52
  *
51
53
  * @example
52
54
  * ```ts
@@ -63,4 +65,5 @@ export declare function throw_form_error(issues: readonly FormIssue[], invalid:
63
65
  * @returns A more descriptive error.
64
66
  * @internal
65
67
  */
66
- export declare function normalize_remote_helper_error(err: unknown, helper_name: string): globalThis.Error;
68
+ export declare function normalize_remote_helper_error(err: unknown, helper_name: string): Error;
69
+ export {};
@@ -1,9 +1,11 @@
1
- import { h as RemoteHelperError, m as RemoteHelperContextError } from "../chunks/errors-0PaWf7Bq.js";
1
+ import { h as RemoteHelperError, m as RemoteHelperContextError } from "../chunks/errors-Dcf0MVbq.js";
2
2
  import { create_serialized_remote_failure_envelope } from "./shared.js";
3
3
  import { Cause, Effect, Exit } from "effect";
4
4
  import { isHttpError, isRedirect, isValidationError } from "@sveltejs/kit";
5
5
  import { stringify } from "devalue";
6
6
  //#region src/remote/server.ts
7
+ const request_event_context_error_start = "Can only read the current request event inside functions invoked during `handle`";
8
+ const request_store_context_error = "Could not get the request store.";
7
9
  /**
8
10
  * Runs a user-supplied Effect program through a ManagedRuntime, maps its
9
11
  * exit into the shape expected by SvelteKit, and returns the result or
@@ -46,7 +48,7 @@ function handle_failure(cause, invalid, error) {
46
48
  */
47
49
  for (const reason of reasons) if (Cause.isFailReason(reason)) {
48
50
  const failure = reason.error;
49
- if (typeof failure === "object" && failure !== null && failure._tag === "FormError") invalid(400, { issues: failure.issues ?? [] });
51
+ if (typeof failure === "object" && failure !== null && failure._tag === "FormError") invalid(...failure.issues ?? []);
50
52
  }
51
53
  error(500, create_serialized_remote_failure_envelope(encode_remote_failure(cause)));
52
54
  }
@@ -62,13 +64,10 @@ function handle_failure(cause, invalid, error) {
62
64
  function encode_remote_failure(cause) {
63
65
  const reasons = get_cause_reasons(cause);
64
66
  for (const reason of reasons) if (Cause.isFailReason(reason)) {
65
- const failure = reason.error;
66
- const encoded = stringify_failure(failure);
67
+ const encoded = stringify_failure(to_public_remote_failure(reason.error));
67
68
  if (encoded !== void 0) return encoded;
68
- const serializable_encoded = stringify_failure(to_serializable_failure(failure));
69
- if (serializable_encoded !== void 0) return serializable_encoded;
70
69
  }
71
- return stringify({ message: "[UNKNOWN_REMOTE_FAILURE]: Unknown error" });
70
+ return stringify_unknown_remote_failure();
72
71
  }
73
72
  function get_cause_reasons(cause) {
74
73
  return cause.reasons ?? [];
@@ -83,13 +82,18 @@ function stringify_failure(value) {
83
82
  return;
84
83
  }
85
84
  }
86
- function to_serializable_failure(value, seen = /* @__PURE__ */ new WeakSet()) {
85
+ function to_public_remote_failure(value) {
86
+ if (!has_public_remote_failure_tag(value)) return create_unknown_remote_failure();
87
+ return to_serializable_public_failure(value) ?? create_unknown_remote_failure();
88
+ }
89
+ function to_serializable_public_failure(value, seen = /* @__PURE__ */ new WeakSet()) {
87
90
  if (typeof value === "function" || typeof value === "symbol") return;
88
91
  if (!is_object_like(value)) return value;
89
92
  if (stringify_failure(value) !== void 0) return value;
93
+ if (is_internal_object(value)) return;
90
94
  if (seen.has(value)) return;
91
95
  seen.add(value);
92
- const serializable = Array.isArray(value) ? value.map((item) => to_serializable_failure(item, seen)) : to_plain_record(value, seen);
96
+ const serializable = Array.isArray(value) ? value.map((item) => to_serializable_public_failure(item, seen)) : to_plain_record(value, seen);
93
97
  seen.delete(value);
94
98
  return serializable;
95
99
  }
@@ -98,7 +102,7 @@ function to_plain_record(value, seen) {
98
102
  const record = {};
99
103
  for (const [key, descriptor] of Object.entries(descriptors)) {
100
104
  if (key === "stack" || !("value" in descriptor)) continue;
101
- record[key] = to_serializable_failure(descriptor.value, seen);
105
+ record[key] = to_serializable_public_failure(descriptor.value, seen);
102
106
  }
103
107
  if (value instanceof Error && !("message" in record)) record.message = value.message;
104
108
  return record;
@@ -106,6 +110,22 @@ function to_plain_record(value, seen) {
106
110
  function is_object_like(value) {
107
111
  return typeof value === "object" && value !== null;
108
112
  }
113
+ function has_public_remote_failure_tag(value) {
114
+ return is_object_like(value) && typeof value._tag === "string";
115
+ }
116
+ function is_internal_object(value) {
117
+ return !Array.isArray(value) && !is_plain_record(value) && !has_public_remote_failure_tag(value);
118
+ }
119
+ function is_plain_record(value) {
120
+ const prototype = Object.getPrototypeOf(value);
121
+ return prototype === Object.prototype || prototype === null;
122
+ }
123
+ function create_unknown_remote_failure() {
124
+ return { message: "[UNKNOWN_REMOTE_FAILURE]: Unknown error" };
125
+ }
126
+ function stringify_unknown_remote_failure() {
127
+ return stringify(create_unknown_remote_failure());
128
+ }
109
129
  /**
110
130
  * Throws a SvelteKit `invalid` response from a {@link FormError}, calling
111
131
  * through to the request-scoped `invalid` helper.
@@ -124,11 +144,11 @@ function is_object_like(value) {
124
144
  * @internal
125
145
  */
126
146
  function throw_form_error(issues, invalid) {
127
- invalid(400, { issues });
147
+ invalid(...issues);
128
148
  }
129
149
  /**
130
- * Remaps low-level SvelteKit errors (e.g. "Cannot use ___ outside a route")
131
- * into clear, actionable messages.
150
+ * Remaps SvelteKit's unbranded request-context errors into clear, actionable
151
+ * messages.
132
152
  *
133
153
  * @example
134
154
  * ```ts
@@ -146,10 +166,13 @@ function throw_form_error(issues, invalid) {
146
166
  * @internal
147
167
  */
148
168
  function normalize_remote_helper_error(err, helper_name) {
149
- const message = err instanceof Error ? err.message : String(err);
150
- if (message.includes("Cannot use") || message.includes("outside a route")) return new RemoteHelperContextError(helper_name);
169
+ if (is_sveltekit_remote_context_error(err)) return new RemoteHelperContextError(helper_name);
151
170
  return err instanceof Error ? err : new RemoteHelperError(err);
152
171
  }
172
+ function is_sveltekit_remote_context_error(err) {
173
+ if (!(err instanceof Error)) return false;
174
+ return err.message.startsWith(request_event_context_error_start) || err.message === request_store_context_error;
175
+ }
153
176
  //#endregion
154
177
  export { encode_remote_failure, normalize_remote_helper_error, run_remote_effect, throw_form_error };
155
178
 
@@ -1 +1 @@
1
- {"version":3,"file":"server.js","names":[],"sources":["../../../modules/svelte-effect-runtime/src/remote/server.ts"],"sourcesContent":["import { create_serialized_remote_failure_envelope } from \"$/remote/shared.ts\";\nimport type { FormIssue } from \"$/remote/shared.ts\";\nimport { RemoteHelperContextError, RemoteHelperError } from \"$/errors.ts\";\nimport { isHttpError, isRedirect, isValidationError } from \"@sveltejs/kit\";\nimport { Cause, Effect, Exit } from \"effect\";\nimport { stringify } from \"devalue\";\n\ntype CauseReason = {\n readonly _tag: string;\n readonly defect?: unknown;\n readonly error?: unknown;\n};\n\n/**\n * Runs a user-supplied Effect program through a ManagedRuntime, maps its\n * exit into the shape expected by SvelteKit, and returns the result or\n * throws a SvelteKit-compatible error.\n *\n * @since 2.0.0\n * @param effect - The Effect program to execute.\n * @param runtime - The server-side ManagedRuntime.\n * @param invalid - SvelteKit's `invalid` helper (bound per-request).\n * @param error - SvelteKit's `error` helper.\n * @returns A Promise that resolves with the effect's success value.\n * @internal\n */\nexport async function run_remote_effect<A>(\n effect: Effect.Effect<A, unknown, unknown>,\n runtime: {\n runPromise: (\n e: Effect.Effect<unknown, unknown, unknown>,\n ) => Promise<unknown>;\n },\n invalid: (status: number, body: unknown) => never,\n error: (status: number, body: unknown) => never,\n): Promise<A> {\n const exit: Exit.Exit<A, unknown> = await runtime.runPromise(\n Effect.exit(effect) as Effect.Effect<unknown, unknown, unknown>,\n ) as Exit.Exit<A, unknown>;\n\n if (Exit.isSuccess(exit)) {\n return exit.value;\n }\n\n handle_failure(exit.cause, invalid, error);\n}\n\n/**\n * Inspects the Effect Cause for failures and either throws a form\n * validation error (via SvelteKit's `invalid`) or encodes the error\n * and throws it via SvelteKit's `error`.\n */\nfunction handle_failure(\n cause: Cause.Cause<unknown>,\n invalid: (status: number, body: unknown) => never,\n error: (status: number, body: unknown) => never,\n): never {\n const reasons = get_cause_reasons(cause);\n\n /**\n * Phase 1 — let SvelteKit's thrown control-flow sentinels escape.\n */\n for (const reason of reasons) {\n if (!Cause.isDieReason(reason as never)) {\n continue;\n }\n\n const defect = reason.defect;\n\n if (is_sveltekit_control_flow(defect)) {\n throw defect;\n }\n }\n\n /**\n * Phase 2 — let cancellation escape without becoming a remote failure.\n */\n if (Cause.hasInterruptsOnly(cause)) {\n throw Cause.squash(cause);\n }\n\n /**\n * Phase 3 — preserve typed form validation failures.\n */\n for (const reason of reasons) {\n if (Cause.isFailReason(reason as never)) {\n const failure = reason.error;\n if (\n typeof failure === \"object\" &&\n failure !== null &&\n (failure as Record<string, unknown>)._tag === \"FormError\"\n ) {\n const issues = (failure as { issues?: readonly FormIssue[] }).issues ??\n [];\n invalid(400, { issues });\n }\n }\n }\n\n /**\n * Phase 4 — encode all other failures for the remote client.\n */\n const encoded = encode_remote_failure(cause);\n const envelope = create_serialized_remote_failure_envelope(encoded);\n\n error(500, envelope);\n}\n\n/**\n * Encodes an Effect Cause into a string that the client-side adapter can\n * decode back into a typed `RemoteFailure`.\n *\n * @since 2.0.0\n * @param cause - The Effect Cause from a failed execution.\n * @returns A devalue-encoded string representing the serialised failure.\n * @internal\n */\nexport function encode_remote_failure(cause: Cause.Cause<unknown>): string {\n const reasons = get_cause_reasons(cause);\n\n for (const reason of reasons) {\n if (Cause.isFailReason(reason as never)) {\n const failure = reason.error;\n\n const encoded = stringify_failure(failure);\n\n if (encoded !== undefined) {\n return encoded;\n }\n\n const serializable_failure = to_serializable_failure(failure);\n const serializable_encoded = stringify_failure(serializable_failure);\n\n if (serializable_encoded !== undefined) {\n return serializable_encoded;\n }\n }\n }\n\n return stringify({ message: \"[UNKNOWN_REMOTE_FAILURE]: Unknown error\" });\n}\n\nfunction get_cause_reasons(\n cause: Cause.Cause<unknown>,\n): readonly CauseReason[] {\n const reasons = (cause as unknown as { reasons?: readonly CauseReason[] })\n .reasons;\n\n return reasons ?? [];\n}\n\nfunction is_sveltekit_control_flow(value: unknown): boolean {\n return isRedirect(value) || isHttpError(value) || isValidationError(value);\n}\n\nfunction stringify_failure(value: unknown): string | undefined {\n try {\n return stringify(value);\n } catch {\n return undefined;\n }\n}\n\nfunction to_serializable_failure(\n value: unknown,\n seen = new WeakSet<object>(),\n): unknown {\n if (typeof value === \"function\" || typeof value === \"symbol\") {\n return undefined;\n }\n\n if (!is_object_like(value)) {\n return value;\n }\n\n if (stringify_failure(value) !== undefined) {\n return value;\n }\n\n if (seen.has(value)) {\n return undefined;\n }\n\n seen.add(value);\n\n const serializable = Array.isArray(value)\n ? value.map((item) => to_serializable_failure(item, seen))\n : to_plain_record(value, seen);\n\n seen.delete(value);\n\n return serializable;\n}\n\nfunction to_plain_record(\n value: object,\n seen: WeakSet<object>,\n): Record<string, unknown> {\n const descriptors = Object.getOwnPropertyDescriptors(value);\n const record: Record<string, unknown> = {};\n\n for (const [key, descriptor] of Object.entries(descriptors)) {\n if (key === \"stack\" || !(\"value\" in descriptor)) {\n continue;\n }\n\n record[key] = to_serializable_failure(descriptor.value, seen);\n }\n\n if (value instanceof Error && !(\"message\" in record)) {\n record.message = value.message;\n }\n\n return record;\n}\n\nfunction is_object_like(value: unknown): value is object {\n return typeof value === \"object\" && value !== null;\n}\n\n/**\n * Throws a SvelteKit `invalid` response from a {@link FormError}, calling\n * through to the request-scoped `invalid` helper.\n *\n * @example\n * ```ts\n * throw_form_error(\n * [{ message: \"Name is required\", path: [\"name\"] }],\n * invalid,\n * );\n * ```\n *\n * @since 2.0.0\n * @param issues - The list of form validation issues.\n * @param invalid - SvelteKit's `invalid` helper bound to the current request.\n * @internal\n */\nexport function throw_form_error(\n issues: readonly FormIssue[],\n invalid: (status: number, body: unknown) => never,\n): never {\n invalid(400, { issues });\n}\n\n/**\n * Remaps low-level SvelteKit errors (e.g. \"Cannot use ___ outside a route\")\n * into clear, actionable messages.\n *\n * @example\n * ```ts\n * try {\n * return native_query(handler);\n * } catch (err) {\n * throw normalize_remote_helper_error(err, \"Query\");\n * }\n * ```\n *\n * @since 2.0.0\n * @param err - The error thrown by SvelteKit's native functions.\n * @param helper_name - Name of the helper that triggered the error.\n * @returns A more descriptive error.\n * @internal\n */\nexport function normalize_remote_helper_error(\n err: unknown,\n helper_name: string,\n): globalThis.Error {\n const message = err instanceof Error ? err.message : String(err);\n\n if (message.includes(\"Cannot use\") || message.includes(\"outside a route\")) {\n return new RemoteHelperContextError(helper_name);\n }\n\n return err instanceof Error ? err : new RemoteHelperError(err);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA0BA,eAAsB,kBACpB,QACA,SAKA,SACA,OACY;CACZ,MAAM,OAA8B,MAAM,QAAQ,WAChD,OAAO,KAAK,MAAM,CACpB;CAEA,IAAI,KAAK,UAAU,IA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/remote/server.ts"],"sourcesContent":["import { create_serialized_remote_failure_envelope } from \"$/remote/shared.ts\";\nimport type { FormIssue } from \"$/remote/shared.ts\";\nimport { RemoteHelperContextError, RemoteHelperError } from \"$/errors.ts\";\nimport { isHttpError, isRedirect, isValidationError } from \"@sveltejs/kit\";\nimport { Cause, Effect, Exit } from \"effect\";\nimport { stringify } from \"devalue\";\n\ntype CauseReason = {\n\treadonly _tag: string;\n\treadonly defect?: unknown;\n\treadonly error?: unknown;\n};\n\ntype SvelteInvalid = (...issues: readonly (FormIssue | string)[]) => never;\n\ntype SvelteError = (status: number, body: unknown) => never;\n\nconst request_event_context_error_start =\n\t\"Can only read the current request event inside functions invoked during `handle`\";\n\nconst request_store_context_error = \"Could not get the request store.\";\n\n/**\n * Runs a user-supplied Effect program through a ManagedRuntime, maps its\n * exit into the shape expected by SvelteKit, and returns the result or\n * throws a SvelteKit-compatible error.\n *\n * @since 2.0.0\n * @param effect - The Effect program to execute.\n * @param runtime - The server-side ManagedRuntime.\n * @param invalid - SvelteKit's `invalid` helper (bound per-request).\n * @param error - SvelteKit's `error` helper.\n * @returns A Promise that resolves with the effect's success value.\n * @internal\n */\nexport async function run_remote_effect<A>(\n\teffect: Effect.Effect<A, unknown, unknown>,\n\truntime: {\n\t\trunPromise: (e: Effect.Effect<unknown, unknown, unknown>) => Promise<unknown>;\n\t},\n\tinvalid: SvelteInvalid,\n\terror: SvelteError,\n): Promise<A> {\n\tconst exit: Exit.Exit<A, unknown> = (await runtime.runPromise(\n\t\tEffect.exit(effect) as Effect.Effect<unknown, unknown, unknown>,\n\t)) as Exit.Exit<A, unknown>;\n\n\tif (Exit.isSuccess(exit)) {\n\t\treturn exit.value;\n\t}\n\n\thandle_failure(exit.cause, invalid, error);\n}\n\n/**\n * Inspects the Effect Cause for failures and either throws a form\n * validation error (via SvelteKit's `invalid`) or encodes the error\n * and throws it via SvelteKit's `error`.\n */\nfunction handle_failure(\n\tcause: Cause.Cause<unknown>,\n\tinvalid: SvelteInvalid,\n\terror: SvelteError,\n): never {\n\tconst reasons = get_cause_reasons(cause);\n\n\t/**\n\t * Phase 1 — let SvelteKit's thrown control-flow sentinels escape.\n\t */\n\tfor (const reason of reasons) {\n\t\tif (!Cause.isDieReason(reason as never)) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst defect = reason.defect;\n\n\t\tif (is_sveltekit_control_flow(defect)) {\n\t\t\tthrow defect;\n\t\t}\n\t}\n\n\t/**\n\t * Phase 2 — let cancellation escape without becoming a remote failure.\n\t */\n\tif (Cause.hasInterruptsOnly(cause)) {\n\t\tthrow Cause.squash(cause);\n\t}\n\n\t/**\n\t * Phase 3 — preserve typed form validation failures.\n\t */\n\tfor (const reason of reasons) {\n\t\tif (Cause.isFailReason(reason as never)) {\n\t\t\tconst failure = reason.error;\n\t\t\tif (\n\t\t\t\ttypeof failure === \"object\" &&\n\t\t\t\tfailure !== null &&\n\t\t\t\t(failure as Record<string, unknown>)._tag === \"FormError\"\n\t\t\t) {\n\t\t\t\tconst issues = (failure as { issues?: readonly FormIssue[] }).issues ?? [];\n\t\t\t\tinvalid(...issues);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Phase 4 — encode all other failures for the remote client.\n\t */\n\tconst encoded = encode_remote_failure(cause);\n\tconst envelope = create_serialized_remote_failure_envelope(encoded);\n\n\terror(500, envelope);\n}\n\n/**\n * Encodes an Effect Cause into a string that the client-side adapter can\n * decode back into a typed `RemoteFailure`.\n *\n * @since 2.0.0\n * @param cause - The Effect Cause from a failed execution.\n * @returns A devalue-encoded string representing the serialised failure.\n * @internal\n */\nexport function encode_remote_failure(cause: Cause.Cause<unknown>): string {\n\tconst reasons = get_cause_reasons(cause);\n\n\tfor (const reason of reasons) {\n\t\tif (Cause.isFailReason(reason as never)) {\n\t\t\tconst failure = to_public_remote_failure(reason.error);\n\t\t\tconst encoded = stringify_failure(failure);\n\n\t\t\tif (encoded !== undefined) {\n\t\t\t\treturn encoded;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn stringify_unknown_remote_failure();\n}\n\nfunction get_cause_reasons(cause: Cause.Cause<unknown>): readonly CauseReason[] {\n\tconst reasons = (cause as unknown as { reasons?: readonly CauseReason[] }).reasons;\n\n\treturn reasons ?? [];\n}\n\nfunction is_sveltekit_control_flow(value: unknown): boolean {\n\treturn isRedirect(value) || isHttpError(value) || isValidationError(value);\n}\n\nfunction stringify_failure(value: unknown): string | undefined {\n\ttry {\n\t\treturn stringify(value);\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction to_public_remote_failure(value: unknown): unknown {\n\tif (!has_public_remote_failure_tag(value)) {\n\t\treturn create_unknown_remote_failure();\n\t}\n\n\tconst serializable_failure = to_serializable_public_failure(value);\n\n\treturn serializable_failure ?? create_unknown_remote_failure();\n}\n\nfunction to_serializable_public_failure(value: unknown, seen = new WeakSet<object>()): unknown {\n\tif (typeof value === \"function\" || typeof value === \"symbol\") {\n\t\treturn undefined;\n\t}\n\n\tif (!is_object_like(value)) {\n\t\treturn value;\n\t}\n\n\tif (stringify_failure(value) !== undefined) {\n\t\treturn value;\n\t}\n\n\tif (is_internal_object(value)) {\n\t\treturn undefined;\n\t}\n\n\tif (seen.has(value)) {\n\t\treturn undefined;\n\t}\n\n\tseen.add(value);\n\n\tconst serializable = Array.isArray(value)\n\t\t? value.map((item) => to_serializable_public_failure(item, seen))\n\t\t: to_plain_record(value, seen);\n\n\tseen.delete(value);\n\n\treturn serializable;\n}\n\nfunction to_plain_record(value: object, seen: WeakSet<object>): Record<string, unknown> {\n\tconst descriptors = Object.getOwnPropertyDescriptors(value);\n\tconst record: Record<string, unknown> = {};\n\n\tfor (const [key, descriptor] of Object.entries(descriptors)) {\n\t\tif (key === \"stack\" || !(\"value\" in descriptor)) {\n\t\t\tcontinue;\n\t\t}\n\n\t\trecord[key] = to_serializable_public_failure(descriptor.value, seen);\n\t}\n\n\tif (value instanceof Error && !(\"message\" in record)) {\n\t\trecord.message = value.message;\n\t}\n\n\treturn record;\n}\n\nfunction is_object_like(value: unknown): value is object {\n\treturn typeof value === \"object\" && value !== null;\n}\n\nfunction has_public_remote_failure_tag(value: unknown): boolean {\n\treturn is_object_like(value) && typeof (value as { _tag?: unknown })._tag === \"string\";\n}\n\nfunction is_internal_object(value: object): boolean {\n\treturn (\n\t\t!Array.isArray(value) && !is_plain_record(value) && !has_public_remote_failure_tag(value)\n\t);\n}\n\nfunction is_plain_record(value: object): boolean {\n\tconst prototype = Object.getPrototypeOf(value);\n\n\treturn prototype === Object.prototype || prototype === null;\n}\n\nfunction create_unknown_remote_failure(): { readonly message: string } {\n\treturn { message: \"[UNKNOWN_REMOTE_FAILURE]: Unknown error\" };\n}\n\nfunction stringify_unknown_remote_failure(): string {\n\treturn stringify(create_unknown_remote_failure());\n}\n\n/**\n * Throws a SvelteKit `invalid` response from a {@link FormError}, calling\n * through to the request-scoped `invalid` helper.\n *\n * @example\n * ```ts\n * throw_form_error(\n * [{ message: \"Name is required\", path: [\"name\"] }],\n * invalid,\n * );\n * ```\n *\n * @since 2.0.0\n * @param issues - The list of form validation issues.\n * @param invalid - SvelteKit's `invalid` helper bound to the current request.\n * @internal\n */\nexport function throw_form_error(issues: readonly FormIssue[], invalid: SvelteInvalid): never {\n\tinvalid(...issues);\n}\n\n/**\n * Remaps SvelteKit's unbranded request-context errors into clear, actionable\n * messages.\n *\n * @example\n * ```ts\n * try {\n * return native_query(handler);\n * } catch (err) {\n * throw normalize_remote_helper_error(err, \"Query\");\n * }\n * ```\n *\n * @since 2.0.0\n * @param err - The error thrown by SvelteKit's native functions.\n * @param helper_name - Name of the helper that triggered the error.\n * @returns A more descriptive error.\n * @internal\n */\nexport function normalize_remote_helper_error(err: unknown, helper_name: string): Error {\n\tif (is_sveltekit_remote_context_error(err)) {\n\t\treturn new RemoteHelperContextError(helper_name);\n\t}\n\n\treturn err instanceof Error ? err : new RemoteHelperError(err);\n}\n\nfunction is_sveltekit_remote_context_error(err: unknown): err is Error {\n\tif (!(err instanceof Error)) {\n\t\treturn false;\n\t}\n\n\treturn (\n\t\terr.message.startsWith(request_event_context_error_start) ||\n\t\terr.message === request_store_context_error\n\t);\n}\n"],"mappings":";;;;;;AAiBA,MAAM,oCACL;AAED,MAAM,8BAA8B;;;;;;;;;;;;;;AAepC,eAAsB,kBACrB,QACA,SAGA,SACA,OACa;CACb,MAAM,OAA+B,MAAM,QAAQ,WAClD,OAAO,KAAK,MAAM,CACnB;CAEA,IAAI,KAAK,UAAU,IAAI,GACtB,OAAO,KAAK;CAGb,eAAe,KAAK,OAAO,SAAS,KAAK;AAC1C;;;;;;AAOA,SAAS,eACR,OACA,SACA,OACQ;CACR,MAAM,UAAU,kBAAkB,KAAK;;;;CAKvC,KAAK,MAAM,UAAU,SAAS;EAC7B,IAAI,CAAC,MAAM,YAAY,MAAe,GACrC;EAGD,MAAM,SAAS,OAAO;EAEtB,IAAI,0BAA0B,MAAM,GACnC,MAAM;CAER;;;;CAKA,IAAI,MAAM,kBAAkB,KAAK,GAChC,MAAM,MAAM,OAAO,KAAK;;;;CAMzB,KAAK,MAAM,UAAU,SACpB,IAAI,MAAM,aAAa,MAAe,GAAG;EACxC,MAAM,UAAU,OAAO;EACvB,IACC,OAAO,YAAY,YACnB,YAAY,QACX,QAAoC,SAAS,aAG9C,QAAQ,GADQ,QAA8C,UAAU,CAAC,CACxD;CAEnB;CASD,MAAM,KAFW,0CADD,sBAAsB,KAC2B,CAE/C,CAAC;AACpB;;;;;;;;;;AAWA,SAAgB,sBAAsB,OAAqC;CAC1E,MAAM,UAAU,kBAAkB,KAAK;CAEvC,KAAK,MAAM,UAAU,SACpB,IAAI,MAAM,aAAa,MAAe,GAAG;EAExC,MAAM,UAAU,kBADA,yBAAyB,OAAO,KACR,CAAC;EAEzC,IAAI,YAAY,KAAA,GACf,OAAO;CAET;CAGD,OAAO,iCAAiC;AACzC;AAEA,SAAS,kBAAkB,OAAqD;CAG/E,OAFiB,MAA0D,WAEzD,CAAC;AACpB;AAEA,SAAS,0BAA0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,SAAS;AAC/E;AAEA,SAAS,mBAAmB,OAAwB;CACnD,OACC,CAAC,MAAM,QAAQ,KAAK,KAAK,CAAC,gBAAgB,KAAK,KAAK,CAAC,8BAA8B,KAAK;AAE1F;AAEA,SAAS,gBAAgB,OAAwB;CAChD,MAAM,YAAY,OAAO,eAAe,KAAK;CAE7C,OAAO,cAAc,OAAO,aAAa,cAAc;AACxD;AAEA,SAAS,gCAA8D;CACtE,OAAO,EAAE,SAAS,0CAA0C;AAC7D;AAEA,SAAS,mCAA2C;CACnD,OAAO,UAAU,8BAA8B,CAAC;AACjD;;;;;;;;;;;;;;;;;;AAmBA,SAAgB,iBAAiB,QAA8B,SAA+B;CAC7F,QAAQ,GAAG,MAAM;AAClB;;;;;;;;;;;;;;;;;;;;AAqBA,SAAgB,8BAA8B,KAAc,aAA4B;CACvF,IAAI,kCAAkC,GAAG,GACxC,OAAO,IAAI,yBAAyB,WAAW;CAGhD,OAAO,eAAe,QAAQ,MAAM,IAAI,kBAAkB,GAAG;AAC9D;AAEA,SAAS,kCAAkC,KAA4B;CACtE,IAAI,EAAE,eAAe,QACpB,OAAO;CAGR,OACC,IAAI,QAAQ,WAAW,iCAAiC,KACxD,IAAI,YAAY;AAElB"}
@@ -9,6 +9,13 @@
9
9
  * Well-known marker injected into every serialised remote-failure envelope
10
10
  * so the client can reliably distinguish runtime errors from domain values.
11
11
  *
12
+ * @example
13
+ * ```ts
14
+ * if (envelope[EFFECT_REMOTE_ERROR_MARKER] === true) {
15
+ * console.log(envelope.encoded);
16
+ * }
17
+ * ```
18
+ *
12
19
  * @since 2.0.0
13
20
  * @internal
14
21
  */
@@ -17,6 +24,11 @@ export declare const EFFECT_REMOTE_ERROR_MARKER = "__svelte_effect_remote__";
17
24
  * Well-known symbol used to attach a payload decoder to a remote function
18
25
  * so the client's transport layer can decode domain error types.
19
26
  *
27
+ * @example
28
+ * ```ts
29
+ * Reflect.get(remote_function, REMOTE_ERROR_DECODER);
30
+ * ```
31
+ *
20
32
  * @since 2.0.0
21
33
  * @internal
22
34
  */
@@ -26,6 +38,14 @@ export declare const REMOTE_ERROR_DECODER: unique symbol;
26
38
  * A single field-level or form-level validation issue reported by a
27
39
  * {@link Form} handler.
28
40
  *
41
+ * @example
42
+ * ```ts
43
+ * const issue: FormIssue = {
44
+ * message: "Use an email address.",
45
+ * path: ["email"],
46
+ * };
47
+ * ```
48
+ *
29
49
  * @since 2.0.0
30
50
  */
31
51
  export interface FormIssue {
@@ -39,6 +59,14 @@ export interface FormIssue {
39
59
  * failures. The `issues` array is forwarded to SvelteKit's `invalid()`
40
60
  * helper on the server and surfaced as `FormError` on the client.
41
61
  *
62
+ * @example
63
+ * ```ts
64
+ * const error: FormError = {
65
+ * _tag: "FormError",
66
+ * issues: [{ message: "Required.", path: ["email"] }],
67
+ * };
68
+ * ```
69
+ *
42
70
  * @since 2.0.0
43
71
  */
44
72
  export interface FormError<SchemaType = unknown> {
@@ -50,6 +78,13 @@ export interface FormError<SchemaType = unknown> {
50
78
  /**
51
79
  * Creates a {@link FormError} holding the given issues.
52
80
  *
81
+ * @example
82
+ * ```ts
83
+ * const error = create_form_error([
84
+ * { message: "Required.", path: ["email"] },
85
+ * ]);
86
+ * ```
87
+ *
53
88
  * @since 2.0.0
54
89
  * @param issues - The validation issues to attach.
55
90
  * @returns A FormError with the `_tag` set and no schema reference.
@@ -58,6 +93,13 @@ export declare function create_form_error(issues: readonly FormIssue[]): FormErr
58
93
  /**
59
94
  * Type guard that narrows an unknown value to a {@link FormError}.
60
95
  *
96
+ * @example
97
+ * ```ts
98
+ * if (is_form_error(error)) {
99
+ * console.log(error.issues);
100
+ * }
101
+ * ```
102
+ *
61
103
  * @since 2.0.0
62
104
  * @param value - The value to check.
63
105
  * @returns `true` when the value is a FormError.
@@ -81,6 +123,15 @@ export type RemoteFailure<ErrorType> = ErrorType | RemoteValidationError | Remot
81
123
  * Validation errors that originate from a server-side {@link Form}
82
124
  * handler calling `invalid()` (SvelteKit's `fail`).
83
125
  *
126
+ * @example
127
+ * ```ts
128
+ * const error: RemoteValidationError = {
129
+ * _tag: "RemoteValidationError",
130
+ * status: 400,
131
+ * issues: [{ message: "Required.", path: ["email"] }],
132
+ * };
133
+ * ```
134
+ *
84
135
  * @since 2.0.0
85
136
  */
86
137
  export interface RemoteValidationError {
@@ -93,6 +144,15 @@ export interface RemoteValidationError {
93
144
  * HTTP-level errors with a status code and optional response body. Raised
94
145
  * when the server explicitly calls SvelteKit's `error(status, body)`.
95
146
  *
147
+ * @example
148
+ * ```ts
149
+ * const error: RemoteHttpError = {
150
+ * _tag: "RemoteHttpError",
151
+ * status: 404,
152
+ * body: { message: "Not found" },
153
+ * };
154
+ * ```
155
+ *
96
156
  * @since 2.0.0
97
157
  */
98
158
  export interface RemoteHttpError {
@@ -105,6 +165,14 @@ export interface RemoteHttpError {
105
165
  * Transport-level errors raised by the client adapter when the network
106
166
  * request fails or the response cannot be decoded.
107
167
  *
168
+ * @example
169
+ * ```ts
170
+ * const error: RemoteTransportError = {
171
+ * _tag: "RemoteTransportError",
172
+ * cause: new Error("Network unavailable"),
173
+ * };
174
+ * ```
175
+ *
108
176
  * @since 2.0.0
109
177
  */
110
178
  export interface RemoteTransportError {
@@ -116,6 +184,14 @@ export interface RemoteTransportError {
116
184
  * Wire format for an encoded remote failure. The server wraps domain
117
185
  * errors in this envelope before serialising them with devalue.
118
186
  *
187
+ * @example
188
+ * ```ts
189
+ * const envelope: SerializedRemoteFailureEnvelope = {
190
+ * __svelte_effect_remote__: true,
191
+ * encoded: "[\"DomainError\"]",
192
+ * };
193
+ * ```
194
+ *
119
195
  * @since 2.0.0
120
196
  * @internal
121
197
  */
@@ -127,6 +203,13 @@ export interface SerializedRemoteFailureEnvelope {
127
203
  /**
128
204
  * Creates a {@link RemoteValidationError} from a list of form issues.
129
205
  *
206
+ * @example
207
+ * ```ts
208
+ * const error = create_remote_validation_error([
209
+ * { message: "Required.", path: ["email"] },
210
+ * ]);
211
+ * ```
212
+ *
130
213
  * @since 2.0.0
131
214
  * @param issues - The validation issues reported by the server handler.
132
215
  * @param body - Optional response body returned alongside the error.
@@ -138,6 +221,11 @@ export declare function create_remote_validation_error(issues: readonly FormIssu
138
221
  /**
139
222
  * Creates a {@link RemoteHttpError} for a given HTTP status.
140
223
  *
224
+ * @example
225
+ * ```ts
226
+ * const error = create_remote_http_error(404, { message: "Not found" });
227
+ * ```
228
+ *
141
229
  * @since 2.0.0
142
230
  * @param status - The HTTP status code.
143
231
  * @param body - Optional response body.
@@ -149,6 +237,11 @@ export declare function create_remote_http_error(status: number, body?: unknown,
149
237
  /**
150
238
  * Creates a {@link RemoteTransportError} for network or decode failures.
151
239
  *
240
+ * @example
241
+ * ```ts
242
+ * const error = create_remote_transport_error(new Error("Network unavailable"));
243
+ * ```
244
+ *
152
245
  * @since 2.0.0
153
246
  * @param cause - The underlying error that caused the transport failure.
154
247
  * @param body - Optional response body if one was received.
@@ -160,6 +253,11 @@ export declare function create_remote_transport_error(cause: unknown, body?: unk
160
253
  * Wraps a devalue-encoded error string inside a
161
254
  * {@link SerializedRemoteFailureEnvelope}.
162
255
  *
256
+ * @example
257
+ * ```ts
258
+ * const envelope = create_serialized_remote_failure_envelope(encoded);
259
+ * ```
260
+ *
163
261
  * @since 2.0.0
164
262
  * @param encoded - The devalue-encoded error value.
165
263
  * @returns The wire-format envelope.
@@ -170,6 +268,13 @@ export declare function create_serialized_remote_failure_envelope(encoded: strin
170
268
  /**
171
269
  * Checks whether a value is a {@link SerializedRemoteFailureEnvelope}.
172
270
  *
271
+ * @example
272
+ * ```ts
273
+ * if (is_serialized_remote_failure_envelope(value)) {
274
+ * console.log(value.encoded);
275
+ * }
276
+ * ```
277
+ *
173
278
  * @since 2.0.0
174
279
  * @param value - The value to check.
175
280
  * @returns `true` when the value is a serialised failure envelope.
@@ -179,6 +284,13 @@ export declare function is_serialized_remote_failure_envelope(value: unknown): v
179
284
  /**
180
285
  * Checks whether a value is a {@link RemoteValidationError}.
181
286
  *
287
+ * @example
288
+ * ```ts
289
+ * if (is_remote_validation_error(error)) {
290
+ * console.log(error.issues);
291
+ * }
292
+ * ```
293
+ *
182
294
  * @since 2.0.0
183
295
  * @param value - The value to check.
184
296
  * @returns `true` when the value is a remote validation error.
@@ -187,6 +299,13 @@ export declare function is_remote_validation_error(value: unknown): value is Rem
187
299
  /**
188
300
  * Checks whether a value is a {@link RemoteHttpError}.
189
301
  *
302
+ * @example
303
+ * ```ts
304
+ * if (is_remote_http_error(error)) {
305
+ * console.log(error.status);
306
+ * }
307
+ * ```
308
+ *
190
309
  * @since 2.0.0
191
310
  * @param value - The value to check.
192
311
  * @returns `true` when the value is a remote HTTP error.
@@ -195,6 +314,13 @@ export declare function is_remote_http_error(value: unknown): value is RemoteHtt
195
314
  /**
196
315
  * Checks whether a value is a {@link RemoteTransportError}.
197
316
  *
317
+ * @example
318
+ * ```ts
319
+ * if (is_remote_transport_error(error)) {
320
+ * console.error(error.cause);
321
+ * }
322
+ * ```
323
+ *
198
324
  * @since 2.0.0
199
325
  * @param value - The value to check.
200
326
  * @returns `true` when the value is a remote transport error.
@@ -1,3 +1,4 @@
1
+ import { Schema } from "effect";
1
2
  //#region src/remote/shared.ts
2
3
  /**
3
4
  * Shared types, markers, and constructors used by both the client-side remote
@@ -10,6 +11,13 @@
10
11
  * Well-known marker injected into every serialised remote-failure envelope
11
12
  * so the client can reliably distinguish runtime errors from domain values.
12
13
  *
14
+ * @example
15
+ * ```ts
16
+ * if (envelope[EFFECT_REMOTE_ERROR_MARKER] === true) {
17
+ * console.log(envelope.encoded);
18
+ * }
19
+ * ```
20
+ *
13
21
  * @since 2.0.0
14
22
  * @internal
15
23
  */
@@ -18,6 +26,11 @@ const EFFECT_REMOTE_ERROR_MARKER = "__svelte_effect_remote__";
18
26
  * Well-known symbol used to attach a payload decoder to a remote function
19
27
  * so the client's transport layer can decode domain error types.
20
28
  *
29
+ * @example
30
+ * ```ts
31
+ * Reflect.get(remote_function, REMOTE_ERROR_DECODER);
32
+ * ```
33
+ *
21
34
  * @since 2.0.0
22
35
  * @internal
23
36
  */
@@ -25,6 +38,13 @@ const REMOTE_ERROR_DECODER = Symbol.for("svelte-effect-runtime/remote-error-deco
25
38
  /**
26
39
  * Creates a {@link FormError} holding the given issues.
27
40
  *
41
+ * @example
42
+ * ```ts
43
+ * const error = create_form_error([
44
+ * { message: "Required.", path: ["email"] },
45
+ * ]);
46
+ * ```
47
+ *
28
48
  * @since 2.0.0
29
49
  * @param issues - The validation issues to attach.
30
50
  * @returns A FormError with the `_tag` set and no schema reference.
@@ -38,17 +58,66 @@ function create_form_error(issues) {
38
58
  /**
39
59
  * Type guard that narrows an unknown value to a {@link FormError}.
40
60
  *
61
+ * @example
62
+ * ```ts
63
+ * if (is_form_error(error)) {
64
+ * console.log(error.issues);
65
+ * }
66
+ * ```
67
+ *
41
68
  * @since 2.0.0
42
69
  * @param value - The value to check.
43
70
  * @returns `true` when the value is a FormError.
44
71
  */
45
72
  function is_form_error(value) {
46
- return typeof value === "object" && value !== null && value._tag === "FormError";
73
+ return is_form_error_value(value);
47
74
  }
75
+ const FormIssuePathSegmentSchema = Schema.Union([Schema.String, Schema.Number]);
76
+ const FormIssueSchema = Schema.Struct({
77
+ message: Schema.String,
78
+ path: Schema.Array(FormIssuePathSegmentSchema)
79
+ });
80
+ const FormErrorSchema = Schema.Struct({
81
+ _tag: Schema.Literal("FormError"),
82
+ issues: Schema.Array(FormIssueSchema)
83
+ });
84
+ const SerializedRemoteFailureEnvelopeSchema = Schema.Struct({
85
+ __svelte_effect_remote__: Schema.Literal(true),
86
+ encoded: Schema.String
87
+ });
88
+ const RemoteValidationErrorSchema = Schema.Struct({
89
+ _tag: Schema.Literal("RemoteValidationError"),
90
+ body: Schema.optional(Schema.Unknown),
91
+ issues: Schema.optional(Schema.Array(FormIssueSchema)),
92
+ status: Schema.Number
93
+ });
94
+ const RemoteHttpErrorSchema = Schema.Struct({
95
+ _tag: Schema.Literal("RemoteHttpError"),
96
+ body: Schema.optional(Schema.Unknown),
97
+ cause: Schema.optional(Schema.Unknown),
98
+ status: Schema.Number
99
+ });
100
+ const RemoteTransportErrorSchema = Schema.Struct({
101
+ _tag: Schema.Literal("RemoteTransportError"),
102
+ body: Schema.optional(Schema.Unknown),
103
+ cause: Schema.optional(Schema.Unknown)
104
+ });
105
+ const is_form_error_value = Schema.is(FormErrorSchema);
106
+ const is_remote_http_error_value = Schema.is(RemoteHttpErrorSchema);
107
+ const is_remote_transport_error_value = Schema.is(RemoteTransportErrorSchema);
108
+ const is_remote_validation_error_value = Schema.is(RemoteValidationErrorSchema);
109
+ const is_serialized_remote_failure_envelope_value = Schema.is(SerializedRemoteFailureEnvelopeSchema);
48
110
  /** Constructors for structured remote error values. */
49
111
  /**
50
112
  * Creates a {@link RemoteValidationError} from a list of form issues.
51
113
  *
114
+ * @example
115
+ * ```ts
116
+ * const error = create_remote_validation_error([
117
+ * { message: "Required.", path: ["email"] },
118
+ * ]);
119
+ * ```
120
+ *
52
121
  * @since 2.0.0
53
122
  * @param issues - The validation issues reported by the server handler.
54
123
  * @param body - Optional response body returned alongside the error.
@@ -67,6 +136,11 @@ function create_remote_validation_error(issues, body, status = 400) {
67
136
  /**
68
137
  * Creates a {@link RemoteHttpError} for a given HTTP status.
69
138
  *
139
+ * @example
140
+ * ```ts
141
+ * const error = create_remote_http_error(404, { message: "Not found" });
142
+ * ```
143
+ *
70
144
  * @since 2.0.0
71
145
  * @param status - The HTTP status code.
72
146
  * @param body - Optional response body.
@@ -85,6 +159,11 @@ function create_remote_http_error(status, body, cause) {
85
159
  /**
86
160
  * Creates a {@link RemoteTransportError} for network or decode failures.
87
161
  *
162
+ * @example
163
+ * ```ts
164
+ * const error = create_remote_transport_error(new Error("Network unavailable"));
165
+ * ```
166
+ *
88
167
  * @since 2.0.0
89
168
  * @param cause - The underlying error that caused the transport failure.
90
169
  * @param body - Optional response body if one was received.
@@ -102,6 +181,11 @@ function create_remote_transport_error(cause, body) {
102
181
  * Wraps a devalue-encoded error string inside a
103
182
  * {@link SerializedRemoteFailureEnvelope}.
104
183
  *
184
+ * @example
185
+ * ```ts
186
+ * const envelope = create_serialized_remote_failure_envelope(encoded);
187
+ * ```
188
+ *
105
189
  * @since 2.0.0
106
190
  * @param encoded - The devalue-encoded error value.
107
191
  * @returns The wire-format envelope.
@@ -117,43 +201,71 @@ function create_serialized_remote_failure_envelope(encoded) {
117
201
  /**
118
202
  * Checks whether a value is a {@link SerializedRemoteFailureEnvelope}.
119
203
  *
204
+ * @example
205
+ * ```ts
206
+ * if (is_serialized_remote_failure_envelope(value)) {
207
+ * console.log(value.encoded);
208
+ * }
209
+ * ```
210
+ *
120
211
  * @since 2.0.0
121
212
  * @param value - The value to check.
122
213
  * @returns `true` when the value is a serialised failure envelope.
123
214
  * @internal
124
215
  */
125
216
  function is_serialized_remote_failure_envelope(value) {
126
- return typeof value === "object" && value !== null && value.__svelte_effect_remote__ === true;
217
+ return is_serialized_remote_failure_envelope_value(value);
127
218
  }
128
219
  /**
129
220
  * Checks whether a value is a {@link RemoteValidationError}.
130
221
  *
222
+ * @example
223
+ * ```ts
224
+ * if (is_remote_validation_error(error)) {
225
+ * console.log(error.issues);
226
+ * }
227
+ * ```
228
+ *
131
229
  * @since 2.0.0
132
230
  * @param value - The value to check.
133
231
  * @returns `true` when the value is a remote validation error.
134
232
  */
135
233
  function is_remote_validation_error(value) {
136
- return typeof value === "object" && value !== null && value._tag === "RemoteValidationError";
234
+ return is_remote_validation_error_value(value);
137
235
  }
138
236
  /**
139
237
  * Checks whether a value is a {@link RemoteHttpError}.
140
238
  *
239
+ * @example
240
+ * ```ts
241
+ * if (is_remote_http_error(error)) {
242
+ * console.log(error.status);
243
+ * }
244
+ * ```
245
+ *
141
246
  * @since 2.0.0
142
247
  * @param value - The value to check.
143
248
  * @returns `true` when the value is a remote HTTP error.
144
249
  */
145
250
  function is_remote_http_error(value) {
146
- return typeof value === "object" && value !== null && value._tag === "RemoteHttpError";
251
+ return is_remote_http_error_value(value);
147
252
  }
148
253
  /**
149
254
  * Checks whether a value is a {@link RemoteTransportError}.
150
255
  *
256
+ * @example
257
+ * ```ts
258
+ * if (is_remote_transport_error(error)) {
259
+ * console.error(error.cause);
260
+ * }
261
+ * ```
262
+ *
151
263
  * @since 2.0.0
152
264
  * @param value - The value to check.
153
265
  * @returns `true` when the value is a remote transport error.
154
266
  */
155
267
  function is_remote_transport_error(value) {
156
- return typeof value === "object" && value !== null && value._tag === "RemoteTransportError";
268
+ return is_remote_transport_error_value(value);
157
269
  }
158
270
  //#endregion
159
271
  export { EFFECT_REMOTE_ERROR_MARKER, REMOTE_ERROR_DECODER, create_form_error, create_remote_http_error, create_remote_transport_error, create_remote_validation_error, create_serialized_remote_failure_envelope, is_form_error, is_remote_http_error, is_remote_transport_error, is_remote_validation_error, is_serialized_remote_failure_envelope };