@sveltejs/kit 3.0.0-next.19 → 3.0.0-next.20

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 (44) hide show
  1. package/package.json +12 -12
  2. package/src/core/adapt/builder.js +2 -1
  3. package/src/core/adapt/index.js +1 -1
  4. package/src/core/env.js +1 -1
  5. package/src/core/postbuild/prerender.js +61 -8
  6. package/src/core/sync/write_env.js +1 -1
  7. package/src/core/sync/write_server.js +2 -2
  8. package/src/core/sync/write_tsconfig/index.js +1 -3
  9. package/src/core/sync/write_tsconfig/utils.js +0 -24
  10. package/src/exports/env/index.js +1 -1
  11. package/src/exports/env/public.d.ts +55 -0
  12. package/src/exports/hooks/public.d.ts +194 -0
  13. package/src/exports/hooks/sequence.js +4 -3
  14. package/src/exports/index.js +1 -1
  15. package/src/exports/internal/env.js +1 -1
  16. package/src/exports/params/public.d.ts +2 -2
  17. package/src/exports/public.d.ts +1 -757
  18. package/src/exports/vite/dev/index.js +2 -2
  19. package/src/exports/vite/index.js +541 -384
  20. package/src/exports/vite/utils.js +0 -16
  21. package/src/runtime/app/internal/transport.js +1 -1
  22. package/src/runtime/app/server/public.d.ts +519 -0
  23. package/src/runtime/app/server/remote/command.js +1 -1
  24. package/src/runtime/app/server/remote/form.js +1 -1
  25. package/src/runtime/app/server/remote/prerender.js +1 -1
  26. package/src/runtime/app/server/remote/query.js +2 -1
  27. package/src/runtime/app/server/remote/requested.js +1 -1
  28. package/src/runtime/client/client.js +1 -1
  29. package/src/runtime/client/remote-functions/command.svelte.js +1 -1
  30. package/src/runtime/client/remote-functions/form.svelte.js +1 -1
  31. package/src/runtime/client/remote-functions/prerender.svelte.js +1 -1
  32. package/src/runtime/client/remote-functions/query/index.js +1 -1
  33. package/src/runtime/client/remote-functions/query-batch.svelte.js +1 -1
  34. package/src/runtime/client/remote-functions/query-live/index.js +1 -1
  35. package/src/runtime/client/remote-functions/shared.svelte.js +1 -1
  36. package/src/runtime/server/errors.js +2 -2
  37. package/src/runtime/server/internal.js +39 -0
  38. package/src/runtime/server/page/load_data.js +1 -1
  39. package/src/runtime/server/remote-functions.js +17 -5
  40. package/src/runtime/server/respond.js +1 -1
  41. package/src/types/internal.d.ts +14 -13
  42. package/src/version.js +1 -1
  43. package/types/index.d.ts +1403 -1403
  44. package/types/index.d.ts.map +68 -62
@@ -16,13 +16,10 @@ import {
16
16
  PrerenderUnseenRoutesHandlerValue,
17
17
  PrerenderOption,
18
18
  RequestOptions,
19
- RouteSegment,
20
- DeepPartial,
21
- IsAny
19
+ RouteSegment
22
20
  } from '../types/private.js';
23
21
  import { BuildData, SSRNodeLoader, SSRRoute, ValidatedConfig } from 'types';
24
22
  import { SvelteConfig } from '@sveltejs/vite-plugin-svelte';
25
- import { StandardSchemaV1 } from '@standard-schema/spec';
26
23
  import { Plugin } from 'vite';
27
24
  import { RouteId as AppRouteId, LayoutParams as AppLayoutParams } from '$app/types';
28
25
  import { ParamMatcher } from '@sveltejs/kit/params';
@@ -32,21 +29,6 @@ export { PrerenderOption } from '../types/private.js';
32
29
  // @ts-ignore this is an optional peer dependency so could be missing. Written like this so dts-buddy preserves the ts-ignore
33
30
  type Span = import('@opentelemetry/api').Span;
34
31
 
35
- type AppErrorWithOptionalDefaults = Omit<App.Error, 'status' | 'message'> & {
36
- status?: App.Error['status'];
37
- message?: App.Error['message'];
38
- };
39
-
40
- /**
41
- * `void` is only a valid `handleError` return when `App.Error` adds no required properties
42
- * beyond `status` and `message` — both of which are optional in the return, since they default
43
- * to those of the caught error. If `App.Error` is augmented with required properties, the hook
44
- * must return them, so returning nothing becomes a type error.
45
- */
46
- type VoidIfNoRequiredAppErrorProperties = { status: number; message: string } extends App.Error
47
- ? void
48
- : never;
49
-
50
32
  /**
51
33
  * [Adapters](https://svelte.dev/docs/kit/adapters) are responsible for taking the production build and turning it into something that can be deployed to a platform of your choosing.
52
34
  */
@@ -949,152 +931,6 @@ export interface KitConfig {
949
931
  };
950
932
  }
951
933
 
