-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
ambient.d.ts
425 lines (400 loc) · 15.8 KB
/
ambient.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
* It's possible to tell SvelteKit how to type objects inside your app by declaring the `App` namespace. By default, a new project will have a file called `src/app.d.ts` containing the following:
*
* ```ts
* /// <reference types="@sveltejs/kit" />
*
* declare namespace App {
* interface Locals {}
*
* interface PageData {}
*
* interface Platform {}
* }
* ```
*
* By populating these interfaces, you will gain type safety when using `event.locals`, `event.platform`, and `data` from `load` functions.
*
* Note that since it's an ambient declaration file, you have to be careful when using `import` statements. Once you add an `import`
* at the top level, the declaration file is no longer considered ambient and you lose access to these typings in other files.
* To avoid this, either use the `import(...)` function:
*
* ```ts
* interface Locals {
* user: import('$lib/types').User;
* }
* ```
* Or wrap the namespace with `declare global`:
* ```ts
* import { User } from '$lib/types';
*
* declare global {
* namespace App {
* interface Locals {
* user: User;
* }
* // ...
* }
* }
* ```
*
*/
declare namespace App {
/**
* The interface that defines `event.locals`, which can be accessed in [hooks](https://kit.svelte.dev/docs/hooks) (`handle`, and `handleError`), server-only `load` functions, and `+server.js` files.
*/
export interface Locals {}
/**
* Defines the common shape of the [$page.data store](https://kit.svelte.dev/docs/modules#$app-stores-page) - that is, the data that is shared between all pages.
* The `Load` and `ServerLoad` functions in `./$types` will be narrowed accordingly.
* Use optional properties for data that is only present on specific pages. Do not add an index signature (`[key: string]: any`).
*/
export interface PageData {}
/**
* If your adapter provides [platform-specific context](https://kit.svelte.dev/docs/adapters#supported-environments-platform-specific-context) via `event.platform`, you can specify it here.
*/
export interface Platform {}
/**
* Defines the common shape of expected and unexpected errors. Expected errors are thrown using the `error` function. Unexpected errors are handled by the `handleError` hooks which should return this shape.
*/
export interface PageError {
message: string;
}
}
/**
* ```ts
* import { browser, dev, prerendering } from '$app/environment';
* ```
*/
declare module '$app/environment' {
/**
* `true` if the app is running in the browser.
*/
export const browser: boolean;
/**
* Whether the dev server is running. This is not guaranteed to correspond to `NODE_ENV` or `MODE`.
*/
export const dev: boolean;
/**
* `true` when prerendering, `false` otherwise.
*/
export const prerendering: boolean;
}
/**
* ```ts
* import { enhance, applyAction } from '$app/forms';
* ```
*/
declare module '$app/forms' {
import type { ActionResult } from '@sveltejs/kit';
export type SubmitFunction<
Success extends Record<string, unknown> | undefined = Record<string, any>,
Invalid extends Record<string, unknown> | undefined = Record<string, any>
> = (input: {
action: string;
data: FormData;
form: HTMLFormElement;
controller: AbortController;
cancel: () => void;
}) =>
| void
| ((opts: {
form: HTMLFormElement;
action: string;
result: ActionResult<Success, Invalid>;
}) => void);
/**
* This action enhances a `<form>` element that otherwise would work without JavaScript.
* @param form The form element
* @param options Callbacks for different states of the form lifecycle
*/
export function enhance<
Success extends Record<string, unknown> | undefined = Record<string, any>,
Invalid extends Record<string, unknown> | undefined = Record<string, any>
>(
form: HTMLFormElement,
/**
* Called upon submission with the given FormData and the `action` that should be triggered.
* If `cancel` is called, the form will not be submitted.
* You can use the abort `controller` to cancel the submission in case another one starts.
* If a function is returned, that function is called with the response from the server.
* If nothing is returned, the fallback will be used.
*
* If this function or its return value isn't set, it
* - falls back to updating the `form` prop with the returned data if the action is one same page as the form
* - updates `$page.status`
* - invalidates all data in case of successful submission with no redirect response
* - redirects in case of a redirect response
* - redirects to the nearest error page in case of an unexpected error
*/
submit?: SubmitFunction<Success, Invalid>
): { destroy: () => void };
/**
* This action updates the `form` property of the current page with the given data and updates `$page.status`.
* In case of an error, it redirects to the nearest error page.
*/
export function applyAction<
Success extends Record<string, unknown> | undefined = Record<string, any>,
Invalid extends Record<string, unknown> | undefined = Record<string, any>
>(result: ActionResult<Success, Invalid>): Promise<void>;
}
/**
* ```ts
* import {
* afterNavigate,
* beforeNavigate,
* disableScrollHandling,
* goto,
* invalidate,
* invalidateAll,
* prefetch,
* prefetchRoutes
* } from '$app/navigation';
* ```
*/
declare module '$app/navigation' {
import { Navigation } from '@sveltejs/kit';
/**
* If called when the page is being updated following a navigation (in `onMount` or `afterNavigate` or an action, for example), this disables SvelteKit's built-in scroll handling.
* This is generally discouraged, since it breaks user expectations.
*/
export function disableScrollHandling(): void;
/**
* Returns a Promise that resolves when SvelteKit navigates (or fails to navigate, in which case the promise rejects) to the specified `url`.
*
* @param url Where to navigate to
* @param opts.replaceState If `true`, will replace the current `history` entry rather than creating a new one with `pushState`
* @param opts.noscroll If `true`, the browser will maintain its scroll position rather than scrolling to the top of the page after navigation
* @param opts.keepfocus If `true`, the currently focused element will retain focus after navigation. Otherwise, focus will be reset to the body
* @param opts.state The state of the new/updated history entry
*/
export function goto(
url: string | URL,
opts?: { replaceState?: boolean; noscroll?: boolean; keepfocus?: boolean; state?: any }
): Promise<void>;
/**
* Causes any `load` functions belonging to the currently active page to re-run if they depend on the `url` in question, via `fetch` or `depends`. Returns a `Promise` that resolves when the page is subsequently updated.
*
* If the argument is given as a `string` or `URL`, it must resolve to the same URL that was passed to `fetch` or `depends` (including query parameters).
* To create a custom identifier, use a string beginning with `[a-z]+:` (e.g. `custom:state`) — this is a valid URL.
*
* The `function` argument can be used define a custom predicate. It receives the full `URL` and causes `load` to rerun if `true` is returned.
* This can be useful if you want to invalidate based on a pattern instead of a exact match.
*
* ```ts
* // Example: Match '/path' regardless of the query parameters
* invalidate((url) => url.pathname === '/path');
* ```
* @param url The invalidated URL
*/
export function invalidate(url: string | URL | ((url: URL) => boolean)): Promise<void>;
/**
* Causes all `load` functions belonging to the currently active page to re-run. Returns a `Promise` that resolves when the page is subsequently updated.
*/
export function invalidateAll(): Promise<void>;
/**
* Programmatically prefetches the given page, which means
* 1. ensuring that the code for the page is loaded, and
* 2. calling the page's load function with the appropriate options.
*
* This is the same behaviour that SvelteKit triggers when the user taps or mouses over an `<a>` element with `data-sveltekit-prefetch`.
* If the next navigation is to `href`, the values returned from load will be used, making navigation instantaneous.
* Returns a Promise that resolves when the prefetch is complete.
*
* @param href Page to prefetch
*/
export function prefetch(href: string): Promise<void>;
/**
* Programmatically prefetches the code for routes that haven't yet been fetched.
* Typically, you might call this to speed up subsequent navigation.
*
* If no argument is given, all routes will be fetched, otherwise you can specify routes by any matching pathname
* such as `/about` (to match `src/routes/about.svelte`) or `/blog/*` (to match `src/routes/blog/[slug].svelte`).
*
* Unlike prefetch, this won't call load for individual pages.
* Returns a Promise that resolves when the routes have been prefetched.
*/
export function prefetchRoutes(routes?: string[]): Promise<void>;
/**
* A navigation interceptor that triggers before we navigate to a new URL, whether by clicking a link, calling `goto(...)`, or using the browser back/forward controls.
* Calling `cancel()` will prevent the navigation from completing.
*
* When navigating to an external URL, `navigation.to` will be `null`.
*
* `beforeNavigate` must be called during a component initialization. It remains active as long as the component is mounted.
*/
export function beforeNavigate(
callback: (navigation: Navigation & { cancel: () => void }) => void
): void;
/**
* A lifecycle function that runs the supplied `callback` when the current component mounts, and also whenever we navigate to a new URL.
*
* `afterNavigate` must be called during a component initialization. It remains active as long as the component is mounted.
*/
export function afterNavigate(callback: (navigation: Navigation) => void): void;
}
/**
* ```ts
* import { base, assets } from '$app/paths';
* ```
*/
declare module '$app/paths' {
/**
* A string that matches [`config.kit.paths.base`](https://kit.svelte.dev/docs/configuration#paths). It must start, but not end with `/` (e.g. `/base-path`), unless it is the empty string.
*/
export const base: `/${string}`;
/**
* An absolute path that matches [`config.kit.paths.assets`](https://kit.svelte.dev/docs/configuration#paths).
*
* > If a value for `config.kit.paths.assets` is specified, it will be replaced with `'/_svelte_kit_assets'` during `vite dev` or `vite preview`, since the assets don't yet live at their eventual URL.
*/
export const assets: `https://${string}` | `http://${string}`;
}
/**
* ```ts
* import { getStores, navigating, page, updated } from '$app/stores';
* ```
*
* Stores on the server are _contextual_ — they are added to the [context](https://svelte.dev/tutorial/context-api) of your root component. This means that `page` is unique to each request, rather than shared between multiple requests handled by the same server simultaneously.
*
* Because of that, you must subscribe to the stores during component initialization (which happens automatically if you reference the store value, e.g. as `$page`, in a component) before you can use them.
*
* In the browser, we don't need to worry about this, and stores can be accessed from anywhere. Code that will only ever run on the browser can refer to (or subscribe to) any of these stores at any time.
*/
declare module '$app/stores' {
import { Readable } from 'svelte/store';
import { Navigation, Page } from '@sveltejs/kit';
/**
* A readable store whose value contains page data.
*/
export const page: Readable<Page>;
/**
* A readable store.
* When navigating starts, its value is a `Navigation` object with `from`, `to`, `type` and (if `type === 'popstate'`) `delta` properties.
* When navigating finishes, its value reverts to `null`.
*/
export const navigating: Readable<Navigation | null>;
/**
* A readable store whose initial value is `false`. If [`version.pollInterval`](https://kit.svelte.dev/docs/configuration#version) is a non-zero value, SvelteKit will poll for new versions of the app and update the store value to `true` when it detects one. `updated.check()` will force an immediate check, regardless of polling.
*/
export const updated: Readable<boolean> & { check: () => boolean };
/**
* A function that returns all of the contextual stores. On the server, this must be called during component initialization.
* Only use this if you need to defer store subscription until after the component has mounted, for some reason.
*/
export function getStores(): {
navigating: typeof navigating;
page: typeof page;
updated: typeof updated;
};
}
/**
* ```ts
* import { build, files, prerendered, version } from '$service-worker';
* ```
*
* This module is only available to [service workers](https://kit.svelte.dev/docs/service-workers).
*/
declare module '$service-worker' {
/**
* An array of URL strings representing the files generated by Vite, suitable for caching with `cache.addAll(build)`.
*/
export const build: string[];
/**
* An array of URL strings representing the files in your static directory, or whatever directory is specified by `config.kit.files.assets`. You can customize which files are included from `static` directory using [`config.kit.serviceWorker.files`](https://kit.svelte.dev/docs/configuration)
*/
export const files: string[];
/**
* An array of pathnames corresponding to prerendered pages and endpoints.
*/
export const prerendered: string[];
/**
* See [`config.kit.version`](https://kit.svelte.dev/docs/configuration#version). It's useful for generating unique cache names inside your service worker, so that a later deployment of your app can invalidate old caches.
*/
export const version: string;
}
declare module '@sveltejs/kit/hooks' {
import { Handle } from '@sveltejs/kit';
/**
* A helper function for sequencing multiple `handle` calls in a middleware-like manner.
*
* ```js
* /// file: src/hooks.js
* import { sequence } from '@sveltejs/kit/hooks';
*
* /** @type {import('@sveltejs/kit').Handle} *\/
* async function first({ event, resolve }) {
* console.log('first pre-processing');
* const result = await resolve(event, {
* transformPageChunk: ({ html }) => {
* // transforms are applied in reverse order
* console.log('first transform');
* return html;
* }
* });
* console.log('first post-processing');
* return result;
* }
*
* /** @type {import('@sveltejs/kit').Handle} *\/
* async function second({ event, resolve }) {
* console.log('second pre-processing');
* const result = await resolve(event, {
* transformPageChunk: ({ html }) => {
* console.log('second transform');
* return html;
* }
* });
* console.log('second post-processing');
* return result;
* }
*
* export const handle = sequence(first, second);
* ```
*
* The example above would print:
*
* ```
* first pre-processing
* second pre-processing
* second transform
* first transform
* second post-processing
* first post-processing
* ```
*
* @param handlers The chain of `handle` functions
*/
export function sequence(...handlers: Handle[]): Handle;
}
/**
* A polyfill for `fetch` and its related interfaces, used by adapters for environments that don't provide a native implementation.
*/
declare module '@sveltejs/kit/node/polyfills' {
/**
* Make various web APIs available as globals:
* - `crypto`
* - `fetch`
* - `Headers`
* - `Request`
* - `Response`
*/
export function installPolyfills(): void;
}
/**
* Utilities used by adapters for Node-like environments.
*/
declare module '@sveltejs/kit/node' {
export function getRequest(
base: string,
request: import('http').IncomingMessage
): Promise<Request>;
export function setResponse(res: import('http').ServerResponse, response: Response): void;
}
declare module '@sveltejs/kit/vite' {
import { Plugin } from 'vite';
/**
* Returns the SvelteKit Vite plugins.
*/
export function sveltekit(): Plugin[];
}