@sveltejs/kit 1.0.0-next.471 → 1.0.0-next.472

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/types/index.d.ts CHANGED
@@ -12,12 +12,11 @@ import {
12
12
  Prerendered,
13
13
  PrerenderOnErrorValue,
14
14
  RequestOptions,
15
- ResponseHeaders,
16
15
  RouteDefinition,
17
16
  TrailingSlash
18
17
  } from './private.js';
19
18
  import { SSRNodeLoader, SSRRoute, ValidatedConfig } from './internal.js';
20
- import { HttpError, Redirect } from '../src/runtime/control.js';
19
+ import { HttpError, Redirect, ValidationError } from '../src/runtime/control.js';
21
20
 
22
21
  export { PrerenderOption } from './private.js';
23
22
 
@@ -36,11 +35,11 @@ export type AwaitedProperties<input extends Record<string, any> | void> = input
36
35
  ? input
37
36
  : unknown;
38
37
 
39
- export type AwaitedErrors<T extends (...args: any) => any> = Awaited<ReturnType<T>> extends {
40
- errors?: any;
41
- }
42
- ? Awaited<ReturnType<T>>['errors']
43
- : undefined;
38
+ export type AwaitedActions<T extends Record<string, (...args: any) => any>> = {
39
+ [Key in keyof T]: UnpackValidationError<Awaited<ReturnType<T[Key]>>>;
40
+ }[keyof T];
41
+
42
+ type UnpackValidationError<T> = T extends ValidationError<infer X> ? X : T;
44
43
 