952
- /**
953
- * The [`handle`](https://svelte.dev/docs/kit/hooks#handle) hook runs every time the SvelteKit server receives a [request](https://svelte.dev/docs/kit/web-standards#Fetch-APIs-Request) and
954
- * determines the [response](https://svelte.dev/docs/kit/web-standards#Fetch-APIs-Response).
955
- * It receives an `event` object representing the request and a function called `resolve`, which renders the route and generates a `Response`.
956
- * This allows you to modify response headers or bodies, or bypass SvelteKit entirely (for implementing routes programmatically, for example).
957
- */
958
- export type Handle = (input: {
959
- event: RequestEvent;
960
- resolve: (event: RequestEvent, opts?: ResolveOptions) => Promise<Response>;
961
- }) => MaybePromise<Response>;
962
-
963
- type CaughtErrorMap = {
964
- app: App.Error;
965
- framework: { status: number; message: string };
966
- unknown: unknown;
967
- };
968
-
969
- type ValidationCaughtError<Issue extends StandardSchemaV1.Issue> = {
970
- kind: 'validation';
971
- error: { status: number; message: string };
972
- issues: Issue[];
973
- };
974
-
975
- /**
976
- * The error passed to the [`handleError`](https://svelte.dev/docs/kit/hooks#handleError) hooks.
977
- * Use the `kind` discriminant to distinguish errors from your app (thrown with the
978
- * [`error`](https://svelte.dev/docs/kit/errors#App-errors) helper), errors generated by
979
- * SvelteKit itself (such as 404s), validation errors, and unknown errors (thrown by your code,
980
- * or code it calls).
981
- */
982
- export type CaughtError<Issue extends StandardSchemaV1.Issue = StandardSchemaV1.Issue> =
983
- | {
984
- [Kind in keyof CaughtErrorMap]: {
985
- /** Identifies the category and origin of the error */
986
- kind: Kind;
987
- /** The caught error. Its type depends on `kind` */
988
- error: CaughtErrorMap[Kind];
989
- /** Only present for validation errors */
990
- issues?: undefined;
991
- };
992
- }[keyof CaughtErrorMap]
993
- | ValidationCaughtError<Issue>;
994
-
995
- /** The error passed to the client-side `handleError` hook. */
996
- export type ClientCaughtError = Exclude<CaughtError, { kind: 'validation' }>;
997
-
998
- /**
999
- * The server-side [`handleError`](https://svelte.dev/docs/kit/hooks#handleError) hook runs for every error thrown while responding to a request, except redirects.
1000
- *
1001
- * The `kind` property discriminates between _app_ errors (thrown with the [`error`](https://svelte.dev/docs/kit/errors#App-errors) helper),
1002
- * _framework_ errors (generated by SvelteKit itself, such as 404s), _validation_ errors (caused by invalid remote function arguments)
1003
- * and _unknown_ errors (thrown by your code, or code it calls).
1004
- *
1005
- * The hook returns an object matching `App.Error`, in which `status` and `message` are optional — return them only to
1006
- * override the defaults. Omitted properties are inherited from the caught error: the body passed to `error(...)` for app errors,
1007
- * the status and safe message for framework and validation errors, and `500`/`'Internal Error'` for unknown errors. Return nothing to
1008
- * keep the defaults entirely (if you augment `App.Error` with required properties, you must return those).
1009
- *
1010
- * Make sure that this function _never_ throws an error.
1011
- */
1012
- export type HandleServerError<Issue extends StandardSchemaV1.Issue = StandardSchemaV1.Issue> = (
1013
- input: CaughtError<Issue> & { event: RequestEvent }
1014
- ) => MaybePromise<AppErrorWithOptionalDefaults | VoidIfNoRequiredAppErrorProperties>;
1015
-
1016
- /**
1017
- * The client-side [`handleError`](https://svelte.dev/docs/kit/hooks#handleError) hook runs for every error thrown while navigating, except redirects.
1018
- * Errors that were already transformed by the server-side hook are not passed to it a second time.
1019
- *
1020
- * The `kind` property discriminates between _app_ errors (thrown with the [`error`](https://svelte.dev/docs/kit/errors#App-errors) helper),
1021
- * _framework_ errors (generated by SvelteKit itself, such as 404s) and _unknown_ errors (thrown by your code, or code it calls).
1022
- *
1023
- * The hook returns an object matching `App.Error`, in which `status` and `message` are optional — return them only to
1024
- * override the defaults. Omitted properties are inherited from the caught error: the body passed to `error(...)` for app errors,
1025
- * the status and safe message for framework errors, and `500`/`'Internal Error'` for unknown errors. Return nothing to
1026
- * keep the defaults entirely (if you augment `App.Error` with required properties, you must return those).
1027
- *
1028
- * Make sure that this function _never_ throws an error.
1029
- */
1030
- export type HandleClientError = (
1031
- input: ClientCaughtError & { event: NavigationEvent }
1032
- ) => MaybePromise<AppErrorWithOptionalDefaults | VoidIfNoRequiredAppErrorProperties>;
1033
-
1034
- /**
1035
- * The [`handleFetch`](https://svelte.dev/docs/kit/hooks#handleFetch) hook allows you to modify (or replace) the result of an [`event.fetch`](https://svelte.dev/docs/kit/load#Making-fetch-requests) call that runs on the server (or during prerendering) inside an endpoint, `load`, `action`, `handle`, `handleError` or `reroute`.
1036
- */
1037
- export type HandleFetch = (input: {
1038
- event: RequestEvent;
1039
- request: Request;
1040
- fetch: typeof fetch;
1041
- }) => MaybePromise<Response>;
1042
-
1043
- /**
1044
- * The [`init`](https://svelte.dev/docs/kit/hooks#init) will be invoked before the server responds to its first request
1045
- * @since 2.10.0
1046
- */
1047
- export type ServerInit = () => MaybePromise<void>;
1048
-
1049
- /**
1050
- * The [`init`](https://svelte.dev/docs/kit/hooks#init) will be invoked once the app starts in the browser
1051
- * @since 2.10.0
1052
- */
1053
- export type ClientInit = () => MaybePromise<void>;
1054
-
1055
- /**
1056
- * The [`reroute`](https://svelte.dev/docs/kit/hooks#reroute) hook allows you to modify the URL before it is used to determine which route to render.
1057
- * @since 2.3.0
1058
- */
1059
- export type Reroute = (event: { url: URL; fetch: typeof fetch }) => MaybePromise<void | string>;
1060
-
1061
- /**
1062
- * The [`transport`](https://svelte.dev/docs/kit/hooks#transport) hook allows you to transport custom types across the server/client boundary.
1063
- *
1064
- * Each transporter has a pair of `encode` and `decode` functions. On the server, `encode` determines whether a value is an instance of the custom type and, if so, returns a non-falsy encoding of the value which can be an object or an array (or `false` otherwise).
1065
- *
1066
- * In the browser, `decode` turns the encoding back into an instance of the custom type.
1067
- *
1068
- * ```ts
1069
- * import type { Transport } from '@sveltejs/kit';
1070
- *
1071
- * declare class MyCustomType {
1072
- * data: any
1073
- * }
1074
- *
1075
- * // hooks.js
1076
- * export const transport: Transport = {
1077
- * MyCustomType: {
1078
- * encode: (value) => value instanceof MyCustomType && [value.data],
1079
- * decode: ([data]) => new MyCustomType(data)
1080
- * }
1081
- * };
1082
- * ```
1083
- * @since 2.11.0
1084
- */
1085
- export type Transport = Record<string, Transporter>;
1086
-
1087
- /**
1088
- * A member of the [`transport`](https://svelte.dev/docs/kit/hooks#transport) hook.
1089
- */
1090
- export interface Transporter<
1091
- T = any,
1092
- U = any /* minus falsy values, but we can't properly express that */
1093
- > {
1094
- encode: (value: T) => false | U;
1095
- decode: (data: U) => T;
1096
- }
1097
-
1098
934
  /**
1099
935
  * The generic form of `PageLoad` and `LayoutLoad`. You should import those from `./$types` (see [generated types](https://svelte.dev/docs/kit/types#Generated-types))
1100
936
  * rather than using `Load` directly.
@@ -1255,67 +1091,6 @@ export interface NavigationEvent<
1255
1091
  url: URL;
1256
1092
  }
1257
1093
 
1258
- /**
1259
- * A single entry yielded by [`requested`](https://svelte.dev/docs/kit/$app-server#requested)
1260
- * when called with a regular `query`. `arg` is the validated argument (the input *after*
1261
- * the query's schema validated and transformed it, if applicable); `query` is a
1262
- * `RemoteQuery` bound to the client's original cache key, so `refresh()` / `set()` will
1263
- * update the correct client entry.
1264
- */
1265
- export type RequestedEntry<Validated, Output> = {
1266
- arg: Validated;
1267
- query: RemoteQuery<Output>;
1268
- };
1269
-
1270
- /**
1271
- * A single entry yielded by [`requested`](https://svelte.dev/docs/kit/$app-server#requested)
1272
- * when called with a `query.live`. `arg` is the validated argument; `query` is a
1273
- * `RemoteLiveQuery` bound to the client's original cache key, so `reconnect()` targets
1274
- * the correct client subscription.
1275
- */
1276
- export type LiveRequestedEntry<Validated, Output> = {
1277
- arg: Validated;
1278
- query: RemoteLiveQuery<Output>;
1279
- };
1280
-
1281
- export type QueryRequestedResult<Validated, Output> = Iterable<RequestedEntry<Validated, Output>> &
1282
- AsyncIterable<RequestedEntry<Validated, Output>> & {
1283
- /**
1284
- * Call `refresh` on all queries selected by this `requested` invocation.
1285
- * This is identical to:
1286
- * ```ts
1287
- * import { requested } from '$app/server';
1288
- *
1289
- * for await (const { query } of requested(getPost, ...)) {
1290
- * void query.refresh();
1291
- * }
1292
- * ```
1293
- */
1294
- refreshAll: () => Promise<void>;
1295
- };
1296
-
1297
- export type LiveQueryRequestedResult<Validated, Output> = Iterable<
1298
- LiveRequestedEntry<Validated, Output>
1299
- > &
1300
- AsyncIterable<LiveRequestedEntry<Validated, Output>> & {
1301
- /**
1302
- * Call `reconnect` on all live queries selected by this `requested` invocation.
1303
- * This is identical to:
1304
- * ```ts
1305
- * import { requested } from '$app/server';
1306
- *
1307
- * for await (const { query } of requested(liveQuery, ...)) {
1308
- * void query.reconnect();
1309
- * }
1310
- * ```
1311
- */
1312
- reconnectAll: () => Promise<void>;
1313
- };
1314
-
1315
- export type RequestedResult<Validated, Output> =
1316
- | QueryRequestedResult<Validated, Output>
1317
- | LiveQueryRequestedResult<Validated, Output>;
1318
-
1319
1094
  export interface RequestEvent<
