@sveltejs/kit 3.0.0-next.21 → 3.0.0-next.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -5
- package/src/core/postbuild/prerender.js +19 -19
- package/src/core/sync/write_types/index.js +1 -3
- package/src/exports/hooks/public.d.ts +1 -2
- package/src/exports/hooks/sequence.js +1 -1
- package/src/exports/index.js +10 -0
- package/src/exports/internal/server/event.js +1 -1
- package/src/exports/public.d.ts +205 -2
- package/src/exports/vite/dev/index.js +1 -2
- package/src/exports/vite/index.js +2 -2
- package/src/runtime/app/server/public.d.ts +480 -168
- package/src/runtime/app/server/remote/command.js +1 -1
- package/src/runtime/app/server/remote/form.js +5 -5
- package/src/runtime/app/server/remote/prerender.js +1 -1
- package/src/runtime/app/server/remote/query.js +2 -2
- package/src/runtime/app/server/remote/requested.js +3 -3
- package/src/runtime/app/server/remote/shared.js +1 -1
- package/src/runtime/client/remote-functions/command.svelte.js +1 -1
- package/src/runtime/client/remote-functions/form.svelte.js +1 -1
- package/src/runtime/client/remote-functions/prerender.svelte.js +1 -1
- package/src/runtime/client/remote-functions/query/index.js +1 -1
- package/src/runtime/client/remote-functions/query/instance.svelte.js +2 -1
- package/src/runtime/client/remote-functions/query-batch.svelte.js +1 -1
- package/src/runtime/client/remote-functions/query-live/index.js +1 -1
- package/src/runtime/client/remote-functions/query-live/instance.svelte.js +4 -3
- package/src/runtime/client/remote-functions/shared.svelte.js +1 -1
- package/src/runtime/server/cookie.js +2 -2
- package/src/runtime/server/data/index.js +1 -1
- package/src/runtime/server/endpoint.js +3 -3
- package/src/runtime/server/errors.js +2 -2
- package/src/runtime/server/fetch.js +1 -1
- package/src/runtime/server/page/actions.js +1 -2
- package/src/runtime/server/page/data_serializer.js +2 -2
- package/src/runtime/server/page/index.js +1 -2
- package/src/runtime/server/page/load_data.js +3 -3
- package/src/runtime/server/page/render.js +1 -1
- package/src/runtime/server/page/respond_with_error.js +1 -1
- package/src/runtime/server/remote-functions.js +2 -3
- package/src/runtime/server/respond.js +2 -2
- package/src/runtime/server/utils.js +1 -1
- package/src/types/internal.d.ts +2 -2
- package/src/utils/promise.js +25 -0
- package/src/version.js +1 -1
- package/types/index.d.ts +750 -762
- package/types/index.d.ts.map +46 -50
- package/src/exports/remote/index.js +0 -11
- package/src/exports/remote/public.d.ts +0 -519
|
@@ -1,201 +1,513 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import { DeepPartial, IsAny, MaybePromise } from 'types';
|
|
2
3
|
|
|
3
4
|
export * from './index.js';
|
|
4
5
|
|
|
5
|
-
//
|
|
6
|
-
type
|
|
6
|
+
// If T is unknown or has an index signature, the types below will recurse indefinitely and create giant unions that TS can't handle
|
|
7
|
+
type WillRecurseIndefinitely<T> = unknown extends T ? true : string extends keyof T ? true : false;
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
// Input type mappings for form fields
|
|
10
|
+
type InputTypeMap = {
|
|
11
|
+
text: string;
|
|
12
|
+
email: string;
|
|
13
|
+
password: string;
|
|
14
|
+
url: string;
|
|
15
|
+
tel: string;
|
|
16
|
+
search: string;
|
|
17
|
+
number: number;
|
|
18
|
+
range: number;
|
|
19
|
+
date: string;
|
|
20
|
+
'datetime-local': string;
|
|
21
|
+
time: string;
|
|
22
|
+
month: string;
|
|
23
|
+
week: string;
|
|
24
|
+
color: string;
|
|
25
|
+
checkbox: boolean | string[];
|
|
26
|
+
radio: string;
|
|
27
|
+
file: File;
|
|
28
|
+
hidden: string | number | boolean;
|
|
29
|
+
submit: string | number | boolean;
|
|
30
|
+
button: string;
|
|
31
|
+
reset: string;
|
|
32
|
+
image: string;
|
|
33
|
+
select: string;
|
|
34
|
+
'select multiple': string[];
|
|
35
|
+
'file multiple': File[];
|
|
36
|
+
};
|
|
15
37
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
getAll: (opts?: import('cookie').ParseOptions) => Array<{ name: string; value: string }>;
|
|
38
|
+
// Valid input types for a given value type
|
|
39
|
+
export type RemoteFormFieldType<T> = {
|
|
40
|
+
[K in keyof InputTypeMap]: T extends InputTypeMap[K] ? K : never;
|
|
41
|
+
}[keyof InputTypeMap];
|
|
21
42
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
43
|
+
// Input element properties based on type
|
|
44
|
+
type InputElementProps<T extends keyof InputTypeMap> = T extends 'checkbox' | 'radio'
|
|
45
|
+
? {
|
|
46
|
+
name: string;
|
|
47
|
+
type: T;
|
|
48
|
+
value?: string;
|
|
49
|
+
'aria-invalid': boolean | 'false' | 'true' | undefined;
|
|
50
|
+
get checked(): boolean;
|
|
51
|
+
set checked(value: boolean);
|
|
52
|
+
readonly defaultChecked?: boolean;
|
|
53
|
+
}
|
|
54
|
+
: T extends 'file'
|
|
55
|
+
? {
|
|
56
|
+
name: string;
|
|
57
|
+
type: 'file';
|
|
58
|
+
'aria-invalid': boolean | 'false' | 'true' | undefined;
|
|
59
|
+
get files(): FileList | null;
|
|
60
|
+
set files(v: FileList | null);
|
|
61
|
+
}
|
|
62
|
+
: T extends 'select'
|
|
63
|
+
? {
|
|
64
|
+
name: string;
|
|
65
|
+
'aria-invalid': boolean | 'false' | 'true' | undefined;
|
|
66
|
+
get value(): string;
|
|
67
|
+
set value(v: string);
|
|
68
|
+
}
|
|
69
|
+
: T extends 'select multiple'
|
|
70
|
+
? {
|
|
71
|
+
name: string;
|
|
72
|
+
multiple: true;
|
|
73
|
+
'aria-invalid': boolean | 'false' | 'true' | undefined;
|
|
74
|
+
get value(): string[];
|
|
75
|
+
set value(v: string[]);
|
|
76
|
+
}
|
|
77
|
+
: T extends 'text'
|
|
78
|
+
? {
|
|
79
|
+
name: string;
|
|
80
|
+
'aria-invalid': boolean | 'false' | 'true' | undefined;
|
|
81
|
+
get value(): string | number;
|
|
82
|
+
set value(v: string | number);
|
|
83
|
+
readonly defaultValue?: string | number;
|
|
84
|
+
}
|
|
85
|
+
: {
|
|
86
|
+
name: string;
|
|
87
|
+
type: T;
|
|
88
|
+
'aria-invalid': boolean | 'false' | 'true' | undefined;
|
|
89
|
+
get value(): string | number;
|
|
90
|
+
set value(v: string | number);
|
|
91
|
+
readonly defaultValue?: string | number;
|
|
92
|
+
};
|
|
33
93
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
94
|
+
type RemoteFormFieldMethods<T> = {
|
|
95
|
+
/** The values that will be submitted */
|
|
96
|
+
value(): DeepPartial<T>;
|
|
97
|
+
/** Set the values that will be submitted */
|
|
98
|
+
set(input: DeepPartial<T>): DeepPartial<T>;
|
|
99
|
+
/** Whether the field or any nested field has been interacted with since the form was mounted */
|
|
100
|
+
touched(): boolean;
|
|
101
|
+
/** Whether the field or any nested field has been edited since the form was mounted */
|
|
102
|
+
dirty(): boolean;
|
|
103
|
+
/** Validation issues, if any */
|
|
104
|
+
issues(): RemoteFormIssue[] | undefined;
|
|
105
|
+
};
|
|
44
106
|
|
|
107
|
+
// These two types use "T extends unknown ? .. : .." to distribute over unions.
|
|
108
|
+
// 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
|
|
109
|
+
type KeysOfUnion<T> = T extends unknown ? keyof T : never;
|
|
110
|
+
type ValueOfUnionKey<T, K extends PropertyKey> = T extends unknown
|
|
111
|
+
? K extends keyof T
|
|
112
|
+
? T[K]
|
|
113
|
+
: never
|
|
114
|
+
: never;
|
|
115
|
+
|
|
116
|
+
export type RemoteFormFieldValue = string | string[] | number | boolean | File | File[];
|
|
117
|
+
|
|
118
|
+
type AsArgs<Type extends keyof InputTypeMap, Value> = Type extends 'checkbox'
|
|
119
|
+
? Value extends string[]
|
|
120
|
+
? [type: Type, value: Value[number] | (string & {})]
|
|
121
|
+
: Value extends boolean
|
|
122
|
+
? [type: Type] | [type: Type, value: boolean]
|
|
123
|
+
: [type: Type] | [type: Type, value: Value | (string & {})]
|
|
124
|
+
: Type extends 'submit' | 'hidden'
|
|
125
|
+
? Value extends string
|
|
126
|
+
? [type: Type, value: Value | (string & {})]
|
|
127
|
+
: [type: Type, value: Value]
|
|
128
|
+
: Type extends 'radio'
|
|
129
|
+
? [type: Type, value: Value | (string & {})]
|
|
130
|
+
: Type extends 'file' | 'file multiple'
|
|
131
|
+
? [type: Type]
|
|
132
|
+
: [type: Type] | [type: Type, value: Value | undefined];
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Form field accessor type that provides name(), value(), and issues() methods
|
|
136
|
+
*/
|
|
137
|
+
export type RemoteFormField<Value extends RemoteFormFieldValue> = RemoteFormFieldMethods<Value> & {
|
|
45
138
|
/**
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
* const response = await fetch('...');
|
|
55
|
-
*
|
|
56
|
-
* for (const str of response.headers.getSetCookie()) {
|
|
57
|
-
* const { name, value, ...options } = cookies.parse(str);
|
|
58
|
-
* cookies.set(name, value, { ...options, path: '/' });
|
|
59
|
-
* }
|
|
60
|
-
*
|
|
61
|
-
* // ...
|
|
62
|
-
* }
|
|
139
|
+
* Returns an object that can be spread onto an input element with the correct type attribute,
|
|
140
|
+
* aria-invalid attribute if the field is invalid, and appropriate value/checked property getters/setters.
|
|
141
|
+
* @example
|
|
142
|
+
* ```svelte
|
|
143
|
+
* <input {...myForm.fields.myString.as('text')} />
|
|
144
|
+
* <input {...myForm.fields.myNumber.as('number')} />
|
|
145
|
+
* <input {...myForm.fields.myBoolean.as('checkbox')} />
|
|
63
146
|
* ```
|
|
64
|
-
*
|
|
65
|
-
* Note the use of `headers.getSetCookie()`, which returns an array of cookie headers, _not_ `headers.get('set-cookie')` which returns a single comma-separated string.
|
|
66
147
|
*/
|
|
67
|
-
|
|
148
|
+
as<T extends RemoteFormFieldType<Value>>(...args: AsArgs<T, Value>): InputElementProps<T>;
|
|
149
|
+
};
|
|
68
150
|
|
|
151
|
+
type RemoteFormFieldContainer<Value> = RemoteFormFieldMethods<Value> & {
|
|
152
|
+
/** Validation issues belonging to this or any of the fields that belong to it, if any */
|
|
153
|
+
allIssues(): RemoteFormIssue[] | undefined;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
type UnknownField<Value> = RemoteFormFieldMethods<Value> & {
|
|
157
|
+
/** Validation issues belonging to this or any of the fields that belong to it, if any */
|
|
158
|
+
allIssues(): RemoteFormIssue[] | undefined;
|
|
69
159
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
160
|
+
* Returns an object that can be spread onto an input element with the correct type attribute,
|
|
161
|
+
* aria-invalid attribute if the field is invalid, and appropriate value/checked property getters/setters.
|
|
162
|
+
* @example
|
|
163
|
+
* ```svelte
|
|
164
|
+
* <input {...myForm.fields.myString.as('text')} />
|
|
165
|
+
* <input {...myForm.fields.myNumber.as('number')} />
|
|
166
|
+
* <input {...myForm.fields.myBoolean.as('checkbox')} />
|
|
167
|
+
* ```
|
|
78
168
|
*/
|
|
79
|
-
|
|
169
|
+
as<T extends RemoteFormFieldType<Value>>(...args: AsArgs<T, Value>): InputElementProps<T>;
|
|
170
|
+
} & {
|
|
171
|
+
[key: string | number]: UnknownField<any>;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
type RemoteFormFieldsRoot<Input extends RemoteFormInput | void> =
|
|
175
|
+
IsAny<Input> extends true
|
|
176
|
+
? RecursiveFormFields
|
|
177
|
+
: Input extends void
|
|
178
|
+
? {
|
|
179
|
+
/** Validation issues, if any */
|
|
180
|
+
issues(): RemoteFormIssue[] | undefined;
|
|
181
|
+
/** Validation issues belonging to this or any of the fields that belong to it, if any */
|
|
182
|
+
allIssues(): RemoteFormIssue[] | undefined;
|
|
183
|
+
}
|
|
184
|
+
: RemoteFormFields<Input>;
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Recursive type to build form fields structure with proxy access
|
|
188
|
+
*/
|
|
189
|
+
export type RemoteFormFields<T> =
|
|
190
|
+
WillRecurseIndefinitely<T> extends true
|
|
191
|
+
? RecursiveFormFields
|
|
192
|
+
: NonNullable<T> extends string | number | boolean | File
|
|
193
|
+
? RemoteFormField<NonNullable<T>>
|
|
194
|
+
: // [NonNullable<T>] is used to prevent distributing over union while still allowing
|
|
195
|
+
// nullable wrappers (e.g. `string[] | undefined` from a schema with `.default([])`)
|
|
196
|
+
// to be treated as arrays; only the last condition should distribute over unions
|
|
197
|
+
[NonNullable<T>] extends [string[] | File[]]
|
|
198
|
+
? RemoteFormField<NonNullable<T>> & {
|
|
199
|
+
[K in number]: RemoteFormField<NonNullable<T>[number]>;
|
|
200
|
+
}
|
|
201
|
+
: [NonNullable<T>] extends [Array<infer U>]
|
|
202
|
+
? RemoteFormFieldContainer<NonNullable<T>> & {
|
|
203
|
+
[K in number]: RemoteFormFields<U>;
|
|
204
|
+
}
|
|
205
|
+
: RemoteFormFieldContainer<T> & {
|
|
206
|
+
[K in KeysOfUnion<T>]-?: RemoteFormFields<ValueOfUnionKey<T, K>>;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
// By breaking this out into its own type, we avoid the TS recursion depth limit
|
|
210
|
+
type RecursiveFormFields = RemoteFormFieldContainer<any> & {
|
|
211
|
+
[key: string | number]: UnknownField<any>;
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
type MaybeArray<T> = T | T[];
|
|
215
|
+
|
|
216
|
+
export interface RemoteFormInput {
|
|
217
|
+
[key: string]: MaybeArray<string | number | boolean | File | RemoteFormInput> | undefined;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export interface RemoteFormIssue {
|
|
221
|
+
message: string;
|
|
222
|
+
path: Array<string | number>;
|
|
80
223
|
}
|
|
81
224
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
225
|
+
// If the schema specifies `id` as a string or number, ensure that `for(...)`
|
|
226
|
+
// only accepts that type. Otherwise, accept `string | number`
|
|
227
|
+
type ExtractId<Input> = Input extends { id: infer Id }
|
|
228
|
+
? Id extends string | number
|
|
229
|
+
? Id
|
|
230
|
+
: string | number
|
|
231
|
+
: string | number;
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* A function and proxy object used to imperatively create validation errors in form handlers.
|
|
235
|
+
*
|
|
236
|
+
* Access properties to create field-specific issues: `issue.fieldName('message')`.
|
|
237
|
+
* The type structure mirrors the input data structure for type-safe field access.
|
|
238
|
+
* Call `invalid(issue.foo(...), issue.nested.bar(...))` to throw a validation error.
|
|
239
|
+
*/
|
|
240
|
+
export type RemoteFormInvalidField<T> =
|
|
241
|
+
WillRecurseIndefinitely<T> extends true
|
|
242
|
+
? Record<string | number, any>
|
|
243
|
+
: NonNullable<T> extends string | number | boolean | File
|
|
244
|
+
? (message: string) => StandardSchemaV1.Issue
|
|
245
|
+
: NonNullable<T> extends Array<infer U>
|
|
246
|
+
? {
|
|
247
|
+
[K in number]: RemoteFormInvalidField<U>;
|
|
248
|
+
} & ((message: string) => StandardSchemaV1.Issue)
|
|
249
|
+
: NonNullable<T> extends RemoteFormInput
|
|
250
|
+
? {
|
|
251
|
+
[K in keyof T]-?: RemoteFormInvalidField<T[K]>;
|
|
252
|
+
} & ((message: string) => StandardSchemaV1.Issue)
|
|
253
|
+
: Record<string, never>;
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* The form instance as received inside an `enhance` callback. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#form) for full documentation.
|
|
257
|
+
*/
|
|
258
|
+
export type RemoteFormEnhanceInstance<
|
|
259
|
+
Input extends RemoteFormInput | void = RemoteFormInput | void,
|
|
260
|
+
Output = any
|
|
261
|
+
> = Omit<RemoteForm<Input, Output>, 'enhance' | 'element'> & {
|
|
262
|
+
readonly element: HTMLFormElement;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* The callback passed to a remote form's `enhance` method. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#form) for full documentation.
|
|
267
|
+
*/
|
|
268
|
+
export type RemoteFormEnhanceCallback<
|
|
269
|
+
Input extends RemoteFormInput | void = RemoteFormInput | void,
|
|
270
|
+
Output = any
|
|
271
|
+
> = (form: RemoteFormEnhanceInstance<Input, Output>) => MaybePromise<void>;
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* The type of a remote `form` function. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#form) for full documentation.
|
|
275
|
+
*/
|
|
276
|
+
export type RemoteForm<Input extends RemoteFormInput | void, Output> = {
|
|
277
|
+
/** Attachment that sets up an event handler that intercepts the form submission on the client to prevent a full page reload */
|
|
278
|
+
[attachment: symbol]: (node: HTMLFormElement) => void;
|
|
279
|
+
method: 'POST';
|
|
280
|
+
/** The URL to send the form to. */
|
|
281
|
+
action: string;
|
|
282
|
+
/** The `<form>` element this instance is currently attached to, if any. */
|
|
283
|
+
get element(): HTMLFormElement | null;
|
|
284
|
+
/** Submit the currently attached form programmatically. */
|
|
285
|
+
submit(): Promise<boolean> & {
|
|
286
|
+
updates: (...updates: RemoteQueryUpdate[]) => Promise<boolean>;
|
|
287
|
+
};
|
|
288
|
+
/** Use the `enhance` method to influence what happens when the form is submitted. */
|
|
289
|
+
enhance(callback: RemoteFormEnhanceCallback<Input, Output>): {
|
|
290
|
+
method: 'POST';
|
|
291
|
+
action: string;
|
|
292
|
+
[attachment: symbol]: (node: HTMLFormElement) => void;
|
|
293
|
+
};
|
|
86
294
|
/**
|
|
87
|
-
*
|
|
295
|
+
* Create an instance of the form for the given `id`.
|
|
296
|
+
* The `id` is stringified and used for deduplication to potentially reuse existing instances.
|
|
297
|
+
* Useful when you have multiple forms that use the same remote form action, for example in a loop.
|
|
298
|
+
* ```svelte
|
|
299
|
+
* {#each todos as todo}
|
|
300
|
+
* {const todoForm = updateTodo.for(todo.id)}
|
|
301
|
+
* <form {...todoForm}>
|
|
302
|
+
* {#if todoForm.result?.invalid}<p>Invalid data</p>{/if}
|
|
303
|
+
* ...
|
|
304
|
+
* </form>
|
|
305
|
+
* {/each}
|
|
306
|
+
* ```
|
|
88
307
|
*/
|
|
89
|
-
|
|
308
|
+
for(id: ExtractId<Input>): Omit<RemoteForm<Input, Output>, 'for'>;
|
|
309
|
+
/** Preflight checks */
|
|
310
|
+
preflight(schema: StandardSchemaV1<Input, any>): RemoteForm<Input, Output>;
|
|
311
|
+
/** Validate the form contents programmatically */
|
|
312
|
+
validate(options?: {
|
|
313
|
+
/**
|
|
314
|
+
* Set this to `true` to also show validation issues of fields that haven't yet been
|
|
315
|
+
* edited and blurred. This option is ignored for forms that have previously been
|
|
316
|
+
* submitted, in which case all fields are always subject to validation
|
|
317
|
+
* (unless the form is reset, at which point it is treated as pristine)
|
|
318
|
+
*/
|
|
319
|
+
all?: boolean;
|
|
320
|
+
/** Set this to `true` to only run the `preflight` validation. */
|
|
321
|
+
preflightOnly?: boolean;
|
|
322
|
+
}): Promise<void>;
|
|
323
|
+
/** The result of the form submission */
|
|
324
|
+
get result(): Output | undefined;
|
|
325
|
+
/** The number of pending submissions */
|
|
326
|
+
get pending(): number;
|
|
327
|
+
/** True if the form has been submitted at least once, and hasn't been reset since */
|
|
328
|
+
get submitted(): boolean;
|
|
329
|
+
/** Access form fields using object notation */
|
|
330
|
+
fields: RemoteFormFieldsRoot<Input>;
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* The type of a remote `command` function. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#command) for full documentation.
|
|
335
|
+
*/
|
|
336
|
+
export type RemoteCommand<Input, Output> = {
|
|
337
|
+
(arg: undefined extends Input ? Input | void : Input): Promise<Output> & {
|
|
338
|
+
updates(...updates: RemoteQueryUpdate[]): Promise<Output>;
|
|
339
|
+
};
|
|
340
|
+
/** The number of pending command executions */
|
|
341
|
+
get pending(): number;
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
export type RemoteQueryUpdate =
|
|
345
|
+
| RemoteQuery<any>
|
|
346
|
+
| RemoteLiveQuery<any>
|
|
347
|
+
| RemoteQueryFunction<any, any>
|
|
348
|
+
| RemoteLiveQueryFunction<any, any>
|
|
349
|
+
| RemoteQueryOverride;
|
|
350
|
+
|
|
351
|
+
export type RemoteResource<T> = Promise<T> & {
|
|
352
|
+
/** The error in case the query fails. */
|
|
353
|
+
get error(): App.Error | undefined;
|
|
354
|
+
/** `true` before the first result is available and during refreshes */
|
|
355
|
+
get loading(): boolean;
|
|
356
|
+
} & (
|
|
357
|
+
| {
|
|
358
|
+
/** The current value of the query. Undefined until `ready` is `true` */
|
|
359
|
+
get current(): undefined;
|
|
360
|
+
ready: false;
|
|
361
|
+
}
|
|
362
|
+
| {
|
|
363
|
+
/** The current value of the query. Undefined until `ready` is `true` */
|
|
364
|
+
get current(): T;
|
|
365
|
+
ready: true;
|
|
366
|
+
}
|
|
367
|
+
);
|
|
368
|
+
|
|
369
|
+
export type RemoteQuery<T> = RemoteResource<T> & {
|
|
90
370
|
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
* - It can be used to make credentialed requests on the server, as it inherits the `cookie` and `authorization` headers for the page request.
|
|
94
|
-
* - It can make relative requests on the server (ordinarily, `fetch` requires a URL with an origin when used in a server context).
|
|
95
|
-
* - Internal requests (e.g. for `+server.js` routes) go directly to the handler function when running on the server, without the overhead of an HTTP call.
|
|
96
|
-
* - During server-side rendering, the response will be captured and inlined into the rendered HTML by hooking into the `text` and `json` methods of the `Response` object. Note that headers will _not_ be serialized, unless explicitly included via [`filterSerializedResponseHeaders`](https://svelte.dev/docs/kit/hooks#handle)
|
|
97
|
-
* - During hydration, the response will be read from the HTML, guaranteeing consistency and preventing an additional network request.
|
|
371
|
+
* On the client, this function will update the value of the query without re-fetching it.
|
|
98
372
|
*
|
|
99
|
-
*
|
|
100
|
-
|
|
101
|
-
readonly fetch: typeof fetch;
|
|
102
|
-
/**
|
|
103
|
-
* The client's IP address, set by the adapter.
|
|
104
|
-
*/
|
|
105
|
-
readonly getClientAddress: () => string;
|
|
106
|
-
/**
|
|
107
|
-
* Contains custom data that was added to the request within the [`server handle hook`](https://svelte.dev/docs/kit/hooks#handle).
|
|
373
|
+
* 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.
|
|
374
|
+
* This prevents SvelteKit needing to refresh all queries on the page in a second server round-trip.
|
|
108
375
|
*/
|
|
109
|
-
|
|
376
|
+
set(value: T): void;
|
|
110
377
|
/**
|
|
111
|
-
*
|
|
378
|
+
* On the client, this function will re-fetch the query from the server.
|
|
112
379
|
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
* the remote function was called from, _not_ the URL of the endpoint SvelteKit creates for the remote function. Never use it
|
|
116
|
-
* to determine whether or not a user is authorized to access certain data, as these values are part of the request which could be manipulated.
|
|
380
|
+
* 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.
|
|
381
|
+
* This prevents SvelteKit needing to refresh all queries on the page in a second server round-trip.
|
|
117
382
|
*/
|
|
118
|
-
|
|
383
|
+
refresh(): Promise<void>;
|
|
119
384
|
/**
|
|
120
|
-
*
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
*
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
*
|
|
385
|
+
* 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.
|
|
386
|
+
*
|
|
387
|
+
* ```svelte
|
|
388
|
+
* <script>
|
|
389
|
+
* import { getTodos, addTodo } from './todos.remote.js';
|
|
390
|
+
* const todos = getTodos();
|
|
391
|
+
* </script>
|
|
392
|
+
*
|
|
393
|
+
* <form {...addTodo.enhance(async (form) => {
|
|
394
|
+
* await form.submit().updates(
|
|
395
|
+
* todos.withOverride((todos) => [...todos, { text: form.fields.text.value() }])
|
|
396
|
+
* );
|
|
397
|
+
* })}>
|
|
398
|
+
* <input type="text" name="text" />
|
|
399
|
+
* <button type="submit">Add Todo</button>
|
|
400
|
+
* </form>
|
|
401
|
+
* ```
|
|
129
402
|
*/
|
|
130
|
-
|
|
403
|
+
withOverride(update: (current: T) => T): RemoteQueryOverride;
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
export type RemoteLiveQuery<T> = RemoteResource<T> &
|
|
407
|
+
AsyncIterable<T> & {
|
|
408
|
+
/** `true` if the live stream is currently connected. */
|
|
409
|
+
readonly connected: boolean;
|
|
410
|
+
/** `true` once the current live stream iterator is done. */
|
|
411
|
+
readonly done: boolean;
|
|
412
|
+
/** Reconnects the live stream immediately. */
|
|
413
|
+
reconnect(): Promise<void>;
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
export type RemoteQueryOverride = () => void;
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* The type of a remote `prerender` function. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#prerender) for full documentation.
|
|
420
|
+
*/
|
|
421
|
+
export type RemotePrerenderFunction<Input, Output> = (
|
|
422
|
+
arg: undefined extends Input ? Input | void : Input
|
|
423
|
+
) => RemoteResource<Output>;
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* The return value of a remote `query` function. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#query) for full documentation.
|
|
427
|
+
*
|
|
428
|
+
* The optional `Validated` generic parameter represents the argument type *after* the
|
|
429
|
+
* query's schema has validated and (optionally) transformed it — this is the type the
|
|
430
|
+
* query's implementation function receives on the server, and the type yielded by
|
|
431
|
+
* [`requested`](https://svelte.dev/docs/kit/$app-server#requested). For queries declared
|
|
432
|
+
* with [Standard Schema](https://standardschema.dev/) it differs from `Input` when the
|
|
433
|
+
* schema contains a transform (e.g. `v.pipe(v.number(), v.transform(String))` has
|
|
434
|
+
* `Input = number` but `Validated = string`). For `'unchecked'` validators and queries
|
|
435
|
+
* without arguments it defaults to `Input`.
|
|
436
|
+
*/
|
|
437
|
+
export type RemoteQueryFunction<Input, Output, _Validated = Input> = (
|
|
438
|
+
arg: undefined extends Input ? Input | void : Input
|
|
439
|
+
) => RemoteQuery<Output>;
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* The type of a remote `query.live` function. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#query.live) for full documentation.
|
|
443
|
+
*
|
|
444
|
+
* The optional `Validated` generic parameter represents the argument type *after* the
|
|
445
|
+
* query's schema has validated and (optionally) transformed it, and matches the type
|
|
446
|
+
* yielded by [`requested`](https://svelte.dev/docs/kit/$app-server#requested).
|
|
447
|
+
*/
|
|
448
|
+
export type RemoteLiveQueryFunction<Input, Output, _Validated = Input> = (
|
|
449
|
+
arg: undefined extends Input ? Input | void : Input
|
|
450
|
+
) => RemoteLiveQuery<Output>;
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* A single entry yielded by [`requested`](https://svelte.dev/docs/kit/$app-server#requested)
|
|
454
|
+
* when called with a regular `query`. `arg` is the validated argument (the input *after*
|
|
455
|
+
* the query's schema validated and transformed it, if applicable); `query` is a
|
|
456
|
+
* `RemoteQuery` bound to the client's original cache key, so `refresh()` / `set()` will
|
|
457
|
+
* update the correct client entry.
|
|
458
|
+
*/
|
|
459
|
+
export type RequestedEntry<Validated, Output> = {
|
|
460
|
+
arg: Validated;
|
|
461
|
+
query: RemoteQuery<Output>;
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* A single entry yielded by [`requested`](https://svelte.dev/docs/kit/$app-server#requested)
|
|
466
|
+
* when called with a `query.live`. `arg` is the validated argument; `query` is a
|
|
467
|
+
* `RemoteLiveQuery` bound to the client's original cache key, so `reconnect()` targets
|
|
468
|
+
* the correct client subscription.
|
|
469
|
+
*/
|
|
470
|
+
export type RemoteLiveQueryRequestedEntry<Validated, Output> = {
|
|
471
|
+
arg: Validated;
|
|
472
|
+
query: RemoteLiveQuery<Output>;
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
export type RemoteQueryRequestedResult<Validated, Output> = Iterable<
|
|
476
|
+
RequestedEntry<Validated, Output>
|
|
477
|
+
> &
|
|
478
|
+
AsyncIterable<RequestedEntry<Validated, Output>> & {
|
|
131
479
|
/**
|
|
132
|
-
*
|
|
480
|
+
* Call `refresh` on all queries selected by this `requested` invocation.
|
|
481
|
+
* This is identical to:
|
|
482
|
+
* ```ts
|
|
483
|
+
* import { requested } from '$app/server';
|
|
133
484
|
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
485
|
+
* for await (const { query } of requested(getPost, ...)) {
|
|
486
|
+
* void query.refresh();
|
|
487
|
+
* }
|
|
488
|
+
* ```
|
|
138
489
|
*/
|
|
139
|
-
|
|
490
|
+
refreshAll: () => Promise<void>;
|
|
140
491
|
};
|
|
141
|
-
/**
|
|
142
|
-
* If you need to set headers for the response, you can do so using the this method. This is useful if you want the page to be cached, for example:
|
|
143
|
-
*
|
|
144
|
-
* ```js
|
|
145
|
-
* /// file: src/routes/blog/+page.js
|
|
146
|
-
* export async function load({ fetch, setHeaders }) {
|
|
147
|
-
* const url = `https://cms.example.com/articles.json`;
|
|
148
|
-
* const response = await fetch(url);
|
|
149
|
-
*
|
|
150
|
-
* setHeaders({
|
|
151
|
-
* age: response.headers.get('age'),
|
|
152
|
-
* 'cache-control': response.headers.get('cache-control')
|
|
153
|
-
* });
|
|
154
|
-
*
|
|
155
|
-
* return response.json();
|
|
156
|
-
* }
|
|
157
|
-
* ```
|
|
158
|
-
*
|
|
159
|
-
* Setting the same header multiple times (even in separate `load` functions) is an error — you can only set a given header once.
|
|
160
|
-
*
|
|
161
|
-
* You cannot add a `set-cookie` header with `setHeaders` — use the [`cookies`](https://svelte.dev/docs/kit/$app-server#Cookies) API instead.
|
|
162
|
-
*/
|
|
163
|
-
readonly setHeaders: (headers: Record<string, string>) => void;
|
|
164
|
-
/**
|
|
165
|
-
* The requested URL.
|
|
166
|
-
*
|
|
167
|
-
* Inside `query` functions (including `query.batch` and `query.live`), accessing this property throws an error.
|
|
168
|
-
* Pass values from the page as arguments to the query instead. Inside `form` and `command` functions it relates to the page
|
|
169
|
-
* the remote function was called from, _not_ the URL of the endpoint SvelteKit creates for the remote function. Never use it
|
|
170
|
-
* to determine whether or not a user is authorized to access certain data, as these values are part of the request which could be manipulated.
|
|
171
|
-
*/
|
|
172
|
-
readonly url: URL;
|
|
173
|
-
/**
|
|
174
|
-
* `true` if the request comes from the client asking for `+page/layout.server.js` data. The `url` property will be stripped of the internal information
|
|
175
|
-
* related to the data request in this case. Use this property instead if the distinction is important to you.
|
|
176
|
-
*/
|
|
177
|
-
readonly isDataRequest: boolean;
|
|
178
|
-
/**
|
|
179
|
-
* `true` for `+server.js` calls coming from SvelteKit without the overhead of actually making an HTTP request. This happens when you make same-origin `fetch` requests on the server.
|
|
180
|
-
*/
|
|
181
|
-
readonly isSubRequest: boolean;
|
|
182
492
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
493
|
+
export type RemoteLiveQueryRequestedResult<Validated, Output> = Iterable<
|
|
494
|
+
RemoteLiveQueryRequestedEntry<Validated, Output>
|
|
495
|
+
> &
|
|
496
|
+
AsyncIterable<RemoteLiveQueryRequestedEntry<Validated, Output>> & {
|
|
497
|
+
/**
|
|
498
|
+
* Call `reconnect` on all live queries selected by this `requested` invocation.
|
|
499
|
+
* This is identical to:
|
|
500
|
+
* ```ts
|
|
501
|
+
* import { requested } from '$app/server';
|
|
502
|
+
*
|
|
503
|
+
* for await (const { query } of requested(liveQuery, ...)) {
|
|
504
|
+
* void query.reconnect();
|
|
505
|
+
* }
|
|
506
|
+
* ```
|
|
507
|
+
*/
|
|
508
|
+
reconnectAll: () => Promise<void>;
|
|
194
509
|
};
|
|
195
510
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
*/
|
|
200
|
-
readonly isRemoteRequest: boolean;
|
|
201
|
-
}
|
|
511
|
+
export type RequestedResult<Validated, Output> =
|
|
512
|
+
| RemoteQueryRequestedResult<Validated, Output>
|
|
513
|
+
| RemoteLiveQueryRequestedResult<Validated, Output>;
|