45
44
  export interface Builder {
46
45
  log: Logger;
@@ -119,6 +118,27 @@ export interface Config {
119
118
  [key: string]: any;
120
119
  }
121
120
 
121
+ export interface Cookies {
122
+ /**
123
+ * Gets a cookie that was previously set with `cookies.set`, or from the request headers.
124
+ */
125
+ get(name: string, opts?: import('cookie').CookieParseOptions): string | undefined;
126
+
127
+ /**
128
+ * Sets a cookie. This will add a `set-cookie` header to the response, but also make
129
+ * the cookie available via `cookies.get` during the current request.
130
+ *
131
+ * The `httpOnly` and `secure` options are `true` by default, and must be explicitly
132
+ * disabled if you want cookies to be readable by client-side JavaScript and/or transmitted over HTTP
133
+ */
134
+ set(name: string, value: string, opts?: import('cookie').CookieSerializeOptions): void;
135
+
136
+ /**
137
+ * Deletes a cookie by setting its value to an empty string and setting the expiry date in the past.
138
+ */
139
+ delete(name: string): void;
140
+ }
141
+
122
142
  export interface KitConfig {
123
143
  adapter?: Adapter;
124
144
  alias?: Record<string, string>;
@@ -147,10 +167,6 @@ export interface KitConfig {
147
167
  errorTemplate?: string;
148
168
  };
149
169
  inlineStyleThreshold?: number;
150
- methodOverride?: {
151
- parameter?: string;
152
- allowed?: string[];
153
- };
154
170
  outDir?: string;
155
171
  paths?: {
156
172
  assets?: string;
@@ -213,7 +229,7 @@ export interface LoadEvent<
213
229
  params: Params;
214
230
  data: Data;
215
231
  routeId: string | null;
216
- setHeaders: (headers: ResponseHeaders) => void;
232
+ setHeaders: (headers: Record<string, string>) => void;
217
233
  url: URL;
218
234
  parent: () => Promise<ParentData>;
219
235
  depends: (...deps: string[]) => void;
@@ -250,13 +266,14 @@ export interface ParamMatcher {
250
266
  export interface RequestEvent<
251
267
  Params extends Partial<Record<string, string>> = Partial<Record<string, string>>
252
268
  > {
269
+ cookies: Cookies;
253
270
  getClientAddress: () => string;
254
271
  locals: App.Locals;
255
272
  params: Params;
256
273
  platform: Readonly<App.Platform>;
257
274
  request: Request;
258
275
  routeId: string | null;
259
- setHeaders: (headers: ResponseHeaders) => void;
276
+ setHeaders: (headers: Record<string, string>) => void;
260
277
  url: URL;
261
278
  }
262
279
 
@@ -324,15 +341,29 @@ export interface ServerLoadEvent<
324
341
  }
325
342
 
326
343
  export interface Action<
327
- Params extends Partial<Record<string, string>> = Partial<Record<string, string>>
344
+ Params extends Partial<Record<string, string>> = Partial<Record<string, string>>,
345
+ OutputData extends Record<string, any> | void = Record<string, any> | void
328
346
  > {
329
- (event: RequestEvent<Params>): MaybePromise<
330
- | { status?: number; errors: Record<string, any>; location?: never }
331
- | { status?: never; errors?: never; location: string }
332
- | void
333
- >;
347
+ (event: RequestEvent<Params>): MaybePromise<OutputData>;
334
348
  }
335
349
 
350
+ export type Actions<
351
+ Params extends Partial<Record<string, string>> = Partial<Record<string, string>>,
352
+ OutputData extends Record<string, any> | void = Record<string, any> | void
353
+ > = Record<string, Action<Params, OutputData>>;
354
+
355
+ /**
356
+ * When calling a form action via fetch, the response will be one of these shapes.
357
+ */
358
+ export type ActionResult<
359
+ Success extends Record<string, unknown> | undefined = Record<string, any>,
360
+ Invalid extends Record<string, unknown> | undefined = Record<string, any>
361
+ > =
362
+ | { type: 'success'; status: number; data?: Success }
363
+ | { type: 'invalid'; status: number; data?: Invalid }
364
+ | { type: 'redirect'; status: number; location: string }
365
+ | { type: 'error'; error: any };
366
+
336
367
  // TODO figure out how to just re-export from '../src/index/index.js' without
337
368
  // breaking the site
338
369
 
@@ -355,3 +386,11 @@ export function redirect(status: number, location: string): Redirect;
355
386
  * Generates a JSON `Response` object from the supplied data.
356
387
  */
357
388
  export function json(data: any, init?: ResponseInit): Response;
389
+
390
+ /**
391
+ * Generates a `ValidationError` object.
392
+ */
393
+ export function invalid<T extends Record<string, unknown> | undefined>(
394
+ status: number,
395
+ data?: T
396
+ ): ValidationError<T>;
@@ -1,7 +1,6 @@
1
1
  import { OutputAsset, OutputChunk } from 'rollup';
2
2
  import { SvelteComponent } from 'svelte/internal';
3
3
  import {
4
- Action,
5
4
  Config,
6
5
  ServerLoad,
7
6
  Handle,
@@ -14,7 +13,8 @@ import {
14
13
  Server,
15
14
  ServerInitOptions,
16
15
  SSRManifest,
17
- HandleFetch
16
+ HandleFetch,
17
+ Actions
18
18
  } from './index.js';
19
19
  import {
20
20
  HttpMethod,
@@ -117,11 +117,6 @@ export interface ManifestData {
117
117
  matchers: Record<string, string>;
118
118
  }
119
119
 
120
- export interface MethodOverride {
121
- parameter: string;
122
- allowed: string[];
123
- }
124
-
125
120
  export interface PageNode {
126
121
  depth: number;
127
122
  component?: string; // TODO supply default component if it's missing (bit of an edge case)
@@ -276,10 +271,7 @@ export interface SSRNode {
276
271
  prerender?: PrerenderOption;
277
272
  ssr?: boolean;
278
273
  csr?: boolean;
279
- POST?: Action;
280
- PATCH?: Action;
281
- PUT?: Action;
282
- DELETE?: Action;
274
+ actions?: Actions;
283
275
  };
284
276
 
285
277
  // store this in dev so we can print serialization errors
@@ -298,7 +290,6 @@ export interface SSROptions {
298
290
  handle_error(error: Error & { frame?: string }, event: RequestEvent): void;
299
291
  hooks: Hooks;
300
292
  manifest: SSRManifest;
301
- method_override: MethodOverride;
302
293
  paths: {
303
294
  base: string;
304
295
  assets: string;
@@ -193,9 +193,6 @@ export interface RequestOptions {
193
193
  platform?: App.Platform;
194
194
  }
195
195
 
196
- /** `string[]` is only for set-cookie, everything else must be type of `string` */
197
- export type ResponseHeaders = Record<string, string | number | string[] | null>;
198
-
199
196
  export interface RouteDefinition {
200
197
  id: string;
201
198
  type: 'page' | 'endpoint';
@@ -1,25 +0,0 @@
1
- /**
2
- * @param {string} hostname
3
- * @param {string} [constraint]
4
- */
5
- export function domain_matches(hostname, constraint) {
6
- if (!constraint) return true;
7
-
8
- const normalized = constraint[0] === '.' ? constraint.slice(1) : constraint;
9
-
10
- if (hostname === normalized) return true;
11
- return hostname.endsWith('.' + normalized);
12
- }
13
-
14
- /**
15
- * @param {string} path
16
- * @param {string} [constraint]
17
- */
18
- export function path_matches(path, constraint) {
19
- if (!constraint) return true;
20
-
21
- const normalized = constraint.endsWith('/') ? constraint.slice(0, -1) : constraint;
22
-
23
- if (path === normalized) return true;
24
- return path.startsWith(normalized + '/');
25
- }