1320
1095
  Params extends AppLayoutParams<'/'> = AppLayoutParams<'/'>,
1321
1096
  RouteId extends AppRouteId | null = AppRouteId | null
@@ -1447,31 +1222,6 @@ export type RequestHandler<
1447
1222
  RouteId extends AppRouteId | null = AppRouteId | null
1448
1223
  > = (event: RequestEvent<Params, RouteId>) => MaybePromise<Response>;
1449
1224
 
1450
- export interface ResolveOptions {
1451
- /**
1452
- * Applies custom transforms to HTML. If `done` is true, it's the final chunk. Chunks are not guaranteed to be well-formed HTML
1453
- * (they could include an element's opening tag but not its closing tag, for example)
1454
- * but they will always be split at sensible boundaries such as `%sveltekit.head%` or layout/page components.
1455
- * @param input the html chunk and the info if this is the last chunk
1456
- */
1457
- transformPageChunk?: (input: { html: string; done: boolean }) => MaybePromise<string | undefined>;
1458
- /**
1459
- * Determines which headers should be included in serialized responses when a `load` function loads a resource with `fetch`.
1460
- * By default, none will be included.
1461
- * @param name header name
1462
- * @param value header value
1463
- */
1464
- filterSerializedResponseHeaders?: (name: string, value: string) => boolean;
1465
- /**
1466
- * Determines which files should be preloaded. Files are preloaded via `<link>` tags added to the
1467
- * `<head>` tag; if `output.linkHeaderPreload` is enabled, dynamically rendered pages use the
1468
- * [`Link` response header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link) instead.
1469
- * By default, `js` and `css` files will be preloaded.
1470
- * @param input the type of the file and its path
1471
- */
1472
- preload?: (input: { type: 'font' | 'css' | 'js' | 'asset'; path: string }) => boolean;
1473
- }
1474
-
1475
1225
  export interface RouteDefinition<Config = any> {
1476
1226
  id: string;
1477
1227
  api: {
@@ -1664,510 +1414,4 @@ export interface Snapshot<T = any> {
1664
1414
  restore: (snapshot: T) => void;
1665
1415
  }
1666
1416
 
1667
- // If T is unknown or has an index signature, the types below will recurse indefinitely and create giant unions that TS can't handle
1668
- type WillRecurseIndefinitely<T> = unknown extends T ? true : string extends keyof T ? true : false;
1669
-
1670
- // Input type mappings for form fields
1671
- type InputTypeMap = {
1672
- text: string;
1673
- email: string;
1674
- password: string;
1675
- url: string;
1676
- tel: string;
1677
- search: string;
1678
- number: number;
1679
- range: number;
1680
- date: string;
1681
- 'datetime-local': string;
1682
- time: string;
1683
- month: string;
1684
- week: string;
1685
- color: string;
1686
- checkbox: boolean | string[];
1687
- radio: string;
1688
- file: File;
1689
- hidden: string | number | boolean;
1690
- submit: string | number | boolean;
1691
- button: string;
1692
- reset: string;
1693
- image: string;
1694
- select: string;
1695
- 'select multiple': string[];
1696
- 'file multiple': File[];
1697
- };
1698
-
1699
- // Valid input types for a given value type
1700
- export type RemoteFormFieldType<T> = {
1701
- [K in keyof InputTypeMap]: T extends InputTypeMap[K] ? K : never;
1702
- }[keyof InputTypeMap];
1703
-
1704
- // Input element properties based on type
1705
- type InputElementProps<T extends keyof InputTypeMap> = T extends 'checkbox' | 'radio'
1706
- ? {
1707
- name: string;
1708
- type: T;
1709
- value?: string;
1710
- 'aria-invalid': boolean | 'false' | 'true' | undefined;
1711
- get checked(): boolean;
1712
- set checked(value: boolean);
1713
- readonly defaultChecked?: boolean;
1714
- }
1715
- : T extends 'file'
1716
- ? {
1717
- name: string;
1718
- type: 'file';
1719
- 'aria-invalid': boolean | 'false' | 'true' | undefined;
1720
- get files(): FileList | null;
1721
- set files(v: FileList | null);
1722
- }
1723
- : T extends 'select'
1724
- ? {
1725
- name: string;
1726
- 'aria-invalid': boolean | 'false' | 'true' | undefined;
1727
- get value(): string;
1728
- set value(v: string);
1729
- }
1730
- : T extends 'select multiple'
1731
- ? {
1732
- name: string;
1733
- multiple: true;
1734
- 'aria-invalid': boolean | 'false' | 'true' | undefined;
1735
- get value(): string[];
1736
- set value(v: string[]);
1737
- }
1738
- : T extends 'text'
1739
- ? {
1740
- name: string;
1741
- 'aria-invalid': boolean | 'false' | 'true' | undefined;
1742
- get value(): string | number;
1743
- set value(v: string | number);
1744
- readonly defaultValue?: string | number;
1745
- }
1746
- : {
1747
- name: string;
1748
- type: T;
1749
- 'aria-invalid': boolean | 'false' | 'true' | undefined;
1750
- get value(): string | number;
1751
- set value(v: string | number);
1752
- readonly defaultValue?: string | number;
1753
- };
1754
-
1755
- type RemoteFormFieldMethods<T> = {
1756
- /** The values that will be submitted */
1757
- value(): DeepPartial<T>;
1758
- /** Set the values that will be submitted */
1759
- set(input: DeepPartial<T>): DeepPartial<T>;
1760
- /** Whether the field or any nested field has been interacted with since the form was mounted */
1761
- touched(): boolean;
1762
- /** Whether the field or any nested field has been edited since the form was mounted */
1763
- dirty(): boolean;
1764
- /** Validation issues, if any */
1765
- issues(): RemoteFormIssue[] | undefined;
1766
- };
1767
-
1768
- // These two types use "T extends unknown ? .. : .." to distribute over unions.
1769
- // Example: if "type T = A | b" then "keyof T" only contains keys that both A and B have, with "KeysOfUnion<T>" we get the keys of both A and B
1770
- type KeysOfUnion<T> = T extends unknown ? keyof T : never;
1771
- type ValueOfUnionKey<T, K extends PropertyKey> = T extends unknown
1772
- ? K extends keyof T
1773
- ? T[K]
1774
- : never
1775
- : never;
1776
-
1777
- export type RemoteFormFieldValue = string | string[] | number | boolean | File | File[];
1778
-
1779
- type AsArgs<Type extends keyof InputTypeMap, Value> = Type extends 'checkbox'
1780
- ? Value extends string[]
1781
- ? [type: Type, value: Value[number] | (string & {})]
1782
- : Value extends boolean
1783
- ? [type: Type] | [type: Type, value: boolean]
1784
- : [type: Type] | [type: Type, value: Value | (string & {})]
1785
- : Type extends 'submit' | 'hidden'
1786
- ? Value extends string
1787
- ? [type: Type, value: Value | (string & {})]
1788
- : [type: Type, value: Value]
1789
- : Type extends 'radio'
1790
- ? [type: Type, value: Value | (string & {})]
1791
- : Type extends 'file' | 'file multiple'
1792
- ? [type: Type]
1793
- : [type: Type] | [type: Type, value: Value | undefined];
1794
-
1795
- /**
1796
- * Form field accessor type that provides name(), value(), and issues() methods
1797
- */
1798
- export type RemoteFormField<Value extends RemoteFormFieldValue> = RemoteFormFieldMethods<Value> & {
1799
- /**
1800
- * Returns an object that can be spread onto an input element with the correct type attribute,
1801
- * aria-invalid attribute if the field is invalid, and appropriate value/checked property getters/setters.
1802
- * @example
1803
- * ```svelte
1804
- * <input {...myForm.fields.myString.as('text')} />
1805
- * <input {...myForm.fields.myNumber.as('number')} />
1806
- * <input {...myForm.fields.myBoolean.as('checkbox')} />
1807
- * ```
1808
- */
1809
- as<T extends RemoteFormFieldType<Value>>(...args: AsArgs<T, Value>): InputElementProps<T>;
1810
- };
1811
-
1812
- type RemoteFormFieldContainer<Value> = RemoteFormFieldMethods<Value> & {
1813
- /** Validation issues belonging to this or any of the fields that belong to it, if any */
1814
- allIssues(): RemoteFormIssue[] | undefined;
1815
- };
1816
-
1817
- type UnknownField<Value> = RemoteFormFieldMethods<Value> & {
1818
- /** Validation issues belonging to this or any of the fields that belong to it, if any */
1819
- allIssues(): RemoteFormIssue[] | undefined;
1820
- /**
1821
- * Returns an object that can be spread onto an input element with the correct type attribute,
1822
- * aria-invalid attribute if the field is invalid, and appropriate value/checked property getters/setters.
1823
- * @example
1824
- * ```svelte
1825
- * <input {...myForm.fields.myString.as('text')} />
1826
- * <input {...myForm.fields.myNumber.as('number')} />
1827
- * <input {...myForm.fields.myBoolean.as('checkbox')} />
1828
- * ```
1829
- */
1830
- as<T extends RemoteFormFieldType<Value>>(...args: AsArgs<T, Value>): InputElementProps<T>;
1831
- } & {
1832
- [key: string | number]: UnknownField<any>;
1833
- };
1834
-
1835
- type RemoteFormFieldsRoot<Input extends RemoteFormInput | void> =
1836
- IsAny<Input> extends true
1837
- ? RecursiveFormFields
1838
- : Input extends void
1839
- ? {
1840
- /** Validation issues, if any */
1841
- issues(): RemoteFormIssue[] | undefined;
1842
- /** Validation issues belonging to this or any of the fields that belong to it, if any */
1843
- allIssues(): RemoteFormIssue[] | undefined;
1844
- }
1845
- : RemoteFormFields<Input>;
1846
-
1847
- /**
1848
- * Recursive type to build form fields structure with proxy access
1849
- */
1850
- export type RemoteFormFields<T> =
1851
- WillRecurseIndefinitely<T> extends true
1852
- ? RecursiveFormFields
1853
- : NonNullable<T> extends string | number | boolean | File
1854
- ? RemoteFormField<NonNullable<T>>
1855
- : // [NonNullable<T>] is used to prevent distributing over union while still allowing
1856
- // nullable wrappers (e.g. `string[] | undefined` from a schema with `.default([])`)
1857
- // to be treated as arrays; only the last condition should distribute over unions
1858
- [NonNullable<T>] extends [string[] | File[]]
1859
- ? RemoteFormField<NonNullable<T>> & {
1860
- [K in number]: RemoteFormField<NonNullable<T>[number]>;
1861
- }
1862
- : [NonNullable<T>] extends [Array<infer U>]
1863
- ? RemoteFormFieldContainer<NonNullable<T>> & {
1864
- [K in number]: RemoteFormFields<U>;
1865
- }
1866
- : RemoteFormFieldContainer<T> & {
1867
- [K in KeysOfUnion<T>]-?: RemoteFormFields<ValueOfUnionKey<T, K>>;
1868
- };
1869
-
1870
- // By breaking this out into its own type, we avoid the TS recursion depth limit
1871
- type RecursiveFormFields = RemoteFormFieldContainer<any> & {
1872
- [key: string | number]: UnknownField<any>;
1873
- };
1874
-
1875
- type MaybeArray<T> = T | T[];
1876
-
1877
- export interface RemoteFormInput {
1878
- [key: string]: MaybeArray<string | number | boolean | File | RemoteFormInput> | undefined;
1879
- }
1880
-
1881
- export interface RemoteFormIssue {
1882
- message: string;
1883
- path: Array<string | number>;
1884
- }
1885
-
1886
- // If the schema specifies `id` as a string or number, ensure that `for(...)`
1887
- // only accepts that type. Otherwise, accept `string | number`
1888
- type ExtractId<Input> = Input extends { id: infer Id }
1889
- ? Id extends string | number
1890
- ? Id
1891
- : string | number
1892
- : string | number;
1893
-
1894
- /**
1895
- * A function and proxy object used to imperatively create validation errors in form handlers.
1896
- *
1897
- * Access properties to create field-specific issues: `issue.fieldName('message')`.
1898
- * The type structure mirrors the input data structure for type-safe field access.
1899
- * Call `invalid(issue.foo(...), issue.nested.bar(...))` to throw a validation error.
1900
- */
1901
- export type InvalidField<T> =
1902
- WillRecurseIndefinitely<T> extends true
1903
- ? Record<string | number, any>
1904
- : NonNullable<T> extends string | number | boolean | File
1905
- ? (message: string) => StandardSchemaV1.Issue
1906
- : NonNullable<T> extends Array<infer U>
1907
- ? {
1908
- [K in number]: InvalidField<U>;
1909
- } & ((message: string) => StandardSchemaV1.Issue)
1910
- : NonNullable<T> extends RemoteFormInput
1911
- ? {
1912
- [K in keyof T]-?: InvalidField<T[K]>;
1913
- } & ((message: string) => StandardSchemaV1.Issue)
1914
- : Record<string, never>;
1915
-
1916
- /**
1917
- * A validation error thrown by `invalid`.
1918
- */
1919
- export interface ValidationError {
1920
- /** The validation issues */
1921
- issues: StandardSchemaV1.Issue[];
1922
- }
1923
-
1924
- /**
1925
- * The form instance as received inside an `enhance` callback. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#form) for full documentation.
1926
- */
1927
- export type RemoteFormEnhanceInstance<
1928
- Input extends RemoteFormInput | void = RemoteFormInput | void,
1929
- Output = any
1930
- > = Omit<RemoteForm<Input, Output>, 'enhance' | 'element'> & {
1931
- readonly element: HTMLFormElement;
1932
- };
1933
-
1934
- /**
1935
- * The callback passed to a remote form's `enhance` method. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#form) for full documentation.
1936
- */
1937
- export type RemoteFormEnhanceCallback<
1938
- Input extends RemoteFormInput | void = RemoteFormInput | void,
1939
- Output = any
1940
- > = (form: RemoteFormEnhanceInstance<Input, Output>) => MaybePromise<void>;
1941
-
1942
- /**
1943
- * The type of a remote `form` function. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#form) for full documentation.
1944
- */
1945
- export type RemoteForm<Input extends RemoteFormInput | void, Output> = {
1946
- /** Attachment that sets up an event handler that intercepts the form submission on the client to prevent a full page reload */
1947
- [attachment: symbol]: (node: HTMLFormElement) => void;
1948
- method: 'POST';
1949
- /** The URL to send the form to. */
1950
- action: string;
1951
- /** The `<form>` element this instance is currently attached to, if any. */
1952
- get element(): HTMLFormElement | null;
1953
- /** Submit the currently attached form programmatically. */
1954
- submit(): Promise<boolean> & {
1955
- updates: (...updates: RemoteQueryUpdate[]) => Promise<boolean>;
1956
- };
1957
- /** Use the `enhance` method to influence what happens when the form is submitted. */
1958
- enhance(callback: RemoteFormEnhanceCallback<Input, Output>): {
1959
- method: 'POST';
1960
- action: string;
1961
- [attachment: symbol]: (node: HTMLFormElement) => void;
1962
- };
1963
- /**
1964
- * Create an instance of the form for the given `id`.
1965
- * The `id` is stringified and used for deduplication to potentially reuse existing instances.
1966
- * Useful when you have multiple forms that use the same remote form action, for example in a loop.
1967
- * ```svelte
1968
- * {#each todos as todo}
1969
- * {@const todoForm = updateTodo.for(todo.id)}
1970
- * <form {...todoForm}>
1971
- * {#if todoForm.result?.invalid}<p>Invalid data</p>{/if}
1972
- * ...
1973
- * </form>
1974
- * {/each}
1975
- * ```
1976
- */
1977
- for(id: ExtractId<Input>): Omit<RemoteForm<Input, Output>, 'for'>;
1978
- /** Preflight checks */
1979
- preflight(schema: StandardSchemaV1<Input, any>): RemoteForm<Input, Output>;
1980
- /** Validate the form contents programmatically */
1981
- validate(options?: {
1982
- /**
1983
- * Set this to `true` to also show validation issues of fields that haven't yet been
1984
- * edited and blurred. This option is ignored for forms that have previously been
1985
- * submitted, in which case all fields are always subject to validation
1986
- * (unless the form is reset, at which point it is treated as pristine)
1987
- */
1988
- all?: boolean;
1989
- /** Set this to `true` to only run the `preflight` validation. */
1990
- preflightOnly?: boolean;
1991
- }): Promise<void>;
1992
- /** The result of the form submission */
1993
- get result(): Output | undefined;
1994
- /** The number of pending submissions */
1995
- get pending(): number;
1996
- /** True if the form has been submitted at least once, and hasn't been reset since */
1997
- get submitted(): boolean;
1998
- /** Access form fields using object notation */
1999
- fields: RemoteFormFieldsRoot<Input>;
2000
- };
2001
-
2002
- /**
2003
- * The type of a remote `command` function. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#command) for full documentation.
2004
- */
2005
- export type RemoteCommand<Input, Output> = {
2006
- (arg: undefined extends Input ? Input | void : Input): Promise<Output> & {
2007
- updates(...updates: RemoteQueryUpdate[]): Promise<Output>;
2008
- };
2009
- /** The number of pending command executions */
2010
- get pending(): number;
2011
- };
2012
-
2013
- export type RemoteQueryUpdate =
2014
- | RemoteQuery<any>
2015
- | RemoteLiveQuery<any>
2016
- | RemoteQueryFunction<any, any>
2017
- | RemoteLiveQueryFunction<any, any>
2018
- | RemoteQueryOverride;
2019
-
2020
- export type RemoteResource<T> = Promise<T> & {
2021
- /** The error in case the query fails. */
2022
- get error(): App.Error | undefined;
2023
- /** `true` before the first result is available and during refreshes */
2024
- get loading(): boolean;
2025
- } & (
2026
- | {
2027
- /** The current value of the query. Undefined until `ready` is `true` */
2028
- get current(): undefined;
2029
- ready: false;
2030
- }
2031
- | {
2032
- /** The current value of the query. Undefined until `ready` is `true` */
2033
- get current(): T;
2034
- ready: true;
2035
- }
2036
- );
2037
-
2038
- export type RemoteQuery<T> = RemoteResource<T> & {
2039
- /**
2040
- * On the client, this function will update the value of the query without re-fetching it.
2041
- *
2042
- * On the server, this can be called in the context of a `command` or `form` and the specified data will accompany the action response back to the client.
2043
- * This prevents SvelteKit needing to refresh all queries on the page in a second server round-trip.
2044
- */
2045
- set(value: T): void;
2046
- /**
2047
- * On the client, this function will re-fetch the query from the server.
2048
- *
2049
- * On the server, this can be called in the context of a `command` or `form` and the refreshed data will accompany the action response back to the client.
2050
- * This prevents SvelteKit needing to refresh all queries on the page in a second server round-trip.
2051
- */
2052
- refresh(): Promise<void>;
2053
- /**
2054
- * Temporarily override a query's value during a [single-flight mutation](https://svelte.dev/docs/kit/remote-functions#Single-flight-mutations) to provide optimistic updates.
2055
- *
2056
- * ```svelte
2057
- * <script>
2058
- * import { getTodos, addTodo } from './todos.remote.js';
2059
- * const todos = getTodos();
2060
- * </script>
2061
- *
2062
- * <form {...addTodo.enhance(async (form) => {
2063
- * await form.submit().updates(
2064
- * todos.withOverride((todos) => [...todos, { text: form.fields.text.value() }])
2065
- * );
2066
- * })}>
2067
- * <input type="text" name="text" />
2068
- * <button type="submit">Add Todo</button>
2069
- * </form>
2070
- * ```
2071
- */
2072
- withOverride(update: (current: T) => T): RemoteQueryOverride;
2073
- };
2074
-
2075
- export type RemoteLiveQuery<T> = RemoteResource<T> &
2076
- AsyncIterable<T> & {
2077
- /** `true` if the live stream is currently connected. */
2078
- readonly connected: boolean;
2079
- /** `true` once the current live stream iterator is done. */
2080
- readonly done: boolean;
2081
- /** Reconnects the live stream immediately. */
2082
- reconnect(): Promise<void>;
2083
- };
2084
-
2085
- export type RemoteQueryOverride = () => void;
2086
-
2087
- /**
2088
- * The type of a remote `prerender` function. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#prerender) for full documentation.
2089
- */
2090
- export type RemotePrerenderFunction<Input, Output> = (
2091
- arg: undefined extends Input ? Input | void : Input
2092
- ) => RemoteResource<Output>;
2093
-
2094
- /**
2095
- * The return value of a remote `query` function. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#query) for full documentation.
2096
- *
2097
- * The optional `Validated` generic parameter represents the argument type *after* the
2098
- * query's schema has validated and (optionally) transformed it — this is the type the
2099
- * query's implementation function receives on the server, and the type yielded by
2100
- * [`requested`](https://svelte.dev/docs/kit/$app-server#requested). For queries declared
2101
- * with [Standard Schema](https://standardschema.dev/) it differs from `Input` when the
2102
- * schema contains a transform (e.g. `v.pipe(v.number(), v.transform(String))` has
2103
- * `Input = number` but `Validated = string`). For `'unchecked'` validators and queries
2104
- * without arguments it defaults to `Input`.
2105
- */
2106
- export type RemoteQueryFunction<Input, Output, _Validated = Input> = (
2107
- arg: undefined extends Input ? Input | void : Input
2108
- ) => RemoteQuery<Output>;
2109
-
2110
- /**
2111
- * The type of a remote `query.live` function. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#query.live) for full documentation.
2112
- *
2113
- * The optional `Validated` generic parameter represents the argument type *after* the
2114
- * query's schema has validated and (optionally) transformed it, and matches the type
2115
- * yielded by [`requested`](https://svelte.dev/docs/kit/$app-server#requested).
2116
- */
2117
- export type RemoteLiveQueryFunction<Input, Output, _Validated = Input> = (
2118
- arg: undefined extends Input ? Input | void : Input
2119
- ) => RemoteLiveQuery<Output>;
2120
-
2121
- /**
2122
- * [Environment variables](https://svelte.dev/docs/kit/environment-variables) can be configured by exporting
2123
- * a `variables` object from `src/env.ts`, using [`defineEnvVars`](https://svelte.dev/docs/kit/@sveltejs-kit-env#defineEnvVars).
2124
- */
2125
- export interface EnvVarConfig<T> {
2126
- /**
2127
- * Whether the environment variable can be accessed by client-side code.
2128
- * - if `true`, it can be imported from `$app/env/public`
2129
- * - if `false`, it can be imported from `$app/env/private`, which is a [server-only module](https://svelte.dev/docs/kit/server-only-modules)
2130
- * @default false
2131
- */
2132
- public?: boolean;
2133
- /**
2134
- * Whether the value is determined at build time or when the app runs.
2135
- * - if `true`, the build time value is inlined into the bundle. This enables optimisations like dead-code elimination
2136
- * - if `false`, the value is read from the environment when the app starts
2137
- * @default false
2138
- */
2139
- static?: boolean;
2140
- /**
2141
- * A [Standard Schema](https://standardschema.dev/) validator that is applied to the value when the app starts.
2142
- * Alternatively, a function that returns the (possibly transformed) value, or throws an error explaining
2143
- * the problem. Returning `undefined` is valid, so a function can describe an optional variable.
2144
- * The validator can output any value — not necessarily a string — but public, non-static values must be
2145
- * serializable by [devalue](https://github.com/sveltejs/devalue) so that they can be sent to the browser.
2146
- *
2147
- * If omitted, the value must be set, but may be an empty string.
2148
- */
2149
- schema?: StandardSchemaV1<string | undefined, T> | ((value: string | undefined) => T | undefined);
2150
- /**
2151
- * A description of the variable that will be used for inline documentation on hover.
2152
- */
2153
- description?: string;
2154
- }
2155
-
2156
- /**
2157
- * The return type of [`defineEnvVars`](https://svelte.dev/docs/kit/@sveltejs-kit-env#defineEnvVars).
2158
- */
2159
- export type DefinedEnvVars<T extends Record<string, EnvVarConfig<any>>> = {
2160
- readonly [K in keyof T]: EnvVarEntry<T[K]>;
2161
- };
2162
-
2163
- /**
2164
- * Normalizes an environment variable config's schema (standard schema or function) to standard schema.
2165
- */
2166
- type EnvVarEntry<C extends EnvVarConfig<any>> =
2167
- C['schema'] extends StandardSchemaV1<any, any>
2168
- ? C
2169
- : C['schema'] extends (value: any) => infer R
2170
- ? Omit<C, 'schema'> & { schema: StandardSchemaV1<string | undefined, R> }
2171
- : C;
2172
-
2173
1417
  export * from './index.js';